package org.dromara.zhishu.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.service.ISysConfigService;
import org.dromara.zhishu.domain.bo.TShopOrderBo;
import org.dromara.zhishu.domain.dto.OrderGoodsDto;
import org.dromara.zhishu.domain.vo.*;
import org.dromara.zhishu.service.*;
import org.dromara.zhishu.util.InterfaceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.zhishu.domain.bo.DepotOrderBo;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 仓库订单信息
 *
 * @author yxy
 * @date 2025-06-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/depotOrder")
public class DepotOrderController extends BaseController {

    private final IDepotOrderService depotOrderService;
    private final ITDepotService depotService;
    private final ITLogisticsService logisticsService;
    private final ITShopOrderService shopOrderService;
    private final IFastMailService fastMailService;
    private final ISysConfigService configService;;

    /**
     * 查询仓库订单信息列表
     */
    @SaCheckPermission("zhishu:depotOrder:list")
    @GetMapping("/list")
    public TableDataInfo<DepotOrderVo> list(DepotOrderBo bo, PageQuery pageQuery) {
        return depotOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出仓库订单信息列表
     */
    @SaCheckPermission("zhishu:depotOrder:export")
    @Log(title = "仓库订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DepotOrderBo bo, HttpServletResponse response) {
        List<DepotOrderVo> list = depotOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "仓库订单信息", DepotOrderVo.class, response);
    }

    /**
     * 获取仓库订单信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:depotOrder:query")
    @GetMapping("/{id}")
    public R<DepotOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(depotOrderService.queryById(id));
    }

    /**
     * 新增仓库订单信息
     */
    @SaCheckPermission("zhishu:depotOrder:add")
    @Log(title = "仓库订单信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DepotOrderBo bo) {
        return toAjax(depotOrderService.insertByBo(bo));
    }

    /**
     * 修改仓库订单信息
     */
    @SaCheckPermission("zhishu:depotOrder:edit")
    @Log(title = "仓库订单信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DepotOrderBo bo) {
        return toAjax(depotOrderService.updateByBo(bo));
    }

    /**
     * 删除仓库订单信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zhishu:depotOrder:remove")
    @Log(title = "仓库订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(depotOrderService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     *  电子面单下单
     *  获取运单号
     */
    @SaIgnore
    @GetMapping("/getMailNo")
    public R<String> getMailNo(String id) throws Exception {
        DepotOrderVo depotOrderVo = depotOrderService.selectDepotOrderById(id);

        if (depotOrderVo != null){
            TShopOrderVo shopOrderVo = shopOrderService.queryById(Long.parseLong(depotOrderVo.getShopOrderId()));


                //查询韵达联调账号密码
                FastMailVo fastMailVo = fastMailService.selectByUserIdAndType(LoginHelper.getUserId().toString(),"1");
                if(fastMailVo == null){
                    return R.fail("未配置韵达快递账号,请前往快递打单账号管理");
                }
                //获取服务id
               String fastMailIp = configService.selectConfigByKey("fastMail.ip");
              //   String fastMailIp = "http://localhost:8100";

                Map map=sendMailToYunda(depotOrderVo,fastMailVo);


                String mailNo = InterfaceUtils.getInterfacePost(fastMailIp,"/api/yunda/createBmOrder", map);

                Map mailNoMap = JsonUtils.parseMap(mailNo);

                List list = (List) mailNoMap.get("data");

                Map dataMap = (Map) list.get(0);

                mailNo = dataMap.get("mail_no").toString();
                //存储到订单中
                TShopOrderBo shopOrderBo = new TShopOrderBo();
                shopOrderBo.setId(Long.parseLong(depotOrderVo.getShopOrderId()));
                shopOrderBo.setTrackingNumber(mailNo);
                shopOrderBo.setOrderStatus(2);
                shopOrderService.updateByBo(shopOrderBo);


                //调用电子面单打印接口
                Map dayinMap = new HashMap();
                dayinMap.put("mailNo",mailNo);
                dayinMap.put("partner_id",fastMailVo.getPartnerId());
                dayinMap.put("secret",fastMailVo.getSecret());

                String pdfDataStr = InterfaceUtils.getInterfacePost(fastMailIp,"/api/yunda/createBmOrderDaYin", dayinMap);
                Map pdfDataMap = JsonUtils.parseMap(pdfDataStr);

                List pdfDataList = (List) pdfDataMap.get("data");
                Map pdfData = (Map) pdfDataList.get(0);
                String pdfInfo = pdfData.get("pdfInfo").toString();


            return R.ok(pdfInfo);
        }else {
            return R.fail("未找到该订单");
        }

    }


    @GetMapping("/getPdf")
    public R<String> getPdf(String mailNo){

        //查询韵达联调账号密码
        FastMailVo fastMailVo = fastMailService.selectByUserIdAndType(LoginHelper.getUserId().toString(),"1");
        if(fastMailVo == null){
            return R.fail("未配置韵达快递账号,请前往快递打单账号管理");
        }
        //获取服务id
       String fastMailIp = configService.selectConfigByKey("fastMail.ip");
    //    String fastMailIp = "http://localhost:8100";
        //调用电子面单打印接口
        Map dayinMap = new HashMap();
        dayinMap.put("mailNo",mailNo);
        dayinMap.put("partner_id",fastMailVo.getPartnerId());
        dayinMap.put("secret",fastMailVo.getSecret());

        String pdfDataStr = InterfaceUtils.getInterfacePost(fastMailIp,"/api/yunda/createBmOrderDaYin", dayinMap);
        Map pdfDataMap = JsonUtils.parseMap(pdfDataStr);

        List pdfDataList = (List) pdfDataMap.get("data");
        Map pdfData = (Map) pdfDataList.get(0);
        String pdfInfo = pdfData.get("pdfInfo").toString();

        return R.ok(pdfInfo);
    }

    /**
     * 圆通获取订单号接口
     */
    @SaIgnore
    @GetMapping("/getMailNoByYuanTong")
    public R<String> getMailNoByYuanTong(String id){
        //获取服务id
//        String fastMailIp = configService.selectConfigByKey("fastMail.ip");
        String fastMailIp = "http://localhost:8100";

        DepotOrderVo depotOrderVo = depotOrderService.selectDepotOrderById(id);
        //获取货号
        Map itemList = JsonUtils.parseMap(depotOrderVo.getItemList());
        String artNo = itemList.get("itemSn").toString();
        String itemName = itemList.get("itemName").toString();
        //货号前两位，仓库编码
//        String code = artNo.substring(0,2);
        TDepotVo depotVo = depotService.selectDepotByCode(artNo);
        //获取仓库绑定的运费模板
        TLogisticsVo tLogisticsVo = logisticsService.queryById(depotVo.getId());

        Map map = new HashMap();
        map.put("logisticsNo",depotOrderVo.getOrderSn());
        map.put("senderName",tLogisticsVo.getContact());
        map.put("senderProvinceName",tLogisticsVo.getDeliveryProvince());
        map.put("senderCityName",tLogisticsVo.getDeliveryCity());
        map.put("senderCountyName",tLogisticsVo.getDeliveryArea());
        map.put("senderAddress",depotOrderVo.getReceiverName());
        map.put("receiverAddress",tLogisticsVo.getFullAddress());
        map.put("senderMobile",tLogisticsVo.getPhoneNumber());
        map.put("recipientName",depotOrderVo.getReceiverName());
        map.put("recipientProvinceName",depotOrderVo.getProvince());
        map.put("recipientCityName",depotOrderVo.getCity());
        map.put("recipientCountyName",depotOrderVo.getTown());
        map.put("recipientMobile",depotOrderVo.getReceiverPhoneMask());
        map.put("recipientAddress",depotOrderVo.getReceiverAddressMask());
        map.put("itemName",itemName);
        //备注（货号）
        map.put("remark",artNo);
        
        // 获取商品信息
        String isbn = itemList.containsKey("isbn") ? itemList.get("isbn").toString() : "";
        String itemId = itemList.containsKey("itemId") ? itemList.get("itemId").toString() : "";
        String quality = itemList.containsKey("quality") ? itemList.get("quality").toString() : "";
        String number = itemList.containsKey("number") ? itemList.get("number").toString() : "1";
        
        // 创建OrderGoodsDto对象并设置相关属性
        OrderGoodsDto goodsDto = new OrderGoodsDto()
            .setName(itemName)
            .setItemSn(isbn)
            .setGoodsId(itemId)
            .setQuality(quality)
            .setQuantity(Integer.parseInt(number));
        
        // 创建物品列表集合
        Set<OrderGoodsDto> goodsSet = new HashSet<>();
        goodsSet.add(goodsDto);
        
        // 添加物品列表到参数中
        map.put("goods", goodsSet);

        String mailNoResponse = InterfaceUtils.getInterfacePost(fastMailIp,"/api/yuanTong/createOrder", map);

        Map mailNoMap = JsonUtils.parseMap(mailNoResponse);

        // 直接从返回数据中获取mailNo，而不是从list中获取
        String mailNo = "";
        if (mailNoMap.containsKey("mailNo")) {
            mailNo = mailNoMap.get("mailNo").toString();
        } else if (mailNoMap.containsKey("data") && mailNoMap.get("data") instanceof Map) {
            Map dataMap = (Map) mailNoMap.get("data");
            if (dataMap.containsKey("mailNo")) {
                mailNo = dataMap.get("mailNo").toString();
            }
        }
        
        // 如果没有获取到mailNo，尝试解析完整的响应数据
        if (mailNo.isEmpty() && mailNoMap.containsKey("data")) {
            Object dataObj = mailNoMap.get("data");
            if (dataObj instanceof String) {
                // 尝试解析data字段中的JSON字符串
                Map dataMap = JsonUtils.parseMap(dataObj.toString());
                if (dataMap.containsKey("mailNo")) {
                    mailNo = dataMap.get("mailNo").toString();
                }
            }
        }
        
        // 如果仍然没有获取到mailNo，记录错误并返回
        if (mailNo.isEmpty()) {
            return R.fail("获取运单号失败，接口返回数据：" + mailNoResponse);
        }
        
        //存储到订单中
        TShopOrderBo shopOrderBo = new TShopOrderBo();
        shopOrderBo.setId(Long.parseLong(depotOrderVo.getShopOrderId()));
        shopOrderBo.setTrackingNumber(mailNo);
        shopOrderBo.setOrderStatus(2);
        shopOrderService.updateByBo(shopOrderBo);

        //调用电子面单打印接口
        Map dayinMap = new HashMap();
        dayinMap.put("waybillNo", mailNo);

        String pdfDataStr = InterfaceUtils.getInterfacePost(fastMailIp,"/api/yuanTong/printWaybill", dayinMap);
        Map pdfDataMap = JsonUtils.parseMap(pdfDataStr);
        
        // 直接从返回数据中获取PDF信息
        String pdfInfo = "";
        if (pdfDataMap.containsKey("data")) {
            Object dataObj = pdfDataMap.get("data");
            if (dataObj instanceof Map) {
                Map dataMap = (Map) dataObj;
                if (dataMap.containsKey("pdfBase64")) {
                    pdfInfo = dataMap.get("pdfBase64").toString();
                }
            } else if (dataObj instanceof String) {
                try {
                    // 尝试解析data字段中的JSON字符串
                    Map dataMap = JsonUtils.parseMap(dataObj.toString());
                    if (dataMap != null && dataMap.containsKey("pdfBase64")) {
                        pdfInfo = dataMap.get("pdfBase64").toString();
                    } else {
                        pdfInfo = dataObj.toString();
                    }
                } catch (Exception e) {
                    // 如果解析失败，直接使用字符串
                    pdfInfo = dataObj.toString();
                }
            }
        }
        
        if (pdfInfo.isEmpty()) {
            return R.fail("获取电子面单失败，接口返回数据：" + pdfDataStr);
        }

        return R.ok(pdfInfo);
    }

    @GetMapping("/getPdfByYuanTong")
    public R<String> getPdfByYuanTong(String waybillNo){
        //获取服务id
//        String fastMailIp = configService.selectConfigByKey("fastMail.ip");

        String fastMailIp = "http://localhost:8100";
        //调用电子面单打印接口
        Map dayinMap = new HashMap();
        dayinMap.put("waybillNo", waybillNo);

        String pdfDataStr = InterfaceUtils.getInterfacePost(fastMailIp,"/api/yuanTong/printWaybill", dayinMap);
        Map pdfDataMap = JsonUtils.parseMap(pdfDataStr);

        // 直接从返回数据中获取PDF信息
        String pdfInfo = "";
        if (pdfDataMap.containsKey("data")) {
            Object dataObj = pdfDataMap.get("data");
            if (dataObj instanceof Map) {
                Map dataMap = (Map) dataObj;
                if (dataMap.containsKey("pdfBase64")) {
                    pdfInfo = dataMap.get("pdfBase64").toString();
                }
            } else if (dataObj instanceof String) {
                try {
                    // 尝试解析data字段中的JSON字符串
                    Map dataMap = JsonUtils.parseMap(dataObj.toString());
                    if (dataMap != null && dataMap.containsKey("pdfBase64")) {
                        pdfInfo = dataMap.get("pdfBase64").toString();
                    } else {
                        pdfInfo = dataObj.toString();
                    }
                } catch (Exception e) {
                    // 如果解析失败，直接使用字符串
                    pdfInfo = dataObj.toString();
                }
            }
        }

        if (pdfInfo.isEmpty()) {
            return R.fail("获取电子面单失败，接口返回数据：" + pdfDataStr);
        }

        return R.ok(pdfInfo);
    }

    /**
     * 中通获取订单号接口
     */
    @SaIgnore
    @GetMapping("/getMailNoByZhongTong")
    public R<String> getMailNoByZhongTong(String id){
        //获取服务id
//        String fastMailIp = configService.selectConfigByKey("fastMail.ip");
        String fastMailIp = "http://localhost:8100";

        DepotOrderVo depotOrderVo = depotOrderService.selectDepotOrderById(id);

        //获取货号
        Map itemList = JsonUtils.parseMap(depotOrderVo.getItemList());
        String artNo = itemList.get("itemSn").toString();
        String itemName = itemList.get("itemName").toString();
        //货号前两位，仓库编码
//        String code = artNo.substring(0,2);
        TDepotVo depotVo = depotService.selectDepotByCode(artNo);
        //获取仓库绑定的运费模板
        TLogisticsVo tLogisticsVo = logisticsService.queryById(depotVo.getId());

        Map map = new HashMap();
        map.put("partnerType","2");
        map.put("orderType","1");
        map.put("partnerOrderCode",depotOrderVo.getOrderSn());
        Map<String, Object> accountInfo = new HashMap<>();
        accountInfo.put("accountId", "test");
        accountInfo.put("accountPassword", "ZTO123");
        accountInfo.put("type", "1");
        map.put("accountInfo", accountInfo);
        
        // 发件人信息
        Map<String, Object> senderInfo = new HashMap<>();
        senderInfo.put("senderName", tLogisticsVo.getContact());
        senderInfo.put("senderMobile", tLogisticsVo.getPhoneNumber());
        senderInfo.put("senderProvince", tLogisticsVo.getDeliveryProvince());
        senderInfo.put("senderCity", tLogisticsVo.getDeliveryCity());
        senderInfo.put("senderDistrict", tLogisticsVo.getDeliveryArea());
        senderInfo.put("senderAddress", tLogisticsVo.getFullAddress());
        map.put("senderInfo", senderInfo);

        // 收件人信息
        Map<String, Object> receiveInfo = new HashMap<>();
        receiveInfo.put("receiverName", depotOrderVo.getReceiverName());
        receiveInfo.put("receiverMobile", depotOrderVo.getReceiverPhoneMask());
        receiveInfo.put("receiverProvince", depotOrderVo.getProvince());
        receiveInfo.put("receiverCity", depotOrderVo.getCity());
        receiveInfo.put("receiverDistrict", depotOrderVo.getTown());
        receiveInfo.put("receiverAddress", depotOrderVo.getReceiverAddressMask());
        map.put("receiveInfo", receiveInfo);
        // 获取商品信息
        String isbn = itemList.containsKey("isbn") ? itemList.get("isbn").toString() : "";
        String itemId = itemList.containsKey("itemId") ? itemList.get("itemId").toString() : "";
        String quality = itemList.containsKey("quality") ? itemList.get("quality").toString() : "";
        String number = itemList.containsKey("number") ? itemList.get("number").toString() : "1";

        // 创建OrderGoodsDto对象并设置相关属性
        OrderGoodsDto goodsDto = new OrderGoodsDto()
                .setName(itemName)
                .setItemSn(isbn)
                .setGoodsId(itemId)
                .setQuality(quality)
                .setQuantity(Integer.parseInt(number));

        // 创建物品列表集合
        Set<OrderGoodsDto> goodsSet = new HashSet<>();
        goodsSet.add(goodsDto);
        Map<String, Object> orderItems = new HashMap<>();
        orderItems.put("name", goodsSet);
        map.put("orderItems", orderItems);

        String mailNoResponse = InterfaceUtils.getInterfacePost(fastMailIp,"/api/zto/createOrder", map);

        Map mailNoMap = JsonUtils.parseMap(mailNoResponse);

        // 直接从返回数据中获取mailNo，而不是从list中获取
        String mailNo = "";
        if (mailNoMap.containsKey("mailNo")) {
            mailNo = mailNoMap.get("mailNo").toString();
        } else if (mailNoMap.containsKey("data") && mailNoMap.get("data") instanceof Map) {
            Map dataMap = (Map) mailNoMap.get("data");
            if (dataMap.containsKey("mailNo")) {
                mailNo = dataMap.get("mailNo").toString();
            }
        }

        // 如果没有获取到mailNo，尝试解析完整的响应数据
        if (mailNo.isEmpty() && mailNoMap.containsKey("data")) {
            Object dataObj = mailNoMap.get("data");
            if (dataObj instanceof String) {
                // 尝试解析data字段中的JSON字符串
                Map dataMap = JsonUtils.parseMap(dataObj.toString());
                if (dataMap.containsKey("mailNo")) {
                    mailNo = dataMap.get("mailNo").toString();
                }
            }
        }

        // 如果仍然没有获取到mailNo，记录错误并返回
        if (mailNo.isEmpty()) {
            return R.fail("获取运单号失败，接口返回数据：" + mailNoResponse);
        }

        //存储到订单中
        TShopOrderBo shopOrderBo = new TShopOrderBo();
        shopOrderBo.setId(Long.parseLong(depotOrderVo.getShopOrderId()));
        shopOrderBo.setTrackingNumber(mailNo);
        shopOrderBo.setOrderStatus(2);
        shopOrderService.updateByBo(shopOrderBo);

        //调用电子面单打印接口
        Map dayinMap = new HashMap();
        dayinMap.put("waybillNo", mailNo);

        String pdfDataStr = InterfaceUtils.getInterfacePost(fastMailIp,"/api/zto/printWaybill", dayinMap);
        Map pdfDataMap = JsonUtils.parseMap(pdfDataStr);

        // 直接从返回数据中获取PDF信息
        String pdfInfo = "";
        if (pdfDataMap.containsKey("data")) {
            Object dataObj = pdfDataMap.get("data");
            if (dataObj instanceof Map) {
                Map dataMap = (Map) dataObj;
                if (dataMap.containsKey("pdfBase64")) {
                    pdfInfo = dataMap.get("pdfBase64").toString();
                }
            } else if (dataObj instanceof String) {
                try {
                    // 尝试解析data字段中的JSON字符串
                    Map dataMap = JsonUtils.parseMap(dataObj.toString());
                    if (dataMap != null && dataMap.containsKey("pdfBase64")) {
                        pdfInfo = dataMap.get("pdfBase64").toString();
                    } else {
                        pdfInfo = dataObj.toString();
                    }
                } catch (Exception e) {
                    // 如果解析失败，直接使用字符串
                    pdfInfo = dataObj.toString();
                }
            }
        }

        if (pdfInfo.isEmpty()) {
            return R.fail("获取电子面单失败，接口返回数据：" + pdfDataStr);
        }

        return R.ok(pdfInfo);
    }


    public  Map  sendMailToYunda(DepotOrderVo depotOrderVo,FastMailVo fastMailVo) {


        Map map = new HashMap();
        // 判断订单来源  1 拼多多 2 孔网
        if(depotOrderVo.getOrderSourceType()==1){
            //获取货号
            Map itemList = JsonUtils.parseMap(depotOrderVo.getItemList());

            String artNo = itemList.get("outerId").toString();
            String itemName = itemList.get("goodsName").toString();
            //货号前两位，仓库编码
            //  String code = artNo.substring(0,2);
            TDepotVo depotVo = depotService.selectDepotByCode(artNo);
            //获取仓库绑定的运费模板
            TLogisticsVo tLogisticsVo = logisticsService.queryById(depotVo.getId());


            map.put("order_serial_no",depotOrderVo.getOrderSn());
            map.put("khddh",depotOrderVo.getOrderSn());
            map.put("senderName",tLogisticsVo.getContact());
            map.put("senderAddress",tLogisticsVo.getFullAddress());
            map.put("senderMobile",tLogisticsVo.getPhoneNumber());
            map.put("receiverName",depotOrderVo.getReceiverName());
            map.put("receiverAddress",depotOrderVo.getReceiverAddress());
            map.put("receiverMobile",depotOrderVo.getReceiverPhoneMask());
            map.put("partner_id",fastMailVo.getPartnerId());
            map.put("secret",fastMailVo.getSecret());
            map.put("itemName",itemName);
            //备注（货号）
            map.put("remark",artNo);
            // 构建自定义区域的商品信息数据
            String isbn = itemList.containsKey("outerGoodsId") ? itemList.get("outerGoodsId").toString() : "";
            String itemId = itemList.containsKey("skuId") ? itemList.get("skuId").toString() : "";
            String quality = itemList.containsKey("quality") ? itemList.get("quality").toString() : "";
            String number = itemList.containsKey("goodsCount") ? itemList.get("goodsCount").toString() : "";

            // 按照要求格式组装数据: isbn|itemId|itemName|quality*number
            String customItemInfo = isbn + "|" + itemId + "|" + itemName + "|[" + quality + "]*" + number;

            // 添加自定义数据到打印参数
            map.put("customData", customItemInfo);


        }
        if(depotOrderVo.getOrderSourceType()==2){

            Map itemList = JSONObject.parseObject(depotOrderVo.getItemList());
            String artNo = itemList.get("itemSn").toString();
            String itemName = itemList.get("itemName").toString();
            //货号前两位，仓库编码
//        String code = artNo.substring(0,2);
            TDepotVo depotVo = depotService.selectDepotByCode(artNo);

            //获取仓库绑定的运费模板
            TLogisticsVo tLogisticsVo = logisticsService.queryById(depotVo.getId());
            //获取仓库绑定的运费模板
            // Logistics logistics = logisticsService.getInfo(depot.getId());


            map.put("order_serial_no",depotOrderVo.getOrderSn());
            map.put("khddh",depotOrderVo.getOrderSn());
            map.put("senderName",tLogisticsVo.getContact());
            map.put("senderAddress",tLogisticsVo.getFullAddress());
            map.put("senderMobile",tLogisticsVo.getPhoneNumber());
            map.put("receiverName",depotOrderVo.getReceiverName());
            map.put("receiverAddress",depotOrderVo.getReceiverAddress());
            map.put("receiverMobile",depotOrderVo.getReceiverPhoneMask());
            map.put("partner_id",fastMailVo.getPartnerId());
            map.put("secret",fastMailVo.getSecret());
            map.put("itemName",itemName);
            //备注（货号）
            map.put("remark",artNo);
            // 构建自定义区域的商品信息数据
            String isbn = itemList.containsKey("isbn") ? itemList.get("isbn").toString() : "";
            String itemId = itemList.containsKey("itemId") ? itemList.get("itemId").toString() : "";
            String quality = itemList.containsKey("quality") ? itemList.get("quality").toString() : "";
            String number = itemList.containsKey("number") ? itemList.get("number").toString() : "";

            // 按照要求格式组装数据: isbn|itemId|itemName|quality*number
            String customItemInfo = isbn + "|" + itemId + "|" + itemName + "|[" + quality + "]*" + number;

            // 添加自定义数据到打印参数
            map.put("customData", customItemInfo);

        }


        return map;
    }


}
