package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcActivityDiscountDomain;
import com.xhpc.activity.service.XhpcActivityDiscountService;
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/discount")
public class XhpcActivityDiscountController extends BaseController {

    @Resource
    XhpcActivityDiscountService discountService;

    @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(discountService.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(discountService.getNameList(params));
    }


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


    @GetMapping("/userList")
    public TableDataInfo getUserList(@RequestParam("activityId")Integer activityId,
                                     @RequestParam("userType") String userType,
                                     @RequestParam("userAccount")String userAccount,
                                     @RequestParam("pageNum")Integer pageNum,
                                     @RequestParam("pageSize")Integer pageSize){

        Map<String, Object> params = new HashMap<>();
        params.put("activityId", activityId);
        params.put("userType", userType);
        params.put("userAccount", userAccount);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);

        return getDataTable(discountService.getUserList(params));
    }


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

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

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


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


    @Log(title = "活动管理-审核折扣活动", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public R updateDomainConfig(HttpServletRequest request, @RequestBody XhpcActivityDiscountDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCheckBy(loginUser.getUserid().toString());
        return R.ok(discountService.checkDomain(domain));
    }


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


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