package com.hzs.activity.draw.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.activity.draw.param.AcDrawRecordParam;
import com.hzs.activity.draw.param.AcDrawRewardNumParam;
import com.hzs.activity.draw.param.PurchasedTimesParam;
import com.hzs.activity.draw.service.IAcDrawRewardNumService;
import com.hzs.activity.draw.service.IAcDrawRewardRecordService;
import com.hzs.activity.draw.vo.AcDrawRewardNumVO;
import com.hzs.activity.draw.vo.AcDrawRewardRecordVO;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.activity.draw.ext.AcDrawRewardNumExt;
import com.hzs.common.domain.activity.draw.ext.AcDrawRewardRecordExt;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.IMemberServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 抽奖 前端控制器
 *
 * @author hzs
 * @since 2023-04-21
 */
@RestController
@RequestMapping("/manage/draw")
public class AcDrawRewardRecordController extends BaseController {

    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private IAcDrawRewardRecordService acDrawRewardRecordService;
    @Autowired
    private IAcDrawRewardNumService acDrawRewardNumService;

    @DubboReference
    IMemberServiceApi memberServiceApi;

    /*
     * @description: 查询会员中奖记录
     * @author: sui q
     * @date: 2023/6/1 13:49
     * @param: null null
     **/
    @Log(module = EOperationModule.DRAW_RECORD, business = EOperationBusiness.DRAW_RECORD, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody AcDrawRecordParam acDrawRecordParam) {
        acDrawRecordParam.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        return getDataTable(acDrawRewardRecordService.queryCuMemberGiftRecordByCondition(acDrawRecordParam));
    }

    /**
     * 导出会员中奖记录
     */
    @PostMapping("/export-record")
    @Log(module = EOperationModule.DRAW_RECORD, business = EOperationBusiness.DRAW_RECORD, method = EOperationMethod.EXPORT)
    public void exportCuMemberRecord(HttpServletResponse response, AcDrawRecordParam acDrawRecordParam) {
        acDrawRecordParam.setPkCountry(SecurityUtils.getPkCountry());
        List<AcDrawRewardRecordExt> acDrawRewardRecordExtList = acDrawRewardRecordService.queryCuMemberGiftRecordByCondition(acDrawRecordParam);
        List<AcDrawRewardRecordVO> acDrawRewardRecordVOList = new ArrayList<>(acDrawRewardRecordExtList.size());
        acDrawRewardRecordExtList.forEach(acDrawRewardRecordExt -> {
            acDrawRewardRecordVOList.add(BeanUtil.copyProperties(acDrawRewardRecordExt, AcDrawRewardRecordVO.class));
        });
        ExcelUtil<AcDrawRewardRecordVO> util = new ExcelUtil<>(AcDrawRewardRecordVO.class);
        util.exportExcel(response, acDrawRewardRecordVOList, "中奖记录导出");
    }

    /**
     * @description: 查询会员礼包收货地址列表
     * @author: sshcode
     * @date: 2022/10/20 10:36
     * @param: [memberAuthorityParam]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.DRAW_DETAIL, business = EOperationBusiness.DRAW_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/list-number")
    public TableDataInfo listDrawNumber(AcDrawRewardNumParam acDrawRewardNumParam) {
        acDrawRewardNumParam.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        // 禅道bug ：8325 问题三 要求无订单也可以展示 来源单号展示空
        return getDataTable(acDrawRewardNumService.queryDrawRewardExtNumByCondition(acDrawRewardNumParam));
    }

    /**
     * 导出会员抽奖次数
     */
    @PostMapping("/export-number")
    @Log(module = EOperationModule.DRAW_DETAIL, business = EOperationBusiness.DRAW_DETAIL, method = EOperationMethod.EXPORT)
    public void exportCuMemberNumber(HttpServletResponse response, AcDrawRewardNumParam acDrawRewardNumParam) {
        acDrawRewardNumParam.setPkCountry(SecurityUtils.getPkCountry());
        List<AcDrawRewardNumExt> acDrawRewardNumExtList = acDrawRewardNumService.queryDrawRewardExtNumByCondition(acDrawRewardNumParam);
        List<AcDrawRewardNumVO> acDrawRewardNumVOList = new ArrayList<>(acDrawRewardNumExtList.size());
        acDrawRewardNumExtList.forEach(acDrawRewardNumExt -> {
            acDrawRewardNumVOList.add(BeanUtil.copyProperties(acDrawRewardNumExt, AcDrawRewardNumVO.class));
        });
        ExcelUtil<AcDrawRewardNumVO> util = new ExcelUtil<>(AcDrawRewardNumVO.class);
        util.exportExcel(response, acDrawRewardNumVOList, "抽奖次数导出");
    }

    /**
     * 充值已购买次数按钮 (审批)
     */
    @PostMapping("/purchased-times")
    public AjaxResult purchasedTimes(@RequestBody PurchasedTimesParam purchasedTimesParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isBlank(purchasedTimesParam.getMemberCode())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }
        if (StringUtils.isNull(purchasedTimesParam.getPkBaseId())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ACTIVITY_NOT_NULL));
        }

        R<CuMember> memberDTO = memberServiceApi.getMember(purchasedTimesParam.getMemberCode());
        if (memberDTO.getData() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }


        purchasedTimesParam.setPkCountry(pkCountry);
        purchasedTimesParam.setLoginUser(userTokenService.getLoginUser());
        purchasedTimesParam.setPkMember(SecurityUtils.getUserId());
        acDrawRewardNumService.purchasedTimes(purchasedTimesParam);
        return AjaxResult.success();
    }


    /**
     * 充值未购买次数 (走审批)
     */
    @PostMapping("/unpurchased-times")
    public AjaxResult unpurchasedTimes(@RequestBody PurchasedTimesParam purchasedTimesParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isBlank(purchasedTimesParam.getMemberCode())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }
        R<CuMember> memberDTO = memberServiceApi.getMember(purchasedTimesParam.getMemberCode());
        if (memberDTO.getData() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }

        purchasedTimesParam.setPkCountry(pkCountry);
        purchasedTimesParam.setLoginUser(userTokenService.getLoginUser());
        purchasedTimesParam.setPkMember(SecurityUtils.getUserId());
        acDrawRewardNumService.unpurchasedTimes(purchasedTimesParam);
        return AjaxResult.success();
    }

}
