package com.hzs.sale.deliver.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.constant.msg.SaOrderMsgConstants;
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.sale.deliver.SaDeliver;
import com.hzs.common.domain.sale.deliver.SaDeliverBarCode;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.sale.deliver.param.*;
import com.hzs.sale.deliver.service.ISaDeliverBarCodeService;
import com.hzs.sale.deliver.service.ISaDeliverItemsService;
import com.hzs.sale.deliver.service.ISaDeliverService;
import com.hzs.sale.deliver.vo.*;
import com.hzs.sale.util.OrderUtil;
import com.hzs.system.base.IAreaServiceApi;
import com.hzs.system.base.IBarCodeServiceApi;
import com.hzs.system.base.dto.BarCodeDTO;
import com.hzs.system.sys.IUserServiceApi;
import com.hzs.system.sys.dto.UserAuthorityDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description: 发货清单已合单控制器
 * @Author: jiang chao
 * @Time: 2022/10/19 16:43
 * @Classname: SaDeliverController
 * @PackageName: com.hzs.sale.order.controller.manager
 */
@RestController
@RequestMapping("/manage/deliver-handled")
public class SaDeliverHandledController extends BaseController {

    @Autowired
    private ISaDeliverService iSaDeliverService;
    @Autowired
    private ISaDeliverItemsService iSaDeliverItemsService;
    @Autowired
    private ISaDeliverBarCodeService iSaDeliverBarCodeService;

    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    @DubboReference
    private IAreaServiceApi iAreaServiceApi;
    @DubboReference
    private IBarCodeServiceApi iBarCodeServiceApi;
    @DubboReference
    private IUserServiceApi iUserServiceApi;


    /**
     * 列表
     *
     * @param param 已合单列表查询入参
     * @return
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.DELIVER_HANDLED, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(DeliverHandledParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        DateUtils.handTimeStartAndEnd(param.getPayTime());

        Integer pkCountry = SecurityUtils.getPkCountry();

        // 返回数据
        List<DeliverHandledVO> resultList = new ArrayList<>();

        if (StringUtils.isNotEmpty(param.getBarCode())) {
            // 追溯条码处理
            QueryWrapper<SaDeliverBarCode> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
            queryWrapper.eq("BAR_CODE", param.getBarCode());
            List<SaDeliverBarCode> barCodeList = iSaDeliverBarCodeService.list(queryWrapper);
            if (CollectionUtil.isEmpty(barCodeList)) {
                // 带有追溯码查询条件，并没有查出来追溯码，则直接返回空数据
                return getDataTable(resultList);
            }
            param.setPkDeliver(barCodeList.get(0).getPkDeliver());
            param.setPkDeliverItems(barCodeList.get(0).getPkDeliverItems());
        }

        // 获取管理员权限（角色地区范围、体系列表、团队列表）
        UserAuthorityDTO userAuthorityDTO = iUserServiceApi.getUserAuthority(SecurityUtils.getUserId()).getData();
        param.setAreaScopeList(userAuthorityDTO.getRoleAreaScopeList());
        param.setVertexIdList(userAuthorityDTO.getVertexIdList());
        param.setTeamList(userAuthorityDTO.getUserTeamList());

        startPage();
        resultList = iSaDeliverService.queryDeliverHandledList(param, pkCountry);

        // 当查询有数据才进行处理
        if (resultList.size() > 0) {
            // 获取行政区划map
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();

            // 发货单明细ID
            List<Long> deliverItemsIdList = resultList.stream().map(DeliverHandledVO::getPkId).collect(Collectors.toList());
            // 根据发货单明细ID，查询对应绑定条码数据
            Map<Long, SaDeliverBarCode> barCodeMap = iSaDeliverBarCodeService.queryListByDeliverItems(deliverItemsIdList, pkCountry);

            for (DeliverHandledVO deliverHandled : resultList) {
                // 省市区处理
                OrderUtil.handleOrderAddress(areaMap, deliverHandled);

                // 处理追溯编号、扫码时间
                SaDeliverBarCode deliverBarCode = barCodeMap.get(deliverHandled.getPkId());
                if (null != deliverBarCode) {
                    deliverHandled.setBarCode(deliverBarCode.getBarCode());
                    deliverHandled.setScanTime(deliverBarCode.getCreationTime());
                }
            }
        }
        return getDataTable(resultList);
    }


    /**
     * 导出
     *
     * @param response
     * @param param    已合单列表查询入参
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.DELIVER_HANDLED, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeliverHandledParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        DateUtils.handTimeStartAndEnd(param.getPayTime());

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

        List<DeliverHandledVO> resultList = new ArrayList<>();

        if (StringUtils.isNotEmpty(param.getBarCode())) {
            // 追溯条码处理
            QueryWrapper<SaDeliverBarCode> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
            queryWrapper.eq("BAR_CODE", param.getBarCode());
            List<SaDeliverBarCode> barCodeList = iSaDeliverBarCodeService.list(queryWrapper);
            if (CollectionUtil.isEmpty(barCodeList)) {
                // 带有追溯码查询条件，并没有查出来追溯码，则直接返回空数据
                ExcelUtil<DeliverHandledVO> util = new ExcelUtil<>(DeliverHandledVO.class);
                util.exportExcel(response, resultList, "发货清单已合单导出");
                return;
            }
            param.setPkDeliver(barCodeList.get(0).getPkDeliver());
            param.setPkDeliverItems(barCodeList.get(0).getPkDeliverItems());
        }

        // 获取管理员权限（角色地区范围、体系列表、团队列表）
        UserAuthorityDTO userAuthorityDTO = iUserServiceApi.getUserAuthority(SecurityUtils.getUserId()).getData();
        param.setAreaScopeList(userAuthorityDTO.getRoleAreaScopeList());
        param.setVertexIdList(userAuthorityDTO.getVertexIdList());
        param.setTeamList(userAuthorityDTO.getUserTeamList());

        resultList = iSaDeliverService.queryDeliverHandledList(param, pkCountry);

        // 当查询有数据才进行处理
        if (resultList.size() > 0) {
            // 获取行政区划map
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();

            // 发货单明细ID
            List<Long> deliverItemsIdList = resultList.stream().map(DeliverHandledVO::getPkId).collect(Collectors.toList());
            // 根据发货单明细ID，查询对应绑定条码数据
            Map<Long, SaDeliverBarCode> barCodeMap = iSaDeliverBarCodeService.queryListByDeliverItems(deliverItemsIdList, pkCountry);

            // 获取需要翻译的枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EDelivery.values(), EOrderStatus.values(), EOrderType.values(), EDeliveryStatus.values(), ESupplyWay.values(), ETransportType.values(), EPresaleStatus.values(), EPrintStatus.values(), EDealerYesNo.values(), EShippingChannel.values());

            for (DeliverHandledVO deliverHandled : resultList) {
                // 省市区处理
                OrderUtil.handleOrderAddress(areaMap, deliverHandled);

                deliverHandled.setOrderStatusVal(transactionMap.get(EnumsPrefixConstants.ORDER_STATUS + deliverHandled.getOrderStatus()));
                deliverHandled.setOrderTypeVal(transactionMap.get(EnumsPrefixConstants.ORDER_TYPE + deliverHandled.getOrderType()));
                deliverHandled.setDeliveryWayVal(transactionMap.get(EnumsPrefixConstants.DELIVERY + deliverHandled.getDeliveryWay()));
                deliverHandled.setOperateScopeVal(transactionMap.get(EnumsPrefixConstants.SUPPLY_WAY + deliverHandled.getOperateScope()));
                deliverHandled.setPreSaleStatusVal(transactionMap.get(EnumsPrefixConstants.PRESALE_STATUS + deliverHandled.getPreSaleStatus()));
                deliverHandled.setPrintStatusVal(transactionMap.get(EnumsPrefixConstants.E_PRINT_STATUS + deliverHandled.getPrintStatus()));
                deliverHandled.setTranTypeVal(transactionMap.get(EnumsPrefixConstants.ENU_TRA + deliverHandled.getTranType()));
                deliverHandled.setShippingChannelVal(transactionMap.get(EnumsPrefixConstants.E_SHIPPING_CHANNEL + deliverHandled.getShippingChannel()));

                deliverHandled.setRecAddress(deliverHandled.getRecProvinceVal()
                        + (StringUtils.isNotEmpty(deliverHandled.getRecCityVal()) ? deliverHandled.getRecCityVal() : "")
                        + (StringUtils.isNotEmpty(deliverHandled.getRecCountyVal()) ? deliverHandled.getRecCountyVal() : "")
                        + deliverHandled.getRecAddress());

                // 处理追溯编号、扫码时间
                SaDeliverBarCode deliverBarCode = barCodeMap.get(deliverHandled.getPkId());
                if (null != deliverBarCode) {
                    deliverHandled.setBarCode(deliverBarCode.getBarCode());
                    deliverHandled.setScanTime(deliverBarCode.getCreationTime());
                }
            }
        }
        ExcelUtil<DeliverHandledVO> util = new ExcelUtil<>(DeliverHandledVO.class);
        util.exportExcel(response, resultList, "发货清单已合单导出");
    }


    /**
     * 打印货单
     *
     * @param deliverIdList 发货单ID列表
     * @return
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.DELIVER_HANDLED, method = EOperationMethod.PRINT)
    @GetMapping("/print")
    public AjaxResult print(@RequestParam("deliverIdList") List<Long> deliverIdList) {
        // 返回数据
        List<DeliverPrintVO> resultList = new ArrayList<>();

        Integer pkCountry = SecurityUtils.getPkCountry();

        // 发货单打印数据
        List<DeliverPrintVO> dataList = iSaDeliverService.deliverPrint(deliverIdList, pkCountry, SecurityUtils.getUserId());

        // 每张发货单能打多少条产品数据
        int pageSize = 0;
        if (CountryConstants.CHINA_COUNTRY == SecurityUtils.getPkCountry()) {
            // 中国使用新的打印模板，每页只能打印5条
            pageSize = 5;
        }

        if (dataList.size() > 0) {
            // 获取行政区划map
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();

            // 遍历发货单，进行处理
            for (DeliverPrintVO deliverPrintVO : dataList) {
                // 发货单打印明细列表
                List<DeliverPrintItemVO> itemList = deliverPrintVO.getItemList();

                // 发货单金额
                BigDecimal deliverPrice = BigDecimal.ZERO;
                for (DeliverPrintItemVO deliverPrintItemVO : itemList) {
                    deliverPrice = deliverPrice.add(deliverPrintItemVO.getPrice());
                }
                deliverPrintVO.setDeliverPrice(deliverPrice);

                // 省市区处理
                OrderUtil.handleOrderAddress(areaMap, deliverPrintVO);

                if (pageSize != 0) {
                    // 发货单能分多少页
                    int orderPageCount = (itemList.size() - 1) / pageSize + 1;
                    for (int i = 0; i < orderPageCount; i++) {
                        // 这一轮截止的下标
                        int end = Math.min((i + 1) * pageSize, itemList.size());

                        // 将发货单打印明细分组
                        DeliverPrintVO tmpDeliverPrint = BeanUtil.copyProperties(deliverPrintVO, DeliverPrintVO.class);

                        // 按pageSize条每页取出数据
                        tmpDeliverPrint.setItemList(itemList.subList(i * pageSize, end));
                        resultList.add(tmpDeliverPrint);
                    }
                } else {
                    resultList.add(deliverPrintVO);
                }
            }
        }
        return AjaxResult.success(resultList);
    }


    /**
     * 绑定条形码数据
     *
     * @param deliverIdList 发货单ID列表
     * @param orderIdList   订单ID列表
     * @return
     */
    @GetMapping("/code-bind-data")
    public AjaxResult codeBindData(@RequestParam("deliverIdList") List<Long> deliverIdList,
                                   @RequestParam(value = "orderIdList", required = false) List<Long> orderIdList) {
        Integer pkCountry = SecurityUtils.getPkCountry();

        // 返回最终结果
        List<DeliverBarCodeVO> resultList = new ArrayList<>();

        // 查询 发货单明细 + 已生成的条码
        List<DeliverBarCodeVO> deliverBarCodeVOList = iSaDeliverItemsService.deliverCodeData(deliverIdList, orderIdList, pkCountry, null);

        // 遍历发货明细列表
        for (DeliverBarCodeVO deliverBarCode : deliverBarCodeVOList) {
            // 发货明细对应的条码明细
            List<DeliverBarCodeDetailVO> detailList = deliverBarCode.getDetailList();

            // 遍历发货明细数量，填充已经绑定过的条码
            for (int i = 0; i < deliverBarCode.getQuantity(); i++) {
                // 发货条码ID
                Long deliverBarCodeId = null;
                // 条码
                String barCode = null;

                // 当前数量小于条码数量，则直接用条码内容，否则直接返回为空
                if (i < detailList.size()) {
                    deliverBarCodeId = detailList.get(i).getDeliverBarCodeId();
                    barCode = detailList.get(i).getBarCode();
                }

                // 生成最终要返回数据
                resultList.add(DeliverBarCodeVO.builder()
                        .pkId(deliverBarCode.getPkId())
                        .pkDeliver(deliverBarCode.getPkDeliver())
                        .pkOrder(deliverBarCode.getPkOrder())
                        .orderCode(deliverBarCode.getOrderCode())
                        .pkProduct(deliverBarCode.getPkProduct())
                        .productName(deliverBarCode.getProductName())
                        .specsName(deliverBarCode.getSpecsName())
                        .deliverBarCodeId(deliverBarCodeId)
                        .barCode(barCode)
                        .build());
            }
        }
        return AjaxResult.success(resultList);
    }


    /**
     * 绑定条码
     *
     * @param paramList 绑定条码参数
     * @return
     */
    @RepeatSubmitSimple
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.DELIVER_HANDLED, method = EOperationMethod.BIND)
    @PostMapping("/code-bind")
    public AjaxResult codeBind(@RequestBody List<BindCodeParam> paramList) {
        if (CollectionUtil.isEmpty(paramList)) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        for (BindCodeParam bindCodeParam : paramList) {
            if (null == bindCodeParam.getPkDeliver() || null == bindCodeParam.getPkId() || null == bindCodeParam.getPkProduct()) {
                // 缺少参数
                return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
            }
        }

        // 获取条码不为空的数据
        List<BindCodeParam> paramNotEmptyList = paramList.stream().filter((param -> StringUtils.isNotEmpty(param.getBarCode()))).collect(Collectors.toList());
        // 校验条码是否重复（获取条码不为空并且去重复数量）
        if (paramNotEmptyList.size() != paramNotEmptyList.stream().distinct().count()) {
            // 追溯码存在重复
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_CHECK_BARCODE_REPEAT));
        }

        // 只保留变动过数据，未变动数据不做处理
        paramNotEmptyList = paramNotEmptyList.stream().filter(param -> null == param.getDeliverBarCodeId()).collect(Collectors.toList());
        // 获取入参中的新条码
        List<String> barCodeStrList = paramNotEmptyList.stream().map(BindCodeParam::getBarCode).collect(Collectors.toList());
        if (barCodeStrList.size() == 0) {
            return AjaxResult.success();
        }

        Integer pkCountry = SecurityUtils.getPkCountry();

        // 查询已打印未使用的条码数据
        List<BarCodeDTO> barCodeList = iBarCodeServiceApi.queryPrintBarCode(barCodeStrList, pkCountry).getData();
        if (barCodeList.size() < barCodeStrList.size()) {
            // 追溯码不存在
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_CHECK_BARCODE_NOT_EXIST));
        }

        barCodeList = barCodeList.stream().filter((barCodeDTO) -> EBarcodeStatus.BOUND.getValue() != barCodeDTO.getCodeStatus()).collect(Collectors.toList());
        if (barCodeList.size() < barCodeStrList.size()) {
            // 追溯码已使用
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_CHECK_BARCODE_USED));
        }

        // 遍历入参以及条码数据，赋值条码ID用以保存，并且校验条码与产品是否一致
        for (BindCodeParam bindCodeParam : paramNotEmptyList) {
            for (BarCodeDTO barCodeDTO : barCodeList) {
                if (bindCodeParam.getBarCode().equals(barCodeDTO.getBarCode())) {
                    if (!bindCodeParam.getPkProduct().equals(barCodeDTO.getPkProduct())) {
                        // 追溯码与产品不对应
                        return AjaxResult.error(bindCodeParam.getBarCode() + TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_CHECK_BARCODE_NOT));
                    }
                    // 赋值条码ID用以保存
                    bindCodeParam.setDeliverBarCodeId(barCodeDTO.getPkId());
                    break;
                }
            }
        }

        // 绑定条码
        iSaDeliverBarCodeService.codeBind(paramNotEmptyList, SecurityUtils.getUserId(), pkCountry);
        return AjaxResult.success();
    }


    /**
     * 重置条码
     *
     * @param param 发货条码ID参数
     * @return
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.DELIVER_HANDLED, method = EOperationMethod.UNBIND, remark = "重置条码")
    @PutMapping("/code-reset")
    public AjaxResult codeReset(@RequestBody ResetCodeParam param) {
        // 条码ID
        Long barCodeId = param.getBarCodeId();
        if (null == barCodeId) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        // 重置条码
        iSaDeliverBarCodeService.codeReset(barCodeId, SecurityUtils.getPkCountry(), SecurityUtils.getUserId());
        return AjaxResult.success();
    }


    /**
     * 解绑条形码数据
     *
     * @param deliverIdList 发货单ID列表
     * @return
     */
    @GetMapping("/code-unbind-data")
    public AjaxResult codeUnbindData(@RequestParam("deliverIdList") List<Long> deliverIdList) {
        Integer pkCountry = SecurityUtils.getPkCountry();

        // 返回最终结果
        List<DeliverBarCodeVO> resultList = new ArrayList<>();

        // 只查询 发货单明细 + 已生成的条码
        List<DeliverBarCodeVO> deliverBarCodeVOList = iSaDeliverItemsService.deliverCodeData(deliverIdList, null, pkCountry, EYesNo.YES.getIntValue());

        // 遍历发货明细列表
        for (DeliverBarCodeVO deliverBarCode : deliverBarCodeVOList) {
            // 发货明细对应的条码明细
            List<DeliverBarCodeDetailVO> detailList = deliverBarCode.getDetailList();

            // 遍历发货明细数量，填充已经绑定过的条码
            for (DeliverBarCodeDetailVO deliverBarCodeDetailVO : detailList) {
                // 生成最终要返回数据
                resultList.add(DeliverBarCodeVO.builder()
                        .pkId(deliverBarCode.getPkId())
                        .pkDeliver(deliverBarCode.getPkDeliver())
                        .orderCode(deliverBarCode.getOrderCode())
                        .pkProduct(deliverBarCode.getPkProduct())
                        .productName(deliverBarCode.getProductName())
                        .specsName(deliverBarCode.getSpecsName())
                        .deliverBarCodeId(deliverBarCodeDetailVO.getDeliverBarCodeId())
                        .barCode(deliverBarCodeDetailVO.getBarCode())
                        .build());
            }
        }

        return AjaxResult.success(resultList);
    }


    /**
     * 解绑条码
     *
     * @param paramList 发货条码ID列表
     * @return
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.DELIVER_HANDLED, method = EOperationMethod.UNBIND)
    @PutMapping("/code-unbind")
    public AjaxResult unbindCode(@RequestBody List<Integer> paramList) {
        if (CollectionUtil.isEmpty(paramList)) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        // 解绑条码
        iSaDeliverBarCodeService.codeClear(paramList, SecurityUtils.getPkCountry(), SecurityUtils.getUserId());
        return AjaxResult.success();
    }


    /**
     * 删除发货单明细
     *
     * @param id 发货明细ID
     * @return
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.DELIVER_HANDLED, method = EOperationMethod.DELETE, remark = "删除发货明细")
    @DeleteMapping("/delete-deliver-items")
    public AjaxResult deleteDeliverItems(@RequestParam Long id) {
        SaDeliver saDeliver = iSaDeliverService.queryDeliverByItems(id, SecurityUtils.getPkCountry());
        if (null == saDeliver) {
            // 发货单不存在
            return AjaxResult.error(TransactionUtils.getContent(TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_NOT_EXIST)));
        }
        if (EDeliveryStatus.NOT_DELIVERY.getValue() != saDeliver.getDeliverStatus()) {
            // 发货单已发货
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_SHIPPED));
        }
        iSaDeliverService.deleteDeliverItems(saDeliver, id, SecurityUtils.getPkCountry(), SecurityUtils.getUserId());
        return AjaxResult.success();
    }


    /**
     * 删除发货单
     *
     * @param idList 发货单ID列表
     * @return
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.DELIVER_HANDLED, method = EOperationMethod.DELETE, remark = "删除发货单")
    @DeleteMapping("/delete-deliver")
    public AjaxResult deleteDeliver(@RequestParam List<Long> idList) {
        List<SaDeliver> deliverList = iSaDeliverService.listByIds(idList);
        if (CollectionUtils.isEmpty(deliverList)) {
            // 发货单不存在
            return AjaxResult.error(TransactionUtils.getContent(TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_NOT_EXIST)));
        }
        for (SaDeliver saDeliver : deliverList) {
            if (EDeliveryStatus.NOT_DELIVERY.getValue() != saDeliver.getDeliverStatus()) {
                // 发货单已发货
                return AjaxResult.error(saDeliver.getDeliverCode() + TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_SHIPPED));
            }
        }
        iSaDeliverService.deleteDeliver(deliverList, idList, SecurityUtils.getPkCountry(), SecurityUtils.getUserId());
        return AjaxResult.success();
    }

}
