package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TEbsRecord;
import com.ruoyi.business.domain.TMiStock;
import com.ruoyi.business.domain.TTransferRecord;
import com.ruoyi.business.service.ITEbsRecordService;
import com.ruoyi.business.service.ITTransferRecordService;
import com.ruoyi.business.utils.EbsUtils;
import com.ruoyi.business.utils.MailUtils;
import com.ruoyi.business.utils.OrderCodeFactory;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.business.service.ITMiStockService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时检查物料是否过期
 *
 * @author liangzhou
 */
@Component("expiryCheck")
public class ExpiryCheckTask {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ITMiStockService tMiStockService;

    @Autowired
    private ITEbsRecordService tEbsRecordService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ITTransferRecordService tTransferRecordService;

    public void excute() throws Exception {
        // 查询过期物料
        List<TMiStock> expiredStocks = tMiStockService.selectTMiStockExpired();
        logger.info("查询到的库存: {}", JSON.toJSONString(expiredStocks));
        if (expiredStocks.size() < 1) {
            return;
        }
        // 获取token
        JSONObject token = JSON.parseObject(HttpUtils.httpPostJson(configService.selectConfigByKey("token_ip"), "", ""));
        for (TMiStock expiredStock : expiredStocks) {
            // 转移到不合格库
            String paramsWmsSubInvProc = JSON.toJSONString(createJsonWmsSubInvProc(expiredStock,
                    expiredStock.getWhseloc(),
                    "121-124"));
            String resultWmsSubInvProc = "";
            try {
                resultWmsSubInvProc = HttpUtils.httpPostJson(configService.selectConfigByKey("ebs_ip"), paramsWmsSubInvProc, token.get("access_token").toString());
            } catch (Exception e) {
                tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                        paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", expiredStock.getGoodsId(), expiredStock.getGoodsName(), expiredStock.getProduclotid(), expiredStock.getShelvesNum()));
                // 接口失败
                continue;
            }
            System.out.println(resultWmsSubInvProc);
            // 子库存转移结果
            if (!JSON.parseObject(resultWmsSubInvProc).getBoolean("success")) {
                tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                        paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", expiredStock.getGoodsId(), expiredStock.getGoodsName(), expiredStock.getProduclotid(), expiredStock.getShelvesNum()));
                continue;
            }
            if (JSON.parseObject(JSON.parseObject(resultWmsSubInvProc).getString("obj")).getIntValue("total") < 1) {
                tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                        paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", expiredStock.getGoodsId(), expiredStock.getGoodsName(), expiredStock.getProduclotid(), expiredStock.getShelvesNum()));
                continue;
            }
            tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                    paramsWmsSubInvProc, resultWmsSubInvProc, "0", "子库存转移", expiredStock.getGoodsId(), expiredStock.getGoodsName(), expiredStock.getProduclotid(), expiredStock.getShelvesNum()));
            expiredStock.setSts("1");
            expiredStock.setWhseloc("121-124");
            expiredStock.setMark("过期不合格");
            tMiStockService.updateTMiStock(expiredStock);
        }
        // 发送邮件
        StringBuilder content = new StringBuilder("<html><head></head><body>");
        content.append("<h3>以下物料已经过期</h3>");
        content.append("<table border=\"1\" style=\"width:1000px; height:100px;border:solid 1px #E8F2F9;font-size=14px;font-size:18px;\">");
        content.append("<tr style=\"align:center\"><th>序号</th><th>物料编码</th><th>物料描述</th><th>供应商</th><th>批次号</th><th>生产日期</th><th>有效日期</th><th>单位</th><th>数量</th></tr>");
        for (int i = 1; i <= expiredStocks.size(); i++) {
            content.append("<tr>" +
                    "<td><span>" + i + "</span></td>" +
                    "<td><span>" + expiredStocks.get(i - 1).getGoodsId() + "</span></td>" +
                    "<td><span>" + expiredStocks.get(i - 1).getGoodsName() + "</span></td>" +
                    "<td><span>" + expiredStocks.get(i - 1).getProviderId() + "</span></td>" +
                    "<td><span>" + expiredStocks.get(i - 1).getProduclotid() + "</span></td>" +
                    "<td><span>" + expiredStocks.get(i - 1).getProductionDate() + "</span></td>" +
                    "<td><span>" + expiredStocks.get(i - 1).getExpirationDate() + "</span></td>" +
                    "<td><span>" + expiredStocks.get(i - 1).getGoodsMeasureId() + "</span></td>" +
                    "<td><span>" + expiredStocks.get(i - 1).getShelvesNum() + "</span></td>" +
                    "</tr>");
        }
        content.append("</table>");
        content.append("</body></html>");
        // 获取所有邮件账户
        String[] mailAccounts = configService.selectConfigByKey("mail.2").split(";");
        // 依次发送邮件
        for (int i = 0; i < mailAccounts.length; i++) {
            for (int j = 0; j < 5; j++) {
                String result = MailUtils.sendMailExchange(mailAccounts[i].trim(), "物料过期", String.valueOf(content));
                if ("邮件发送成功".equals(result)) {
                    break;
                }
            }
        }
    }

    /**
     * 申报EKP
     */
    public void addEkpExpireItem() {
        // 有效期不足7天发送ekp申报
        List<TMiStock> ekpExpireItems = tMiStockService.selectStockEkpExpireItem(new TMiStock());
        logger.info("查询到的库存: {}", JSON.toJSONString(ekpExpireItems));
        if (ekpExpireItems.size() < 1) {
            return;
        }
        // 获取token
        JSONObject token;
        try {
            token = JSON.parseObject(HttpUtils.httpPostJson(configService.selectConfigByKey("token_ip"), "", ""));
        } catch (Exception e) {
            return;
        }
        // 申报ekp过期
        for (TMiStock ekpExpireItem : ekpExpireItems) {
            String paramsAddEkpExpireItem = JSON.toJSONString(createJsonAddEkpExpireItem(ekpExpireItem));
            String resultAddEkpExpireItem = "";
            try {
                resultAddEkpExpireItem = HttpUtils.httpPostJson(configService.selectConfigByKey("ekp_ip"), paramsAddEkpExpireItem, token.get("access_token").toString());
            } catch (Exception e) {
                tEbsRecordService.insertTEbsRecord(new TEbsRecord("addEkpExpireItem", 3, configService.selectConfigByKey("ekp_ip"),
                        paramsAddEkpExpireItem, resultAddEkpExpireItem, "-1", "申报EKP过期", ekpExpireItem.getGoodsId(), ekpExpireItem.getGoodsName(), ekpExpireItem.getProduclotid(), ekpExpireItem.getShelvesNum()));
                // 接口失败
                continue;
            }
            System.out.println(resultAddEkpExpireItem);
            if (!JSON.parseObject(resultAddEkpExpireItem).getBoolean("success")) {
                tEbsRecordService.insertTEbsRecord(new TEbsRecord("addEkpExpireItem", 3, configService.selectConfigByKey("ekp_ip"),
                        paramsAddEkpExpireItem, resultAddEkpExpireItem, "-1", "申报EKP过期", ekpExpireItem.getGoodsId(), ekpExpireItem.getGoodsName(), ekpExpireItem.getProduclotid(), ekpExpireItem.getShelvesNum()));
            }
            tEbsRecordService.insertTEbsRecord(new TEbsRecord("addEkpExpireItem", 3, configService.selectConfigByKey("ekp_ip"),
                    paramsAddEkpExpireItem, resultAddEkpExpireItem, "0", "申报EKP过期", ekpExpireItem.getGoodsId(), ekpExpireItem.getGoodsName(), ekpExpireItem.getProduclotid(), ekpExpireItem.getShelvesNum()));
        }

        // 发送邮件
        StringBuilder content = new StringBuilder("<html><head></head><body>");
        content.append("<h3>以下物料即将过期</h3>");
        content.append("<table border=\"1\" style=\"width:1000px; height:100px;border:solid 1px #E8F2F9;font-size=14px;font-size:18px;\">");
        content.append("<tr style=\"align:center\"><th>序号</th><th>物料编码</th><th>物料描述</th><th>供应商</th><th>批次号</th><th>生产日期</th><th>有效日期</th><th>单位</th><th>数量</th></tr>");
        for (int i = 1; i <= ekpExpireItems.size(); i++) {
            content.append("<tr>" +
                    "<td><span>" + i + "</span></td>" +
                    "<td><span>" + ekpExpireItems.get(i - 1).getGoodsId() + "</span></td>" +
                    "<td><span>" + ekpExpireItems.get(i - 1).getGoodsName() + "</span></td>" +
                    "<td><span>" + ekpExpireItems.get(i - 1).getProviderId() + "</span></td>" +
                    "<td><span>" + ekpExpireItems.get(i - 1).getProduclotid() + "</span></td>" +
                    "<td><span>" + ekpExpireItems.get(i - 1).getProductionDate() + "</span></td>" +
                    "<td><span>" + ekpExpireItems.get(i - 1).getExpirationDate() + "</span></td>" +
                    "<td><span>" + ekpExpireItems.get(i - 1).getGoodsMeasureId() + "</span></td>" +
                    "<td><span>" + ekpExpireItems.get(i - 1).getShelvesNum() + "</span></td>" +
                    "</tr>");
        }
        content.append("</table>");
        content.append("</body></html>");
        // 获取所有邮件账户
        String[] mailAccounts = configService.selectConfigByKey("mail.1").split(";");
        // 依次发送邮件
        for (int i = 0; i < mailAccounts.length; i++) {
            for (int j = 0; j < 5; j++) {
                String result = MailUtils.sendMailExchange(mailAccounts[i].trim(), "物料即将过期", String.valueOf(content));
                if ("邮件发送成功".equals(result)) {
                    break;
                }
            }
        }
    }

    /**
     * 申报EKP过后十五分钟，检查Ekp申报结果
     */
    public void checkEkpResult() throws Exception {
        // 查询EKP申报的结果
        TEbsRecord getEkpExpireItemForQuery = new TEbsRecord("addEkpExpireItem", 3, configService.selectConfigByKey("ekp_ip"), "", "", "0", "申报EKP过期", "", "", "" , null);
        List<TEbsRecord> getEkpExpireItemList = tEbsRecordService.selectTEbsRecordList(getEkpExpireItemForQuery);
        if (getEkpExpireItemList.size() < 1) {
            return;
        }
        // 获取token
        JSONObject token = JSON.parseObject(HttpUtils.httpPostJson(configService.selectConfigByKey("token_ip"), "", ""));
        // 账户别名发放结果查询
        for (TEbsRecord getEkpExpireItem : getEkpExpireItemList) {
            String fdId = JSON.parseObject(getEkpExpireItem.getResult()).getString("obj");
            String paramGetEkpExpireItem = JSON.toJSONString(createJsonGetEkpExpireItem(fdId, getEkpExpireItem.getGoodsId(), getEkpExpireItem.getLotId()));
            String resultGetEkpExpireItem = "";
            try {
                resultGetEkpExpireItem = HttpUtils.httpPostJson(configService.selectConfigByKey("ekp_ip"), paramGetEkpExpireItem, token.get("access_token").toString());
            } catch (Exception e) {
                // 接口调用失败
                return;
            }
            System.out.println(resultGetEkpExpireItem);
            JSONObject resultJsonGetEkpExpireItem = (JSONObject) JSON.parseObject(resultGetEkpExpireItem).get("obj");
            List<JSONObject> dataGetOthersInf = JSON.parseArray(resultJsonGetEkpExpireItem.getString("rows"), JSONObject.class);
            if (dataGetOthersInf.size() > 0) {
                // 查找当前库存
                TMiStock stockForQuery = new TMiStock();
                stockForQuery.setGoodsId(getEkpExpireItem.getGoodsId());
                stockForQuery.setProduclotid(getEkpExpireItem.getLotId());
                List<TMiStock> currentStockList = tMiStockService.selectTMiStockList(stockForQuery);
                if (dataGetOthersInf.get(0).getString("fdPanduanjieguo").contains("延期")) {
                    // 延期物料，进延期库
                    for (TMiStock currStock: currentStockList) {
                        TMiStock stockAfter = new TMiStock();
                        BeanUtils.copyProperties(currStock, stockAfter);
                        // 转移子库
                        String paramsWmsSubInvProc = JSON.toJSONString(createJsonWmsSubInvProc(currStock,
                                currStock.getWhseloc(),
                                "121-121"));
                        String resultWmsSubInvProc = "";
                        try {
                            resultWmsSubInvProc = HttpUtils.httpPostJson(configService.selectConfigByKey("ebs_ip"), paramsWmsSubInvProc, token.get("access_token").toString());
                        } catch (Exception e) {
                            tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                                    paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", currStock.getGoodsId(), currStock.getGoodsName(), currStock.getProduclotid(), currStock.getShelvesNum()));
                            // 接口失败
                            continue;
                        }
                        System.out.println(resultWmsSubInvProc);
                        // 子库存转移结果
                        if (!JSON.parseObject(resultWmsSubInvProc).getBoolean("success")) {
                            tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                                    paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", currStock.getGoodsId(), currStock.getGoodsName(), currStock.getProduclotid(), currStock.getShelvesNum()));
                            continue;
                        }
                        if (JSON.parseObject(JSON.parseObject(resultWmsSubInvProc).getString("obj")).getIntValue("total") < 1) {
                            tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                                    paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", currStock.getGoodsId(), currStock.getGoodsName(), currStock.getProduclotid(), currStock.getShelvesNum()));
                            continue;
                        }
                        tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                                paramsWmsSubInvProc, resultWmsSubInvProc, "0", "子库存转移", currStock.getGoodsId(), currStock.getGoodsName(), currStock.getProduclotid(), currStock.getShelvesNum()));
                        stockAfter.setSts("2");
                        stockAfter.setWhseloc("121-121");
                        stockAfter.setExpirationDate(new SimpleDateFormat("yyyy-MM-dd").parse(dataGetOthersInf.get(0).getString("fdYanqiriqi")));
                        if (tMiStockService.updateTMiStock(stockAfter) > 0) {
                            // 插入子库转移记录
                            TTransferRecord transferRecord = new TTransferRecord();
                            transferRecord.setTransferid(OrderCodeFactory.getOrderCode("YQ", ""));
                            transferRecord.setDelaydate(stockAfter.getProductionDate());
                            transferRecord.setGoodsid(stockAfter.getGoodsId());
                            transferRecord.setGoodsname(stockAfter.getGoodsName());
                            transferRecord.setProductlotid(stockAfter.getProduclotid());
                            transferRecord.setShelfnum(stockAfter.getShelvesNum().toString());
                            transferRecord.setLocation(stockAfter.getLocationId());
                            transferRecord.setCtl(stockAfter.getCtl());
                            transferRecord.setProductiondate(stockAfter.getProductionDate());
                            transferRecord.setExpirationdate(currStock.getExpirationDate());
                            transferRecord.setCreateTime(new Date());
                            transferRecord.setFromstatus(currStock.getSts());
                            transferRecord.setTostatus(stockAfter.getSts());
                            transferRecord.setUsername("EKP申报延期");
                            tTransferRecordService.insertTTransferRecord(transferRecord);
                        }
                    }
                } else if (dataGetOthersInf.get(0).getString("fdPanduanjieguo").contains("报废")) {
                    // 报废
                    for (TMiStock currStock: currentStockList) {
                        TMiStock stockAfter = new TMiStock();
                        BeanUtils.copyProperties(currStock, stockAfter);
                        // 转移子库
                        String paramsWmsSubInvProc = JSON.toJSONString(createJsonWmsSubInvProc(currStock,
                                currStock.getWhseloc(),
                                "121-127"));
                        String resultWmsSubInvProc = "";
                        try {
                            resultWmsSubInvProc = HttpUtils.httpPostJson(configService.selectConfigByKey("ebs_ip"), paramsWmsSubInvProc, token.get("access_token").toString());
                        } catch (Exception e) {
                            tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                                    paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", currStock.getGoodsId(), currStock.getGoodsName(), currStock.getProduclotid(), currStock.getShelvesNum()));
                            // 接口失败
                            continue;
                        }
                        System.out.println(resultWmsSubInvProc);
                        // 子库存转移结果
                        if (!JSON.parseObject(resultWmsSubInvProc).getBoolean("success")) {
                            tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                                    paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", currStock.getGoodsId(), currStock.getGoodsName(), currStock.getProduclotid(), currStock.getShelvesNum()));
                            continue;
                        }
                        if (JSON.parseObject(JSON.parseObject(resultWmsSubInvProc).getString("obj")).getIntValue("total") < 1) {
                            tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                                    paramsWmsSubInvProc, resultWmsSubInvProc, "-1", "子库存转移", currStock.getGoodsId(), currStock.getGoodsName(), currStock.getProduclotid(), currStock.getShelvesNum()));
                            continue;
                        }
                        tEbsRecordService.insertTEbsRecord(new TEbsRecord("wmsSubInvProc", 2, configService.selectConfigByKey("ebs_ip"),
                                paramsWmsSubInvProc, resultWmsSubInvProc, "0", "子库存转移", currStock.getGoodsId(), currStock.getGoodsName(), currStock.getProduclotid(), currStock.getShelvesNum()));
                        stockAfter.setSts("3");
                        stockAfter.setWhseloc("121-127");
                        if (tMiStockService.updateTMiStock(stockAfter) > 0) {
                            // 插入子库转移记录
                            TTransferRecord transferRecord = new TTransferRecord();
                            transferRecord.setTransferid(OrderCodeFactory.getOrderCode("BF", ""));
                            transferRecord.setGoodsid(stockAfter.getGoodsId());
                            transferRecord.setGoodsname(stockAfter.getGoodsName());
                            transferRecord.setProductlotid(stockAfter.getProduclotid());
                            transferRecord.setShelfnum(stockAfter.getShelvesNum().toString());
                            transferRecord.setLocation(stockAfter.getLocationId());
                            transferRecord.setCtl(stockAfter.getCtl());
                            transferRecord.setProductiondate(stockAfter.getProductionDate());
                            transferRecord.setExpirationdate(currStock.getExpirationDate());
                            transferRecord.setCreateTime(new Date());
                            transferRecord.setFromstatus(currStock.getSts());
                            transferRecord.setTostatus(stockAfter.getSts());
                            transferRecord.setUsername("EKP申报报废");
                            tTransferRecordService.insertTTransferRecord(transferRecord);
                        }
                    }
                }
                // 更新账户别名发放结果
                getEkpExpireItem.setStatus("1");
                getEkpExpireItem.setResult(resultGetEkpExpireItem);
                tEbsRecordService.updateTEbsRecord(getEkpExpireItem);
            }
        }
    }

    /**
     * 创建参数Json(申报EKP过期)
     *
     * @return
     */
    public JSONObject createJsonAddEkpExpireItem(TMiStock ekpExpireItem) {
        // 创建系统参数
        JSONObject systemParams = new JSONObject();
        systemParams.put("requestId", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
        systemParams.put("application", "WMS");
        systemParams.put("sourceSystem", "WMS");
        systemParams.put("targetSystem", "EKP");
        systemParams.put("serviceName", "addEkpExpireItem");
        systemParams.put("nonce", 1);
        systemParams.put("timestamp", 1);
        systemParams.put("serviceOperation", "S");
        systemParams.put("serviceVersion", "1.0");
        systemParams.put("token", "fd");

        // 创建查询参数
        JSONObject baseQueryParams = new JSONObject();

        JSONArray jsonArray = new JSONArray();
        JSONObject businessData = new JSONObject();
        businessData.put("fdTemplateId", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
        businessData.put("docSubject", "物料过期流程");
        businessData.put("docCreator", configService.selectConfigByKey("docCreator"));
        businessData.put("futureNodeId", configService.selectConfigByKey("futureNodeId"));
        businessData.put("effectiveDate", new SimpleDateFormat("yyyy-MM-dd").format(ekpExpireItem.getExpirationDate()));
        businessData.put("expireType", "1");
        businessData.put("changbie", configService.selectConfigByKey("organizationId"));

        JSONArray lines = new JSONArray();
        // 创建数据
        JSONObject line = new JSONObject();
        line.put("dayNo", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        line.put("subInventory", ekpExpireItem.getWhseloc());
        line.put("itemNo", ekpExpireItem.getGoodsId());
        line.put("itemDesc", ekpExpireItem.getGoodsName());
        line.put("lotNumber", ekpExpireItem.getProduclotid());
        line.put("quantity", ekpExpireItem.getShelvesNum());
        line.put("expirationDate", new SimpleDateFormat("yyyy-MM-dd").format(ekpExpireItem.getExpirationDate()));
        lines.add(line);

        businessData.put("lines", lines);
        jsonArray.add(businessData);

        JSONObject result = new JSONObject();
        result.put("systemParams", systemParams);
        result.put("baseQueryParams", baseQueryParams);
        result.put("businessData", jsonArray);

        return result;
    }

    /**
     * 创建参数Json（子库存转移）
     * @return
     */
    public JSONObject createJsonWmsSubInvProc(TMiStock wmsStock, String fromSubInventoryCode, String toSubInventoryCode) {
        // 创建系统参数
        JSONObject systemParams = new JSONObject();
        systemParams.put("requestId", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
        systemParams.put("application", "NT_WMS");
        systemParams.put("sourceSystem", "NT_WMS");
        systemParams.put("targetSystem", "EBS");
        systemParams.put("serviceName", "wmsSubInvProc");
        systemParams.put("nonce", 1);
        systemParams.put("timestamp", 1);
        systemParams.put("serviceOperation", "E");
        systemParams.put("serviceVersion", "1.0");
        systemParams.put("token", "fd");

        // 创建查询参数
        JSONObject baseQueryParams = new JSONObject();

        // 创建数据
        JSONObject businessData = new JSONObject();
        businessData.put("transTypeId", 2);
        businessData.put("organizationId", configService.selectConfigByKey("organizationId"));
        businessData.put("itemId", wmsStock.getGoodsTypeid());
        businessData.put("quantity", wmsStock.getShelvesNum());
        businessData.put("subInventory", fromSubInventoryCode);
        businessData.put("locatorId", EbsUtils.LOCATOR_AREA_MAP.get(fromSubInventoryCode));
        businessData.put("transDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        businessData.put("transUom", wmsStock.getGoodsMeasureId());
        businessData.put("transSubInventory", toSubInventoryCode);
        businessData.put("transLocatorId", EbsUtils.LOCATOR_AREA_MAP.get(toSubInventoryCode));
        businessData.put("transLotNumber", wmsStock.getProduclotid());
        businessData.put("sourceHeaderId", 1);
        businessData.put("sourceLineId", 1);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(businessData);

        JSONObject result = new JSONObject();
        result.put("systemParams", systemParams);
        result.put("baseQueryParams", baseQueryParams);
        result.put("businessData", jsonArray);

        return result;
    }

    /**
     * 创建参数Json（查询EKP）
     * @return
     */
    public JSONObject createJsonGetEkpExpireItem(String fdId, String itemNo, String lotNumber) {
        // 创建系统参数
        JSONObject systemParams = new JSONObject();
        systemParams.put("requestId", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
        systemParams.put("application", "WMS");
        systemParams.put("sourceSystem", "WMS");
        systemParams.put("targetSystem", "EKP");
        systemParams.put("serviceName", "getEkpExpireItem");
        systemParams.put("nonce", 1);
        systemParams.put("timestamp", 1);
        systemParams.put("serviceOperation", "S");
        systemParams.put("serviceVersion", "1.0");
        systemParams.put("token", "fd");

        // 创建查询参数
        JSONObject baseQueryParams = new JSONObject();

        // 创建数据
        JSONObject businessData = new JSONObject();
        businessData.put("fdId", fdId);
        businessData.put("itemNo", itemNo);
        businessData.put("lotNumber", lotNumber);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(businessData);

        JSONObject result = new JSONObject();
        result.put("systemParams", systemParams);
        result.put("baseQueryParams", baseQueryParams);
        result.put("businessData", jsonArray);

        return result;
    }
}
