package com.hzs.member.base.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
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.member.base.CuAwardsControl;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.account.dto.BusinessCommissionDTO;
import com.hzs.member.account.dto.MemberAmountDTO;
import com.hzs.member.account.dto.MemberBalanceDTO;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.service.ICuMemberTradeService;
import com.hzs.member.base.service.ICuAwardsControlService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.base.vo.*;
import com.hzs.member.detail.service.ICuMemberServiceLogService;
import com.hzs.member.detail.vo.CuMemberServiceLogVO;
import com.hzs.system.config.IServiceChargeServiceApi;
import com.hzs.system.config.dto.ServiceChargeDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 真实奖衔控制 前端控制器
 *
 * @author zhangjing
 * @since 2023-04-07
 */
@RestController
@RequestMapping("/manage/awards-control")
public class CuAwardsControlController extends BaseController {

    @Autowired
    private ICuAwardsControlService controlService;
    @Autowired
    private ICuMemberService cuMemberService;
    @Autowired
    private ICuMemberServiceLogService memberServiceLogService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;
    @Autowired
    private ICuMemberTradeService cuMemberTradeService;
    @Autowired
    private ICuMemberBaseService cuMemberBaseService;

    @DubboReference
    IServiceChargeServiceApi serviceChargeServiceApi;


    /**
     * @description: 查询真实奖衔控制列表
     * @author: zhang jing
     * @date: 2023/4/8 14:09
     * @param: [controlVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @AccessPermissions("AwardControl")
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(CuAwardsControlVO controlVO) {
        startPage();
        Integer pkCountry = SecurityUtils.getPkCountry();
        controlVO.setPkCountry(pkCountry);
        List<CuAwardsControlVO> list = controlService.queryAwardsControlList(controlVO);
        return getDataTable(list);
    }

    /**
     * @description: 真实奖衔控制查询会员信息
     * @author: zhang jing
     * @date: 2023/4/10 18:08
     * @param: [controlVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.SELECT)
    @GetMapping("/memberlist")
    public TableDataInfo memberlist(CuAwardsControlVO controlVO) {
        startPage();
        Integer pkCountry = SecurityUtils.getPkCountry();
        controlVO.setPkCountry(pkCountry);
        if (controlVO.getAddAwards() == EDefault.NO.getValue()) {
            LambdaQueryWrapper<CuMember> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotEmpty(controlVO.getMemberCode())) {
                queryWrapper.eq(CuMember::getMemberCode, controlVO.getMemberCode());
            }
            if (StringUtils.isNotEmpty(controlVO.getMemberName())) {
                queryWrapper.like(CuMember::getMemberName, controlVO.getMemberName());
            }
            List<CuMember> list = cuMemberService.list(queryWrapper);
            List<CuAwardsControlVO> list1 = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(list)) {
                list.forEach(item -> {
                    CuAwardsControlVO controlVO1 = BeanUtil.copyProperties(item, CuAwardsControlVO.class);
                    controlVO1.setPkMember(item.getPkId());
                    list1.add(controlVO1);
                });
            }
            return getDataTable(list1);
        } else {
            List<CuAwardsControlVO> list = controlService.queryAwardsControlList(controlVO);
            return getDataTable(list);
        }
    }

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2023/4/8 14:12
     * @param: [response, controlVO]
     * @return: void
     **/
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuAwardsControlVO controlVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        controlVO.setPkCountry(pkCountry);

        List<CuAwardsControlVO> list = controlService.queryAwardsControlList(controlVO);
        if (CollectionUtil.isNotEmpty(list)) {
            Map<Integer, String> transactionMap1 = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_GRADE,
                    EnumsPrefixConstants.KEY_AWARD);
            list.forEach(itm -> {
                itm.setRealAwardsKeyVal(transactionMap1.get(itm.getRealAwardsKey()));
                itm.setMentoringAwardKeyVal(transactionMap1.get(itm.getMentoringAwardKey()));
                itm.setDividendAwardKeyVal(transactionMap1.get(itm.getDividendAwardKey()));
                itm.setExtremeAwardsKeyVal(transactionMap1.get(itm.getExtremeAwardsKey()));
                itm.setReplyExamAwardKeyVal(transactionMap1.get(itm.getReplyExamAwardKey()));
                itm.setDivvyExamAwardsKeyVal(transactionMap1.get(itm.getDivvyExamAwardsKey()));
            });
        }

        ExcelUtil<CuAwardsControlVO> util = new ExcelUtil<>(CuAwardsControlVO.class);
        util.exportExcel(response, list, "真实奖衔控制");
    }

    /**
     * 下载模板
     *
     * @param response
     * @param
     */
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.EXPORT)
    @PostMapping("/download")
    public void download(HttpServletResponse response) {
        List<CuAwardsControlImportVO> list = new ArrayList<>();
        ExcelUtil<CuAwardsControlImportVO> util = new ExcelUtil<>(CuAwardsControlImportVO.class);
        util.exportExcel(response, list, "真实奖衔控制");
    }


    /**
     * @description: 真实奖衔控制确认方法
     * @author: zhang jing
     * @date: 2023/4/11 15:22
     * @param: [controlVO, language]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.SELECT)
    @PostMapping("/affirm")
    public AjaxResult affirm(@RequestBody CuAwardsControlVO controlVO) {
        if (CollectionUtil.isNotEmpty(controlVO.getUpdateParentParamList())) {
            Integer pkCountry = SecurityUtils.getPkCountry();
            controlVO.setPkCountry(pkCountry);
            controlVO.setCreationTime(new Date());
            controlVO.setPkCreator(SecurityUtils.getUserId());
            BusinessCommissionDTO businessCommissionDTO = new BusinessCommissionDTO();
            if (StringUtils.isNull(controlVO.getApprovalBusiness())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WORK_TYPE_NOT_NULL));
            }
            List<MemberAmountDTO> memberAmountDTOList = new ArrayList<>();
            List<CuAwardsControlVO> errorlist = new ArrayList<>();
            //数据库内容翻译
            Map<Integer, String> transactionMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_AWARD);
            for (CuAwardsControlVO ca : controlVO.getUpdateParentParamList()) {
                if (StringUtils.isNotNull(ca.getRealAwards())) {
                    ca.setRealAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getRealAwards().intValue())));
                }
                if (StringUtils.isNotNull(ca.getMentoringAward())) {
                    ca.setMentoringAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getMentoringAward().intValue())));
                }
                if (StringUtils.isNotNull(ca.getDividendAward())) {
                    ca.setDividendAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getDividendAward().intValue())));
                }
                if (StringUtils.isNotNull(ca.getExtremeAwards())) {
                    ca.setExtremeAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getExtremeAwards().intValue())));
                }
                if (StringUtils.isNotNull(ca.getReplyExamAward())) {
                    ca.setReplyExamAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getReplyExamAward().intValue())));
                }
                if (StringUtils.isNotNull(ca.getDivvyExamAwards())) {
                    ca.setDivvyExamAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getDivvyExamAwards().intValue())));
                }
                int editType = 0;
                if (controlVO.getAddAwards() == EDelFlag.UN_DELETE.getValue()) {
                    editType = EApprovalBusiness.AWARDS_CONTROL_SAVE.getValue();
                }
                //校验唯一性
                if (controlVO.getAddAwards() == EDelFlag.UN_DELETE.getValue()) {
                    editType = EApprovalBusiness.AWARDS_CONTROL_SAVE.getValue();
                    LambdaQueryWrapper<CuAwardsControl> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(CuAwardsControl::getPkMember, ca.getPkMember());
                    List<CuAwardsControl> list = controlService.list(queryWrapper);
                    if (CollectionUtil.isNotEmpty(list)) {
                        CuAwardsControlVO error = new CuAwardsControlVO();
                        error.setMemberCode(ca.getMemberCode());
                        error.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.AWARDS_CONTROL_SAVE_REPEAT));
                        errorlist.add(error);
                    }
                }
                if (controlVO.getAddAwards() == EDelFlag.DELETE.getValue()) {
                    editType = EApprovalBusiness.AWARDS_CONTROL_MODIFY.getValue();
                }
                if (controlVO.getAddAwards() == 2) {
                    editType = EApprovalBusiness.AWARDS_CONTROL_DELETE.getValue();
                }
                //当前最大次数
                Integer approveNumber = memberServiceLogService.selectByMaxApproveNumber(ca.getPkMember(),
                        editType, EApproveStatus.FINISH.getValue());
                ca.setChangeNumber(approveNumber + 1);
                if (controlVO.getProcessType() == EProcessType.AVOIDSIGNING.getValue() ||
                        controlVO.getProcessType() == EProcessType.DOUBLEPAY.getValue()) {

                    //查询扣费金额，账户信息
                    ServiceChargeDTO serviceChargeDTO = new ServiceChargeDTO();
                    serviceChargeDTO.setType(editType);
                    serviceChargeDTO.setPkCountry(pkCountry);
                    serviceChargeDTO.setQuantity(approveNumber + 1);
                    R<ServiceChargeDTO> serviceChargeDTOR = serviceChargeServiceApi.getServiceChargeByType(serviceChargeDTO);
                    if (null == serviceChargeDTOR.getData() ||
                            null == serviceChargeDTOR.getData().getPkAccount()) {
                        return AjaxResult.error(ConfigMsgConstants.BUSINESS_ACCOUNT_IS_NULL);
                    }
                    //接口查询金额是否足够
                    businessCommissionDTO.setPkAccount(serviceChargeDTOR.getData().getPkAccount());
                    businessCommissionDTO.setPkCountry(pkCountry);
                    MemberAmountDTO memberAmountDTO = new MemberAmountDTO();
                    memberAmountDTO.setPkMember(ca.getPkMember());
                    memberAmountDTO.setTradeAmount(serviceChargeDTOR.getData().getCost());
                    ca.setCost(serviceChargeDTOR.getData().getCost());
                    ca.setPkAccount(serviceChargeDTOR.getData().getPkAccount());
                    ca.setPkAccountKey(cuMemberBaseService.translateAccountValue(serviceChargeDTOR.getData().getPkAccount()));
                    memberAmountDTOList.add(memberAmountDTO);
                    controlVO.setMemberAmountDTOList(memberAmountDTOList);
                    controlVO.setPkAccount(serviceChargeDTOR.getData().getPkAccount());
                }
            }
            businessCommissionDTO.setMemberAmountDTOList(memberAmountDTOList);
            controlVO.setMemberAmountDTOList(memberAmountDTOList);
            if (CollectionUtil.isNotEmpty(memberAmountDTOList)) {
                //接口查询金额是否足够
                List<MemberBalanceDTO> memberBalanceList = cuMemberTradeService.selectBalanceNotEnoughMember(businessCommissionDTO);
                if (CollectionUtil.isNotEmpty(memberBalanceList)) {
                    memberBalanceList.forEach(item -> {
                        CuAwardsControlVO error = BeanUtil.copyProperties(item, CuAwardsControlVO.class);
                        error.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_BALANCE_INSUFFICIENT));
                        errorlist.add(error);
                    });
                }
            }
            if (CollectionUtil.isNotEmpty(errorlist)) {
                return AjaxResult.error(504, ConfigMsgConstants.MEMBER_DATA_ERROR, errorlist);
            }
            if (controlVO.getProcessType() == EProcessType.AVOIDSIGNING.getValue() ||
                    controlVO.getProcessType() == EProcessType.DOUBLEAVOID.getValue()) {
                controlVO = controlService.affirm(controlVO);
            }

        }
        return AjaxResult.success(controlVO);
    }

    /**
     * @description: 导入真实奖衔控制
     * @author: zhang jing
     * @date: 2023/4/8 14:53
     * @param: [file]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam(value = "file") MultipartFile file,
                                 CuAwardsControlVO controlVO) throws Exception {
        ExcelUtil<CuAwardsControlImportVO> util = new ExcelUtil<>(CuAwardsControlImportVO.class);
        List<CuAwardsControlImportVO> list = util.importExcel(file.getInputStream());
        List<CuAwardsControlVO> errorlist = new ArrayList<>();
        List<String> memberCodeList = new ArrayList<>();
        if (CollectionUtil.isEmpty(list)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        for (CuAwardsControlImportVO ca : list) {
            CuMember cuMember = cuMemberService.getMember(ca.getMemberCode());
            if (StringUtils.isNull(cuMember)) {
                CuAwardsControlVO codeVO = BeanUtil.copyProperties(ca, CuAwardsControlVO.class);
                codeVO.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
                errorlist.add(codeVO);
            } else {
                memberCodeList.add(ca.getMemberCode());
            }
        }
        if (CollectionUtil.isNotEmpty(errorlist)) {
            return AjaxResult.error(504, ConfigMsgConstants.MEMBER_DATA_ERROR, errorlist);
        }
        if (controlVO.getAddAwards() == EDefault.NO.getValue()) {
            List<String> memeberCodes = new ArrayList<>();
            list.forEach(item -> memeberCodes.add(item.getMemberCode()));
            List<CuMember> meList = cuMemberService.findMemberByMembercodesAndPkcountry(memeberCodes, SecurityUtils.getPkCountry());
            List<CuAwardsControlVO> newlist = new ArrayList<>();
            meList.forEach(item -> {
                CuAwardsControlVO awardsControlVO = new CuAwardsControlVO();
                awardsControlVO.setPkMember(item.getPkId());
                awardsControlVO.setMemberCode(item.getMemberCode());
                awardsControlVO.setMemberName(item.getMemberName());
                newlist.add(awardsControlVO);
            });
            return AjaxResult.success(newlist);
        } else {
            controlVO.setMemberCodeList(memberCodeList);
            List<CuAwardsControlVO> newlist = controlService.queryAwardsControlList(controlVO);
            return AjaxResult.success(newlist);
        }
    }

    /**
     * @description: 添加真实奖衔控制发起签呈
     * @author: zhang jing
     * @date: 2023/4/8 15:22
     * @param: [controlVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.SUBMIT)
    @PostMapping("/save-petition")
    public AjaxResult savePetition(@RequestBody CuAwardsControlVO controlVO) {
        if (CollectionUtil.isEmpty(controlVO.getUpdateParentParamList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_NOT_NULL));
        }
        if (StringUtils.isNull(controlVO.getSignType())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SIGN_TYPE_NOT_NULL));
        }
        if (CollectionUtil.isEmpty(controlVO.getUserIdList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EXAMINE_AND_APPROVE_NOT_NULL));
        }

        Set<Long> pkMemberIds = new HashSet<>();
        List<Long> pkMemberList = new ArrayList<>();
        List<CuAwardsControlVO> errorlist = new ArrayList<>();
        List<Integer> editTypeList = new ArrayList<>();
        List<MemberAmountDTO> memberAmountDTOList = new ArrayList<>();
        Integer pkCountry = SecurityUtils.getPkCountry();
        //数据库内容翻译
        Map<Integer, String> transactionMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_AWARD);
        editTypeList.add(EApprovalBusiness.AWARDS_CONTROL_SAVE.getValue());
        for (CuAwardsControlVO ca : controlVO.getUpdateParentParamList()) {
            pkMemberIds.add(ca.getPkMember());
            pkMemberList.add(ca.getPkMember());
            if (StringUtils.isNotNull(ca.getRealAwards())) {
                ca.setRealAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getRealAwards().intValue())));
            }
            if (StringUtils.isNotNull(ca.getMentoringAward())) {
                ca.setMentoringAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getMentoringAward().intValue())));
            }
            if (StringUtils.isNotNull(ca.getDividendAward())) {
                ca.setDividendAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getDividendAward().intValue())));
            }
            if (StringUtils.isNotNull(ca.getExtremeAwards())) {
                ca.setExtremeAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getExtremeAwards().intValue())));
            }
            if (StringUtils.isNotNull(ca.getReplyExamAward())) {
                ca.setReplyExamAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getReplyExamAward().intValue())));
            }
            if (StringUtils.isNotNull(ca.getDivvyExamAwards())) {
                ca.setDivvyExamAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getDivvyExamAwards().intValue())));
            }
            //校验重复
            LambdaQueryWrapper<CuAwardsControl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CuAwardsControl::getPkMember, ca.getPkMember());
            List<CuAwardsControl> newlist = controlService.list(queryWrapper);
            if (CollectionUtil.isNotEmpty(newlist)) {
                ca.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.AWARDS_CONTROL_SAVE_REPEAT));
                errorlist.add(ca);
            }
            MemberAmountDTO ma = new MemberAmountDTO();
            ma.setPkMember(ca.getPkMember());
            ma.setTradeAmount(ca.getCost());
            memberAmountDTOList.add(ma);
            controlVO.setPkAccount(ca.getPkAccount());
        }
        if (CollectionUtil.isNotEmpty(errorlist)) {
            return AjaxResult.error(504, ConfigMsgConstants.MEMBER_DATA_ERROR, errorlist);
        }
        controlVO.setPkMemberList(pkMemberIds);
        controlVO.setMemberAmountDTOList(memberAmountDTOList);
        //校验重复审核
        CuMemberServiceLogVO cuMemberServiceLogVO = new CuMemberServiceLogVO();
        cuMemberServiceLogVO.setPkMemberList(pkMemberList);
        cuMemberServiceLogVO.setEditTypeList(editTypeList);
        List<CuMemberServiceLogVO> memberServiceLog = memberServiceLogService.selectServiceLogList(cuMemberServiceLogVO);
        if (CollectionUtil.isNotEmpty(memberServiceLog)) {
            for (CuMemberServiceLogVO cs : memberServiceLog) {
                CuAwardsControlVO bev = BeanUtil.copyProperties(cs, CuAwardsControlVO.class);
                bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_UPDATE_CODE_REPEAT));
                bev.setMemberCode(cs.getMemberCode());
                errorlist.add(bev);
            }
        }
        if (CollectionUtil.isNotEmpty(errorlist)) {
            return AjaxResult.error(504, ConfigMsgConstants.MEMBER_DATA_ERROR, errorlist);
        }
        return AjaxResult.success(controlService.savePetition(controlVO));
    }

    /**
     * @description: 修改真实奖衔控制发起签呈
     * @author: zhang jing
     * @date: 2023/4/8 16:31
     * @param: [controlVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.SUBMIT)
    @PostMapping("/up-petition")
    public AjaxResult upPetition(@RequestBody CuAwardsControlVO controlVO) {
        if (CollectionUtil.isEmpty(controlVO.getUpdateParentParamList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_NOT_NULL));
        }
        if (StringUtils.isNull(controlVO.getSignType())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SIGN_TYPE_NOT_NULL));
        }
        if (CollectionUtil.isEmpty(controlVO.getUserIdList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EXAMINE_AND_APPROVE_NOT_NULL));
        }

        Set<Long> pkMemberIds = new HashSet<>();
        List<Long> pkMemberList = new ArrayList<>();
        List<CuAwardsControlVO> errorlist = new ArrayList<>();
        List<Integer> editTypeList = new ArrayList<>();
        List<MemberAmountDTO> memberAmountDTOList = new ArrayList<>();
        Integer pkCountry = SecurityUtils.getPkCountry();
        //数据库内容翻译
        Map<Integer, String> transactionMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_AWARD);
        editTypeList.add(EApprovalBusiness.AWARDS_CONTROL_MODIFY.getValue());
        for (CuAwardsControlVO ca : controlVO.getUpdateParentParamList()) {
            pkMemberIds.add(ca.getPkMember());
            pkMemberList.add(ca.getPkMember());
            if (StringUtils.isNotNull(ca.getRealAwards())) {
                ca.setRealAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getRealAwards().intValue())));
            }
            if (StringUtils.isNotNull(ca.getMentoringAward())) {
                ca.setMentoringAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getMentoringAward().intValue())));
            }
            if (StringUtils.isNotNull(ca.getDividendAward())) {
                ca.setDividendAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getDividendAward().intValue())));
            }
            if (StringUtils.isNotNull(ca.getExtremeAwards())) {
                ca.setExtremeAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getExtremeAwards().intValue())));
            }
            if (StringUtils.isNotNull(ca.getReplyExamAward())) {
                ca.setReplyExamAwardKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getReplyExamAward().intValue())));
            }
            if (StringUtils.isNotNull(ca.getDivvyExamAwards())) {
                ca.setDivvyExamAwardsKeyVal(transactionMap.get(cuMemberService.getAwardsTranslateValue(pkCountry, ca.getDivvyExamAwards().intValue())));
            }

            if (controlVO.getProcessType() == EProcessType.DOUBLEPAY.getValue()) {
                //接口查询金额是否足够
                MemberAmountDTO memberAmountDTO = new MemberAmountDTO();
                memberAmountDTO.setPkMember(ca.getPkMember());
                memberAmountDTO.setTradeAmount(ca.getCost());
                memberAmountDTOList.add(memberAmountDTO);
                controlVO.setPkAccount(ca.getPkAccount());

            }
        }
        controlVO.setPkCountry(pkCountry);
        controlVO.setMemberAmountDTOList(memberAmountDTOList);
        controlVO.setPkMemberList(pkMemberIds);
        //校验重复审核
        CuMemberServiceLogVO cuMemberServiceLogVO = new CuMemberServiceLogVO();
        cuMemberServiceLogVO.setPkMemberList(pkMemberList);
        cuMemberServiceLogVO.setEditTypeList(editTypeList);
        List<CuMemberServiceLogVO> memberServiceLog = memberServiceLogService.selectServiceLogList(cuMemberServiceLogVO);
        if (CollectionUtil.isNotEmpty(memberServiceLog)) {
            for (CuMemberServiceLogVO cs : memberServiceLog) {
                CuAwardsControlVO bev = BeanUtil.copyProperties(cs, CuAwardsControlVO.class);
                bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_UPDATE_CODE_REPEAT));
                bev.setMemberCode(cs.getMemberCode());
                errorlist.add(bev);
            }
        }
        if (CollectionUtil.isNotEmpty(errorlist)) {
            return AjaxResult.error(504, ConfigMsgConstants.MEMBER_DATA_ERROR, errorlist);
        }
        return AjaxResult.success(controlService.upPetition(controlVO));
    }

    /**
     * @description: 删除真实奖衔控制发起签呈
     * @author: zhang jing
     * @date: 2023/4/8 16:31
     * @param: [controlVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SPECIAL_BUSINESS, business = EOperationBusiness.AWARD_CONTROL, method = EOperationMethod.SUBMIT)
    @PostMapping("/del-petition")
    public AjaxResult delPetition(@RequestBody CuAwardsControlVO controlVO) {
        if (CollectionUtil.isEmpty(controlVO.getUpdateParentParamList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_NOT_NULL));
        }
        if (StringUtils.isNull(controlVO.getSignType())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SIGN_TYPE_NOT_NULL));
        }
        if (CollectionUtil.isEmpty(controlVO.getUserIdList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EXAMINE_AND_APPROVE_NOT_NULL));
        }

        Set<Long> pkMemberIds = new HashSet<>();
        List<Long> pkMemberList = new ArrayList<>();
        List<CuAwardsControlVO> errorlist = new ArrayList<>();
        List<Integer> editTypeList = new ArrayList<>();
        List<MemberAmountDTO> memberAmountDTOList = new ArrayList<>();
        editTypeList.add(EApprovalBusiness.AWARDS_CONTROL_DELETE.getValue());
        for (CuAwardsControlVO ca : controlVO.getUpdateParentParamList()) {
            pkMemberIds.add(ca.getPkMember());
            pkMemberList.add(ca.getPkMember());
            //当前最大次数
            Integer approveNumber = memberServiceLogService.selectByMaxApproveNumber(ca.getPkMember(),
                    EApprovalBusiness.AWARDS_CONTROL_DELETE.getValue(), EApproveStatus.FINISH.getValue());
            ca.setChangeNumber(approveNumber + 1);
            MemberAmountDTO ma = new MemberAmountDTO();
            ma.setPkMember(ca.getPkMember());
            ma.setTradeAmount(ca.getCost());
            memberAmountDTOList.add(ma);
            controlVO.setPkAccount(ca.getPkAccount());
        }
        controlVO.setPkCountry(SecurityUtils.getPkCountry());
        controlVO.setPkMemberList(pkMemberIds);
        controlVO.setMemberAmountDTOList(memberAmountDTOList);
        //校验重复审核
        CuMemberServiceLogVO cuMemberServiceLogVO = new CuMemberServiceLogVO();
        cuMemberServiceLogVO.setPkMemberList(pkMemberList);
        cuMemberServiceLogVO.setEditTypeList(editTypeList);
        List<CuMemberServiceLogVO> memberServiceLog = memberServiceLogService.selectServiceLogList(cuMemberServiceLogVO);
        if (CollectionUtil.isNotEmpty(memberServiceLog)) {
            for (CuMemberServiceLogVO cs : memberServiceLog) {
                CuAwardsControlVO bev = BeanUtil.copyProperties(cs, CuAwardsControlVO.class);
                bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_UPDATE_CODE_REPEAT));
                bev.setMemberCode(cs.getMemberCode());
                errorlist.add(bev);
            }
        }
        if (CollectionUtil.isNotEmpty(errorlist)) {
            return AjaxResult.error(504, ConfigMsgConstants.MEMBER_DATA_ERROR, errorlist);
        }
        return AjaxResult.success(controlService.delPetition(controlVO));
    }


}
