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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
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.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.constant.msg.DocumentMsgConstants;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
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.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.empty.CuMemberEmpty;
import com.hzs.common.domain.member.empty.CuMemberEmptyDetail;
import com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt;
import com.hzs.common.domain.member.empty.ext.CuMemberEmptyProductDTO;
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.empty.dto.MemberEmptyDetailDTO;
import com.hzs.member.empty.dto.MemberEmptyProductDTO;
import com.hzs.member.empty.param.MemberEmptyAddParam;
import com.hzs.member.empty.param.MemberEmptySignParam;
import com.hzs.member.empty.param.MemberEmptyQueryParam;
import com.hzs.member.empty.service.ICuMemberEmptyDetailService;
import com.hzs.member.empty.service.ICuMemberEmptyService;
import com.hzs.member.empty.service.IMemberEmptyService;
import com.hzs.member.empty.vo.CuMemberEmptyDetailTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 空单注册控制器
 */
@RequestMapping("/manage/member-empty")
@RestController
public class CuMemberEmptyController extends BaseController {

    @Autowired
    private ICuMemberEmptyService iCuMemberEmptyService;
    @Autowired
    private ICuMemberEmptyDetailService iCuMemberEmptyDetailService;
    @Autowired
    private IMemberEmptyService iMemberEmptyService;
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * 下载模板
     *
     * @param response
     */
    @PostMapping("/export-template")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<CuMemberEmptyDetailTemplate> util = new ExcelUtil<>(CuMemberEmptyDetailTemplate.class);
        util.exportExcel(response, null, "下载模板");
    }

    /**
     * 导入模板
     *
     * @param file          导入文件
     * @param pkMemberEmpty 空单批次ID
     * @return
     * @throws Exception
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER, method = EOperationMethod.IMPORT)
    @PostMapping("/import-template")
    public AjaxResult importTemplate(MultipartFile file, Long pkMemberEmpty) throws Exception {
        if (null != pkMemberEmpty) {
            CuMemberEmpty cuMemberEmpty = iCuMemberEmptyService.getById(pkMemberEmpty);
            if (null == cuMemberEmpty) {
                return AjaxResult.error("导入空单批次不存在");
            }
        }

        ExcelUtil<CuMemberEmptyDetailTemplate> util = new ExcelUtil<>(CuMemberEmptyDetailTemplate.class);
        // 获取导入数据
        List<CuMemberEmptyDetailTemplate> importList = util.importExcel(file.getInputStream());

        // 所属国家
        Integer pkCountry = SecurityUtils.getPkCountry();

        // 处理数据
        List<MemberEmptyDetailDTO> resultList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(importList)) {
            for (CuMemberEmptyDetailTemplate detailTemplate : importList) {
                if (StringUtils.isAnyEmpty(detailTemplate.getMemberCode(), detailTemplate.getMemberName(), detailTemplate.getPhone(),
                        detailTemplate.getParentCode())) {
                    // 导入数据存在为空
                    return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.EMPTY_IMPORT_EMPTY));
                }

                MemberEmptyDetailDTO detail = BeanUtil.copyProperties(detailTemplate, MemberEmptyDetailDTO.class, "placeDept");
                // 默认全为正常，后续进行校验处理是否异常
                detail.setPointStatus(EPointStatus.NORMAL.getValue());
                detail.setPkCountry(pkCountry);
                detail.setPkSettleCountry(pkCountry);
                detail.setPkMemberEmpty(pkMemberEmpty);
                resultList.add(detail);
            }

            // 校验数据
            iMemberEmptyService.importTemplate(resultList);

            // 导入数据入库
            List<CuMemberEmptyDetailExt> returnList = iMemberEmptyService.saveDate(resultList, SecurityUtils.getUserId(), pkCountry, SecurityUtils.getSystemType());
            if (null != returnList) {
                // 返回结果
                resultList = new ArrayList<>(returnList.size());
                for (CuMemberEmptyDetailExt emptyDetail : returnList) {
                    MemberEmptyDetailDTO detailDTO = BeanUtil.copyProperties(emptyDetail, MemberEmptyDetailDTO.class);
                    detailDTO.setPkCountryVal(emptyDetail.getCountryName());
                    detailDTO.setPkSettleCountryVal(emptyDetail.getSettleCountryName());
                    resultList.add(detailDTO);
                }
                return AjaxResult.success(resultList);
            }
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 添加数据
     *
     * @param param 空单添加数据入参
     * @return
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER, method = EOperationMethod.INSERT)
    @PostMapping("/add-data")
    public AjaxResult addData(@RequestBody MemberEmptyAddParam param) {
        if (null != param.getPkMemberEmpty()) {
            if (null == iCuMemberEmptyService.getById(param.getPkMemberEmpty())) {
                return AjaxResult.error("空单批次不存在");
            }
        }

        CuMemberEmptyDetailExt emptyDetail = iCuMemberEmptyDetailService.addData(param, SecurityUtils.getUserId(), SecurityUtils.getPkCountry(), SecurityUtils.getSystemType());
        if (null != emptyDetail) {
            MemberEmptyDetailDTO detailDTO = BeanUtil.copyProperties(emptyDetail, MemberEmptyDetailDTO.class);
            detailDTO.setPkCountryVal(emptyDetail.getCountryName());
            detailDTO.setPkSettleCountryVal(emptyDetail.getSettleCountryName());
            detailDTO.setAutoFlag(EDealerYesNo.YES.getValue());
            detailDTO.setAutoFlagVal(EDealerYesNo.YES.getLabel());
            return AjaxResult.success(detailDTO);
        }
        return AjaxResult.error();
    }

    /**
     * 删除数据
     *
     * @param detailIdList 批次明细ID
     * @return
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER, method = EOperationMethod.DELETE)
    @DeleteMapping("/create-delete/{detailIdList}")
    public AjaxResult createDelete(@PathVariable List<Long> detailIdList) {
        // 批次明细ID删除为null数据
        List<Long> pkIdList = detailIdList.stream().filter(Objects::nonNull).collect(Collectors.toList());

        if (CollectionUtil.isNotEmpty(pkIdList)) {
            LambdaQueryWrapper<CuMemberEmptyDetail> emptyDetailUpdateWrapper = new LambdaQueryWrapper<>();
            emptyDetailUpdateWrapper.in(CuMemberEmptyDetail::getPkId, pkIdList);
            List<CuMemberEmptyDetail> emptyDetailList = iCuMemberEmptyDetailService.list(emptyDetailUpdateWrapper);

            if (CollectionUtil.isNotEmpty(emptyDetailList)) {
                iCuMemberEmptyDetailService.delData(emptyDetailList, SecurityUtils.getUserId(), SecurityUtils.getPkCountry());
            }
        }
        return AjaxResult.success();
    }

    /**
     * 空单注册生成
     *
     * @param paramList 生成空单参数
     * @return
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER, method = EOperationMethod.CREATE)
    @PostMapping("/create-data")
    public AjaxResult createData(@RequestBody List<MemberEmptyDetailDTO> paramList) {
        if (CollectionUtil.isEmpty(paramList)) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        for (MemberEmptyDetailDTO detail : paramList) {
            if (StringUtils.isAnyEmpty(detail.getMemberCode(), detail.getMemberName(), detail.getPhone(), detail.getParentCode())
                    || null == detail.getPkId() || null == detail.getPkMemberEmpty()
                    || null == detail.getPkRegisterGrade() || null == detail.getRegisterAchieve() || null == detail.getOrderSource()
                    || null == detail.getPkSettleCountry() || null == detail.getPkCountry() || null == detail.getPkAwards() || null == detail.getBackAmount()) {
                // 缺少参数
                return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
            }

            // 进行结算，需要校验结算业绩和上传业绩
            if (EYesNo.YES.getIntValue() == detail.getSettlementFlag()) {
                if (null == detail.getSettlementAchieve() || null == detail.getTransferAchieve()) {
                    // 缺少参数
                    return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
                }
            }
            // 选购产品，需要校验产品以及地址信息
            if (EYesNo.YES.getIntValue() == detail.getProductFlag()) {
                if (StringUtils.isAnyEmpty(detail.getRecName(), detail.getRecPhone(), detail.getRecAddress())
                        || null == detail.getRecProvince() || null == detail.getRecCity() || CollectionUtil.isEmpty(detail.getProductList())) {
                    // 缺少参数
                    return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
                }
                for (MemberEmptyProductDTO product : detail.getProductList()) {
                    if (StringUtils.isAnyEmpty(product.getSpecsName(), product.getSpecsNameId())
                            || null == product.getPkProduct() || null == product.getQuantity()) {
                        // 缺少参数
                        return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
                    }
                }
            }
        }

        if (iMemberEmptyService.updateDate(paramList, SecurityUtils.getUserId(), SecurityUtils.getPkCountry())) {
            // 先保存或更新数据，然后校验点位状态
            Boolean bool = iMemberEmptyService.handleDate(paramList, SecurityUtils.getSystemType());
            if (null == bool || bool) {
                // 处理成功
                return AjaxResult.success();
            }
            // 存在异常数据列表，需要把异常信息更新入库
            List<MemberEmptyDetailDTO> abnormalList = paramList.stream().filter(dto -> EPointStatus.ABNORMAL.getValue() == dto.getPointStatus()).collect(Collectors.toList());
            iCuMemberEmptyDetailService.updateAbnormalBatch(abnormalList, SecurityUtils.getUserId());

            // 点位状态存在异常，请检查
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.EMPTY_POINT_ERROR, abnormalList.size()), paramList);
        } else {
            // 操作失败
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.OPERATION_FAILED));
        }
    }

    /**
     * 生成列表
     *
     * @param param 生成入参
     * @return
     */
    @AccessPermissions("EmptyOrder")
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_CREATE, method = EOperationMethod.SELECT)
    @GetMapping("/create-list")
    public TableDataInfo createList(MemberEmptyQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());

        startPage();
        List<CuMemberEmpty> resultList = iCuMemberEmptyService.queryList(param, SecurityUtils.getPkCountry(), SecurityUtils.getSystemType());

        return getDataTable(resultList);
    }

    /**
     * 生成详情
     *
     * @param pkId         批次ID
     * @param businessCode 批次编号
     * @return
     */
    @GetMapping("/create-detail")
    public AjaxResult createDetail(Long pkId, String businessCode) {
        if (null != pkId) {
            return AjaxResult.success(iCuMemberEmptyService.getById(pkId));
        }
        if (StringUtils.isNotEmpty(businessCode)) {
            LambdaQueryWrapper<CuMemberEmpty> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CuMemberEmpty::getBusinessCode, businessCode);
            return AjaxResult.success(iCuMemberEmptyService.getOne(queryWrapper));
        }
        // 缺少参数
        return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
    }

    /**
     * 架构
     *
     * @param pkId 批次ID
     * @param type 类型（1=安置架构，2=推荐架构）
     * @return
     */
    @GetMapping("/framework/{pkId}/{type}")
    public AjaxResult framework(@PathVariable Long pkId, @PathVariable Integer type) {
        // 返回数据
        List<CuMemberEmptyDetailExt> resultList;
        if (type == 1) {
            // 安置架构
            resultList = iCuMemberEmptyDetailService.queryPlaceParentTop(pkId);
        } else if (type == 2) {
            // 推荐架构
            resultList = iCuMemberEmptyDetailService.queryParentTop(pkId);
        } else {
            resultList = new ArrayList<>();
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 架构数据
     *
     * @param detailId 批次明细ID
     * @param type     类型（1=安置架构，2=推荐架构）
     * @return
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_CREATE, method = EOperationMethod.SELECT, remark = "查看架构数据")
    @GetMapping("/framework-data/{detailId}/{type}")
    public AjaxResult frameworkData(@PathVariable Long detailId, @PathVariable Integer type) {
        CuMemberEmptyDetail emptyDetail = iCuMemberEmptyDetailService.getById(detailId);
        if (null == emptyDetail) {
            // 数据不存在
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }

        // 返回数据
        List<Tree<String>> resultList = null;
        // 树配置
        TreeNodeConfig defaultConfig = TreeNodeConfig.DEFAULT_CONFIG;
        defaultConfig.setWeightKey("pkId");

        if (type == 1) {
            // 安置架构
            List<CuMemberEmptyDetailExt> dataList = iCuMemberEmptyDetailService.queryPlaceParentTree(detailId, emptyDetail.getPkMemberEmpty());

            List<Tree<String>> build = TreeUtil.build(dataList, emptyDetail.getPlaceParentCode(), defaultConfig, (node, tree) -> {
                tree.setId(node.getMemberCode());
                tree.setName(node.getMemberName());
                tree.setParentId(node.getPlaceParentCode());
                tree.putExtra("pkId", node.getMemberCode());
                tree.putExtra("parent", node.getPlaceParentCode());
                tree.putExtra("color", node.getGradeColor());
                tree.putExtra("pointStatus", node.getPointStatus());
                tree.putExtra("placeDept", node.getPlaceDept());
            });
            if (build != null) {
                resultList = build;
            }
        } else if (type == 2) {
            // 推荐架构
            List<CuMemberEmptyDetailExt> dataList = iCuMemberEmptyDetailService.queryParentTree(detailId, emptyDetail.getPkMemberEmpty());

            List<Tree<String>> build = TreeUtil.build(dataList, emptyDetail.getParentCode(), defaultConfig, (node, tree) -> {
                tree.setId(node.getMemberCode());
                tree.setName(node.getMemberName());
                tree.setParentId(node.getParentCode());
                tree.putExtra("pkId", node.getMemberCode());
                tree.putExtra("parent", node.getParentCode());
                tree.putExtra("color", node.getGradeColor());
                tree.putExtra("pointStatus", node.getPointStatus());
            });
            if (build != null) {
                resultList = build;
            }
        } else {
            resultList = new ArrayList<>();
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 删除批次
     *
     * @param pkIdList 批次ID列表
     * @return
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_CREATE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkIdList}")
    public AjaxResult delete(@PathVariable List<Long> pkIdList) {
        List<CuMemberEmpty> list = iCuMemberEmptyService.listByIds(pkIdList);

        for (CuMemberEmpty cuMemberEmpty : list) {
            if (EApproveStatus.ALREADY_SUBMIT.getValue() == cuMemberEmpty.getStatus()
                    || EApproveStatus.FINISH.getValue() == cuMemberEmpty.getStatus()) {
                // 业务类型存在审批中数据，不能进行删除
                return AjaxResult.error(TransactionUtils.getContent(DocumentMsgConstants.DOCUMENT_FLOW_NOT_DELETE));
            }
        }

        iCuMemberEmptyService.deleteData(pkIdList, SecurityUtils.getUserId(), SecurityUtils.getPkCountry());
        return AjaxResult.success();
    }

    /**
     * 生成明细列表
     *
     * @param pkId 批次ID
     * @return
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_CREATE, method = EOperationMethod.SELECT, remark = "明细列表")
    @GetMapping("/create-detail/{pkId}")
    public AjaxResult createDetail(@PathVariable Long pkId) {
        // 查询空单批次
        CuMemberEmpty cuMemberEmpty = iCuMemberEmptyService.getById(pkId);

        // 查询空单批次明细
        List<CuMemberEmptyDetailExt> detailList = iCuMemberEmptyDetailService.queryEmptyDetailList(pkId);
        // 数据列表
        List<MemberEmptyDetailDTO> dataList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(detailList)) {
            // 处理翻译数据
            Set<Integer> pkTransaction = new HashSet<>();
            for (CuMemberEmptyDetailExt cuMemberEmptyDetailExt : detailList) {
                if (null != cuMemberEmptyDetailExt.getPkGradeKey()) {
                    pkTransaction.add(cuMemberEmptyDetailExt.getPkGradeKey());
                }
                if (null != cuMemberEmptyDetailExt.getPkAwardKey()) {
                    pkTransaction.add(cuMemberEmptyDetailExt.getPkAwardKey());
                }
            }
            // 获取翻译数据
            Map<Integer, String> cacheIntMap = iTransactionCommonService.enumTransactionByDBFromPkId(pkTransaction);

            for (CuMemberEmptyDetailExt emptyDetail : detailList) {
                MemberEmptyDetailDTO detailDTO = BeanUtil.copyProperties(emptyDetail, MemberEmptyDetailDTO.class);
                detailDTO.setRemark(cuMemberEmpty.getRemark());

                detailDTO.setPkRegisterGradeVal(cacheIntMap.get(emptyDetail.getPkGradeKey()));
                detailDTO.setPkAwardsVal(cacheIntMap.get(emptyDetail.getPkAwardKey()));

                detailDTO.setPkCountryVal(emptyDetail.getCountryName());
                detailDTO.setPkSettleCountryVal(emptyDetail.getSettleCountryName());

                if (CollectionUtil.isNotEmpty(emptyDetail.getProductDTOList())) {
                    // 处理产品信息
                    detailDTO.setProductList(BeanUtil.copyToList(emptyDetail.getProductDTOList(), MemberEmptyProductDTO.class));
                }
                dataList.add(detailDTO);
            }
        }
        return AjaxResult.success(dataList);
    }

    /**
     * 生成导出
     *
     * @param pkId     批次ID
     * @param response
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_CREATE, method = EOperationMethod.EXPORT, remark = "明细列表导出")
    @PostMapping("/export-detail/{pkId}")
    public void exportDetail(@PathVariable Long pkId, HttpServletResponse response) {
        List<MemberEmptyDetailDTO> resultList = new ArrayList<>();

        // 查询明细数据
        List<CuMemberEmptyDetailExt> detailList = iCuMemberEmptyDetailService.queryEmptyDetailList(pkId);
        if (CollectionUtil.isNotEmpty(detailList)) {
            // 处理翻译数据
            Set<Integer> pkTransaction = new HashSet<>();
            for (CuMemberEmptyDetailExt cuMemberEmptyDetailExt : detailList) {
                if (null != cuMemberEmptyDetailExt.getPkGradeKey()) {
                    pkTransaction.add(cuMemberEmptyDetailExt.getPkGradeKey());
                }
                if (null != cuMemberEmptyDetailExt.getPkAwardKey()) {
                    pkTransaction.add(cuMemberEmptyDetailExt.getPkAwardKey());
                }
            }
            Map<Integer, String> cacheIntMap = iTransactionCommonService.enumTransactionByDBFromPkId(pkTransaction);

            // 获取翻译内容
            Map<String, String> cacheMap = iTransactionCommonService.exportEnumTransaction(EPlaceDept.values(), EOrderSource.values(), EOrderType.values(), EPointStatus.values(), EYesNo.values(), EDealerYesNo.values());

            for (CuMemberEmptyDetailExt emptyDetail : detailList) {
                MemberEmptyDetailDTO detailDTO = BeanUtil.copyProperties(emptyDetail, MemberEmptyDetailDTO.class);

                detailDTO.setPkRegisterGradeVal(cacheIntMap.get(emptyDetail.getPkGradeKey()));
                detailDTO.setPkAwardsVal(cacheIntMap.get(emptyDetail.getPkAwardKey()));

                detailDTO.setPkCountryVal(emptyDetail.getCountryName());
                detailDTO.setPkSettleCountryVal(emptyDetail.getSettleCountryName());

                detailDTO.setPlaceDeptVal(cacheMap.get(EnumsPrefixConstants.PLACE_DEPT + emptyDetail.getPlaceDept()));
                detailDTO.setOrderSourceVal(cacheMap.get(EnumsPrefixConstants.ORDER_SOURCE + emptyDetail.getOrderSource()));
                detailDTO.setOrderTypeVal(cacheMap.get(EnumsPrefixConstants.ORDER_TYPE + emptyDetail.getOrderType()));
                detailDTO.setPointStatusVal(cacheMap.get(EnumsPrefixConstants.POINT_STATUS + emptyDetail.getPointStatus()));
                detailDTO.setSettlementFlagVal(cacheMap.get(EnumsPrefixConstants.YES_NO + emptyDetail.getSettlementFlag()));
                detailDTO.setProductFlagVal(cacheMap.get(EnumsPrefixConstants.YES_NO + emptyDetail.getProductFlag()));
                detailDTO.setAutoFlagVal(cacheMap.get(EnumsPrefixConstants.DEALER_YES_NO + emptyDetail.getAutoFlag()));

                // 处理产品显示数据
                if (CollectionUtil.isNotEmpty(emptyDetail.getProductDTOList())) {
                    // 产品信息拼接
                    StringBuilder productSb = new StringBuilder();
                    List<CuMemberEmptyProductDTO> productList = emptyDetail.getProductDTOList();
                    for (CuMemberEmptyProductDTO productDTO : productList) {
                        productSb.append(productDTO.getProductName())
                                .append("(")
                                .append(productDTO.getSpecsName())
                                .append(") * ")
                                .append(productDTO.getQuantity())
                                .append("; \n");
                    }
                    detailDTO.setProductStr(productSb.toString());
                }

                resultList.add(detailDTO);
            }
        }
        ExcelUtil<MemberEmptyDetailDTO> util = new ExcelUtil<>(MemberEmptyDetailDTO.class);
        util.exportExcel(response, resultList, "空单明细导出");
    }

    /**
     * 空单生成-免签
     *
     * @param param 免签入参
     * @return
     */
    @RepeatSubmitSimple
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_CREATE, method = EOperationMethod.CREATE)
    @PostMapping("/create-free-sign")
    public AjaxResult createFreeSign(@RequestBody MemberEmptySignParam param) {
        if (null == param.getPkId()) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        // 查询批次明细数据
        List<CuMemberEmptyDetailExt> detailList = iCuMemberEmptyDetailService.queryDetailAndProductList(param.getPkId());
        if (CollectionUtil.isEmpty(detailList)) {
            // 该批次下没有报单数据
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.EMPTY_DATA_DETAIL_EMPTY));
        }

        List<MemberEmptyDetailDTO> paramList = new ArrayList<>();

        for (CuMemberEmptyDetailExt emptyDetail : detailList) {
            MemberEmptyDetailDTO detailDTO = BeanUtil.copyProperties(emptyDetail, MemberEmptyDetailDTO.class);

            // 处理产品信息
            if (CollectionUtil.isNotEmpty(emptyDetail.getProductDTOList())) {
                detailDTO.setProductList(BeanUtil.copyToList(emptyDetail.getProductDTOList(), MemberEmptyProductDTO.class));
            }
            paramList.add(detailDTO);
        }

        // 校验点位状态
        Boolean bool = iMemberEmptyService.handleDate(paramList, SecurityUtils.getSystemType());
        if (null == bool || bool) {
            // 校验成功，入库数据
            if (iMemberEmptyService.saveEmptyMember(param.getPkId(), detailList, SecurityUtils.getUserId(), SecurityUtils.getPkCountry(), SecurityUtils.getSystemType(), true)) {
                return AjaxResult.success();
            }
            // 空单注册数据保存失败
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.EMPTY_SAVE_ERROR));
        }
        // 点位状态存在异常，请检查
        return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.EMPTY_POINT_ERROR), paramList);
    }

    /**
     * 提交签呈
     *
     * @param param
     * @return
     */
    @RepeatSubmitSimple
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_CREATE, method = EOperationMethod.SUBMIT)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody MemberEmptySignParam param) {
        if (null == param.getPkId()) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        if (null == param.getSignType() || CollectionUtil.isEmpty(param.getUserIdList())) {
            // 签呈必填参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        // 查询批次明细数据
        List<CuMemberEmptyDetailExt> detailList = iCuMemberEmptyDetailService.queryDetailAndProductList(param.getPkId());
        if (CollectionUtil.isEmpty(detailList)) {
            // 该批次下没有报单数据
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.EMPTY_DATA_DETAIL_EMPTY));
        }

        List<MemberEmptyDetailDTO> paramList = new ArrayList<>();

        for (CuMemberEmptyDetailExt emptyDetail : detailList) {
            MemberEmptyDetailDTO detailDTO = BeanUtil.copyProperties(emptyDetail, MemberEmptyDetailDTO.class);

            // 处理产品信息
            if (CollectionUtil.isNotEmpty(emptyDetail.getProductDTOList())) {
                detailDTO.setProductList(BeanUtil.copyToList(emptyDetail.getProductDTOList(), MemberEmptyProductDTO.class));
            }
            paramList.add(detailDTO);
        }

        // 校验点位状态
        Boolean bool = iMemberEmptyService.handleDate(paramList, SecurityUtils.getSystemType());
        if (null == bool || bool) {
            // 校验成功，提交签呈
            String str = iMemberEmptyService.submit(param, detailList, userTokenService.getLoginUser());
            if (null == str) {
                return AjaxResult.success();
            }
            return AjaxResult.error(str);
        }
        // 点位状态存在异常，请检查
        return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.EMPTY_POINT_ERROR), paramList);
    }

}
