package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcActivityTemplateDomain;
import com.xhpc.activity.service.XhpcActivityTemplateService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/activity/template")
public class XhpcActivityTemplateController extends BaseController {

    @Resource
    XhpcActivityTemplateService templateService;

    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getPage")
    public TableDataInfo getPage(HttpServletRequest request,
                                 String status){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("status", status);
        return getDataTable(templateService.getPage(params));
    }


    @GetMapping("/getNameList")
    public R getNameList(HttpServletRequest request){
        LoginUser loginUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        return R.ok(templateService.getNameList(params));
    }


    @GetMapping("/detail")
    public R getDetail(@RequestParam("templateId")Integer templateId){
        return R.ok(templateService.getDomainByPk(templateId));
    }


    @Log(title = "活动管理-新增折扣活动模版", businessType = BusinessType.INSERT)
    @PostMapping("/")
    public R insertDomain(HttpServletRequest request, @RequestBody XhpcActivityTemplateDomain domain){

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCreateBy(loginUser.getUserid().toString());
        domain.setUpdateBy(loginUser.getUserid().toString());
        domain.setTenantId(loginUser.getTenantId());

        return R.ok(templateService.insertDomain(domain));
    }


    @Log(title = "活动管理-更新折扣活动模板", businessType = BusinessType.UPDATE)
    @PutMapping("/detail")
    public R updateDomain(HttpServletRequest request, @RequestBody XhpcActivityTemplateDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(templateService.updateDomain(domain));
    }


    @Log(title = "活动管理-更新折扣活动模板状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public R updateDomainStatus(HttpServletRequest request, @RequestBody XhpcActivityTemplateDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(templateService.updateDomainStatus(domain));
    }


    @Log(title = "活动管理-删除折扣活动模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateId}")
    public R deleteDomainByPk(@PathVariable("templateId")Integer templateId){
        return R.ok(templateService.deleteByPk(templateId));
    }

}
