package org.ta.pddserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.ta.pddserver.config.PddAPIConfig;
import org.ta.pddserver.entity.*;
import org.ta.pddserver.model.api.request.*;
import org.ta.pddserver.model.api.response.*;
import org.ta.pddserver.model.enums.ErrorEnum;
import org.ta.pddserver.model.enums.StatusEnum;
import org.ta.pddserver.model.pddjy.request.*;
import org.ta.pddserver.model.pddlg.common.TraceObject;
import org.ta.pddserver.model.pddlg.request.PddLogisticsCoTrackNotifyRequest;
import org.ta.pddserver.model.pddlg.response.PddLogisticsCoTrackNotifyResponse;
import org.ta.pddserver.service.impl.*;
import org.ta.pddserver.utils.*;

import java.util.*;

@Slf4j
@RestController
public class PddLocalController {


    @Resource
    LogOrderImpl logOrderImpl;

    @Resource
    LogOrderMailDetailImpl logOrderMailDetailImpl;

    @Resource
    TimeTool timeTool;

    @Resource
    CollectOutMailDetailImpl collectOutMailDetailImpl;

    @Resource
    CollectOrderSendImpl collectOrderSendImpl;

    @Resource
    LogOrderMailDetailIoImpl logOrderMailDetailIoImpl;

    @Resource
    LogOrderPackageImpl logOrderPackageImpl;

    @Resource
    PackageNoticeImpl packageNoticeImpl;

    @Resource
    private CollectReceiverDetailImpl collectReceiverDetailImpl;
    @Resource
    private CollectMailDetailImpl collectMailDetailImpl;

    @Resource
    private CollectOrderImpl collectOrderImpl;
    @Autowired
    private CollectOrderDetailImpl collectOrderDetailImpl;
    @Autowired
    private TraceObjectImpl traceObjectImpl;

    /*集运单列表*/
    @PostMapping("/logOrderList")
    public JSONObject logOrderList(HttpServletRequest request, @RequestBody LogOrderListRequest logOrderListRequest) {
        return JsonResponse.generateLocalCommonSuccessResponse(logOrderImpl.getListByRequest(logOrderListRequest));
    }

    /*包裹列表*/
    @PostMapping("/logOrderMailDetailList")
    public JSONObject logOrderMailDetailList(HttpServletRequest request, @RequestBody LogOrderMailDetailListRequest logOrderMailDetailListRequest) {
        return JsonResponse.generateLocalCommonSuccessResponse(logOrderMailDetailImpl.getListByCodeNo(logOrderMailDetailListRequest));
    }

    /*运单号查询*/
    @PostMapping("/mailNoSelect")
    public JSONObject mailNoSelect(HttpServletRequest request, @RequestBody MailNoRequest mailNoRequest) {
        MailNoResponse mailNoResponse = logOrderMailDetailImpl.getInfoByMailNo(mailNoRequest.getMailNo());
        return JsonResponse.generateLocalCommonSuccessResponse(mailNoResponse);
    }

    /*待拆包列表*/
    @PostMapping("/packageNoticeList")
    public JSONObject packageNoticeList(HttpServletRequest request, @RequestBody PackageNoticeRequest packageNoticeRequest) {

        return JsonResponse.generateLocalCommonSuccessResponse(packageNoticeImpl.getList(packageNoticeRequest));
    }

    /*待拆包查询*/
    @PostMapping("/packageScan")
    public Map<String, Object> packageScan(HttpServletRequest request, @RequestBody MailNoRequest mailNoRequest) {
        List<LogOrderMailDetailEntity> list = logOrderMailDetailImpl.getListByCode(mailNoRequest.getMailNo());
        if (list.isEmpty()) {
            return JsonResponse.generateLocalCommonFailedResponse("-1", "信息获取失败，请检查上传信息");
        }

        LogOrderEntity logOrderEntity = logOrderImpl.getByOrderCode(list.get(0).getLogisticsOrderCode());

        Map<String, Object> map = new HashMap<>();
        map.put("logOrder", logOrderEntity);
        map.put("list", list);

        return JsonResponse.generateLocalCommonSuccessResponse(map);
    }

    /*待拆包列表*/
    @PostMapping("/traceObject")
    public JSONObject traceObject(HttpServletRequest request, @RequestBody TraceObjectRequest traceObjectRequest) {
        TraceObjectEntity traceObject = new TraceObjectEntity();
        traceObject.setTrackingNumber(traceObjectRequest.getTrackingNumber());
        traceObject.setTrackingOrderNo(traceObjectRequest.getTrackingOrderNo());
        traceObject.setId(traceObjectRequest.getId());
        traceObject.setStatusCode(traceObjectRequest.getStatusCode());
        traceObject.setDescription(traceObjectRequest.getDescription());
        traceObject.setOperationTime(traceObjectRequest.getOperationTime());
        traceObject.setTransferFlag(traceObjectRequest.getTransferFlag());
        traceObject.setTransferNo(traceObjectRequest.getTransferNo());
        traceObject.setTransferPartnerId(traceObjectRequest.getTransferPartnerId());
        traceObject.setTimeZone(traceObjectRequest.getTimeZone());
        traceObject.setSiteName(traceObjectRequest.getSiteName());
        traceObject.setSiteType(traceObjectRequest.getSiteType());
        traceObject.setSiteNo(traceObjectRequest.getSiteNo());
        traceObject.setNextSiteName(traceObjectRequest.getNextSiteName());
        traceObject.setNextSiteNo(traceObjectRequest.getNextSiteNo());
        traceObject.setFailReason(traceObjectRequest.getFailReason());
        traceObject.setWeight(traceObjectRequest.getWeight());
        traceObject.setCountry(traceObjectRequest.getCountry());
        traceObject.setCity(traceObjectRequest.getCity());
        traceObject.setProvince(traceObjectRequest.getProvince());
        traceObject.setDistrict(traceObjectRequest.getDistrict());
        traceObject.setContact(traceObjectRequest.getContact());
        traceObject.setContactInfo(traceObjectRequest.getContactInfo());
        traceObject.setSignerName(traceObjectRequest.getSignerName());
        traceObject.setAddress(traceObjectRequest.getAddress());
        traceObject.setRemark(traceObjectRequest.getRemark());

        traceObjectImpl.save(traceObject);

        //推送pdd
        PddLogisticsCoTrackNotifyRequest requests = new PddLogisticsCoTrackNotifyRequest();
        requests.setClient_id(PddAPIConfig.CLIENT_ID);
        requests.setShip_id(PddAPIConfig.SHIP_ID);
        requests.setTarget_client_id(PddAPIConfig.CLIENT_ID);
        requests.setTimestamp(System.currentTimeMillis() / 1000);
        PddLogisticsCoTrackNotifyRequest.DataObject dataObject = getDataObject(traceObjectRequest.getTrackingNumber());
        requests.setDataObject(dataObject);
        requests.encodeData();
        requests.setSign(PddSignTool.generateSign(requests, PddAPIConfig.CLIENT_SECRET));
        log.info("requestModel:{}", JSON.toJSONString(requests));
        String resp = PddHttpTool.sendJsonPost(requests);
        log.info("resp = {}", resp);
        PddLogisticsCoTrackNotifyResponse response = JSON.parseObject(resp, PddLogisticsCoTrackNotifyResponse.class);
        log.info("response = {}", JSON.toJSONString(response));

        return JsonResponse.generateLocalCommonSuccessResponse(true);
    }


    /*出库列表*/
    @PostMapping("/collectMailDetailList")
    public JSONObject collectMailDetailList(HttpServletRequest request, @RequestBody CollectMailDetailListRequest collectMailDetailListRequest) {

        return JsonResponse.generateLocalCommonSuccessResponse(collectMailDetailImpl.getList(collectMailDetailListRequest));
    }

    /*出库详情*/
    @PostMapping("/collectOrderInfo")
    public JSONObject collectOrderInfo(HttpServletRequest request, @RequestBody CollectOrderRequest collectOrderRequest) {
        Map<String, Object> map = new HashMap<>();
        CollectOrderInfoResponse collectOrderInfoResponse = collectOrderImpl.getInfoByOrderCode(collectOrderRequest.getOrderCode());
        List<CollectOrderDetailResponse> list = collectOrderDetailImpl.getListByOrderCode(collectOrderRequest.getOrderCode());
        CollectReceiverDetailResponse collectOrderReceiverDetailResponse = collectReceiverDetailImpl.getByOrderCode(collectOrderRequest.getOrderCode());

        map.put("info", collectOrderInfoResponse);
        map.put("detail", list);
        map.put("receiver", collectOrderReceiverDetailResponse);
        return JsonResponse.generateLocalCommonSuccessResponse(map);
    }


    /*待拣货列表*/
    @PostMapping("/collectOrderList")
    public JSONObject collectOrderList(HttpServletRequest request, @RequestBody CollectOrderListRequest collectOrderListRequest) {

        return JsonResponse.generateLocalCommonSuccessResponse(collectOrderImpl.getList(collectOrderListRequest));
    }

    /*拆包列表*/
    @PostMapping("/logOrderPackageList")
    public JSONObject logOrderPackageList(HttpServletRequest request, @RequestBody LogOrderPackageListRequest logOrderPackageListRequest) {
        return JsonResponse.generateLocalCommonSuccessResponse(logOrderPackageImpl.getList(logOrderPackageListRequest));
    }


    /*出库扫码*/
    @PostMapping("/collectOrderScan")
    public Map<String, Object> collectOrderScan(HttpServletRequest request, @RequestBody CollectOrderScanRequest collectOrderScanRequest) {

        Map<String, Object> map = new HashMap<>();

        CollectOrderResponse collectOrderResponse = collectOrderImpl.getByOrderCode(collectOrderScanRequest.getOrderCode());
        List<CollectOutMailDetailResponse> list = collectOutMailDetailImpl.getListByOrderCode(collectOrderScanRequest.getOrderCode());

        map.put("collectOrder", collectOrderResponse);
        map.put("list", list);

        return JsonResponse.generateLocalCommonSuccessResponse(map);
    }

    /*拣货扫码*/
    @PostMapping("/collectMailDetailScan")
    public Map<String, Object> collectMailDetailScan(HttpServletRequest request, @RequestBody CollectMailDetailRequest collectMailDetailRequest) {

        Map<String, Object> map = new HashMap<>();
        CollectMailDetailEntity mailDetailEntity = collectMailDetailImpl.getByMailNo(collectMailDetailRequest.getMailNo());
        if (mailDetailEntity == null) {
            return JsonResponse.generateLocalCommonFailedResponse("-1", "未找到需要拣货的包裹信息");
        }


        List<CollectMailDetailEntity> list = collectMailDetailImpl.getListByOrderCode(mailDetailEntity.getOrderCode());

        CollectOrderResponse collectOrderResponse = collectOrderImpl.getByOrderCode(mailDetailEntity.getOrderCode());

        map.put("collectOrder", collectOrderResponse);
        map.put("collectOutMailDetail", mailDetailEntity);
        map.put("list", list);

        return JsonResponse.generateLocalCommonSuccessResponse(map);
    }


    /*待拣包裹列表接口*/
    @PostMapping("/mailNoSelectAll")
    public Map<String, Object> mailNoSelectAll(HttpServletRequest request, @RequestBody MailNoRequest mailNoRequest) {
        Map<String, Object> map = new HashMap<>();

        CollectOutMailDetailEntity collectOutMailDetail = collectOutMailDetailImpl.getInfoByMailNo(mailNoRequest.getMailNo());
        List<CollectOutMailDetailResponse> list = collectOutMailDetailImpl.getListByOrderCode(collectOutMailDetail.getOrderCode());

        CollectOrderResponse collectOrderResponse = collectOrderImpl.getByOrderCode(collectOutMailDetail.getOrderCode());

        map.put("list", list);
        map.put("collectOutMailDetail", collectOutMailDetail);

        return JsonResponse.generateLocalCommonSuccessResponse(map);
    }


    /*签收*/
    @PostMapping("/receive")
    public JSONObject receive(HttpServletRequest request, @RequestBody ReceiveRequest receiveRequest) {
        LogOrderMailDetailEntity mailNoResponse = logOrderMailDetailImpl.getAllInfoByMailNo(receiveRequest.getMailNo());

        String action = receiveRequest.getAction();

        PddServiceConsoFirstBizActionNotifyRequest notifyRequest = new PddServiceConsoFirstBizActionNotifyRequest();
        notifyRequest.setProviderCode(PddAPIConfig.PROVIDER_CODE);
        notifyRequest.setExecuteTime(timeTool.getNowString());
        notifyRequest.setTimeZone("UTC+8");
        notifyRequest.setLogisticsOrderCode(mailNoResponse.getLogisticsOrderCode());
        notifyRequest.setBizAction(action);
        PddServiceConsoFirstBizActionNotifyRequest.Result notifyResult = new PddServiceConsoFirstBizActionNotifyRequest.Result();
        notifyRequest.setMailNo(mailNoResponse.getMailNo());
        notifyRequest.setExpressCode(mailNoResponse.getExpressCode());

        LogOrderMailDetailIoEntity logOrderMailDetailIoEntity = new LogOrderMailDetailIoEntity();

        if (action.equals("warehouse_sign")) {
            notifyRequest.setBizActionDesc("仓库签收");
            notifyResult.setCode(0);
            logOrderMailDetailIoEntity.setStatus(StatusEnum.SIGNED.getCode());
        } else if (action.equals("warehouse_reject_sign")) {
            notifyRequest.setBizActionDesc("仓库拒签");
            notifyResult.setCode(receiveRequest.getCode());
            notifyResult.setDesc(receiveRequest.getDesc());
            notifyResult.setRemark(receiveRequest.getDesc());

            if (receiveRequest.getLength() != null && receiveRequest.getLength() > 0) {

                notifyRequest.setPackageInfo(new PddServiceConsoFirstBizActionNotifyRequest.PackageInfo());
                notifyRequest.getPackageInfo().setWeight(receiveRequest.getWeight());
                notifyRequest.getPackageInfo().setActualWeight(receiveRequest.getActualWeight());
                notifyRequest.getPackageInfo().setLength(receiveRequest.getLength());
                notifyRequest.getPackageInfo().setWidth(receiveRequest.getWidth());
                notifyRequest.getPackageInfo().setHeight(receiveRequest.getHeight());
            }

            notifyRequest.setResult(notifyResult);
            logOrderMailDetailIoEntity.setStatus(StatusEnum.REJECTED.getCode());
        } else if (action.equals("destroy")) {
            notifyRequest.setBizActionDesc("包裹销毁");
            notifyResult.setCode(0);
            logOrderMailDetailIoEntity.setStatus(StatusEnum.DESTROY.getCode());
        }

        notifyRequest.setResult(notifyResult);

        JYBaseRequestModel requestModel = new JYBaseRequestModel();
        requestModel.setRequest(JSON.toJSONString(notifyRequest));
        requestModel.setRequest_id("" + new Date().getTime());
        requestModel.setType("pdd.service.conso.first.biz.action.notify");
        requestModel.setTarget_client_id(PddAPIConfig.CLIENT_ID);
        requestModel.setClient_id(PddAPIConfig.CLIENT_ID);
        long time = new Date().getTime() / 1000;
        requestModel.setTimestamp(time);
        requestModel.setSign(PddSignTool.generateSign(requestModel, PddAPIConfig.CLIENT_SECRET));
        String resp = PddHttpTool.sendJsonPost(requestModel);

        //更新log_order_mail_detail
        Boolean result = logOrderMailDetailImpl.updateByMailNo(receiveRequest.getMailNo(), logOrderMailDetailIoEntity.getStatus());

        logOrderMailDetailIoEntity.setMailNo(receiveRequest.getMailNo());

        logOrderMailDetailIoEntity.setLogisticsOrderCode(mailNoResponse.getLogisticsOrderCode());
        logOrderMailDetailIoEntity.setExpressCode(mailNoResponse.getExpressCode());
        logOrderMailDetailIoEntity.setReq(JSON.toJSONString(requestModel));
        logOrderMailDetailIoEntity.setResp(resp);

        logOrderMailDetailIoImpl.save(logOrderMailDetailIoEntity);
//        ioEntity.setReq(JSON.toJSONString(requestModel));
//        ioEntity.setResp(resp);
//        logOrderMailDetailIo.save(ioEntity);

        return JsonResponse.generateLocalCommonSuccessResponse(true);
    }

    /*入库*/
    @PostMapping("/inBound")
    public JSONObject inBound(HttpServletRequest request, @RequestBody InBoundRequest inBoundRequest) {
        LogOrderMailDetailEntity mailNoResponse = logOrderMailDetailImpl.getByMailNo(inBoundRequest.getMailNo());
        PddServiceConsoInboundRequest notifyRequest = new PddServiceConsoInboundRequest();
        // 查询orderNO 下所有mailNo,判断是全部还是部分
        Long unNum = logOrderMailDetailImpl.getSumByCodeStatus(mailNoResponse.getLogisticsOrderCode(), (long) StatusEnum.SIGNED.getCode());
        LogOrderEntity logOrderEntity = logOrderImpl.getByOrderCode(mailNoResponse.getLogisticsOrderCode());
        Long num = (long) logOrderEntity.getNum();
        log.info("已入库数量 {} ； 未入库数量 {}", num, unNum);

        //判断是否存在入库信息
        if (mailNoResponse.getStatus() == StatusEnum.INBOUND.getCode()) {
            //重复入库
            mailNoResponse.setEnterTime(timeTool.getNowDate());
        } else {
            num += 1;
            mailNoResponse.setEnterTime(timeTool.getNowDate());
            mailNoResponse.setStatus(StatusEnum.INBOUND.getCode());
        }
        notifyRequest.setPackageQuantity(num);
        mailNoResponse.setLength(inBoundRequest.getLength().intValue());
        mailNoResponse.setWidth(inBoundRequest.getWidth().intValue());
        mailNoResponse.setHeight(inBoundRequest.getHeight().intValue());
        mailNoResponse.setWeight(inBoundRequest.getWeight().intValue());

        notifyRequest.setProviderCode(PddAPIConfig.PROVIDER_CODE);
        notifyRequest.setLogisticsOrderCode(mailNoResponse.getLogisticsOrderCode());
        notifyRequest.setExecuteTime(timeTool.getNowString());
        notifyRequest.setTimeZone("UTC+8");


        if (notifyRequest.getPackageQuantity() == num) {
            notifyRequest.setStatus("FULL_INBOUND");
        } else {
            notifyRequest.setStatus("PART_INBOUND");
        }

        notifyRequest.setPackageInfo(new PddServiceConsoInboundRequest.PackageInfo());
        notifyRequest.getPackageInfo().setMailNo(mailNoResponse.getMailNo());
        notifyRequest.getPackageInfo().setExpressCode(mailNoResponse.getExpressCode());
        notifyRequest.getPackageInfo().setLength(inBoundRequest.getLength());
        notifyRequest.getPackageInfo().setWidth(inBoundRequest.getWidth());
        notifyRequest.getPackageInfo().setHeight(inBoundRequest.getHeight());
        notifyRequest.getPackageInfo().setWeight(inBoundRequest.getWeight());
        notifyRequest.getPackageInfo().setActualWeight((long) PddJYPriceTool.calculateWeight(inBoundRequest.getLength().intValue(), inBoundRequest.getWidth().intValue(), inBoundRequest.getHeight().intValue(), inBoundRequest.getWeight().intValue(), true));
        notifyRequest.getPackageInfo().setGoodsType("NORMAL");

        notifyRequest.setResult(new PddServiceConsoInboundRequest.Result());
        notifyRequest.getResult().setCode(0);

        JYBaseRequestModel requestModel = new JYBaseRequestModel();
        requestModel.setRequest(JSON.toJSONString(notifyRequest));
        requestModel.setRequest_id("" + new Date().getTime());
        requestModel.setType("pdd.service.conso.inbound");
        requestModel.setTarget_client_id(PddAPIConfig.CLIENT_ID);
        requestModel.setClient_id(PddAPIConfig.CLIENT_ID);
        long time = new Date().getTime() / 1000;
        requestModel.setTimestamp(time);
        requestModel.setSign(PddSignTool.generateSign(requestModel, PddAPIConfig.CLIENT_SECRET));
        String resp = PddHttpTool.sendJsonPost(requestModel);

        LogOrderMailDetailIoEntity logOrderMailDetailIoEntity = new LogOrderMailDetailIoEntity();
        logOrderMailDetailIoEntity.setMailNo(inBoundRequest.getMailNo());
        logOrderMailDetailIoEntity.setExpressCode(mailNoResponse.getExpressCode());
        logOrderMailDetailIoEntity.setStatus(StatusEnum.INBOUND.getCode());
        logOrderMailDetailIoEntity.setLogisticsOrderCode(mailNoResponse.getLogisticsOrderCode());
        logOrderMailDetailIoEntity.setExpressCode(mailNoResponse.getExpressCode());
        logOrderMailDetailIoEntity.setReq(JSON.toJSONString(requestModel));
        logOrderMailDetailIoEntity.setResp(resp);

        logOrderMailDetailIoImpl.save(logOrderMailDetailIoEntity);

        //更新入库包裹状态
        mailNoResponse.setStatus(logOrderMailDetailIoEntity.getStatus());
        mailNoResponse.setEnterTime(timeTool.getNowDate());
        logOrderMailDetailImpl.updateById(mailNoResponse);

        logOrderEntity.setNum(notifyRequest.getPackageQuantity().intValue());

        //判断是否全部入库
        if (notifyRequest.getPackageQuantity() <= num) {
            //全部入库
            logOrderEntity.setStatus(StatusEnum.FULLINBOUND.getCode());
        } else {
            logOrderEntity.setStatus(StatusEnum.PARTINBOUND.getCode());
        }

        logOrderImpl.updateById(logOrderEntity);

        return JsonResponse.generateLocalCommonSuccessResponse(true);
    }

    /**
     * 入库异常
     */
    @PostMapping("/inBoundError")
    public JSONObject inBoundError(HttpServletRequest request, @RequestBody InBoundRequest inBoundRequest) {

        LogOrderMailDetailEntity mailNoResponse = logOrderMailDetailImpl.getByMailNo(inBoundRequest.getMailNo());


        PddServiceConsoInboundRequest notifyRequest = new PddServiceConsoInboundRequest();

        LogOrderEntity logOrderEntity = logOrderImpl.getByOrderCode(mailNoResponse.getLogisticsOrderCode());

        Long num = (long) logOrderEntity.getNum();

        // 查询orderNO 下所有mailNo,判断是全部还是部分
//        Long unNum = logOrderMailDetailImpl.getSumByCodeStatus(mailNoResponse.getLogisticsOrderCode(), (long) StatusEnum.SIGNED.getCode());


        notifyRequest.setProviderCode(PddAPIConfig.PROVIDER_CODE);
        notifyRequest.setLogisticsOrderCode(mailNoResponse.getLogisticsOrderCode());
        notifyRequest.setExecuteTime(timeTool.getNowString());
        notifyRequest.setTimeZone("UTC+8");

        if (num >= logOrderEntity.getPackageQuantity().intValue()) {
            //部分入库
            notifyRequest.setStatus("PART_INBOUND");
            logOrderEntity.setStatus(StatusEnum.PARTINBOUND.getCode());
        } else {
            //全部入库
            notifyRequest.setStatus("FULL_INBOUND");
            logOrderEntity.setStatus(StatusEnum.FULLINBOUND.getCode());
        }

        notifyRequest.setPackageQuantity(num + 1);

        notifyRequest.setPackageInfo(new PddServiceConsoInboundRequest.PackageInfo());
        notifyRequest.getPackageInfo().setMailNo(mailNoResponse.getMailNo());
        notifyRequest.getPackageInfo().setExpressCode(mailNoResponse.getExpressCode());
        notifyRequest.getPackageInfo().setLength(inBoundRequest.getLength());
        notifyRequest.getPackageInfo().setWidth(inBoundRequest.getWidth());
        notifyRequest.getPackageInfo().setHeight(inBoundRequest.getHeight());
        notifyRequest.getPackageInfo().setWeight(inBoundRequest.getWeight());
        notifyRequest.getPackageInfo().setActualWeight((long) PddJYPriceTool.calculateWeight(inBoundRequest.getLength().intValue(), inBoundRequest.getWidth().intValue(), inBoundRequest.getHeight().intValue(), inBoundRequest.getWeight().intValue(), true));
        notifyRequest.getPackageInfo().setGoodsType(inBoundRequest.getGoodsType());

        notifyRequest.setResult(new PddServiceConsoInboundRequest.Result());
        notifyRequest.getResult().setCode(inBoundRequest.getCode());
        notifyRequest.getResult().setDesc(ErrorEnum.getByCode(inBoundRequest.getCode()).getDesc());
        notifyRequest.getResult().setRemark(ErrorEnum.getByCode(inBoundRequest.getCode()).getDesc());

        JYBaseRequestModel requestModel = new JYBaseRequestModel();
        requestModel.setRequest(JSON.toJSONString(notifyRequest));
        requestModel.setRequest_id("" + new Date().getTime());
        requestModel.setType("pdd.service.conso.inbound");
        requestModel.setTarget_client_id(PddAPIConfig.CLIENT_ID);
        requestModel.setClient_id(PddAPIConfig.CLIENT_ID);
        long time = new Date().getTime() / 1000;
        requestModel.setTimestamp(time);
        requestModel.setSign(PddSignTool.generateSign(requestModel, PddAPIConfig.CLIENT_SECRET));
//        String resp = PddHttpTool.postRequest(requestModel);
        String resp = PddHttpTool.sendJsonPost(requestModel);

        LogOrderMailDetailIoEntity logOrderMailDetailIoEntity = new LogOrderMailDetailIoEntity();

        logOrderMailDetailIoEntity.setMailNo(inBoundRequest.getMailNo());
        logOrderMailDetailIoEntity.setExpressCode(mailNoResponse.getExpressCode());
        logOrderMailDetailIoEntity.setLogisticsOrderCode(mailNoResponse.getLogisticsOrderCode());
        logOrderMailDetailIoEntity.setExpressCode(mailNoResponse.getExpressCode());
        logOrderMailDetailIoEntity.setStatus(StatusEnum.BOUNDERROR.getCode());
        logOrderMailDetailIoEntity.setReq(JSON.toJSONString(requestModel));
        logOrderMailDetailIoEntity.setResp(resp);

        logOrderMailDetailIoImpl.save(logOrderMailDetailIoEntity);

        mailNoResponse.setStatus(StatusEnum.BOUNDERROR.getCode());

        logOrderMailDetailImpl.updateById(mailNoResponse);

        return JsonResponse.generateLocalCommonSuccessResponse(true);
    }

    /**
     * 拣货异常
     */
    @PostMapping("/unpackError")
    public JSONObject beginPickError(HttpServletRequest request, @RequestBody FinishUnpackRequest finishUnpackRequest) {

        CollectOrderEntity collectOrderEntity = new CollectOrderEntity();

        PddServiceConsoCoBizActionNotifyRequest notifyRequest = new PddServiceConsoCoBizActionNotifyRequest();
        notifyRequest.setProviderCode(PddAPIConfig.PROVIDER_CODE);
        notifyRequest.setOrderCode(finishUnpackRequest.getOrderCode());
        notifyRequest.setBizAction(finishUnpackRequest.getAction());
        notifyRequest.setExecuteTime(timeTool.getNowString());
        notifyRequest.setTimeZone("UTC+8");

        LogOrderMailDetailIoEntity logOrderMailDetailIoEntity = new LogOrderMailDetailIoEntity();

        if (("begin_pick").equals(finishUnpackRequest.getAction())) {

            collectOrderEntity = collectOrderImpl.getAllInfoByOrderCode(finishUnpackRequest.getOrderCode());

            if (collectOrderEntity == null) {
                return JsonResponse.generateLocalCommonFailedResponse("-1", "信息获取失败，请检查参数");
            }

            logOrderMailDetailIoEntity.setStatus(StatusEnum.BEGINPICKERROR.getCode());

            notifyRequest.setBizActionDesc("开始拣货");


        } else if (("finish_pick").equals(finishUnpackRequest.getAction())) {

            collectOrderEntity = collectOrderImpl.getAllInfoByOrderCode(finishUnpackRequest.getOrderCode());

            if (collectOrderEntity == null) {
                return JsonResponse.generateLocalCommonFailedResponse("-1", "信息获取失败，请检查参数");
            }

            logOrderMailDetailIoEntity.setStatus(StatusEnum.FINISHPICKERROR.getCode());

            notifyRequest.setBizActionDesc("拣货完成");

        } else if (("finish_unpack").equals(finishUnpackRequest.getAction())) {

            PackageNoticeEntity packageNoticeEntity = packageNoticeImpl.getByOrderCode(finishUnpackRequest.getOrderCode());

            if (packageNoticeEntity == null) {
                return JsonResponse.generateLocalCommonFailedResponse("-1", "未查到拆包信息，请检查参数");
            }

            LogOrderEntity logOrderEntity = logOrderImpl.getByOrderCode(finishUnpackRequest.getOrderCode());

            if (logOrderEntity == null) {
                return JsonResponse.generateLocalCommonFailedResponse("-1", "未查到集运单信息，请检查参数");
            }


            notifyRequest.setBizActionDesc("拆包完成");

            logOrderMailDetailIoEntity.setStatus(StatusEnum.FINISHUNPACKERROR.getCode());

            notifyRequest.setPackageInfos(new ArrayList<>());

            for (FinishUnpackRequest.PackageInfo lists : finishUnpackRequest.getPackageInfo()) {
                PddServiceConsoCoBizActionNotifyRequest.PackageInfo packageInfo = new PddServiceConsoCoBizActionNotifyRequest.PackageInfo();
                packageInfo.setWeight(lists.getWeight());
                packageInfo.setWidth(lists.getWidth());
                packageInfo.setHeight(lists.getHeight());
                packageInfo.setLength(lists.getLength());
                packageInfo.setActualWeight((long) PddJYPriceTool.calculateWeight(lists.getLength().intValue(), lists.getWidth().intValue(), lists.getHeight().intValue(), lists.getWeight().intValue(), true));
                packageInfo.setConsoType(logOrderEntity.getConsoType());
                packageInfo.setGoodsType(finishUnpackRequest.getGoodsType());

                notifyRequest.getPackageInfos().add(packageInfo);

                LogOrderPackageEntity logOrderPackageEntity = new LogOrderPackageEntity();
                logOrderPackageEntity.setStatus("" + StatusEnum.FINISHUNPACK.getCode());
                logOrderPackageEntity.setLogisticsOrderCode(finishUnpackRequest.getOrderCode());
                logOrderPackageEntity.setPackageType(finishUnpackRequest.getGoodsType());
                logOrderPackageEntity.setLength(lists.getLength().intValue());
                logOrderPackageEntity.setWidth(lists.getWidth().intValue());
                logOrderPackageEntity.setHeight(lists.getHeight().intValue());
                logOrderPackageEntity.setWeight(lists.getWeight().intValue());

                logOrderPackageImpl.save(logOrderPackageEntity);


                logOrderEntity.setStatus(logOrderMailDetailIoEntity.getStatus());
                logOrderImpl.updateById(logOrderEntity);

                packageNoticeEntity.setStatus("" + logOrderMailDetailIoEntity.getStatus());
                packageNoticeImpl.updateById(packageNoticeEntity);
            }
        }
        notifyRequest.setResult(new PddServiceConsoCoBizActionNotifyRequest.Result());

        notifyRequest.getResult().setCode(finishUnpackRequest.getCode());
        notifyRequest.getResult().setDesc(ErrorEnum.getByCode(finishUnpackRequest.getCode()).getDesc());
        notifyRequest.getResult().setRemark(ErrorEnum.getByCode(finishUnpackRequest.getCode()).getDesc());


        JYBaseRequestModel requestModel = new JYBaseRequestModel();
        requestModel.setRequest(JSON.toJSONString(notifyRequest));
        requestModel.setRequest_id("" + new Date().getTime());
        requestModel.setType("pdd.service.conso.co.biz.action.notify");
        requestModel.setTarget_client_id(PddAPIConfig.CLIENT_ID);
        requestModel.setClient_id(PddAPIConfig.CLIENT_ID);
        long time = new Date().getTime() / 1000;
        requestModel.setTimestamp(time);
        requestModel.setSign(PddSignTool.generateSign(requestModel, PddAPIConfig.CLIENT_SECRET));
        String resp = PddHttpTool.sendJsonPost(requestModel);


        logOrderMailDetailIoEntity.setMailNo(finishUnpackRequest.getMailNo());
        logOrderMailDetailIoEntity.setLogisticsOrderCode(finishUnpackRequest.getOrderCode());

        logOrderMailDetailIoEntity.setReq(JSON.toJSONString(requestModel));
        logOrderMailDetailIoEntity.setResp(resp);


        logOrderMailDetailIoImpl.save(logOrderMailDetailIoEntity);

        if (("begin_pick").equals(finishUnpackRequest.getAction()) || ("finish_pick").equals(finishUnpackRequest.getAction())) {

            //更新collect_mail_detail
            List<CollectMailDetailEntity> collectMailDetailEntityList = collectMailDetailImpl.getListByOrderCode(finishUnpackRequest.getOrderCode());
            for (CollectMailDetailEntity lists : collectMailDetailEntityList) {
                lists.setStatus(logOrderMailDetailIoEntity.getStatus());
                collectMailDetailImpl.updateById(lists);
            }

            collectOrderEntity.setStatus(logOrderMailDetailIoEntity.getStatus());

            collectOrderImpl.updateById(collectOrderEntity);
        }

        return JsonResponse.generateLocalCommonSuccessResponse(true);
    }

    /*收到出库通知后 开始拣货 拣货完成 */
    @PostMapping("/finishUnpack")
    public JSONObject finishUnpack(HttpServletRequest request, @RequestBody FinishUnpackRequest finishUnpackRequest) {

        String action = finishUnpackRequest.getAction();

        PddServiceConsoCoBizActionNotifyRequest notifyRequest = new PddServiceConsoCoBizActionNotifyRequest();
        notifyRequest.setProviderCode(PddAPIConfig.PROVIDER_CODE);
        notifyRequest.setOrderCode(finishUnpackRequest.getOrderCode());
        notifyRequest.setBizAction(action);
        notifyRequest.setExecuteTime(timeTool.getNowString());
        notifyRequest.setTimeZone("UTC+8");

        LogOrderMailDetailIoEntity logOrderMailDetailIoEntity = new LogOrderMailDetailIoEntity();

        PddServiceConsoCoBizActionNotifyRequest.Result notifyResult = new PddServiceConsoCoBizActionNotifyRequest.Result();

        CollectOrderEntity collectOrderEntity = new CollectOrderEntity();

        if (action.equals("begin_pick")) {

            collectOrderEntity = collectOrderImpl.getAllInfoByOrderCode(finishUnpackRequest.getOrderCode());

            if (collectOrderEntity == null) {
                return JsonResponse.generateLocalCommonFailedResponse("-1", "信息获取失败，请检查参数");
            }

            logOrderMailDetailIoEntity.setStatus(StatusEnum.BEGINPICK.getCode());

            notifyRequest.setBizActionDesc("开始拣货");
            notifyResult.setCode(0);
        } else if (action.equals("finish_pick")) {

            collectOrderEntity = collectOrderImpl.getAllInfoByOrderCode(finishUnpackRequest.getOrderCode());

            if (collectOrderEntity == null) {
                return JsonResponse.generateLocalCommonFailedResponse("-1", "信息获取失败，请检查参数");
            }

            logOrderMailDetailIoEntity.setStatus(StatusEnum.FINISHPICK.getCode());

            notifyRequest.setBizActionDesc("拣货完成");
            notifyResult.setCode(0);
        } else if (action.equals("finish_unpack")) {

            PackageNoticeEntity packageNoticeEntity = packageNoticeImpl.getByOrderCode(finishUnpackRequest.getOrderCode());

            if (packageNoticeEntity == null) {
                return JsonResponse.generateLocalCommonFailedResponse("-1", "未查到拆包信息，请检查参数");
            }

            LogOrderEntity logOrderEntity = logOrderImpl.getByOrderCode(finishUnpackRequest.getOrderCode());

            if (logOrderEntity == null) {
                return JsonResponse.generateLocalCommonFailedResponse("-1", "未查到集运单信息，请检查参数");
            }


            logOrderMailDetailIoEntity.setStatus(StatusEnum.FINISHUNPACK.getCode());

            notifyRequest.setBizActionDesc("拆包完成");
            notifyRequest.setUnpackRemark("");
            notifyResult.setCode(0);
            notifyRequest.setPackageInfos(new ArrayList<>());

            List<LogOrderPackageEntity> list = new ArrayList<>();

            for (FinishUnpackRequest.PackageInfo lists : finishUnpackRequest.getPackageInfo()) {

                PddServiceConsoCoBizActionNotifyRequest.PackageInfo packageInfo = new PddServiceConsoCoBizActionNotifyRequest.PackageInfo();
                packageInfo.setWeight(lists.getWeight());
                packageInfo.setWidth(lists.getWidth());
                packageInfo.setHeight(lists.getHeight());
                packageInfo.setLength(lists.getLength());
                packageInfo.setActualWeight((long) PddJYPriceTool.calculateWeight(lists.getLength().intValue(), lists.getWidth().intValue(), lists.getHeight().intValue(), lists.getWeight().intValue(), true));
                packageInfo.setConsoType(logOrderEntity.getConsoType());
                packageInfo.setGoodsType(finishUnpackRequest.getGoodsType());

                notifyRequest.getPackageInfos().add(packageInfo);

                LogOrderPackageEntity logOrderPackageEntity = new LogOrderPackageEntity();
                logOrderPackageEntity.setStatus("" + logOrderMailDetailIoEntity.getStatus());
                logOrderPackageEntity.setLogisticsOrderCode(finishUnpackRequest.getOrderCode());
                logOrderPackageEntity.setPackageType(finishUnpackRequest.getGoodsType());
                logOrderPackageEntity.setLength(lists.getLength().intValue());
                logOrderPackageEntity.setWidth(lists.getWidth().intValue());
                logOrderPackageEntity.setHeight(lists.getHeight().intValue());
                logOrderPackageEntity.setWeight(lists.getWeight().intValue());

                logOrderPackageImpl.save(logOrderPackageEntity);

                logOrderEntity.setStatus(logOrderMailDetailIoEntity.getStatus());
                logOrderImpl.updateById(logOrderEntity);

                packageNoticeEntity.setStatus("" + logOrderMailDetailIoEntity.getStatus());
                packageNoticeImpl.updateById(packageNoticeEntity);

            }

        }
        notifyRequest.setResult(notifyResult);

        JYBaseRequestModel requestModel = new JYBaseRequestModel();
        requestModel.setRequest(JSON.toJSONString(notifyRequest));
        requestModel.setRequest_id("" + new Date().getTime());
        requestModel.setType("pdd.service.conso.co.biz.action.notify");
        requestModel.setTarget_client_id(PddAPIConfig.CLIENT_ID);
        requestModel.setClient_id(PddAPIConfig.CLIENT_ID);
        long time = new Date().getTime() / 1000;
        requestModel.setTimestamp(time);
        requestModel.setSign(PddSignTool.generateSign(requestModel, PddAPIConfig.CLIENT_SECRET));
        String resp = PddHttpTool.sendJsonPost(requestModel);


        logOrderMailDetailIoEntity.setMailNo(finishUnpackRequest.getMailNo());
        logOrderMailDetailIoEntity.setLogisticsOrderCode(finishUnpackRequest.getOrderCode());
        logOrderMailDetailIoEntity.setReq(JSON.toJSONString(requestModel));
        logOrderMailDetailIoEntity.setResp(resp);

        logOrderMailDetailIoImpl.save(logOrderMailDetailIoEntity);

        if (action.equals("begin_pick") || action.equals("finish_pick")) {

            //更新collect_mail_detail
            List<CollectMailDetailEntity> collectMailDetailEntityList = collectMailDetailImpl.getListByOrderCode(finishUnpackRequest.getOrderCode());
            for (CollectMailDetailEntity lists : collectMailDetailEntityList) {
                lists.setStatus(logOrderMailDetailIoEntity.getStatus());
                collectMailDetailImpl.updateById(lists);
            }

            collectOrderEntity.setStatus(logOrderMailDetailIoEntity.getStatus());

            collectOrderImpl.updateById(collectOrderEntity);
        }

        return JsonResponse.generateLocalCommonSuccessResponse(true);
    }


    /*通知拆包后拣货*/
    @PostMapping("/finishUnpackPackage")
    public JSONObject finishUnpackPackage(HttpServletRequest request, @RequestBody FinishUnpackRequest finishUnpackRequest) {
        LogOrderPackageEntity logOrderPackageEntity = logOrderPackageImpl.getInfoByCode(finishUnpackRequest.getLogisticsOrderCode());

        String action = finishUnpackRequest.getAction();

        LogOrderMailDetailIoEntity logOrderMailDetailIoEntity = new LogOrderMailDetailIoEntity();

        PddServiceConsoCoBizActionNotifyRequest.Result notifyResult = new PddServiceConsoCoBizActionNotifyRequest.Result();
        if (action.equals("begin_pick")) {
            logOrderMailDetailIoEntity.setStatus(StatusEnum.UNPACKBEGINPICK.getCode());

        } else if (action.equals("finish_pick")) {
            logOrderMailDetailIoEntity.setStatus(StatusEnum.UNPACKFINISHPICK.getCode());

        } else if (action.equals("finish_unpack")) {
            logOrderMailDetailIoEntity.setStatus(StatusEnum.FINISHUNPACK.getCode());

            PddServiceConsoCoBizActionNotifyRequest notifyRequest = new PddServiceConsoCoBizActionNotifyRequest();
            notifyRequest.setProviderCode(PddAPIConfig.PROVIDER_CODE);
            notifyRequest.setOrderCode(logOrderPackageEntity.getLogisticsOrderCode());
            notifyRequest.setBizAction(action);
            notifyRequest.setExecuteTime(timeTool.getNowString());
            notifyRequest.setTimeZone("UTC+8");

            notifyRequest.setBizActionDesc("拆包完成");
            notifyRequest.setUnpackRemark("");
            notifyResult.setCode(0);
            notifyRequest.setPackageInfos(new ArrayList<>());
            PddServiceConsoCoBizActionNotifyRequest.PackageInfo packageInfo = new PddServiceConsoCoBizActionNotifyRequest.PackageInfo();
            notifyRequest.getPackageInfos().add(packageInfo);
            packageInfo.setWeight((long) logOrderPackageEntity.getWeight());
            packageInfo.setWidth((long) logOrderPackageEntity.getWidth());
            packageInfo.setHeight((long) logOrderPackageEntity.getHeight());
            packageInfo.setLength((long) logOrderPackageEntity.getLength());
            packageInfo.setActualWeight((long) logOrderPackageEntity.getWeight());
            packageInfo.setConsoType("SEA");
            packageInfo.setGoodsType(logOrderPackageEntity.getPackageType());

            notifyRequest.setResult(notifyResult);

            JYBaseRequestModel requestModel = new JYBaseRequestModel();
            requestModel.setRequest(JSON.toJSONString(notifyRequest));
            requestModel.setRequest_id("" + new Date().getTime());
            requestModel.setType("pdd.service.conso.co.biz.action.notify");
            requestModel.setTarget_client_id(PddAPIConfig.CLIENT_ID);
            requestModel.setClient_id(PddAPIConfig.CLIENT_ID);
            long time = new Date().getTime() / 1000;
            requestModel.setTimestamp(time);
            requestModel.setSign(PddSignTool.generateSign(requestModel, PddAPIConfig.CLIENT_SECRET));
            String resp = PddHttpTool.sendJsonPost(requestModel);

            logOrderMailDetailIoEntity.setReq(JSON.toJSONString(requestModel));
            logOrderMailDetailIoEntity.setResp(resp);

        }

        logOrderMailDetailIoEntity.setMailNo(logOrderPackageEntity.getMailNo());
        logOrderMailDetailIoEntity.setExpressCode(logOrderPackageEntity.getExpressCode());
        logOrderMailDetailIoEntity.setLogisticsOrderCode(logOrderPackageEntity.getLogisticsOrderCode());
        logOrderMailDetailIoEntity.setExpressCode(logOrderPackageEntity.getExpressCode());


        logOrderMailDetailIoImpl.save(logOrderMailDetailIoEntity);

        logOrderPackageEntity.setStatus(logOrderMailDetailIoEntity.getStatus().toString());

        return JsonResponse.generateLocalCommonSuccessResponse(true);

    }

    /*拣货完成后出库*/
    @PostMapping("/outBound")
    public JSONObject outBound(HttpServletRequest request, @RequestBody OutBoundRequest outBoundRequest) {
        PddServiceConsoOutboundRequest notifyRequest = new PddServiceConsoOutboundRequest();

        List<OutBoundRequest.OutBoundList> outBoundList = outBoundRequest.getOutBound();

        // TODO 此处异常，需要处理
        //获取所有可出库订单
        List<CollectOutMailDetailResponse> list = collectOutMailDetailImpl.getListByOrderCode(outBoundRequest.getOrderCode());

        if (list != null && !list.isEmpty()) {
            // 拆单操作
            // 存储需要删除的元素
            List<CollectOutMailDetailResponse> toRemoveList = new ArrayList<>();
            List<OutBoundRequest.OutBoundList> toRemoveOutBound = new ArrayList<>();


            for (CollectOutMailDetailResponse lists : list) {
                for (OutBoundRequest.OutBoundList outBoundLists : outBoundList) {
                    if (lists.getMailNo().equals(outBoundLists.getMailNo())) {
                        toRemoveList.add(lists);
                        toRemoveOutBound.add(outBoundLists);
                    }
                }
            }

            list.removeAll(toRemoveList);
            outBoundList.removeAll(toRemoveOutBound);

            if (list.size() > 0 || outBoundList.size() > 0) {
                //出库信息与已存信息不匹配，无法出库
                return JsonResponse.generateLocalCommonFailedResponse("-1", "出库单数异常");
            }
        } else {
            // 非拆单出库

        }


        CollectOrderResponse collectOrderResponse = collectOrderImpl.getByOrderCode(outBoundRequest.getOrderCode());


        notifyRequest.setProviderCode(PddAPIConfig.PROVIDER_CODE);
        notifyRequest.setOrderCode(outBoundRequest.getOrderCode());
        notifyRequest.setSegmentCode(collectOrderResponse.getSegmentCode());
        notifyRequest.setExecuteTime(timeTool.getNowString());
        notifyRequest.setTimeZone("UTC+8");
        notifyRequest.setIsSplitOut("N");
        notifyRequest.setPackageInfos(new ArrayList<>());

        PddServiceConsoOutboundRequest.PackageInfo packageInfo = new PddServiceConsoOutboundRequest.PackageInfo();

        notifyRequest.getPackageInfos().add(packageInfo);
//        packageInfo.setOutboundMailNo(outBoundRequest.getMailNo());
//        packageInfo.setExpressCode(outBoundRequest.getExpressCode());
//        packageInfo.setPackageType(outBoundRequest.getPackageType());
//        packageInfo.setLength(outBoundRequest.getLength());
//        packageInfo.setWidth(outBoundRequest.getWidth());
//        packageInfo.setHeight(outBoundRequest.getHeight());
//        packageInfo.setWeight(outBoundRequest.getWeight());


        notifyRequest.setSenderDetail(new PddServiceConsoOutboundRequest.SenderDetail());

        CollectOrderSendEntity collectOrderSendEntity = collectOrderSendImpl.getInfo();

        notifyRequest.getSenderDetail().setName(collectOrderSendEntity.getName());
        notifyRequest.getSenderDetail().setTelePhone(collectOrderSendEntity.getTelePhone());
        notifyRequest.getSenderDetail().setCountry(collectOrderSendEntity.getCountry());
        notifyRequest.getSenderDetail().setProvince(collectOrderSendEntity.getProvince());
        notifyRequest.getSenderDetail().setCity(collectOrderSendEntity.getCity());
        notifyRequest.getSenderDetail().setDistrict(collectOrderSendEntity.getDistrict());
        notifyRequest.getSenderDetail().setDetailAddress(collectOrderSendEntity.getDetailAddress());


        CollectReceiverDetailEntity collectReceiverDetailEntity = collectReceiverDetailImpl.getInfoByOrderCode(outBoundRequest.getOrderCode());
        notifyRequest.setReceiverDetail(new PddServiceConsoOutboundRequest.ReceiverDetail());
        notifyRequest.getReceiverDetail().setName(collectReceiverDetailEntity.getName());
        notifyRequest.getReceiverDetail().setTelePhone(collectReceiverDetailEntity.getTelePhone());
        notifyRequest.getReceiverDetail().setCountry(collectReceiverDetailEntity.getCountry());
        notifyRequest.getReceiverDetail().setProvince(collectReceiverDetailEntity.getProvince());
        notifyRequest.getReceiverDetail().setCity(collectReceiverDetailEntity.getCity());
        notifyRequest.getReceiverDetail().setDistrict(collectReceiverDetailEntity.getDistrict());
        notifyRequest.getReceiverDetail().setDetailAddress(collectReceiverDetailEntity.getDetailAddress());

        notifyRequest.setResult(new PddServiceConsoOutboundRequest.Result());
        notifyRequest.getResult().setCode(0);


        log.info("derelictionRequest:{}", JSON.toJSONString(notifyRequest));
        JYBaseRequestModel requestModel = new JYBaseRequestModel();
        requestModel.setRequest(JSON.toJSONString(notifyRequest));
        requestModel.setRequest_id("" + new Date().getTime());
        requestModel.setType("pdd.service.conso.outbound");
        requestModel.setTarget_client_id(PddAPIConfig.CLIENT_ID);
        requestModel.setClient_id(PddAPIConfig.CLIENT_ID);
        long time = new Date().getTime() / 1000;
        requestModel.setTimestamp(time);
        requestModel.setSign(PddSignTool.generateSign(requestModel, PddAPIConfig.CLIENT_SECRET));
        String resp = PddHttpTool.sendJsonPost(requestModel);

        LogOrderMailDetailIoEntity logOrderMailDetailIoEntity = new LogOrderMailDetailIoEntity();
//        logOrderMailDetailIoEntity.setMailNo(outBoundRequest.getMailNo());
//        logOrderMailDetailIoEntity.setExpressCode(outBoundRequest.getExpressCode());
//        logOrderMailDetailIoEntity.setLogisticsOrderCode(outBoundRequest.getOrderCode());
//        logOrderMailDetailIoEntity.setExpressCode(outBoundRequest.getExpressCode());
        logOrderMailDetailIoEntity.setStatus(StatusEnum.FINISHOUTBOUND.getCode());
        logOrderMailDetailIoEntity.setReq(JSON.toJSONString(requestModel));
        logOrderMailDetailIoEntity.setResp(resp);

        logOrderMailDetailIoImpl.save(logOrderMailDetailIoEntity);

        //更新collect_mail_detail
        for (CollectOutMailDetailResponse lists : list) {
            Boolean result = collectMailDetailImpl.updateByMailNo(lists.getMailNo(), logOrderMailDetailIoEntity.getStatus());
        }
        return JsonResponse.generateLocalCommonSuccessResponse(true);
    }


    /*流水查询*/
    @PostMapping("/logOrderMailDetailIoSelect")
    public JSONObject logOrderMailDetailIoSelect(HttpServletRequest request, @RequestBody LogOrderMailDetailIoRequest param) {
        return JsonResponse.generateLocalCommonSuccessResponse(logOrderMailDetailIoImpl.getListByRequest(param));
    }

    private PddLogisticsCoTrackNotifyRequest.DataObject getDataObject(String code) {
        PddLogisticsCoTrackNotifyRequest.DataObject dataObjects = new PddLogisticsCoTrackNotifyRequest.DataObject();
        dataObjects.setTraces(new ArrayList<>());

        List<TraceObjectEntity> list = traceObjectImpl.selectByTrackingNumber(code);
        List<org.ta.pddserver.model.pddlg.common.TraceObject> traceObjects = new ArrayList<>();
        for (TraceObjectEntity traceObjectEntity : list) {
            dataObjects.getTraces().add(TraceObject.createFromTraceEntity(traceObjectEntity));
        }

        return dataObjects;
    }

}
