package com.hzs.member.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.*;
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.exception.ServiceException;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.detail.CuMemberAchieveLog;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.dto.BusinessCommissionDTO;
import com.hzs.member.account.dto.MemberAmountDTO;
import com.hzs.member.account.service.ICuMemberTradeService;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.base.mapper.CuMemberAchieveLogMapper;
import com.hzs.member.base.service.ICuMemberAchieveLogService;
import com.hzs.member.base.vo.CuMemberAchieveLogVO;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.sys.IApprovalServiceApi;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import com.hzs.system.sys.dto.ApprovalSubmitDTO;
import com.hzs.system.sys.dto.BusinessLogDTO;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 注水业绩记录表 服务实现类
 */
@Service
public class CuMemberAchieveLogServiceImpl extends ServiceImpl<CuMemberAchieveLogMapper, CuMemberAchieveLog> implements ICuMemberAchieveLogService {

    @Autowired
    private ICuMemberSettlePeriodService iCuMemberSettlePeriodService;
    @Autowired
    private ICuMemberTradeService iCuMemberTradeService;
    @Autowired
    private ICuMemberAchieveLogService iCuMemberAchieveLogService;
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @DubboReference
    IApprovalServiceApi iApprovalServiceApi;
    @DubboReference
    ICurrencyServiceApi iCurrencyServiceApi;

    @Override
    public List<CuMemberAchieveLogVO> queryMemberAchieveLogList(CuMemberAchieveLogVO cuMemberAchieveLogVO) {
        //先查询注水日志表
        return baseMapper.queryMemberAchieveLogList(cuMemberAchieveLogVO.getMemberCode(),
                cuMemberAchieveLogVO.getMemberName(), cuMemberAchieveLogVO.getPkCountry(),
                cuMemberAchieveLogVO.getPkAwards(), cuMemberAchieveLogVO.getPkTeamCode(),
                cuMemberAchieveLogVO.getPkVertex(), cuMemberAchieveLogVO.getStartDate(),
                cuMemberAchieveLogVO.getEndtDate(), cuMemberAchieveLogVO.getStaDueDate(),
                cuMemberAchieveLogVO.getEndDueDate(), cuMemberAchieveLogVO.getWaterStatus(),
                cuMemberAchieveLogVO.getInfusingWaterType());
    }

    @Override
    public List<CuMemberAchieveLogVO> queryMemberAchieveHistoryList(List<CuMemberAchieveLogVO> calList) {
        List<CuMemberAchieveLogVO> list = baseMapper.queryMemberAchieveHistoryList(calList);
        R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
        BigDecimal exchangeRate = SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY) ? currency.getData().getInExchangeRate() : BigDecimal.ONE;
        List<CuMemberAchieveLogVO> listnew = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(itm -> {
                itm.setAsumRealModifyPv(itm.getAsumRealPv().multiply(exchangeRate));
                itm.setBsumRealModifyPv(itm.getBsumRealPv().multiply(exchangeRate));
                itm.setAfirstModifyPv(itm.getAsumPv().multiply(exchangeRate));
                itm.setBfirstModifyPv(itm.getBsumPv().multiply(exchangeRate));
                itm.setRepAFirstModifyBalance(itm.getAbalance().multiply(exchangeRate));
                itm.setRepBFirstModifyBalance(itm.getBbalance().multiply(exchangeRate));
                itm.setRepAModifySumPv(itm.getRepASumPv().multiply(exchangeRate));
                itm.setRepBModifySumPv(itm.getRepBSumPv().multiply(exchangeRate));
                itm.setRepAModifyBalance(itm.getRepABalance().multiply(exchangeRate));
                itm.setRepBModifyBalance(itm.getRepBBalance().multiply(exchangeRate));
                listnew.add(itm);
            });
        }
        return listnew;
    }

    @Override
    public List<CuMemberAchieveLogVO> queryTryAchieveLogList(CuMemberAchieveLogVO cuMemberAchieveLogVO) {
        return baseMapper.queryTryAchieveLogList(cuMemberAchieveLogVO.getApproveStatus(), cuMemberAchieveLogVO.getBusinessNo());
    }

    @Override
    public Integer selectByMaxApproveNumber(Long pkMember, int approveStatus) {
        return baseMapper.selectByMaxApproveNumber(pkMember, approveStatus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CuMemberAchieveLogVO infusingWater(CuMemberAchieveLogVO cuMemberAchieveLogVO) {
        Date date = new Date();
        Long userID = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();

        // 封装修改会员编号业务日志
        List<BusinessLogDTO> dtoList = new ArrayList<>();
        // 业务单号
        String code = CommonUtil.createSerialNumber(EOrderPrefix.MEMBER_ACHIEVE.getValue());
        cuMemberAchieveLogVO.setPkAccount(cuMemberAchieveLogVO.getCmalvList().get(0).getPkAccount());
        for (CuMemberAchieveLogVO cmal : cuMemberAchieveLogVO.getCmalvList()) {
            // 历史期业绩
            String datestr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cmal.getSettleDate());
            String month = DateUtils.getMonth(datestr);
            String tableName = TableNameConstants.CU_MEMBER_ACHIEVE + month;
            //查询最新期数
            int period = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToStr(datestr)).getPkId();
            cuMemberAchieveLogVO.setTableName(tableName);
            cuMemberAchieveLogVO.setPeriod(period);
            cuMemberAchieveLogVO.setPkMember(cmal.getPkMember());
            List<CuMemberAchieveLogVO> calList = iCuMemberAchieveLogService.queryMemberAchieveHistoryList(Collections.singletonList(cuMemberAchieveLogVO));

            CuMemberAchieveLog memberAchieveLog = BeanUtil.copyProperties(cmal, CuMemberAchieveLog.class);
            // 2025.05.15 不需要真实业绩，此处保留与业绩一致，方便后续奖衔等计算
            memberAchieveLog.setASumRealModifyPv(cmal.getAfirstModifyPv());
            memberAchieveLog.setBSumRealModifyPv(cmal.getBfirstModifyPv());
            memberAchieveLog.setAFirstModifyPv(cmal.getAfirstModifyPv());
            memberAchieveLog.setBFirstModifyPv(cmal.getBfirstModifyPv());
            memberAchieveLog.setRepAFirstModifyBalance(cmal.getRepAFirstModifyBalance());
            memberAchieveLog.setRepBFirstModifyBalance(cmal.getRepBFirstModifyBalance());
//            memberAchieveLog.setRepAModifySumPv(cmal.getRepAModifySumPv());
//            memberAchieveLog.setRepBModifySumPv(cmal.getRepBModifySumPv());
//            memberAchieveLog.setRepAModifyBalance(cmal.getRepAModifyBalance());
//            memberAchieveLog.setRepBModifyBalance(cmal.getRepBModifyBalance());
            memberAchieveLog.setPkCountry(pkCountry);
            memberAchieveLog.setInfusingWaterType(cuMemberAchieveLogVO.getInfusingWaterType());
            memberAchieveLog.setProcessType(cuMemberAchieveLogVO.getProcessType());
            memberAchieveLog.setApproveStatus(EApproveStatus.FINISH.getValue());
            memberAchieveLog.setEditType(EApprovalBusiness.MEMBER_ACHIEVE.getValue());
            memberAchieveLog.setBusinessNo(code);
            memberAchieveLog.setCost(cmal.getCost());
            memberAchieveLog.setPkAccount(cmal.getPkAccount());
            memberAchieveLog.setRemarks(cmal.getRemarks());
            memberAchieveLog.setDueDate(cmal.getDueDate());
            memberAchieveLog.setSettleDate(cmal.getSettleDate());
            memberAchieveLog.setCreationTime(date);
            memberAchieveLog.setPkCreator(userID);
            if (CollectionUtil.isNotEmpty(calList)) {
                memberAchieveLog.setAsumRealPv(calList.get(0).getAsumRealPv());
                memberAchieveLog.setBsumRealPv(calList.get(0).getBsumRealPv());
                memberAchieveLog.setAsumPv(calList.get(0).getAsumPv());
                memberAchieveLog.setBsumPv(calList.get(0).getBsumPv());
                memberAchieveLog.setAbalance(calList.get(0).getAbalance());
                memberAchieveLog.setBbalance(calList.get(0).getBbalance());
//                memberAchieveLog.setRepASumPv(calList.get(0).getRepASumPv());
//                memberAchieveLog.setRepBSumPv(calList.get(0).getRepBSumPv());
//                memberAchieveLog.setRepABalance(calList.get(0).getRepABalance());
//                memberAchieveLog.setRepBBalance(calList.get(0).getRepBBalance());
            }
            baseMapper.insert(memberAchieveLog);

            String content = EApprovalBusiness.MEMBER_ACHIEVE.getLabel();
            String waterType = "";
            if (cuMemberAchieveLogVO.getInfusingWaterType() == EInfusingWaterType.ADD.getValue()) {
                waterType = EInfusingWaterType.ADD.getLabel();
            } else if (cuMemberAchieveLogVO.getInfusingWaterType() == EInfusingWaterType.BANK_TRANSFER.getValue()) {
                waterType = EInfusingWaterType.BANK_TRANSFER.getLabel();
            } else if (cuMemberAchieveLogVO.getInfusingWaterType() == EInfusingWaterType.FULL_PV.getValue()) {
                waterType = EInfusingWaterType.FULL_PV.getLabel();
            }
            if (cmal.getAfirstModifyPv().compareTo(BigDecimal.ZERO) > 0) {
                content = content + " 左区业绩" + waterType + ": " + cmal.getAfirstModifyPv();
            }
            if (cmal.getBfirstModifyPv().compareTo(BigDecimal.ZERO) > 0) {
                content = content + " 右区业绩" + waterType + ": " + cmal.getBfirstModifyPv();
            }
            if (cmal.getRepAFirstModifyBalance().compareTo(BigDecimal.ZERO) > 0) {
                content = content + " 左区结余" + waterType + ": " + cmal.getRepAFirstModifyBalance();
            }
            if (cmal.getRepBFirstModifyBalance().compareTo(BigDecimal.ZERO) > 0) {
                content = content + " 右区结余" + waterType + ": " + cmal.getRepBFirstModifyBalance();
            }

            //组装日志数据
            BusinessLogDTO businessLog = BusinessLogDTO.builder()
                    .businessType(EApprovalBusiness.MEMBER_ACHIEVE.getValue())
                    .businessCode(code)
                    .pkMember(cmal.getPkMember())
                    .content(content)
                    .pkCreator(userID)
                    .pkCountry(pkCountry)
                    .freeSignFlag(true).build();
            dtoList.add(businessLog);
        }
        //发送MQ到操作日志
        rabbitTemplate.convertAndSend(RabbitMqConstants.BUSINESS_LOG_EXCHANGE, RabbitMqConstants.BUSINESS_LOG_KEY, dtoList);
        return cuMemberAchieveLogVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CuMemberAchieveLogVO petition(CuMemberAchieveLogVO cuMemberAchieveLogVO) {
        Date date = new Date();
        Long userID = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        // 封装修改会员编号业务日志
        List<BusinessLogDTO> dtoList = new ArrayList<>();
        //业务单号
        String code = CommonUtil.createSerialNumber(EOrderPrefix.MEMBER_ACHIEVE.getValue());
        Set<Long> pkMemberIds = new HashSet<>();
        cuMemberAchieveLogVO.setPkAccount(cuMemberAchieveLogVO.getCmalvList().get(0).getPkAccount());

        for (CuMemberAchieveLogVO cmal : cuMemberAchieveLogVO.getCmalvList()) {
            //历史期业绩
            String datestr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cmal.getSettleDate());
            String month = DateUtils.getMonth(datestr);
            String tableName = TableNameConstants.CU_MEMBER_ACHIEVE + month;
            //查询最新期数
            int period = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(
                    DateUtils.parseDateToStr(datestr)).getPkId();
            cuMemberAchieveLogVO.setTableName(tableName);
            cuMemberAchieveLogVO.setPeriod(period);
            cuMemberAchieveLogVO.setPkMember(cmal.getPkMember());
            List<CuMemberAchieveLogVO> calList = new ArrayList<>();
            calList.add(cuMemberAchieveLogVO);
            calList = iCuMemberAchieveLogService.queryMemberAchieveHistoryList(calList);

            //免支付 或 需签呈+支付
            if (cuMemberAchieveLogVO.getProcessType() == EProcessType.AVOIDPAY.getValue() ||
                    cuMemberAchieveLogVO.getProcessType() == EProcessType.DOUBLEPAY.getValue()) {
                pkMemberIds.add(cmal.getPkMember());
                CuMemberAchieveLog memberAchieveLog = BeanUtil.copyProperties(cmal, CuMemberAchieveLog.class);
                memberAchieveLog.setASumRealModifyPv(cmal.getAsumRealModifyPv());
                memberAchieveLog.setBSumRealModifyPv(cmal.getBsumRealModifyPv());
                memberAchieveLog.setAFirstModifyPv(cmal.getAfirstModifyPv());
                memberAchieveLog.setBFirstModifyPv(cmal.getBfirstModifyPv());
                memberAchieveLog.setRepAFirstModifyBalance(cmal.getRepAFirstModifyBalance());
                memberAchieveLog.setRepBFirstModifyBalance(cmal.getRepBFirstModifyBalance());
                memberAchieveLog.setRepAModifySumPv(cmal.getRepAModifySumPv());
                memberAchieveLog.setRepBModifySumPv(cmal.getRepBModifySumPv());
                memberAchieveLog.setRepAModifyBalance(cmal.getRepAModifyBalance());
                memberAchieveLog.setRepBModifyBalance(cmal.getRepBModifyBalance());
                memberAchieveLog.setCreationTime(date);
                memberAchieveLog.setInfusingWaterType(cuMemberAchieveLogVO.getInfusingWaterType());
                memberAchieveLog.setProcessType(cuMemberAchieveLogVO.getProcessType());
                memberAchieveLog.setPkCreator(userID);
                memberAchieveLog.setPeriod(cmal.getPeriod());
                memberAchieveLog.setPkCountry(pkCountry);
                memberAchieveLog.setBusinessNo(code);
                memberAchieveLog.setCost(cmal.getCost());
                memberAchieveLog.setPkAccount(cmal.getPkAccount());
                memberAchieveLog.setRemarks(cmal.getRemarks());
                memberAchieveLog.setApproveStatus(EApproveStatus.ALREADY_SUBMIT.getValue());
                memberAchieveLog.setEditType(EApprovalBusiness.MEMBER_ACHIEVE.getValue());
                memberAchieveLog.setDueDate(cmal.getDueDate());
                memberAchieveLog.setSettleDate(cmal.getSettleDate());
                if (CollectionUtil.isNotEmpty(calList)) {
                    memberAchieveLog.setAsumRealPv(calList.get(0).getAsumRealPv());
                    memberAchieveLog.setBsumRealPv(calList.get(0).getBsumRealPv());
                    memberAchieveLog.setAsumPv(calList.get(0).getAsumPv());
                    memberAchieveLog.setBsumPv(calList.get(0).getBsumPv());
                    memberAchieveLog.setAbalance(calList.get(0).getAbalance());
                    memberAchieveLog.setBbalance(calList.get(0).getBbalance());
                    memberAchieveLog.setRepASumPv(calList.get(0).getRepASumPv());
                    memberAchieveLog.setRepBSumPv(calList.get(0).getRepBSumPv());
                    memberAchieveLog.setRepABalance(calList.get(0).getRepABalance());
                    memberAchieveLog.setRepBBalance(calList.get(0).getRepBBalance());
                }
                baseMapper.insert(memberAchieveLog);
                String content = EApprovalBusiness.MEMBER_ACHIEVE.getLabel();
                String waterType = "";
                //注水方式
                if (cuMemberAchieveLogVO.getInfusingWaterType() == EInfusingWaterType.ADD.getValue()) {
                    waterType = EInfusingWaterType.ADD.getLabel();
                } else if (cuMemberAchieveLogVO.getInfusingWaterType() == EInfusingWaterType.BANK_TRANSFER.getValue()) {
                    waterType = EInfusingWaterType.BANK_TRANSFER.getLabel();
                } else if (cuMemberAchieveLogVO.getInfusingWaterType() == EInfusingWaterType.FULL_PV.getValue()) {
                    waterType = EInfusingWaterType.FULL_PV.getLabel();
                }
                if (cmal.getAsumRealModifyPv().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 左区真实业绩" + waterType + ": " + cmal.getAsumRealModifyPv();
                }
                if (cmal.getBsumRealModifyPv().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 更后右区真实业绩" + waterType + ": " + cmal.getBsumRealModifyPv();
                }
                if (cmal.getAfirstModifyPv().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 左区首购业绩" + waterType + ": " + cmal.getAfirstModifyPv();
                }
                if (cmal.getBfirstModifyPv().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 右区首购业绩" + waterType + ": " + cmal.getBfirstModifyPv();
                }
                if (cmal.getRepAFirstModifyBalance().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 左区首购结余" + waterType + ": " + cmal.getRepAFirstModifyBalance();
                }
                if (cmal.getRepBFirstModifyBalance().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 右区首购结余" + waterType + ": " + cmal.getRepBFirstModifyBalance();
                }
                if (cmal.getRepAModifySumPv().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 左区复购业绩累计业绩" + waterType + ": " + cmal.getRepAModifySumPv();
                }
                if (cmal.getRepBModifySumPv().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 右区复购业绩累计业绩" + waterType + ": " + cmal.getRepBModifySumPv();
                }
                if (cmal.getRepAModifyBalance().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 左区复购结余" + waterType + ": " + cmal.getRepAModifyBalance();
                }
                if (cmal.getRepBModifyBalance().compareTo(BigDecimal.ZERO) > 0) {
                    content = content + " 右区复购结余" + waterType + ": " + cmal.getRepBModifyBalance();
                }

                //组装日志数据
                BusinessLogDTO businessLog = BusinessLogDTO.builder()
                        .businessType(EApprovalBusiness.MEMBER_ACHIEVE.getValue())
                        .businessCode(code)
                        .pkMember(cmal.getPkMember())
                        .content(content)
                        .pkCreator(userID)
                        .pkCountry(pkCountry)
                        .freeSignFlag(false).build();
                dtoList.add(businessLog);
            }
        }
        //生成签呈
        LoginUser loginUser = userTokenService.getLoginUser();
        ApprovalSubmitDTO approvalSubmitDTO = ApprovalSubmitDTO.builder().eApprovalBusiness(EApprovalBusiness.MEMBER_ACHIEVE)
                .businessCode(code)
                .signType(ESignType.getEnumByValue(cuMemberAchieveLogVO.getSignType()))
                .remark(cuMemberAchieveLogVO.getRemark())
                .pkMemberList(cuMemberAchieveLogVO.getPkMemberList())
                .userIdList(cuMemberAchieveLogVO.getUserIdList())
                .sendIdList(cuMemberAchieveLogVO.getSendIdList())
                .build();
        R<String> submit = iApprovalServiceApi.submit(approvalSubmitDTO, loginUser);
        if (!submit.isSuccess()) {
            log.error("====" + submit.getData());
            throw new RuntimeException(ConfigMsgConstants.MEMBER_ACHIEVE_DEDUCT_PETITION);
        }

        //扣钱
        if (cuMemberAchieveLogVO.getProcessType() == EProcessType.DOUBLEPAY.getValue()) {
            BusinessCommissionDTO businessCommissionDTO = new BusinessCommissionDTO();
            businessCommissionDTO.setTradeCode(code);
            businessCommissionDTO.setPkCountry(pkCountry);
            businessCommissionDTO.setPkAccount(cuMemberAchieveLogVO.getPkAccount());
            businessCommissionDTO.setMemberAmountDTOList(cuMemberAchieveLogVO.getMemberAmountDTOList());
            businessCommissionDTO.setTradeType(EApprovalBusiness.MEMBER_ACHIEVE.getValue());
            Boolean b = iCuMemberTradeService.businessCommissionSubmit(businessCommissionDTO);
            if (!b) {
                throw new ServiceException(ConfigMsgConstants.MEMBER_ACHIEVE_DEDUCT_MONEY);
            }
        }
        //发送MQ到操作日志
        rabbitTemplate.convertAndSend(RabbitMqConstants.BUSINESS_LOG_EXCHANGE, RabbitMqConstants.BUSINESS_LOG_KEY,
                dtoList);
        return cuMemberAchieveLogVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean toExamine(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        Date date = new Date();
        Long userID = approvalBusinessResultDTO.getLoginUser().getUserId();
        Integer pkCountry = approvalBusinessResultDTO.getLoginUser().getDataCountry();
        QueryWrapper<CuMemberAchieveLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BUSINESS_NO", approvalBusinessResultDTO.getBusinessCode());
        queryWrapper.eq("APPROVE_STATUS", EApproveStatus.ALREADY_SUBMIT.getValue());
        List<CuMemberAchieveLog> malList = baseMapper.selectList(queryWrapper);
        //审核通过
        if (approvalBusinessResultDTO.getEApproveStatus().getValue() == EApproveStatus.FINISH.getValue()) {
            if (CollectionUtil.isNotEmpty(malList)) {
                for (CuMemberAchieveLog cmal : malList) {
                    //修改日志表审核状态
                    UpdateWrapper<CuMemberAchieveLog> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("PK_MEMBER", cmal.getPkMember());
                    updateWrapper.eq("BUSINESS_NO", cmal.getBusinessNo());
                    updateWrapper.set("APPROVE_STATUS", EApproveStatus.FINISH.getValue());
                    updateWrapper.set("MODIFIED_TIME", date);
                    updateWrapper.set("PK_MODIFIED", userID);
                    baseMapper.update(null, updateWrapper);
                }
            }
        }
        //审核驳回
        if (approvalBusinessResultDTO.getEApproveStatus().getValue() == EApproveStatus.REJECTED.getValue()) {
            List<MemberAmountDTO> memberAmountDTOList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(malList)) {
                for (CuMemberAchieveLog cmal : malList) {
                    if (StringUtils.isNotNull(cmal.getCost()) && cmal.getCost().compareTo(BigDecimal.ZERO) > 0) {
                        MemberAmountDTO memberAmountDTO = new MemberAmountDTO();
                        memberAmountDTO.setPkMember(cmal.getPkMember());
                        memberAmountDTO.setTradeAmount(cmal.getCost());
                        memberAmountDTO.setPkAccount(cmal.getPkAccount());
                        memberAmountDTOList.add(memberAmountDTO);
                    }
                    //修改日志表审核状态
                    CuMemberAchieveLog memberAchieveLog = BeanUtil.copyProperties(cmal, CuMemberAchieveLog.class);
                    memberAchieveLog.setModifiedTime(date);
                    memberAchieveLog.setPkModified(userID);
                    memberAchieveLog.setPkCountry(pkCountry);
                    memberAchieveLog.setApproveStatus(EApproveStatus.REJECTED.getValue());
                    UpdateWrapper<CuMemberAchieveLog> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("PK_MEMBER", cmal.getPkMember());
                    updateWrapper.eq("BUSINESS_NO", cmal.getBusinessNo());
                    baseMapper.update(memberAchieveLog, updateWrapper);
                }
                if (CollectionUtil.isNotEmpty(memberAmountDTOList)) {
                    //退款
                    BusinessCommissionDTO businessCommissionDTO = new BusinessCommissionDTO();
                    businessCommissionDTO.setPkCountry(pkCountry);
                    businessCommissionDTO.setPkAccount(memberAmountDTOList.get(0).getPkAccount());
                    businessCommissionDTO.setMemberAmountDTOList(memberAmountDTOList);
                    businessCommissionDTO.setTradeType(EApprovalBusiness.MEMBER_ACHIEVE.getValue());
                    businessCommissionDTO.setTradeCode(approvalBusinessResultDTO.getBusinessCode());
                    businessCommissionDTO.setPkCreator(approvalBusinessResultDTO.getLoginUser().getUserId());
                    Boolean result = iCuMemberTradeService.businessCommissionSubmit(businessCommissionDTO, EApproveStatus.REJECTED);
                    if (!result) {
                        throw new ServiceException(ConfigMsgConstants.MEMBER_ACHIEVE_IS_REFUND);
                    }
                }
            }
        }
        return true;
    }

    @Override
    public List<CuMemberAchieveLogVO> checkAchieveList(CuMemberAchieveLogVO memberAchieveLogVO) {
        return baseMapper.checkAchieveList(memberAchieveLogVO);
    }

}
