package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcActivityCashDomain;
import com.xhpc.activity.domain.XhpcActivityRecordDomain;
import com.xhpc.activity.service.XhpcActivityCashService;
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/cash")
public class XhpcActivityCashController extends BaseController {

    @Resource
    XhpcActivityCashService cashService;

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


    @GetMapping("/detail")
    public R getDetail(@RequestParam("activityId") Integer activityId) {

        return R.ok(cashService.getDomainByPk(activityId));
    }


    @Log(title = "活动管理-新增现金红包活动", businessType = BusinessType.INSERT)
    @PostMapping("/")
    public R insertDomain(HttpServletRequest request, @RequestBody XhpcActivityCashDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCreateBy(loginUser.getUserid().toString());
        domain.setUpdateBy(loginUser.getUserid().toString());
        domain.setTenantId(loginUser.getTenantId());
        return R.ok(cashService.insertDomain(domain));
    }


    @Log(title = "活动管理-更新现金红包活动", businessType = BusinessType.UPDATE)
    @PutMapping("/detail")
    public R updateDomain(HttpServletRequest request, @RequestBody XhpcActivityCashDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(cashService.updateDomain(domain));
    }


    @Log(title = "活动管理-审核现金红包活动", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public R updateDomainConfig(HttpServletRequest request, @RequestBody XhpcActivityCashDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCheckBy(loginUser.getUserid().toString());
        return R.ok(cashService.checkDomain(domain));
    }


    @Log(title = "活动管理-更新现金红包活动状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public R updateDomainStatus(HttpServletRequest request, @RequestBody XhpcActivityCashDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(cashService.updateDomainStatus(domain));
    }


    @Log(title = "活动管理-删除现金红包活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activityId}")
    public R deleteDomainByPk(@PathVariable("activityId") Integer activityId) {

        return R.ok(cashService.deleteByPk(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(cashService.getUserList(params));
    }



    @Log(title = "活动管理-领取红包", businessType = BusinessType.INSERT)
    @PostMapping("/receive")
    public R insertCashReceive(@RequestBody XhpcActivityRecordDomain domain) {

        return R.ok(cashService.insertReceiveDomain(domain));
    }


    @GetMapping("/getReceivePage")
    public TableDataInfo getReceivePage(HttpServletRequest request,
                                        Integer activityId) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("activityId", activityId);
        return getDataTable(cashService.getReceivePage(params));
    }

}
