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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.hzs.common.core.annotation.ColumnAuthority;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.MemberFieldConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.utils.reflect.ReflectUtils;
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.CuMemberRecharge;
import com.hzs.common.domain.member.account.CuMemberRechargeExt;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.domain.system.config.BdAccount;
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.core.utils.TransactionUtils;
import com.hzs.member.account.dto.CuMemberBatchRechargeDetailParam;
import com.hzs.member.account.dto.CuMemberBatchRechargeParam;
import com.hzs.member.account.dto.CuMemberRechargeParam;
import com.hzs.member.account.service.ICuMemberAccountService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.service.ICuMemberRechargeService;
import com.hzs.member.account.vo.*;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.sys.IMenuColumnServiceApi;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 会员充值表 前端控制器
 *
 * @author hzs
 * @since 2022-09-08
 */
@RestController
@RequestMapping("/manager/recharge")
public class CuMemberRechargeController extends BaseController {

    @Autowired
    ICuMemberRechargeService cuMemberRechargeService;

    @Autowired
    ICuMemberService cuMemberService;

    @Autowired
    ICuMemberBaseService baseService;

    @Autowired
    ICuMemberAccountService cuMemberAccountService;

    @Autowired
    private UserTokenService userTokenService;

    @Autowired
    ITransactionCommonService iTransactionCommonService;


    @DubboReference
    IMenuColumnServiceApi menuColumnServiceApi;

    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.SELECT, remark = "会员编号查会员信息")
    @GetMapping("/member-name")
    public AjaxResult getMemberNameByCOde(String memberCode) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(memberCode)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_NOT_EMPTY));
        }
        //充值方信息
        List<CuMemberExt> CuMemberExtList = cuMemberService.selectMemberByMemberCode(memberCode, null, SecurityUtils.getPkCountry());
        if (CollUtil.isEmpty(CuMemberExtList)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_NOT_ERROR));
        }
        CuMemberExt cuMemberExt = CuMemberExtList.get(0);
        //用户未支付
        if (cuMemberExt.getPayStatus().equals(EPayStatus.UNPAID.getValue())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.USER_HAS_NOT_PAID));
        }

        ajaxResult.put("memberName", cuMemberExt.getMemberName());
        return ajaxResult;
    }

    /**
     * 发起签呈显示
     *
     * @param memberRechargeParam 充值参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.SELECT, remark = "发起签呈")
    @PostMapping("/show-petition")
    public AjaxResult showMemberPetition(@RequestBody CuMemberRechargeParam memberRechargeParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberRechargeParam.setPkCountry(pkCountry);
        Integer rechargeMethod = memberRechargeParam.getRechargeMethod();
        List<Integer> paymentMethods = memberRechargeParam.getPaymentMethods();
        SysUser loginUser = userTokenService.getLoginUser().getLoginSysUser();
        memberRechargeParam.setSysUser(loginUser);
        //查询用户存在有审批的记录
        List<String> memberCodeList = cuMemberRechargeService.selectMemberRechargeApproveByUserCode(pkCountry, memberRechargeParam.getMemberCode());
        if (CollUtil.isNotEmpty(memberCodeList)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SUBMITTED_RECHARGE_INFORMATION, memberCodeList));
        }
        if (CollUtil.isEmpty(paymentMethods)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_PAY_METHODS));
        }
        if (null == rechargeMethod) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_METHOD_NOT_EMPTY));
        }
        return AjaxResult.success(cuMemberRechargeService.showMemberPetition(memberRechargeParam));
    }

    /**
     * 根据用户编号查询用户余额
     *
     * @param cuMemberBalanceVO 余额信息
     * @return AjaxResult
     */
    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.SELECT, remark = "查询余额")
    @PostMapping("/balance-show")
    public AjaxResult showMemberBalance(@RequestBody CuMemberBalanceVO cuMemberBalanceVO) {
        List<Integer> accountTypes = cuMemberBalanceVO.getAccountTypes();
        String memberCode = cuMemberBalanceVO.getMemberCode();
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (CollUtil.isEmpty(accountTypes)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_WALLET_TYPE));
        }
        Assert.notNull(memberCode, TransactionUtils.getContent(FinanceMsgConstants.USER_NUMBER_CANNOT_EMPTY));
        //充值方信息
        CuMemberExt cuMemberLoginExt = cuMemberService.getMemberByCode(memberCode);
        if (null == cuMemberLoginExt) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_USER_NOT_EXIST));
        }
        Integer payStatus = cuMemberLoginExt.getPayStatus();
        if (payStatus.equals(EPayStatus.UNPAID.getValue())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.USER_HAS_NOT_PAID));
        }
        return AjaxResult.success(cuMemberAccountService.queryMemberAccountByMemberCode(memberCode, accountTypes, pkCountry));

    }

    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.SELECT)
    @PostMapping("/batch-show")
    public AjaxResult queryBatchRechargeAccount(@RequestBody CuMemberBalanceVO cuMemberBalanceVO) {
        List<Integer> accountTypes = cuMemberBalanceVO.getAccountTypes();
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (CollUtil.isEmpty(accountTypes)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_WALLET_TYPE));
        }
        return AjaxResult.success(cuMemberAccountService.queryBatchRechargeAccount(accountTypes, pkCountry));
    }


    /**
     * 查询详情
     *
     * @param pkId 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.SELECT)
    @GetMapping("/details")
    public AjaxResult findRechargeDetails(Long pkId) {
        Assert.notNull(pkId, TransactionUtils.getContent(FinanceMsgConstants.SELECT_VIEW_RECHARGE_RECORD));
        CuMemberRechargeDetailVO cuMemberRechargeDetailVO = cuMemberRechargeService.queryCuMemberRechargeDetail(pkId);
        return AjaxResult.success(cuMemberRechargeDetailVO);
    }


    /**
     * 查询列表
     *
     * @param cuMemberRechargeVO 会员充值信息参数
     * @return TableDataInfo
     */

    @Log(module = EOperationModule.RECHARGE_DETAIL, business = EOperationBusiness.RECHARGE_DETAIL, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    @ColumnAuthority(perms = "RechargeDetail")
    public TableDataInfo findRechargeList(@RequestBody CuMemberRechargeVO cuMemberRechargeVO) {
        Integer countryId = SecurityUtils.getPkCountry();
        cuMemberRechargeVO.setPkCountry(countryId);
        startPage();
        Object[] objects = cuMemberRechargeService.findRechargeList(cuMemberRechargeVO);
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberRechargeExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberRechargeVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 充值明细列表导出
     *
     * @param response           响应
     * @param cuMemberRechargeVO 查询入参
     */
    @Log(module = EOperationModule.RECHARGE_DETAIL, business = EOperationBusiness.RECHARGE_DETAIL, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberRechargeVO cuMemberRechargeVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        cuMemberRechargeVO.setPkCountry(pkCountry);
        List<CuMemberRechargeVO> cuMemberRechargeVOList = (List<CuMemberRechargeVO>) cuMemberRechargeService.findRechargeList(cuMemberRechargeVO)[1];
        //多语言翻译
        if (CollUtil.isNotEmpty(cuMemberRechargeVOList)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EApproveRechargeStatus.values());
            for (CuMemberRechargeVO memberRecharge : cuMemberRechargeVOList) {
                Integer pkAccount = memberRecharge.getPkAccount();
                memberRecharge.setPkAccountVal(baseService.getAccountTranslateFromDataBase(pkAccount));
                memberRecharge.setPaymentMethodVal(transactionMap.get(EnumsPrefixConstants.PAYMENT_TYPE + memberRecharge.getPaymentMethod()));
                memberRecharge.setApproveStateVal(transactionMap.get(EnumsPrefixConstants.APPROVE_RECHARGE_OPERATION + memberRecharge.getApproveState()));
            }
        }
        ExcelUtil<CuMemberRechargeVO> util = new ExcelUtil<>(CuMemberRechargeVO.class, menuColumnServiceApi.queryMenuColumn("RechargeDetail", SecurityUtils.getUserId()).getData());
        util.exportExcel(response, cuMemberRechargeVOList, TransactionUtils.getContent(FinanceMsgConstants.RECHARGE_DETAILS_EXPORT));
    }


    /**
     * 批量导入会员信息
     *
     * @param file 文件
     * @return AjaxResult
     * @throws Exception
     */
    @PostMapping("/import-member-code")
    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.IMPORT)
    public AjaxResult importMemberCode(MultipartFile file) throws Exception {
        List<CuMemberBatchRechargeDetailParam> paramList = new ArrayList<>();
        CuMemberBatchRechargeDetailParam param;
        Integer pkCountry = SecurityUtils.getPkCountry();
        ExcelUtil<RechargeTemplateVO> util = new ExcelUtil<>(RechargeTemplateVO.class);
        List<RechargeTemplateVO> rechargeVOList = util.importExcel(file.getInputStream(), 0);
        if (CollUtil.isEmpty(rechargeVOList)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_IMPORT_DATA_ERROR));
        }
        List<String> memberCodes = rechargeVOList.stream().map(RechargeTemplateVO::getMemberCode).distinct().collect(Collectors.toList());
        List<CuMember> cuMemberList = cuMemberService.findMemberByMembercodesAndPkcountry(memberCodes, pkCountry);
        if (cuMemberList.size() != memberCodes.size()) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.IMPORT_MEMBER_ERROR));
        }
        Map<String, String> memberMap = cuMemberList.stream().collect(Collectors.toMap(CuMember::getMemberCode, CuMember::getMemberName));
        //查询用户存在有审批的记录
        List<String> memberCodeList = cuMemberRechargeService.selectMemberRechargeApproveByUserCode(pkCountry, memberCodes);
        if (CollUtil.isNotEmpty(memberCodeList)) {
            return AjaxResult.error( memberCodeList.stream().collect(Collectors.joining(",")) + TransactionUtils.getContent(FinanceMsgConstants.SUBMITTED_RECHARGE_INFORMATION));
        }
        //只允许充值币种
        List<BdAccount> accountList = baseService.getAccountByCountryId(pkCountry);
        Map<Integer, Integer> accountMap = accountList.stream().filter(a -> a.getIsRecharge().equals(EYesNo.YES.getIntValue())).collect(Collectors.toMap(BdAccount::getFieldValue, BdAccount::getPkId));
        //同会员同账户存在重复充值
        if (getCuMemberAccountNum(rechargeVOList, accountMap)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.DUPLICATE_RECHARGES));
        }

        for (RechargeTemplateVO templateVO : rechargeVOList) {
            if (StringUtils.isEmpty(templateVO.getMemberCode())) {
                return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.IMPORT_MEMBER_NOT_EMPTY));
            }
            //转换交款试
            String paymentMethod = templateVO.getPaymentMethod();
            if (StringUtils.isEmpty(paymentMethod)) {
                return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_PAY_METHODS));
            }
            EPaymentType ePaymentType = EPaymentType.getEnumByLabel(paymentMethod);
            param = BeanUtil.copyProperties(templateVO, CuMemberBatchRechargeDetailParam.class, "paymentMethod");
            param.setPaymentMethod(ePaymentType.getValue());
            param.setMemberName(memberMap.get(templateVO.getMemberCode()));
            //充值账户金额
            String fillAccountAmount = getFillAccountAmount(templateVO, accountMap);
            if (StringUtils.isEmpty(fillAccountAmount)) {
                return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_AMOUNT_NOT_EMPTY));
            }
            param.setRechargeDetail(fillAccountAmount);
            paramList.add(param);

        }
        return AjaxResult.success(paramList);
    }

    /**
     * 判断同一会员同一账户是否存在多条充值信息
     *
     * @param rechargeVOList 导入数据
     * @param accountMap     账户Map
     * @return boolean
     */
    private boolean getCuMemberAccountNum(List<RechargeTemplateVO> rechargeVOList, Map<Integer, Integer> accountMap) {
        Map<String, Integer> map = new HashMap<>();
        for (RechargeTemplateVO templateVO : rechargeVOList) {
            String memberCode = templateVO.getMemberCode();
            if (MapUtil.isNotEmpty(accountMap)) {
                for (Map.Entry<Integer, Integer> entry : accountMap.entrySet()) {
                    //币种ID
                    Integer id = entry.getValue();
                    //币种值
                    Integer value = entry.getKey();
                    BigDecimal amount = ReflectUtils.invokeGetter(templateVO, MemberFieldConstants.ACCOUNT + value);
                    if (null != amount) {
                        if (null != map.get(memberCode + id)) {
                            return Boolean.TRUE;
                        } else {
                            map.put((memberCode + id), id);
                        }
                    }
                }
            }
        }
        return Boolean.FALSE;
    }

    /**
     * 封装账户、金额为map
     *
     * @param templateVO 导入数据
     * @param accountMap 账户Map
     * @return Map<String, BigDecimal>
     */
    private String getFillAccountAmount(RechargeTemplateVO templateVO, Map<Integer, Integer> accountMap) {
        Map<String, BigDecimal> map = new HashMap<>();
        if (MapUtil.isNotEmpty(accountMap)) {
            for (Map.Entry<Integer, Integer> entry : accountMap.entrySet()) {
                //币种值
                Integer value = entry.getKey();
                BigDecimal amount = ReflectUtils.invokeGetter(templateVO, MemberFieldConstants.ACCOUNT + value);
                //币种ID
                Integer id = entry.getValue();
                if (null != amount) {
                    map.put(id.toString(), amount);
                }
            }
        }
        return map.size() == 0 ? null : JSONUtil.toJsonStr(map);

    }


    /**
     * 下载模板
     *
     * @return
     */
    @PostMapping("/download-template")
    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.EXPORT, remark = "下载模板")
    public void downloadTemplate(HttpServletResponse response) {
        List<BdAccount> bdAccounts = baseService.getAccountByCountryId(SecurityUtils.getPkCountry());
        //配置的充值账户
        List<BdAccount> rechargeAccounts = bdAccounts.stream().filter(a -> a.getIsRecharge().equals(EYesNo.YES.getIntValue())).collect(Collectors.toList());
        ExcelUtil<RechargeTemplateVO> util = new ExcelUtil<>(RechargeTemplateVO.class, getShowField(rechargeAccounts));
        util.exportExcel(response, null, TransactionUtils.getContent(FinanceMsgConstants.RECHARGE_TEMPLATE));
    }


    /**
     * 根据配置充值账户返回显示字段
     *
     * @param rechargeAccounts 充值配置显示账户
     * @return List<String>
     */
    private List<String> getShowField(List<BdAccount> rechargeAccounts) {
        List<String> fields = new ArrayList<>();
        fields.add("memberCode");
        fields.add("memberName");
        fields.add("paymentMethod");
        fields.add("memberRemarks");
        fields.add("remarks");
        if (CollUtil.isNotEmpty(rechargeAccounts)) {
            for (BdAccount bdAccount : rechargeAccounts) {
                fields.add(MemberFieldConstants.ACCOUNT + bdAccount.getFieldValue());
            }
        }
        return fields;
    }

    @GetMapping("/test")
    public AjaxResult updateOnlineRechargeByRechargeCode() {
        Date startDate = DateUtils.currentDate();
        Date endDate = DateUtils.afterDate(1, ChronoUnit.DAYS);
        CuMemberRecharge cuMemberRecharge = cuMemberRechargeService.selectRechargeTotalAmount(startDate, endDate);
        return AjaxResult.success(cuMemberRecharge);
    }


    // 以下为充值重构

    /**
     * 管理员充值
     *
     * @param batchRechargeParam 会员充值参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.INSERT, remark = "单笔充值")
    @PostMapping("/fill")
    public AjaxResult addRechargeInfo(@RequestBody CuMemberBatchRechargeParam batchRechargeParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        LoginUser loginUser = userTokenService.getLoginUser();
        batchRechargeParam.setLoginUser(loginUser);

        List<CuMemberBatchRechargeDetailParam> detailParams = batchRechargeParam.getDetailParams();
        if (CollUtil.isEmpty(detailParams)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.PARAMETER_NOT_EMPTY));
        }

        String memberCodePara = detailParams.get(0).getMemberCode();
        if (StringUtils.isEmpty(memberCodePara)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_NOT_EMPTY));
        }
        //充值编号转换为大写
        String memberCode = memberCodePara.toUpperCase();
        batchRechargeParam.setMemberCodes(Collections.singletonList(memberCode));

        //充值方式
        Integer rechargeMethod = batchRechargeParam.getRechargeMethod();
        if (null == rechargeMethod) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_METHOD_NOT_EMPTY));
        }
        //交款方式
        Integer paymentMethod = detailParams.get(0).getPaymentMethod();
        if (null == paymentMethod) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_PAY_METHODS));
        }
        //签呈类型
        Integer signType = batchRechargeParam.getSignType();
        if (null == signType) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_SIGN_TYPE));
        }
        //审批人
        List<Long> userIdList = batchRechargeParam.getUserIdList();
        if (CollUtil.isEmpty(userIdList)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.APPROVED_BY));
        }

        //充值方信息
        CuMemberExt cuMember = cuMemberService.getMemberByCode(memberCode);

        if (null == cuMember) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_USER_NOT_EXIST));
        }

        Integer payStatus = cuMember.getPayStatus();
        if (payStatus.equals(EPayStatus.UNPAID.getValue())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.USER_HAS_NOT_PAID));
        }

        //查询用户存在有审批的记录
        List<String> memberCodeList = cuMemberRechargeService.selectMemberRechargeApproveByUserCode(pkCountry, Collections.singletonList(memberCode));
        if (CollUtil.isNotEmpty(memberCodeList)) {
            return AjaxResult.error( TransactionUtils.getContent(FinanceMsgConstants.SUBMITTED_RECHARGE_INFORMATION));
        }
        //不允许跨国充值
        if (!pkCountry.equals(cuMember.getPkSettleCountry())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_NOT_CROSS_COUNTRY));
        }
        batchRechargeParam.setCuMembers(Collections.singletonList(cuMember));
        batchRechargeParam.setMemberMap(MapUtil.of(memberCode, cuMember.getPkId()));
        //汇率
        CurrencyDTO currencyDTO = baseService.getCurrency(pkCountry);
        batchRechargeParam.setPkRate(currencyDTO.getPkId());

        batchRechargeParam.setPkCountry(pkCountry);
        return toAjax(cuMemberRechargeService.saveBatchMemberRecharge(batchRechargeParam));
    }


    /**
     * 根据用户名、用户编号搜索用户信息
     *
     * @param memberCode 用户编号
     * @param memberName 用户名
     * @return AjaxResult
     */
    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.SELECT, remark = "搜索用户")
    @GetMapping("/show-member")
    public AjaxResult showMember(String memberCode, String memberName) {
        if (StringUtils.isEmpty(memberCode) && StringUtils.isEmpty(memberName)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHARGE_PARAMETER_NOT_EMPTY));
        }
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuMemberExt> cuMemberExts = cuMemberService.selectMemberByMemberCode(memberCode, memberName, pkCountry);
        return AjaxResult.success(BeanUtil.copyToList(cuMemberExts, CuMemberBaseVO.class));
    }


    /**
     * 管理员批量充值
     *
     * @param batchRechargeParam 会员充值参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.INSERT, remark = "批量充值")
    @PostMapping("/batch-fill")
    public AjaxResult addBatchRechargeInfo2(@RequestBody CuMemberBatchRechargeParam batchRechargeParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        LoginUser loginUser = userTokenService.getLoginUser();
        batchRechargeParam.setLoginUser(loginUser);
        batchRechargeParam.setPkCountry(pkCountry);
        //充值方式
        Integer rechargeMethod = batchRechargeParam.getRechargeMethod();
        if (null == rechargeMethod) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_METHOD_NOT_EMPTY));
        }
        //签呈类型
        Integer signType = batchRechargeParam.getSignType();
        if (null == signType) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_SIGN_TYPE));
        }
        //审批人
        List<Long> userIdList = batchRechargeParam.getUserIdList();
        if (CollUtil.isEmpty(userIdList)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.APPROVED_BY));
        }

        List<CuMemberBatchRechargeDetailParam> detailParams = batchRechargeParam.getDetailParams();
        //会员编号(入参)
        List<String> memberCodes = detailParams.stream().map(CuMemberBatchRechargeDetailParam::getMemberCode).distinct().collect(Collectors.toList());
        batchRechargeParam.setMemberCodes(memberCodes);

        //交款方式
        List<CuMemberBatchRechargeDetailParam> paymentMethodList = detailParams.stream().filter(d -> null == d.getPaymentMethod()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(paymentMethodList)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_PAY_METHODS));
        }
        //查询数据用户编号
        List<CuMember> cuMembers = cuMemberService.findMemberByMembercodesAndPkcountry(memberCodes, pkCountry);
        if (CollUtil.isEmpty(cuMembers)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_USER_NOT_EXIST));
        }

        //查询用户存在有审批的记录
        List<String> memberCodeList = cuMemberRechargeService.selectMemberRechargeApproveByUserCode(pkCountry, memberCodes);
        if (CollUtil.isNotEmpty(memberCodeList)) {
            return AjaxResult.error( TransactionUtils.getContent(FinanceMsgConstants.SUBMITTED_RECHARGE_INFORMATION));
        }
        //判断跨国充值
        List<CuMember> cuMembersNoCountry = cuMemberService.findMemberByMembercodesAndPkcountry(memberCodes, null);
        //跨国的会员
        List<String> transMembers = cuMembersNoCountry.stream().filter(m -> !pkCountry.equals(m.getPkSettleCountry())).map(CuMember::getMemberCode).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(transMembers)) {
            return AjaxResult.error(StringUtils.join(transMembers, ",") + TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_NOT_CROSS_COUNTRY));
        }

        //数据库里的用户数
        List<String> memberCodeTables = cuMembers.stream().map(CuMember::getMemberCode).collect(Collectors.toList());
        //查询提交的用户编号不在数据库里的编号
        List<String> notTables = memberCodes.stream().filter(m -> !memberCodeTables.contains(m)).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(notTables)) {
            return AjaxResult.error(StringUtils.join(notTables, ",") + TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_IMPOR_CODE_ERROR));
        }

        Map<String, Long> memberMap = cuMembers.stream().collect(Collectors.toMap(CuMember::getMemberCode, CuMember::getPkId));
        batchRechargeParam.setCuMembers(cuMembers);
        batchRechargeParam.setMemberMap(memberMap);

        //查询未支付的编号（账户余额表不存在用户）
        List<String> noPayMember = cuMemberService.findNoPayMember(memberCodes, pkCountry);
        if (CollUtil.isNotEmpty(noPayMember)) {
            return AjaxResult.error(StringUtils.join(noPayMember, ",") + TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_NO_PAY));
        }

        //汇率
        CurrencyDTO currencyDTO = baseService.getCurrency(pkCountry);
        batchRechargeParam.setPkRate(currencyDTO.getPkId());
        return toAjax(cuMemberRechargeService.saveBatchMemberRecharge(batchRechargeParam));
    }


    @Log(module = EOperationModule.RECHARGE, business = EOperationBusiness.RECHARGE, method = EOperationMethod.SELECT, remark = "操作人")
    @GetMapping("/show-operator")
    public AjaxResult selectOperator() {
        Integer pkCountry = SecurityUtils.getPkCountry();
        return AjaxResult.success(cuMemberRechargeService.selectOperator(pkCountry));
    }


}
