package com.hzs.member.handlebusiness.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.account.CuMemberAccount;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.business.SignOnlineInfoVo;
import com.hzs.common.domain.member.detail.CuMemberServiceLog;
import com.hzs.common.domain.member.handleBusiness.CuHandleBusiness;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.sys.SysUser;
import com.hzs.common.security.service.UserTokenService;
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.MemberBusinessBalanceDTO;
import com.hzs.member.account.service.ICuMemberAccountService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.service.ICuMemberTradeService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.detail.service.ICuMemberServiceLogService;
import com.hzs.member.detail.vo.CuMemberServiceLogVO;
import com.hzs.member.handlebusiness.ICuHandleBusinessServiceApi;
import com.hzs.member.handlebusiness.dto.AnalysisBusinessAppealParam;
import com.hzs.member.handlebusiness.service.ICuHandleBusinessService;
import com.hzs.member.handlebusiness.vo.*;
import com.hzs.system.base.ICountryServiceApi;
import com.hzs.system.config.IAwardsServiceApi;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.IServiceChargeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import com.hzs.system.config.dto.ServiceChargeDTO;
import com.hzs.system.sys.IApprovalServiceApi;
import com.hzs.system.sys.dto.ApprovalInfoDTO;
import com.hzs.system.sys.dto.ApprovalItemInfoDTO;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.http.client.utils.DateUtils;
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.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 业务办理表 前端控制器
 */
@RestController
@RequestMapping("/manage/handle-business")
public class CuHandleBusinessController extends BaseController {

    @Autowired
    private ICuHandleBusinessService cuHandleBusinessService;
    @Autowired
    private ICuMemberService cuMemberService;
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private ICuMemberServiceLogService memberServiceLogService;
    @Autowired
    private ICuMemberTradeService cuMemberTradeService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;
    @Autowired
    private ICuMemberBaseService cuMemberBaseService;
    @Autowired
    private ICuMemberAccountService cuMemberAccountService;

    @DubboReference
    IGradeServiceApi gradeService;
    @DubboReference
    IAwardsServiceApi awardsService;
    @DubboReference
    ICountryServiceApi countryServiceApi;
    @DubboReference
    IServiceChargeServiceApi serviceChargeServiceApi;
    @DubboReference
    ICuHandleBusinessServiceApi handleBusinessServiceApi;
    @DubboReference
    IApprovalServiceApi approvalServiceApi;


    /**
     * 查询办理业务列表
     **/
    @AccessPermissions("BasicBusiness")
    @Log(module = EOperationModule.HANDLE_BUSINESS, business = EOperationBusiness.BASE_BUSINESS, method = EOperationMethod.SELECT)
    @GetMapping("/businessList")
    public TableDataInfo businessList(HandleBusinessShowVO handleBusinessVO) {
        if (StringUtils.isEmpty(handleBusinessVO.getMemberCode()) &&
                StringUtils.isEmpty(handleBusinessVO.getMemberName()) &&
                StringUtils.isNull(handleBusinessVO.getPkAwards()) &&
                StringUtils.isNull(handleBusinessVO.getVertexId()) &&
                StringUtils.isNull(handleBusinessVO.getTeamId()) &&
                StringUtils.isEmpty(handleBusinessVO.getPhone()) &&
                StringUtils.isEmpty(handleBusinessVO.getCenterMemberCode()) &&
                StringUtils.isEmpty(handleBusinessVO.getCenterMemberName()) &&
                StringUtils.isNull(handleBusinessVO.getStartDate()) &&
                StringUtils.isNull(handleBusinessVO.getEndDate()) &&
                StringUtils.isNull(handleBusinessVO.getStartDatePay()) &&
                StringUtils.isNull(handleBusinessVO.getEndDatePay())
        ) {
            return getDataTable(new ArrayList<>());
        }
        handleBusinessVO.setPkCountry(SecurityUtils.getPkCountry());
        handleBusinessVO.setSystemType(SecurityUtils.getSystemType());
        startPage();
        List<HandleBusinessShowVO> list = cuHandleBusinessService.queryHandleBusinessList(handleBusinessVO);
        return getDataTable(list);
    }

    /**
     * 保存业务办理
     *
     * @param handleBusinessVO 业务办理表Vo
     * @return AjaxResult
     */
    @Log(module = EOperationModule.HANDLE_BUSINESS, business = EOperationBusiness.BASE_BUSINESS, method = EOperationMethod.SUBMIT)
    @PostMapping("/save")
    public AjaxResult saveHandleBusiness(@RequestBody HandleBusinessVO handleBusinessVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        LoginUser loginUser = userTokenService.getLoginUser();
        handleBusinessVO.setPkCountry(pkCountry);
        Integer processType = handleBusinessVO.getProcessType();
        if (null == processType) {
            return AjaxResult.error("请选择按钮类型！");
        }
        return toAjax(cuHandleBusinessService.businessHandling(handleBusinessVO, loginUser));
    }

    /**
     * 校验同姓名同手机号是否满足规则
     **/
    public List<BusinessErrorVO> checkNamePhone(List<BusinessMemberVO> businessMemberList) {
        //如果只有修改姓名业务或者修改手机业务
        boolean has100 = false;
        boolean has101 = false;
        List<BusinessErrorVO> errorList = new ArrayList<>();
        //去重
        businessMemberList = businessMemberList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                new TreeSet<>(Comparator.comparing(v -> v.getBusinessType() + "-" + StringUtils.join(v.getPkMember(), ",")))), ArrayList::new));
        Map<String, List<BusinessMemberVO>> buTypeMap = businessMemberList.stream()
                .collect(Collectors.groupingBy(BusinessMemberVO::getMemberCode));
        // 检查列表中每个元素的 businessType
        for (BusinessMemberVO item : businessMemberList) {
            int businessType = item.getBusinessType();
//            if (businessType == EApprovalBusiness.CHANGE_NAME.getValue()) {
//                has100 = true;
//            } else
            if (businessType == EApprovalBusiness.CHANGE_PHONE.getValue()) {
                has101 = true;
            }
            // 存在两种业务
            if (has100 && has101) {
                break;
            }
        }

        // 存在两种业务
        if (has100 && has101) {
            for (Map.Entry<String, List<BusinessMemberVO>> entry : buTypeMap.entrySet()) {
                BusinessErrorVO errorVO = new BusinessErrorVO();
                String memberCode = entry.getKey();
                errorVO.setMemberCode(memberCode);
                //查询推荐人信息
                CuMember member = cuMemberService.getParentMember(memberCode);
                List<BusinessMemberVO> hbList = entry.getValue();
                for (BusinessMemberVO item : hbList) {
                    if (item.getBusinessType().equals(EApprovalBusiness.CHANGE_NAME.getValue())) {
                        member.setMemberName(item.getNewValue());
                    }
                    if (item.getBusinessType().equals(EApprovalBusiness.CHANGE_PHONE.getValue())) {
                        member.setPhone(item.getNewValue());
                    }
                }
                //查询是否满足规则
                String msg = cuMemberService.checkRegisterRelation(member.getPhone(), member.getMemberCode());
                if (msg != null) {
                    errorVO.setErrorMsg(msg);
                    errorList.add(errorVO);
                }
            }
        } else if (has100 || has101) {
            // 只存在一种业务
            for (Map.Entry<String, List<BusinessMemberVO>> entry : buTypeMap.entrySet()) {
                BusinessErrorVO errorVO = new BusinessErrorVO();
                String memberCode = entry.getKey();
                errorVO.setMemberCode(memberCode);
                //查询推荐人信息
                CuMember member = cuMemberService.getParentMember(memberCode);
                List<BusinessMemberVO> hbList = entry.getValue();
                for (BusinessMemberVO item : hbList) {
                    if (item.getBusinessType().equals(EApprovalBusiness.CHANGE_NAME.getValue())) {
                        member.setMemberName(item.getNewValue());
                        member.setPhone(item.getPhone());
                    } else if (item.getBusinessType().equals(EApprovalBusiness.CHANGE_PHONE.getValue())) {
                        member.setMemberName(item.getMemberName());
                        member.setPhone(item.getNewValue());
                    }
                }
                //查询是否满足规则
                String msg = cuMemberService.checkRegisterRelation(member.getPhone(), member.getMemberCode());
                if (msg != null) {
                    errorVO.setErrorMsg(msg);
                    errorList.add(errorVO);
                }
            }
        }
        return errorList;
    }

    /**
     * 业务办理导入数据
     **/
    @PostMapping("/importData")
    @Log(module = EOperationModule.HANDLE_BUSINESS, business = EOperationBusiness.BASE_BUSINESS, method = EOperationMethod.IMPORT)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<HandleBusinessShowVO> util = new ExcelUtil<>(HandleBusinessShowVO.class);
        List<HandleBusinessShowVO> list = util.importExcel(file.getInputStream());
        List<HandleBusinessShowVO> newlist = new ArrayList<>();
        List<HandleBusinessShowVO> errorlist = new ArrayList<>();
        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer systemType = SecurityUtils.getSystemType();
        if (CollectionUtil.isNotEmpty(list)) {
            for (HandleBusinessShowVO hbv : list) {
                hbv.setPkCountry(pkCountry);
                hbv.setSystemType(systemType);
                List<HandleBusinessShowVO> listNew = cuHandleBusinessService.queryHandleBusinessList(hbv);
                if (CollectionUtil.isEmpty(listNew)) {
                    errorlist.add(hbv);
                    continue;
                } else {
                    listNew.get(0).setNewMemberName(hbv.getNewMemberName());
                    listNew.get(0).setNewPhone(hbv.getNewPhone());
                }
                newlist.add(listNew.get(0));
            }
        }
        if (CollectionUtil.isNotEmpty(errorlist)) {
            return AjaxResult.error(504, TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID), errorlist);
        }
        return AjaxResult.success(newlist);
    }

    /**
     * 下载模板
     *
     * @return
     */
    @PostMapping("/download-template")
    @Log(module = EOperationModule.RECHARGE, method = EOperationMethod.EXPORT)
    public void downloadTemplate(HttpServletResponse response) {
        ExcelUtil<HandleBusinessShowVO> util = new ExcelUtil<>(HandleBusinessShowVO.class);
        util.exportExcel(response, null, TransactionUtils.getContent(ConfigMsgConstants.BASIC_BUSINESS));
    }

    /**
     * 办理业务-基础业务校验
     **/
    @Log(module = EOperationModule.HANDLE_BUSINESS, business = EOperationBusiness.BASE_BUSINESS, method = EOperationMethod.SELECT)
    @PostMapping("/businessCheck")
    public AjaxResult businessCheck(@RequestBody HandleBusinessVO businessSingleVO) {
        List<BusinessErrorVO> errorMemberList = new ArrayList<>();
        Integer pkCountry = SecurityUtils.getPkCountry();
        //业务类型选中值后再传参数
        if (CollectionUtil.isEmpty(businessSingleVO.getHandleBusinessList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WORK_TYPE_NOT_NULL));

        }
        List<Integer> businessValueList = new ArrayList<>();
        businessSingleVO.getHandleBusinessList().forEach(itm ->
                businessValueList.addAll(itm.getBusinessValueList())
        );
        if (CollectionUtil.isEmpty(businessValueList)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WORK_TYPE_NOT_NULL));
        }
        if (CollectionUtil.isEmpty(businessSingleVO.getBusinessMemberList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_NOT_NULL));
        }
        for (BusinessMemberVO uc : businessSingleVO.getBusinessMemberList()) {
            if (StringUtils.isEmpty(uc.getMemberCode())) {
                BusinessErrorVO bev = new BusinessErrorVO();
                bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
                bev.setMemberCode(uc.getMemberCode());
                errorMemberList.add(bev);
            }
            if (StringUtils.isNull(uc.getPkMember())) {
                BusinessErrorVO bev = new BusinessErrorVO();
                bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.PKID_NOT_NULL));
                bev.setMemberCode(uc.getMemberCode());
                errorMemberList.add(bev);
            }
        }
        if (CollectionUtil.isNotEmpty(errorMemberList)) {
            return AjaxResult.error(504, ConfigMsgConstants.MEMBER_DATA_ERROR, errorMemberList);
        }
        // 获取需要翻译的枚举翻译
        Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EApprovalBusiness.values(), EModifyName.values(),
                EModifyPhone.values(), EResetPassword.values(), EResetBank.values(), EProfitState.values(), ESignInState.values(),
                EMemberAccount.values(), EFreeAssessmentState.values(), ERegistrationAuthority.values(),
                EYesNoAttestation.values(), EDealerYesNo.values(), EYesNo.values(), ESignInState.values());
        /**
         * 会员编号
         */
        List<Long> pkMemberList = new ArrayList<>();
        /**
         * 审批业务类型 来源于枚举  EApprovalBusiness
         */
        List<Integer> editTypeList = new ArrayList<>();
        for (BusinessMemberVO bmv : businessSingleVO.getBusinessMemberList()) {
            pkMemberList.add(bmv.getPkMember());
            if (StringUtils.isNotEmpty(bmv.getNewMemberName()) &&
                    StringUtils.isNotEmpty(bmv.getMemberName())) {
                bmv.setBusinessType(EApprovalBusiness.CHANGE_NAME.getValue());
                bmv.setOldValue(bmv.getMemberName());
                bmv.setNewValue(bmv.getNewMemberName());
            }
        }
        BusinessTableVO btvo = new BusinessTableVO();
        for (BusinessTypeValueVO btv : businessSingleVO.getHandleBusinessList()) {
            if (CollectionUtil.isNotEmpty(btv.getBusinessValueList())) {
                editTypeList.add(btv.getBusinessType());
            }
            btvo.setTable1(EYesNo.YES.getIntValue());
            btvo.setTable2(EYesNo.YES.getIntValue());
            if (CollectionUtil.isNotEmpty(btv.getBusinessValueList()) &&
                    btv.getBusinessType() == EApprovalBusiness.CHANGE_NAME.getValue()) {
                btvo.setTable3(EYesNo.YES.getIntValue());
                btvo.setTable4(EYesNo.YES.getIntValue());
            }
            if (CollectionUtil.isNotEmpty(btv.getBusinessValueList()) && btv.getBusinessType() == EApprovalBusiness.CHANGE_PHONE.getValue()) {
                btvo.setTable5(EYesNo.YES.getIntValue());
                btvo.setTable6(EYesNo.YES.getIntValue());
                btvo.setTable7(EYesNo.YES.getIntValue());
            }
        }
        businessSingleVO.setBtvo(btvo);
        //校验重复审核
        CuMemberServiceLogVO cuMemberServiceLogVO = new CuMemberServiceLogVO();
        cuMemberServiceLogVO.setPkMemberList(pkMemberList);
        cuMemberServiceLogVO.setEditTypeList(editTypeList);
        List<CuMemberServiceLogVO> memberServiceLog = memberServiceLogService.selectServiceLogList(cuMemberServiceLogVO);
        if (CollectionUtil.isNotEmpty(memberServiceLog)) {
            for (CuMemberServiceLogVO cs : memberServiceLog) {
                BusinessErrorVO bev = new BusinessErrorVO();
                bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_UPDATE_CODE_REPEAT));
                bev.setMemberCode(cs.getMemberCode());
                errorMemberList.add(bev);
            }
        }

        // 查询国家
        R<Map<Integer, BdCountry>> countryAllMap = countryServiceApi.getAllCountryMap();
        //查询会员原值
        BusinessMemberVO businessMemberVO = new BusinessMemberVO();
        businessMemberVO.setPkMemberList(pkMemberList);
        List<BusinessMemberVO> businessOldMemberList = memberServiceLogService.selectOldValue(businessMemberVO);
        //发起签呈会员信息集合(入库数据)
        List<BusinessMemberVO> businessMemberList = new ArrayList<>();
        //返回页面会员信息集合（发起签呈页面展示）
        List<BusinessMemberVO> signMemberList = new ArrayList<>();

        //返回页面会员信息集合（确认页面展示）
        List<BusinessMemberVO> businessNewMemberList = new ArrayList<>();
        //校验会员金额是否充足临时集合
        List<BusinessCostVO> businessCostVOList = new ArrayList<>();
        //校验办理业务的费用
        List<BusinessTypeValueVO> handleBusinessCostList = new ArrayList<>();
        R<List<GradeDTO>> gradeList = gradeService.queryGradeConfigByCondition(pkCountry);
        R<List<BdAwards>> awList = awardsService.queryAwards(pkCountry);
        //会员信息集合
        int count = 1;//计数器
        for (BusinessMemberVO uc : businessSingleVO.getBusinessMemberList()) {
            //返回页面会员信息（确认页面显示）
            BusinessMemberVO nuc = new BusinessMemberVO();
            BigDecimal singleAmountTotal = new BigDecimal(0);
            if (CollectionUtil.isNotEmpty(businessOldMemberList)) {
                for (BusinessMemberVO cov : businessOldMemberList) {
                    if (cov.getPkMember().intValue() == uc.getPkMember().intValue()) {
                        nuc.setMemberCode(cov.getMemberCode());
                        nuc.setMemberName(cov.getMemberName());
                        nuc.setPhone(cov.getPhone());
                        if (StringUtils.isNotNull(uc.getEndValidityDate())) {
                            LocalDateTime localDateTime = uc.getEndValidityDate().toInstant()
                                    .atZone(ZoneId.systemDefault())
                                    .toLocalDateTime();
                            LocalDateTime modifiedDateTime = localDateTime
                                    .withHour(23)
                                    .withMinute(59)
                                    .withSecond(59);
                            // 将 LocalDateTime 转换回 Date
                            Date modifiedDate = Date.from(modifiedDateTime
                                    .atZone(ZoneId.systemDefault())
                                    .toInstant());
                            uc.setEndValidityDate(modifiedDate);
                        }
                        nuc.setEndValidityDate(uc.getEndValidityDate());
                        nuc.setAreaId(uc.getAreaId());
                        cov.setAreaId(uc.getAreaId());
                    }
                }
            }
            //循环业务类型集合

            BigDecimal singleAmount100 = new BigDecimal(0);
            for (BusinessTypeValueVO etv : businessSingleVO.getHandleBusinessList()) {
                if (CollectionUtil.isNotEmpty(etv.getBusinessValueList())) {
                    //发起签呈列表集合对象（页面显示）
                    BusinessMemberVO sig = new BusinessMemberVO();
                    BusinessTypeValueVO nbtvo = BeanUtil.copyProperties(etv, BusinessTypeValueVO.class);
                    sig.setPkMember(uc.getPkMember());
                    sig.setMemberName(uc.getMemberName());
                    sig.setMemberCode(uc.getMemberCode());
                    sig.setBusinessType(etv.getBusinessType());
                    sig.setPhone(uc.getPhone());
                    sig.setEndValidityDate(uc.getEndValidityDate());
                    nbtvo.setPkMember(uc.getPkMember());
                    nbtvo.setMemberCode(uc.getMemberCode());
                    nbtvo.setMemberName(uc.getMemberName());
                    nbtvo.setPkId(count);
                    sig.setPkId(count);
                    count++;
                    if (etv.getBusinessType() != EApprovalBusiness.CHANGE_NAME.getValue() &&
                            etv.getBusinessType() != EApprovalBusiness.CHANGE_PHONE.getValue() &&
                            etv.getBusinessType() != EApprovalBusiness.RESET_LOGIN_PASSWORD.getValue()) {
                        nbtvo.setAfterTheChange(ConfigMsgConstants.AFTER_THE_CHANGE);
                        nbtvo.setButtonType(EYesNo.YES.getIntValue());
                    }
                    //发起签呈集合对象
                    BusinessMemberVO bum = BeanUtil.copyProperties(sig, BusinessMemberVO.class);
                    //当前最大次数
                    Integer approveNumber = memberServiceLogService.selectByMaxApproveNumber(uc.getPkMember(), etv.getBusinessType(), EApproveStatus.FINISH.getValue());
                    if (StringUtils.isNull(approveNumber)) {
                        //当前最大次数
                        approveNumber = 0;
                    }
                    //查询扣费金额，账户信息 singleAmount
                    ServiceChargeDTO serviceChargeDTO = new ServiceChargeDTO();
                    serviceChargeDTO.setType(etv.getBusinessType());
                    serviceChargeDTO.setQuantity(approveNumber + 1);
                    serviceChargeDTO.setPkCountry(SecurityUtils.getPkCountry());
                    R<ServiceChargeDTO> serviceChargeDtoR = serviceChargeServiceApi.getServiceChargeByType(serviceChargeDTO);
                    if (StringUtils.isNotNull(serviceChargeDtoR.getData()) &&
                            StringUtils.isNotNull(serviceChargeDtoR.getData().getPkAccount()) &&
                            StringUtils.isNotNull(serviceChargeDtoR.getData().getCost())) {
                        bum.setSingleAmount(serviceChargeDtoR.getData().getCost());
                        bum.setApproveNumber(approveNumber);
                        nbtvo.setPkAccountKey(cuMemberBaseService.translateAccountValue(serviceChargeDtoR.getData().getPkAccount()));
                        nuc.setPkAccountKey(cuMemberBaseService.translateAccountValue(serviceChargeDtoR.getData().getPkAccount()));
                        nbtvo.setPkAccount(serviceChargeDtoR.getData().getPkAccount());
                        nbtvo.setDeductionCurrency(ConfigMsgConstants.DEDUCTION_CURRENCY);
                        nbtvo.setCost(serviceChargeDtoR.getData().getCost());
                        nbtvo.setTradeAmount(serviceChargeDtoR.getData().getCost());
                        singleAmountTotal = singleAmountTotal.add(serviceChargeDtoR.getData().getCost());
                    } else {
                        BusinessErrorVO bev = new BusinessErrorVO();
                        bev.setErrorMsg(transactionMap.get(EnumsPrefixConstants.APPROVAL_BUSINESS + etv.getBusinessType()) + " " + TransactionUtils.getContent(ConfigMsgConstants.BUSINESS_ACCOUNT_IS_NULL));
                        bev.setMemberCode(uc.getMemberCode());
                        errorMemberList.add(bev);
                    }

                    //修改会员姓名校验姓名不能为空
                    if (StringUtils.isNotNull(etv.getBusinessType()) && etv.getBusinessType() == EApprovalBusiness.CHANGE_NAME.getValue()) {
                        if (StringUtils.isEmpty(uc.getNewValue())) {
                            BusinessErrorVO bev = new BusinessErrorVO();
                            bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NAME_NOT_NULL));
                            bev.setMemberCode(uc.getMemberCode());
                            errorMemberList.add(bev);
                        }
                    }
                    //修改会员手机校验手机不能为空
                    if (StringUtils.isNotNull(etv.getBusinessType()) && etv.getBusinessType() == EApprovalBusiness.CHANGE_PHONE.getValue()) {
                        if (StringUtils.isEmpty(uc.getNewPhone())) {
                            BusinessErrorVO bev = new BusinessErrorVO();
                            bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_PHONE_NOT_NULL));
                            bev.setMemberCode(uc.getMemberCode());
                            errorMemberList.add(bev);
                        }
                    }
                    //根据业务类型更新会员原值
                    if (CollectionUtil.isNotEmpty(businessOldMemberList)) {
                        for (BusinessMemberVO cov : businessOldMemberList) {
                            if (cov.getPkMember().intValue() == uc.getPkMember().intValue()) {
                                //修改姓名
                                if (etv.getBusinessType() == EApprovalBusiness.CHANGE_NAME.getValue()) {
                                    nuc.setSingleAmount100(singleAmount100);
                                    nuc.setNewMemberName(uc.getNewMemberName());
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.EMODIFY_NAME + etv.getBusinessValueList().get(0)));
                                    sig.setOldValue(uc.getMemberName());
                                    sig.setNewValue(uc.getNewMemberName());
                                    bum.setOldValue(uc.getMemberName());
                                    bum.setNewValue(uc.getNewMemberName());
                                    break;
                                }
                                //修改手机号
                                if (etv.getBusinessType() == EApprovalBusiness.CHANGE_PHONE.getValue()) {
                                    nuc.setNewPhone(uc.getNewPhone());
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.EMODIFY_PHONE + etv.getBusinessValueList().get(0)));
                                    sig.setOldValue(uc.getPhone());
                                    sig.setNewValue(uc.getNewPhone());
                                    bum.setOldValue(uc.getPhone());
                                    bum.setNewValue(uc.getNewPhone());
                                    break;
                                }
                                //重置会员密码
                                if (etv.getBusinessType() == EApprovalBusiness.RESET_LOGIN_PASSWORD.getValue()) {
                                    bum.setOldValue("  -  ");
                                    bum.setNewValue("*****");
                                    sig.setOldValue("  -  ");
                                    sig.setNewValue("*****");
                                    if (etv.getBusinessValueList().get(0) == EResetPassword.LOGIN_PASSWORD.getValue()) {
                                        nuc.setNewResetPassword("  -  ");
                                        nuc.setOldResetPassword("*****");
                                    }
                                    if (etv.getBusinessValueList().get(0) == EResetPassword.PAY_PASSWORD.getValue()) {
                                        nuc.setNewPaymentPassword("  -  ");
                                        nuc.setOldPaymentPassword("*****");
                                    }
                                    if (etv.getBusinessValueList().get(0) == EResetPassword.ALL_PASSWORD.getValue()) {
                                        nuc.setNewResetPassword("  -  ");
                                        nuc.setOldResetPassword("*****");
                                        nuc.setNewPaymentPassword("  -  ");
                                        nuc.setOldPaymentPassword("*****");
                                    }
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.ERESET_PASSWORD + etv.getBusinessValueList().get(0)));
                                    break;
                                }
                                //修改注册等级
                                if (etv.getBusinessType() == EApprovalBusiness.CHANGE_REGISTER_GRADE.getValue()) {
                                    bum.setOldValue(cov.getPkRegisterGrade().toString());
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));

                                    //更新会员新值
                                    uc.setNewValue(newValue(etv.getBusinessValueList()));
                                    for (GradeDTO gr : gradeList.getData()) {
                                        if (etv.getBusinessValueList().get(0).intValue() == gr.getPkId().intValue()) {
                                            nbtvo.setNewValue(gr.getGradeName());
                                            sig.setNewValue(gr.getGradeName());
                                            nuc.setNewPkRegisterGrade(gr.getGradeName());
                                        }
                                        if (cov.getPkRegisterGrade().intValue() == gr.getPkId().intValue()) {
                                            sig.setOldValue(gr.getGradeName());
                                            nuc.setOldPkRegisterGrade(gr.getGradeName());
                                        }
                                    }
                                    break;
                                }
                                //修改奖衔
                                if (etv.getBusinessType() == EApprovalBusiness.CHANGE_AWARDS.getValue()) {
                                    bum.setOldValue(cov.getPkAwards().toString());
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    bum.setEndValidityDate(uc.getEndValidityDate());
                                    for (BdAwards ba : awList.getData()) {
                                        if (etv.getBusinessValueList().get(0).intValue() == ba.getPkId().intValue()) {
                                            nbtvo.setNewValue(ba.getAwardsName());
                                            sig.setNewValue(ba.getAwardsName());
                                            sig.setEndValidityDate(uc.getEndValidityDate());
                                            nuc.setNewPkAwards(ba.getAwardsName());

                                        }
                                        if (cov.getPkAwards().intValue() == ba.getPkId().intValue()) {
                                            sig.setOldValue(ba.getAwardsName());
                                            nuc.setOldPkAwards(ba.getAwardsName());
                                        }
                                    }
                                    break;
                                }
                                //修改历史最高奖衔
                                if (etv.getBusinessType() == EApprovalBusiness.CHANGE_MAX_AWARDS.getValue()) {
                                    if (cov.getPkMaxAwards() != null) {
                                        bum.setOldValue(cov.getPkMaxAwards().toString());
                                    }
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));

                                    for (BdAwards ba : awList.getData()) {
                                        if (etv.getBusinessValueList().get(0).intValue() == ba.getPkId().intValue()) {
                                            nbtvo.setNewValue(ba.getAwardsName());
                                            sig.setNewValue(ba.getAwardsName());
                                            nuc.setNewPkMaxAwards(ba.getAwardsName());
                                        }
                                        if (cov.getPkMaxAwards() != null && (cov.getPkMaxAwards().intValue() == ba.getPkId().intValue())) {
                                            sig.setOldValue(ba.getAwardsName());
                                            nuc.setOldPkMaxAwards(ba.getAwardsName());
                                        }
                                    }
                                    break;
                                }
                                //修改会员礼包收货地址
                                if (etv.getBusinessType() == EApprovalBusiness.CU_MEMBER_GIFT.getValue()) {
                                    nbtvo.setNewValue(etv.getBusinessValueList().get(0).toString());
                                    sig.setNewValue(etv.getBusinessValueList().get(0).toString());
                                    break;
                                }
                                //重置银行
                                if (etv.getBusinessType() == EApprovalBusiness.RESET_BANK.getValue()) {
                                    bum.setOldValue("****");
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    nuc.setOldBank("****");
                                    nuc.setNewBank("  -  ");
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.ERESET_BANK + etv.getBusinessValueList().get(0)));
                                    sig.setNewValue(transactionMap.get(EnumsPrefixConstants.ERESET_BANK + etv.getBusinessValueList().get(0)));
                                    break;
                                }
                                //修改结算等级
                                if (etv.getBusinessType() == EApprovalBusiness.CHANGE_SETTLEMENT_GRADE.getValue()) {
                                    bum.setOldValue(cov.getPkSettleGrade().toString());
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    bum.setAreaId(cov.getAreaId());
                                    for (GradeDTO gr : gradeList.getData()) {
                                        if (etv.getBusinessValueList().get(0).intValue() == gr.getPkId().intValue()) {
                                            nbtvo.setNewValue(gr.getGradeName());
                                            sig.setNewValue(gr.getGradeName());
                                            nuc.setNewPkSettleGrade(gr.getGradeName());
                                        }
                                        if (cov.getPkSettleGrade().intValue() == gr.getPkId().intValue()) {
                                            sig.setOldValue(gr.getGradeName());
                                            nuc.setOldPkSettleGrade(gr.getGradeName());
                                        }
                                    }
                                    break;
                                }
                                //修改自然国
                                if (etv.getBusinessType() == EApprovalBusiness.CHANGE_NATURAL_COUNTRY.getValue()) {
                                    bum.setOldValue(cov.getPkCountry().toString());
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    BdCountry oldCountry = countryAllMap.getData().get(cov.getPkCountry());
                                    BdCountry newCountry = countryAllMap.getData().get(etv.getBusinessValueList().get(0));
                                    nuc.setOldPkCountry(oldCountry.getName());
                                    nuc.setNewPkCountry(newCountry.getName());
                                    nbtvo.setNewValue(newCountry.getName());
                                    sig.setNewValue(newCountry.getName());
                                    sig.setOldValue(oldCountry.getName());
                                    break;
                                }
                                //修改登录状态
                                if (etv.getBusinessType() == EApprovalBusiness.SIGN_IN_STATE.getValue()) {
                                    bum.setOldValue(cov.getAccountStatus().toString());
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    nuc.setNewAccountStatus(transactionMap.get(EnumsPrefixConstants.ESIGN_IN_STATE + etv.getBusinessValueList().get(0)));
                                    nuc.setOldAccountStatus(transactionMap.get(EnumsPrefixConstants.ESIGN_IN_STATE + cov.getAccountStatus()));
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.ESIGN_IN_STATE + etv.getBusinessValueList().get(0)));
                                    sig.setNewValue(transactionMap.get(EnumsPrefixConstants.ESIGN_IN_STATE + etv.getBusinessValueList().get(0)));
                                    sig.setOldValue(transactionMap.get(EnumsPrefixConstants.ESIGN_IN_STATE + cov.getAccountStatus()));
                                    break;
                                }
                                //收益状态
                                if (etv.getBusinessType() == EApprovalBusiness.PROFIT_STATE.getValue()) {
                                    bum.setOldValue(cov.getIncomeStatus().toString());
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    nuc.setNewIncomeStatus(transactionMap.get(EnumsPrefixConstants.EPROFIT_STATE + etv.getBusinessValueList().get(0)));
                                    nuc.setOldIncomeStatus(transactionMap.get(EnumsPrefixConstants.EPROFIT_STATE + cov.getAccountStatus()));
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.EPROFIT_STATE + etv.getBusinessValueList().get(0)));
                                    sig.setNewValue(transactionMap.get(EnumsPrefixConstants.EPROFIT_STATE + etv.getBusinessValueList().get(0)));
                                    sig.setOldValue(transactionMap.get(EnumsPrefixConstants.EPROFIT_STATE + cov.getAccountStatus()));
                                    break;
                                }
                                //钱包状态
                                if (etv.getBusinessType() == EApprovalBusiness.WALLET_STATE.getValue()) {
                                    CuMemberAccount cat = cuMemberAccountService.queryCuMemberAccountByPkMember(uc.getPkMember());
                                    StringBuilder newValueVal = new StringBuilder();
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    if (etv.getBusinessValueList().size() > 1) {
                                        for (int i = 0; i < etv.getBusinessValueList().size(); i++) {
                                            if (i < etv.getBusinessValueList().size() - 1) {
                                                newValueVal.append(transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + etv.getBusinessValueList().get(i))).append("/");
                                            } else {
                                                newValueVal.append(transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + etv.getBusinessValueList().get(i)));
                                            }
                                        }
                                    } else {
                                        newValueVal = new StringBuilder(transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + etv.getBusinessValueList().get(0)));
                                    }
                                    if (StringUtils.isNotNull(cat)) {
                                        Integer isTransfer = cat.getIsTransfer() == EYesNo.NO.getIntValue() ? EMemberAccount.PROHIBIT_TRANSFER.getValue() : null;
                                        Integer isWithdraw = cat.getIsWithdraw() == EYesNo.NO.getIntValue() ? EMemberAccount.PROHIBIT_WITHDRAW.getValue() : null;
                                        Integer isConsume = cat.getIsConsume() == EYesNo.NO.getIntValue() ? EMemberAccount.PROHIBIT_CONSUME.getValue() : null;
                                        sig.setOldValue(transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + isTransfer) + "/" +
                                                transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + isWithdraw) + "/" +
                                                transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + isConsume));
                                        nuc.setOldWalletStatus(transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + isTransfer) + "/" +
                                                transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + isWithdraw) + "/" +
                                                transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + isConsume));

                                        // 使用 StringBuilder 拼接字符串
                                        StringBuilder sb = new StringBuilder();
                                        if (isTransfer != null) {
                                            sb.append(isTransfer);
                                        }
                                        if (isWithdraw != null) {
                                            if (sb.length() > 0) {
                                                sb.append(",");
                                            }
                                            sb.append(isWithdraw);
                                        }
                                        if (isConsume != null) {
                                            if (sb.length() > 0) {
                                                sb.append(",");
                                            }
                                            sb.append(isConsume);
                                        }
                                        // 判断拼接后的字符串是否为空，为空则表示正常
                                        int normalValue = EMemberAccount.NORMAL.getValue();
                                        bum.setOldValue(sb.length() == 0 ? Integer.toString(normalValue) : sb.toString());
                                        if (sb.length() == 0) {
                                            sig.setOldValue(transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + normalValue));
                                            nuc.setOldWalletStatus(transactionMap.get(EnumsPrefixConstants.MEMBER_ACCOUNT + normalValue));
                                        }

                                    } else {
                                        sig.setOldValue("-");
                                        nuc.setOldWalletStatus("-");
                                    }
                                    nbtvo.setNewValue(newValueVal.toString());
                                    sig.setNewValue(newValueVal.toString());
                                    nuc.setNewWalletStatus(newValueVal.toString());
                                    break;
                                }
                                //免考状态
                                if (etv.getBusinessType() == EApprovalBusiness.FREE_ASSESSMENT_STATE.getValue()) {

                                    StringBuilder newValueVal = new StringBuilder();
                                    if (etv.getBusinessValueList().size() > 1) {
                                        if ((etv.getBusinessValueList().get(0) == EFreeAssessmentState.REPURCHASE.getValue() &&
                                                etv.getBusinessValueList().get(1) == EFreeAssessmentState.CANCEL_REPURCHASE.getValue()
                                        ) || (etv.getBusinessValueList().get(1) == EFreeAssessmentState.REPURCHASE.getValue() &&
                                                etv.getBusinessValueList().get(0) == EFreeAssessmentState.CANCEL_REPURCHASE.getValue()
                                        )) {
                                            BusinessErrorVO bev = new BusinessErrorVO();
                                            bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.FREE_ASSESSMENT_STATE_INVALID));
                                            bev.setMemberCode(bum.getMemberCode());
                                            errorMemberList.add(bev);
                                        }
                                        if ((etv.getBusinessValueList().get(0) == EFreeAssessmentState.BONUS.getValue() &&
                                                etv.getBusinessValueList().get(1) == EFreeAssessmentState.CANCEL_BONUS.getValue()) ||
                                                (etv.getBusinessValueList().get(1) == EFreeAssessmentState.BONUS.getValue() &&
                                                        etv.getBusinessValueList().get(0) == EFreeAssessmentState.CANCEL_BONUS.getValue())) {
                                            BusinessErrorVO bev = new BusinessErrorVO();
                                            bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.FREE_ASSESSMENT_STATE_INVALID));
                                            bev.setMemberCode(bum.getMemberCode());
                                            errorMemberList.add(bev);
                                        }
                                    }

                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    if (etv.getBusinessValueList().size() > 1) {
                                        for (int i = 0; i < etv.getBusinessValueList().size(); i++) {
                                            if (i < etv.getBusinessValueList().size() - 1) {
                                                newValueVal.append(transactionMap.get(EnumsPrefixConstants.EFREE_ASSESSMENT_STATE + etv.getBusinessValueList().get(i))).append("/");
                                            } else {
                                                newValueVal.append(transactionMap.get(EnumsPrefixConstants.EFREE_ASSESSMENT_STATE + etv.getBusinessValueList().get(i)));
                                            }
                                        }
                                    } else {
                                        newValueVal = new StringBuilder(transactionMap.get(EnumsPrefixConstants.EFREE_ASSESSMENT_STATE + etv.getBusinessValueList().get(0)));
                                    }
                                    nbtvo.setNewValue(newValueVal.toString());
                                    sig.setNewValue(newValueVal.toString());
                                    Integer purchaseStatus = null;
                                    Integer shareStatus = null;
                                    if (cov.getPurchaseStatus() == EYesNo.YES.getIntValue()) {
                                        purchaseStatus = EFreeAssessmentState.REPURCHASE.getValue();
                                    } else {
                                        purchaseStatus = EFreeAssessmentState.CANCEL_REPURCHASE.getValue();
                                    }
                                    if (cov.getShareStatus() == EYesNo.YES.getIntValue()) {
                                        shareStatus = EFreeAssessmentState.BONUS.getValue();
                                    } else {
                                        shareStatus = EFreeAssessmentState.CANCEL_BONUS.getValue();
                                    }
                                    bum.setOldValue(purchaseStatus + "," + shareStatus);
                                    sig.setOldValue(transactionMap.get(EnumsPrefixConstants.EFREE_ASSESSMENT_STATE + purchaseStatus) + "/" +
                                            transactionMap.get(EnumsPrefixConstants.EFREE_ASSESSMENT_STATE + shareStatus));
                                    nuc.setOldAvoid(transactionMap.get(EnumsPrefixConstants.EFREE_ASSESSMENT_STATE + purchaseStatus) + "/" +
                                            transactionMap.get(EnumsPrefixConstants.EFREE_ASSESSMENT_STATE + shareStatus));
                                    nuc.setNewAvoid(newValueVal.toString());
                                    break;
                                }
                                //注册权限状态
                                if (etv.getBusinessType() == EApprovalBusiness.REGISTER_POWER_STATE.getValue()) {
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    bum.setOldValue(cov.getRegisterAuthority().toString());
                                    nuc.setNewRegisterAuthority(transactionMap.get(EnumsPrefixConstants.REGISTRATION_AUTHORITY + etv.getBusinessValueList().get(0)));
                                    nuc.setOldRegisterAuthority(transactionMap.get(EnumsPrefixConstants.REGISTRATION_AUTHORITY + cov.getRegisterAuthority()));
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.REGISTRATION_AUTHORITY + etv.getBusinessValueList().get(0)));
                                    sig.setNewValue(transactionMap.get(EnumsPrefixConstants.REGISTRATION_AUTHORITY + etv.getBusinessValueList().get(0)));
                                    sig.setOldValue(transactionMap.get(EnumsPrefixConstants.REGISTRATION_AUTHORITY + cov.getRegisterAuthority()));
                                    break;
                                }

                                //实名认证
                                if (etv.getBusinessType() == EApprovalBusiness.REAL_NAME_AUTHENTICATION.getValue()) {
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    if (StringUtils.isNotNull(cov.getIsRealName())) {
                                        bum.setOldValue(cov.getIsRealName().toString());
                                    }
                                    if (bum.getNewValue().equals(bum.getOldValue())) {
                                        BusinessErrorVO bev = new BusinessErrorVO();
                                        bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.REAL_NAME_AUTHENTICATION_REPEAT));
                                        bev.setMemberCode(bum.getMemberCode());
                                        errorMemberList.add(bev);
                                    }
                                    CuMember member = cuMemberService.getMember(bum.getPkMember());
                                    //奖衔key
                                    Integer pkAwards = cuMemberService.getAwardsTranslateValue(SecurityUtils.getPkCountry(), member.getPkAwards());
                                    sig.setAward(pkAwards);
                                    nuc.setNewIsRealName(transactionMap.get(EnumsPrefixConstants.YES_NO_ATTESTATION + etv.getBusinessValueList().get(0)));
                                    nuc.setOldIsRealName(transactionMap.get(EnumsPrefixConstants.YES_NO_ATTESTATION + cov.getIsRealName()));
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.YES_NO_ATTESTATION + etv.getBusinessValueList().get(0)));
                                    sig.setNewValue(transactionMap.get(EnumsPrefixConstants.YES_NO_ATTESTATION + etv.getBusinessValueList().get(0)));
                                    sig.setOldValue(transactionMap.get(EnumsPrefixConstants.YES_NO_ATTESTATION + cov.getIsRealName()));
                                    break;
                                }

                                //是否收益区域
                                if (etv.getBusinessType() == EApprovalBusiness.IS_REGION.getValue()) {
                                    bum.setOldValue(cov.getIsRegion().toString());
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    nuc.setNewisRegion(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + etv.getBusinessValueList().get(0)));
                                    nuc.setOldisRegion(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + cov.getIsRegion()));
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + etv.getBusinessValueList().get(0)));
                                    sig.setNewValue(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + etv.getBusinessValueList().get(0)));
                                    sig.setOldValue(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + cov.getIsRegion()));
                                    break;
                                }

                                //是否激活
                                if (etv.getBusinessType() == EApprovalBusiness.IS_ACTIVATE.getValue()) {
                                    bum.setOldValue(cov.getIsActivate().toString());
                                    bum.setNewValue(newValue(etv.getBusinessValueList()));
                                    nuc.setNewIsActivate(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + etv.getBusinessValueList().get(0)));
                                    nuc.setOldIsActivate(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + cov.getIsActivate()));
                                    nbtvo.setNewValue(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + etv.getBusinessValueList().get(0)));
                                    sig.setNewValue(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + etv.getBusinessValueList().get(0)));
                                    sig.setOldValue(transactionMap.get(EnumsPrefixConstants.DEALER_YES_NO + cov.getIsActivate()));
                                    break;
                                }
                            }
                        }
                    }
                    handleBusinessCostList.add(nbtvo);
                    signMemberList.add(sig);
                    businessMemberList.add(bum);
                }
            }
            businessNewMemberList.add(nuc);
        }

        businessSingleVO.setSignMemberList(signMemberList);
        businessSingleVO.setBusinessMemberList(businessMemberList);
        businessSingleVO.setHandleBusinessList(handleBusinessCostList);
        businessSingleVO.setBusinessOldMemberList(businessNewMemberList);
//        businessSingleVO.setBusinessCostVOList(businessCostVOList);
        //校验同姓名同手机号是否满足规则
        List<BusinessErrorVO> errorList = checkNamePhone(businessMemberList);
        if (CollectionUtil.isNotEmpty(errorList)) {
            return AjaxResult.error(504, TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_ERROR), errorList);
        }
        if (CollectionUtil.isNotEmpty(handleBusinessCostList)) {
            List<MemberBusinessBalanceDTO> mbbList = new ArrayList<>();
            handleBusinessCostList.forEach(item -> mbbList.add(BeanUtil.copyProperties(item, MemberBusinessBalanceDTO.class)));
            List<MemberBusinessBalanceDTO> memberBalanceList = cuMemberTradeService.selectBusinessBalanceNotEnoughMember(mbbList, pkCountry);
            for (BusinessTypeValueVO bt : handleBusinessCostList) {
                for (MemberBusinessBalanceDTO mbb : memberBalanceList) {
                    if (bt.getPkMember().equals(mbb.getPkMember()) &&
                            bt.getBusinessType().equals(mbb.getBusinessType())) {
                        if (mbb.getPkAccount() != null) {
                            bt.setPkAccount(mbb.getPkAccount());
                            bt.setPkAccountKey(cuMemberBaseService.translateAccountValue(mbb.getPkAccount()));
                        } else {
                            bt.setPkAccount(null);
                            bt.setPkAccountKey(null);
                            bt.setPkAccountKeyVal(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_BALANCE_INSUFFICIENT));
                        }
                    }
                }
            }
            handleBusinessCostList.forEach(item -> businessCostVOList.add(BeanUtil.copyProperties(item, BusinessCostVO.class)));
            businessSingleVO.setBusinessCostVOList(businessCostVOList);
            businessSingleVO.setHandleBusinessList(handleBusinessCostList);
        }
        if (CollectionUtil.isNotEmpty(errorMemberList)) {
            return AjaxResult.error(504, TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_ERROR), errorMemberList);
        }
        if (CollectionUtil.isNotEmpty(errorMemberList)) {
            return AjaxResult.error(504, TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_ERROR), errorMemberList);
        }
        return AjaxResult.success(businessSingleVO);
    }


    /**
     * 校验用户余额是否充足
     **/
    @Log(module = EOperationModule.HANDLE_BUSINESS, business = EOperationBusiness.BASE_BUSINESS, method = EOperationMethod.SELECT)
    @PostMapping("/balanceCheck")
    public AjaxResult balanceCheck(@RequestBody HandleBusinessVO businessSingleVO) {
        List<BusinessErrorVO> errorMemberList = new ArrayList<>();
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (CollectionUtil.isEmpty(businessSingleVO.getBusinessCostVOList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_ERROR));
        }

        if (CollectionUtil.isNotEmpty(businessSingleVO.getBusinessCostVOList())) {
            List<MemberBusinessBalanceDTO> mbbList = new ArrayList<>();
            businessSingleVO.getBusinessCostVOList().forEach(item -> mbbList.add(BeanUtil.copyProperties(item, MemberBusinessBalanceDTO.class)));
            List<MemberBusinessBalanceDTO> memberBalanceList = cuMemberTradeService.selectBusinessBalanceNotEnoughMember(mbbList, pkCountry);
            for (MemberBusinessBalanceDTO mbb : memberBalanceList) {
                if (mbb.getPkAccount() == null) {
                    BusinessErrorVO bev = new BusinessErrorVO();
                    bev.setErrorMsg(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_BALANCE_INSUFFICIENT));
                    bev.setMemberCode(mbb.getMemberCode());
                    errorMemberList.add(bev);
                }
            }
        }
        if (CollectionUtil.isNotEmpty(errorMemberList)) {
            return AjaxResult.error(504, TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_ERROR), errorMemberList);
        } else {
            return success();
        }
    }

    /**
     * 更新新值
     **/
    private String newValue(List<Integer> businessValueList) {
        //更新会员新值
        StringBuilder newStr = new StringBuilder();
        if (businessValueList.size() > 1) {
            for (int i = 0; i < businessValueList.size(); i++) {
                if (i < businessValueList.size() - 1) {
                    newStr.append(businessValueList.get(i).toString()).append(",");
                } else {
                    newStr.append(businessValueList.get(i).toString());
                }
            }
        } else if (businessValueList.size() == 1) {
            newStr = new StringBuilder(businessValueList.get(0).toString());
        }
        return newStr.toString();
    }


    /**
     * 重新申请办理业务
     **/
    @Log(module = EOperationModule.HANDLE_BUSINESS, business = EOperationBusiness.BASE_BUSINESS, method = EOperationMethod.SUBMIT)
    @PostMapping("/againBusiness")
    public AjaxResult againBusiness(@RequestBody CuMemberServiceLogVO cuMemberServiceLogVO) {
        if (StringUtils.isNull(cuMemberServiceLogVO.getBusinessNo())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WORK_NO_NOT_NULL));
        }
        if (StringUtils.isNull(cuMemberServiceLogVO.getEditType())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WORK_TYPE_NOT_NULL));
        }
        LambdaQueryWrapper<CuMemberServiceLog> querywrapper = new LambdaQueryWrapper<>();
        querywrapper.eq(CuMemberServiceLog::getApproveStatus, EApproveStatus.REJECTED.getValue());
        querywrapper.eq(CuMemberServiceLog::getBusinessNo, cuMemberServiceLogVO.getBusinessNo());
        List<CuMemberServiceLog> cmslList = memberServiceLogService.list(querywrapper);
        if (CollectionUtil.isNotEmpty(cmslList)) {
            cuMemberServiceLogVO = memberServiceLogService.againBusiness(cmslList, cuMemberServiceLogVO);
        }
        return AjaxResult.success(cuMemberServiceLogVO);
    }

    /**
     * 在线签呈(发起人)
     *
     * @return
     */
    @Log(module = EOperationModule.HANDLE_BUSINESS, business = EOperationBusiness.BASE_BUSINESS, method = EOperationMethod.SUBMIT)
    @PostMapping("/online-petition")
    public AjaxResult signOnline(@RequestBody OnlinePetitionVo linePetitionVo) {
        SysUser loginUser = userTokenService.getLoginUser().getLoginSysUser();

        List<CuHandleBusiness> list = cuHandleBusinessService.selectByOrderNumber(linePetitionVo.getOrderNumber());

        List<Integer> approvalBusinessList = list.stream().map(CuHandleBusiness::getEditType).collect(Collectors.toList());
        R<Map<Integer, ApprovalInfoDTO>> mapR = approvalServiceApi.queryApprovalInfo(approvalBusinessList, loginUser.getPkCountry());
        List<OnlinePetitionVo> onlinePetitionVoList = new ArrayList<>();
        for (CuHandleBusiness handleBusiness : list) {
            OnlinePetitionVo onlinePetitionVo = new OnlinePetitionVo();
            //发起部门
            onlinePetitionVo.setDeptId(loginUser.getPkDept());
            onlinePetitionVo.setDeptStr(loginUser.getDeptName());
            // 发起人
            onlinePetitionVo.setSponsorMemberId(loginUser.getUserId());
            onlinePetitionVo.setSponsorMemberStr(loginUser.getUserName());
            // 日期
            onlinePetitionVo.setApprovalDate(DateUtils.formatDate(new Date(), com.hzs.common.core.utils.DateUtils.YYYY_MM_DD_HH_MM_SS));

            // 抄送部门
            ApprovalInfoDTO approvalInfoDTO = mapR.getData().get(handleBusiness.getEditType());
            if (approvalInfoDTO != null) {
                List<String> collect = approvalInfoDTO.getItemList().stream().map(ApprovalItemInfoDTO::getDeptName).collect(Collectors.toList());
                onlinePetitionVo.setDeptList(collect);
            }

            //  业务诉求
            AnalysisBusinessAppealParam analysisBusinessAppealParam = new AnalysisBusinessAppealParam();
            // 业务大单号
            analysisBusinessAppealParam.setOrderNumber(handleBusiness.getOrderNumber());
            // 签呈类型
            onlinePetitionVo.setSignType(handleBusiness.getSignType());
            onlinePetitionVo.setEditType(handleBusiness.getEditType());
            // 小单号
            analysisBusinessAppealParam.setBusinessNo(handleBusiness.getBusinessNo());
            analysisBusinessAppealParam.setEditType(handleBusiness.getEditType());
            R<List<SignOnlineInfoVo>> signOnlineVoR = handleBusinessServiceApi.analysisBusinessAppeal(analysisBusinessAppealParam);
            onlinePetitionVo.setItemList(signOnlineVoR.getData());
            onlinePetitionVoList.add(onlinePetitionVo);
        }
        return AjaxResult.success(onlinePetitionVoList);
    }

    /**
     * 在线签呈-发起人-确认
     *
     * @return
     */
    @Log(module = EOperationModule.HANDLE_BUSINESS, business = EOperationBusiness.BASE_BUSINESS, method = EOperationMethod.CONFIRM)
    @PostMapping("/online-petition-confirm")
    public AjaxResult onlinePetitionConfirm(@RequestBody HandleBusinessVO handleBusinessVO) {
        LoginUser loginUser = userTokenService.getLoginUser();
        handleBusinessVO.setPkCountry(SecurityUtils.getPkCountry());
        Integer processType = handleBusinessVO.getProcessType();
        if (null == processType) {
            return AjaxResult.error("请选择按钮类型！");
        }
        return toAjax(cuHandleBusinessService.onlinePetitionConfirm(handleBusinessVO, loginUser));
    }


}
