package com.unis.crk.common.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.basic.entity.BasicEnum;
import com.unis.crk.basic.entity.BasicStorehouse;
import com.unis.crk.basic.entity.BasicWarehouse;
import com.unis.crk.basic.entity.SysParams;
import com.unis.crk.basic.service.IBasicEnumService;
import com.unis.crk.basic.service.IBasicStorehouseService;
import com.unis.crk.basic.service.IBasicWarehouseService;
import com.unis.crk.basic.service.ISysParamsService;
import com.unis.crk.business.kcgl.entity.*;
import com.unis.crk.business.kcgl.service.*;
import com.unis.crk.business.qcck.entity.DataCrkywCkcmDefault;
import com.unis.crk.business.qcck.entity.DataCrkywCkcmzDefault;
import com.unis.crk.business.qcck.entity.DataCrkywCkcpzDefault;
import com.unis.crk.business.qcck.entity.DataCrkywCkrmdjDefault;
import com.unis.crk.business.qcck.service.IDataCrkywCkcmDefaultService;
import com.unis.crk.business.qcck.service.IDataCrkywCkcmzDefaultService;
import com.unis.crk.business.qcck.service.IDataCrkywCkcpzDefaultService;
import com.unis.crk.business.qcck.service.IDataCrkywCkrmdjDefaultService;
import com.unis.crk.business.qcrk.entity.*;
import com.unis.crk.business.qcrk.service.*;
import com.unis.crk.common.entity.Bizflowtrail;
import com.unis.crk.common.entity.DeleteDataRecord;
import com.unis.crk.common.entity.UpdataLog;
import com.unis.crk.common.entity.vo.*;
import com.unis.crk.common.mapper.UpdataLogMapper;
import com.unis.crk.common.service.IBizflowtrailService;
import com.unis.crk.common.service.IDeleteDataRecordService;
import com.unis.crk.common.service.IUpdataLogService;
import com.unis.crk.cplbusiness.entity.*;
import com.unis.crk.cplbusiness.service.*;
import com.unis.crk.utils.HttpsClientRequestFactory;
import com.unis.crk.utils.ddmPostCall;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 上传数据日志 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-03-17
 */
@Service
public class UpdataLogServiceImpl extends ServiceImpl<UpdataLogMapper, UpdataLog> implements IUpdataLogService {

    @Resource
    private UpdataLogMapper updataLogMapper;

    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;
    //枚举
    @Autowired
    private IBasicEnumService basicEnumService;
    //系统参数
    @Autowired
    private ISysParamsService sysParamsService;
    @Autowired
    IBasicStorehouseService basicStorehouseService;
    @Autowired
    IBasicWarehouseService basicWarehouseService;
    //原粮入库
    @Autowired
    IBizflowtrailService bizflowtrailService;
    @Autowired
    IDataCrkywRmdjDefaultService dataCrkywRmdjDefaultService;
    @Autowired
    IDataCrkywQyxxDefaultService dataCrkywQyxxDefaultService;
    @Autowired
    IDataCrkywJyxxDefaultService dataCrkywJyxxDefaultService;

    @Autowired
    IDataCrkywJyxxFlt001Service dataCrkywJyxxFlt001Service;

    @Autowired
    IDataCrkywCmzDefaultService dataCrkywCmzDefaultService;
    @Autowired
    IDataCrkywCpzDefaultService dataCrkywCpzDefaultService;
    @Autowired
    IDataCrkywJsxxDefaultService dataCrkywJsxxDefaultService;
    @Autowired
    IDataCrkywRkcmDefaultService dataCrkywRkcmDefaultService;
    @Autowired
    IDataCrkywTzdxxDefaultService dataCrkywTzdxxDefaultService;
    @Autowired
    IDataCrkywTzdxxFlt001Service dataCrkywTzdxxFlt001Service;
    //原粮出库
    @Autowired
    IDataCrkywCkrmdjDefaultService dataCrkywCkrmdjDefaultService;
    @Autowired
    IDataCrkywCkcpzDefaultService dataCrkywCkcpzDefaultService;
    @Autowired
    IDataCrkywCkcmzDefaultService dataCrkywCkcmzDefaultService;
    @Autowired
    IDataCrkywCkcmDefaultService dataCrkywCkcmDefaultService;

    //原粮库存管理
    @Autowired
    IDataKcglFcbgzDefaultService dataKcglFcbgzDefaultService;
    @Autowired
    IDataKcglKcbdDefaultService dataKcglKcbdDefaultService;
    @Autowired
    IDataKcglKcswDefaultService dataKcglKcswDefaultService;
    @Autowired
    IDataKcglQckcDefaultService dataKcglQckcDefaultService;
    @Autowired
    IDataKcglXckcDefaultService dataKcglXckcDefaultService;

    //成品粮出库
    @Autowired
    IDataCrkywCplckDefaultService dataCrkywCplckDefaultService;
    //成品粮抽样质检主表
    @Autowired
    IDataCrkywCpljyxxDefaultService dataCrkywCpljyxxDefaultService;
    //成品粮抽样质检从表
    @Autowired
    IDataCrkywCpljyxxFlt001Service dataCrkywCpljyxxFlt001Service;
    //成品粮入库
    @Autowired
    IDataCrkywCplrkDefaultService dataCrkywCplrkDefaultService;
    @Autowired
    IDataCrkywCplrkFlt001Service dataCrkywCplrkFlt001Service;
    //成品粮分库保管账
    @Autowired
    IDataKcglCplfcbgzDefaultService dataKcglCplfcbgzDefaultService;
    //成品粮库存变动
    @Autowired
    IDataKcglCplkcbdDefaultService dataKcglCplkcbdDefaultService;
    //成品粮库存实物
    @Autowired
    IDataKcglCplkcswDefaultService dataKcglCplkcswDefaultService;
    //成品粮期初库存
    @Autowired
    IDataKcglCplqckcDefaultService dataKcglCplqckcDefaultService;
    //成品粮形成库存
    @Autowired
    IDataKcglCplxckcDefaultService dataKcglCplxckcDefaultService;

    //成品粮商品信息
    @Autowired
    IDataCrkywCplspDefaultService dataCrkywCplspDefaultService;
    //成品粮通知单主表
    @Autowired
    IDataCrkywCpltzdxxDefaultService dataCrkywCpltzdxxDefaultService;
    //成品粮通知单从表
    @Autowired
    IDataCrkywCpltzdxxFlt001Service dataCrkywCpltzdxxFlt001Service;

    @Override
    public boolean saveUploadLog(String unitId, String operatlog, String errorlog) {
        UpdataLog updataLog = new UpdataLog();
        updataLog.setUintId(unitId);
        updataLog.setOperator(unitId);
        updataLog.setUpdateTime(new Date());
        updataLog.setOperatlog(operatlog);
        updataLog.setErrorlog(errorlog);
        return super.save(updataLog);
    }

    /**
     * 按照条件查询上传数据日志信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(UpdataLogVo pageQuery){
        {
        //条形查询器
        QueryWrapper<UpdataLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getUnitId())){
        queryWrapper.like("UNIT_ID",pageQuery.getUnitId());
        }
        //分页相关
        Page<UpdataLog> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

        IPage<Map<String,Object>> page = updataLogMapper.selectMyMapsPage(p,queryWrapper);
        return page;
        }
    }

    @Override
    public JSONObject uploadData(String unitId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","300"); //失败
        SysParams sysParams = sysParamsService.getById("curUnit");
        if (StrUtil.isBlank(unitId)){
            if (sysParams == null ||StrUtil.isBlank(sysParams.getParamVal())) { //当前单位id
                jsonObject.put("message","未传入当前单位Id");
                saveUploadLog(unitId,"",jsonObject.getString("message"));
                return jsonObject;
            }
            unitId = sysParams.getParamVal();
        }
        //得到是否上传数据
        sysParams = sysParamsService.getById("isStopUploadData");
        if (sysParams == null ||StrUtil.equals("1", sysParams.getParamVal())) { //表示停止上传数据
            jsonObject.put("message","停止上传数据");
            saveUploadLog(unitId,"",jsonObject.getString("message"));
            return jsonObject;
        }
        sysParams = sysParamsService.getById("updata.url");
        if (sysParams == null || StrUtil.isBlank(sysParams.getParamVal())) { //上传数据地址
            jsonObject.put("message","未查询到上传数据地址");
            saveUploadLog(unitId,"",jsonObject.getString("message"));
            return jsonObject;
        }

        //将unitId放入json
        jsonObject.put("unitId",unitId);
        String limit = "limit 0,25";
        try {
            //解析JSON数据
            //{basicStorehouse:[{},{}],basicWarehouse[{},{}]}
            JSONObject data = new JSONObject();
            StringBuilder operatlog = new StringBuilder("1000");
            operatlog =  getUploadData(unitId, limit, data, operatlog);

            if (data.size() <=0){
                jsonObject.put("message","未查询到上传数据");
                saveUploadLog(unitId,"",jsonObject.getString("message"));
                return jsonObject;
            }
            //上传数据
            JSONObject postData = new JSONObject();
            postData.put("server_code", "1111");
    //        postData.put("untId", jsonObject.getString("unitId"));
            postData.put("data",data);


                RestTemplate client = new RestTemplate();
                JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();
                JSONObject retunrJson = json.getJSONObject("data");
                //更新上传时间
                updateUplpadTimeData(data,retunrJson.getString("updateFailData"));
            jsonObject.put("status","200");
            if (!retunrJson.getBoolean("success")){
                jsonObject.put("message",retunrJson.getString("message")+",此表数据未上传"+retunrJson.getString("updateFailData"));
            }else {
                jsonObject.put("message","上传数据成功,"+operatlog);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("message","上传数据失败："+e.getMessage());
        }

        return jsonObject;
    }

    /**
     * 得到上传数据
     * @param unitId
     * @param limit
     * @param data
     * @param operatlog
     * @return
     */
    private StringBuilder getUploadData(String unitId, String limit, JSONObject data, StringBuilder operatlog) {
        //仓房
        QueryWrapper<BasicStorehouse> basicStorehouseQueryWrapper = new QueryWrapper<>();
        basicStorehouseQueryWrapper.eq("org_id",unitId);
        basicStorehouseQueryWrapper.isNull("updatetime"); //上传时间为空
        basicStorehouseQueryWrapper.last(limit);
        List<BasicStorehouse> basicStorehouseList = basicStorehouseService.list(basicStorehouseQueryWrapper);
        if (basicStorehouseList.size() > 0){
            data.put("basicStorehouse",basicStorehouseList);
        }
        operatlog.append("basicStorehouse:").append(basicStorehouseList.size()).append("条;");
        //货位
        QueryWrapper<BasicWarehouse> basicWarehouseQueryWrapper = new QueryWrapper<>();
        basicWarehouseQueryWrapper.eq("org_id",unitId);
        basicWarehouseQueryWrapper.isNull("updatetime"); //上传时间为空
        basicWarehouseQueryWrapper.last(limit);
        List<BasicWarehouse> basicWarehouseList = basicWarehouseService.list(basicWarehouseQueryWrapper);
        if (basicStorehouseList.size() > 0){
            data.put("basicWarehouse",basicWarehouseList);
        }
        operatlog.append("basicWarehouse:").append(basicWarehouseList.size()).append("条;");
        //原粮轨迹表
        QueryWrapper<Bizflowtrail> bizflowtrailQueryWrapper = new QueryWrapper<>();
        bizflowtrailQueryWrapper.eq("UnitID",unitId);
        bizflowtrailQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        bizflowtrailQueryWrapper.last(limit);
        List<Bizflowtrail> bizflowtrailList = bizflowtrailService.list(bizflowtrailQueryWrapper);
        if (bizflowtrailList.size() > 0){
            data.put("bizflowtrail",bizflowtrailList);
        }
        operatlog.append("bizflowtrail:").append(bizflowtrailList.size()).append("条;");
        //原粮入库入门
        QueryWrapper<DataCrkywRmdjDefault> rkdjQueryWrapper = new QueryWrapper<>();
        rkdjQueryWrapper.eq("UnitID",unitId);
        rkdjQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        rkdjQueryWrapper.last(limit);
        List<DataCrkywRmdjDefault> rkdjList = dataCrkywRmdjDefaultService.list(rkdjQueryWrapper);
        if (rkdjList.size() > 0){
            data.put("dataCrkywRmdjDefault",rkdjList);
            operatlog.append("dataCrkywRmdjDefault:").append(rkdjList.size()).append("条;");
        }
        //原粮入库扦样
        QueryWrapper<DataCrkywQyxxDefault> qyxxQueryWrapper = new QueryWrapper<>();
        qyxxQueryWrapper.eq("UnitID",unitId);
        qyxxQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        qyxxQueryWrapper.last(limit);
        List<DataCrkywQyxxDefault> qyxxList = dataCrkywQyxxDefaultService.list(qyxxQueryWrapper);
        if (qyxxList.size() > 0){
            data.put("dataCrkywQyxxDefault",qyxxList);
        }
        operatlog.append("dataCrkywQyxxDefault:").append(qyxxList.size()).append("条;");
        //原粮入库化验
        QueryWrapper<DataCrkywJyxxDefault> jyxxQueryWrapper = new QueryWrapper<>();
        jyxxQueryWrapper.eq("UnitID",unitId);
        jyxxQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        jyxxQueryWrapper.last(limit);
        List<DataCrkywJyxxDefault> jyxxList = dataCrkywJyxxDefaultService.list(jyxxQueryWrapper);
        if (jyxxList.size() > 0){
            //得到从表信息，因为有级联
            for (int i = 0; i < jyxxList.size(); i++) {
                DataCrkywJyxxDefault dataCrkywJyxxDefault = jyxxList.get(i);
                QueryWrapper<DataCrkywJyxxFlt001> jyxxFlt001QueryWrapper = new QueryWrapper<>();
                jyxxQueryWrapper.eq("UnitID",unitId);
                jyxxQueryWrapper.eq("MainDataID",jyxxList.get(i).getDataID()); //主表主键Id
                jyxxQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
                //得到从表数据
                dataCrkywJyxxDefault.setFltList(dataCrkywJyxxFlt001Service.list(jyxxFlt001QueryWrapper));
            }
            data.put("dataCrkywJyxxDefault",jyxxList);
        }
        operatlog.append("dataCrkywJyxxDefault:").append(jyxxList.size()).append("条;");
        //原粮入库称毛重
        QueryWrapper<DataCrkywCmzDefault> cmzQueryWrapper = new QueryWrapper<>();
        cmzQueryWrapper.eq("UnitID",unitId);
        cmzQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        cmzQueryWrapper.last(limit);
        List<DataCrkywCmzDefault> cmzList = dataCrkywCmzDefaultService.list(cmzQueryWrapper);
        if (cmzList.size() > 0){
            data.put("dataCrkywCmzDefault",cmzList);
        }
        operatlog.append("dataCrkywCmzDefault:").append(cmzList.size()).append("条;");
        //原粮入库称皮重
        QueryWrapper<DataCrkywCpzDefault> cpzQueryWrapper = new QueryWrapper<>();
        cpzQueryWrapper.eq("UnitID",unitId);
        cpzQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        cpzQueryWrapper.last(limit);
        List<DataCrkywCpzDefault> cpzList = dataCrkywCpzDefaultService.list(cpzQueryWrapper);
        if (cpzList.size() > 0){
            data.put("dataCrkywCpzDefault",cpzList);
        }
        operatlog.append("dataCrkywCpzDefault:").append(cpzList.size()).append("条;");
        //原粮入库结算
        QueryWrapper<DataCrkywJsxxDefault> jsxxQueryWrapper = new QueryWrapper<>();
        jsxxQueryWrapper.eq("UnitID",unitId);
        jsxxQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        jsxxQueryWrapper.last(limit);
        List<DataCrkywJsxxDefault> jsxxList = dataCrkywJsxxDefaultService.list(jsxxQueryWrapper);
        if (jsxxList.size() > 0){
            data.put("dataCrkywJsxxDefault",jsxxList);
        }
        operatlog.append("dataCrkywJsxxDefault:").append(jsxxList.size()).append("条;");
        //原粮入库出门
        QueryWrapper<DataCrkywRkcmDefault> rkcmQueryWrapper = new QueryWrapper<>();
        rkcmQueryWrapper.eq("UnitID",unitId);
        rkcmQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        rkcmQueryWrapper.last(limit);
        List<DataCrkywRkcmDefault> rkcmList = dataCrkywRkcmDefaultService.list(rkcmQueryWrapper);
        if (rkcmList.size() > 0){
            data.put("dataCrkywRkcmDefault",rkcmList);
        }
        operatlog.append("dataCrkywRkcmDefault:").append(rkcmList.size()).append("条;");
        //原粮出库入门
        QueryWrapper<DataCrkywCkrmdjDefault> ckrmQueryWrapper = new QueryWrapper<>();
        ckrmQueryWrapper.eq("UnitID",unitId);
        ckrmQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        ckrmQueryWrapper.last(limit);
        List<DataCrkywCkrmdjDefault> ckrmdjList = dataCrkywCkrmdjDefaultService.list(ckrmQueryWrapper);
        if (ckrmdjList.size() > 0){
            data.put("dataCrkywCkrmdjDefault",ckrmdjList);
        }
        operatlog.append("dataCrkywCkrmdjDefault:").append(ckrmdjList.size()).append("条;");
        //原粮出库称皮重
        QueryWrapper<DataCrkywCkcpzDefault> ckCpzQueryWrapper = new QueryWrapper<>();
        ckCpzQueryWrapper.eq("UnitID",unitId);
        ckCpzQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        ckCpzQueryWrapper.last(limit);
        List<DataCrkywCkcpzDefault> ckCpzList = dataCrkywCkcpzDefaultService.list(ckCpzQueryWrapper);
        if (ckCpzList.size() > 0){
            data.put("dataCrkywCkcpzDefault",ckCpzList);
        }
        operatlog.append("dataCrkywCkcpzDefault:").append(ckCpzList.size()).append("条;");
        //原粮出库称毛重
        QueryWrapper<DataCrkywCkcmzDefault> ckCmzQueryWrapper = new QueryWrapper<>();
        ckCmzQueryWrapper.eq("UnitID",unitId);
        ckCmzQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        ckCmzQueryWrapper.last(limit);
        List<DataCrkywCkcmzDefault> ckCmzList = dataCrkywCkcmzDefaultService.list(ckCmzQueryWrapper);
        if (ckCmzList.size() > 0){
            data.put("dataCrkywCkcmzDefault",ckCmzList);
        }
        operatlog.append("dataCrkywCkcmzDefault:").append(ckCmzList.size()).append("条;");
        //原粮出库出门
        QueryWrapper<DataCrkywCkcmDefault> ckCmQueryWrapper = new QueryWrapper<>();
        ckCmQueryWrapper.eq("UnitID",unitId);
        ckCmQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        ckCmQueryWrapper.last(limit);
        List<DataCrkywCkcmDefault> ckCmList = dataCrkywCkcmDefaultService.list(ckCmQueryWrapper);
        if (ckCmList.size() > 0){
            data.put("dataCrkywCkcmDefault",ckCmList);
        }
        operatlog.append("dataCrkywCkcmDefault:").append(ckCmList.size()).append("条;");
        //通知单
        QueryWrapper<DataCrkywTzdxxDefault> tzdQueryWrapper = new QueryWrapper<>();
        tzdQueryWrapper.eq("UnitID",unitId);
        tzdQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        tzdQueryWrapper.last(limit);
        List<DataCrkywTzdxxDefault> tzdList = dataCrkywTzdxxDefaultService.list(tzdQueryWrapper);
        if (tzdList.size() > 0){
            //得到从表信息，因为有级联
            for (int i = 0; i < tzdList.size(); i++) {
                DataCrkywTzdxxDefault tzdxxDefault = tzdList.get(i);
                QueryWrapper<DataCrkywTzdxxFlt001> tzdFlt001QueryWrapper = new QueryWrapper<>();
                tzdQueryWrapper.eq("UnitID",unitId);
                tzdQueryWrapper.eq("MainDataID",tzdList.get(i).getDataID()); //主表主键Id
                tzdQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
                //得到从表数据
                tzdxxDefault.setFltList(dataCrkywTzdxxFlt001Service.list(tzdFlt001QueryWrapper));
            }
            data.put("dataCrkywTzdxxDefault",tzdList);
        }
        operatlog.append("dataCrkywTzdxxDefault:").append(tzdList.size()).append("条;");

        //库存管理-期初库存
        QueryWrapper<DataKcglQckcDefault> qckcQueryWrapper = new QueryWrapper<>();
        qckcQueryWrapper.eq("UnitID",unitId);
        qckcQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        qckcQueryWrapper.last(limit);
        List<DataKcglQckcDefault> qckcCmList = dataKcglQckcDefaultService.list(qckcQueryWrapper);
        if (qckcCmList.size() > 0){
            data.put("dataKcglQckcDefault",qckcCmList);
        }
        operatlog.append("dataKcglQckcDefault:").append(qckcCmList.size()).append("条;");
        //库存管理-库存变动
        QueryWrapper<DataKcglKcbdDefault> kcbdQueryWrapper = new QueryWrapper<>();
        kcbdQueryWrapper.eq("UnitID",unitId);
        kcbdQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        kcbdQueryWrapper.last(limit);
        List<DataKcglKcbdDefault> kcbdList = dataKcglKcbdDefaultService.list(kcbdQueryWrapper);
        if (kcbdList.size() > 0){
            data.put("dataKcglKcbdDefault",kcbdList);
        }
        operatlog.append("dataKcglKcbdDefault:").append(kcbdList.size()).append("条;");
        //库存管理-形成库存
        QueryWrapper<DataKcglXckcDefault> xckcQueryWrapper = new QueryWrapper<>();
        xckcQueryWrapper.eq("UnitID",unitId);
        xckcQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        xckcQueryWrapper.last(limit);
        List<DataKcglXckcDefault> xckcList = dataKcglXckcDefaultService.list(xckcQueryWrapper);
        if (xckcList.size() > 0){
            data.put("dataKcglXckcDefault",xckcList);
        }
        operatlog.append("dataKcglXckcDefault:").append(xckcList.size()).append("条;");

        //库存管理-分仓保管账
        QueryWrapper<DataKcglFcbgzDefault> fcbgzQueryWrapper = new QueryWrapper<>();
        fcbgzQueryWrapper.eq("UnitID",unitId);
        fcbgzQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        fcbgzQueryWrapper.last(limit);
        List<DataKcglFcbgzDefault> fcbgzList = dataKcglFcbgzDefaultService.list(fcbgzQueryWrapper);
        if (fcbgzList.size() > 0){
            data.put("dataKcglFcbgzDefault",fcbgzList);
        }
        operatlog.append("dataKcglFcbgzDefault:").append(fcbgzList.size()).append("条;");
        //库存管理-库存实物
        QueryWrapper<DataKcglKcswDefault> kcswQueryWrapper = new QueryWrapper<>();
        kcswQueryWrapper.eq("UnitID",unitId);
        kcswQueryWrapper.isNull("UPDATE_TIME"); //上传时间为空
        kcswQueryWrapper.last(limit);
        List<DataKcglKcswDefault> kcswList = dataKcglKcswDefaultService.list(kcswQueryWrapper);
        if (kcswList.size() > 0){
            data.put("dataKcglKcswDefault",kcswList);
        }
        operatlog.append("dataKcglKcswDefault:").append(kcswList.size()).append("条;");

        //成品粮出库
        QueryWrapper<DataCrkywCplckDefault> cplckQueryWrapper = new QueryWrapper<>();
        cplckQueryWrapper.eq("unitid",unitId);
        cplckQueryWrapper.isNull("updateTime"); //上传时间为空
        cplckQueryWrapper.last(limit);
        List<DataCrkywCplckDefault> cplckList = dataCrkywCplckDefaultService.list(cplckQueryWrapper);
        if (cplckList.size() > 0){
            data.put("dataCrkywCplckDefault",cplckList);
        }
        operatlog.append("dataCrkywCplckDefault:").append(cplckList.size()).append("条;");

        //成品粮抽样质检
        QueryWrapper<DataCrkywCpljyxxDefault> cplJyxxQueryWrapper = new QueryWrapper<>();
        cplJyxxQueryWrapper.eq("unitid",unitId);
        cplJyxxQueryWrapper.isNull("updateTime"); //上传时间为空
        cplJyxxQueryWrapper.last(limit);
        List<DataCrkywCpljyxxDefault> cplJyxxList = dataCrkywCpljyxxDefaultService.list(cplJyxxQueryWrapper);
        if (cplJyxxList.size() > 0){
            //得到从表信息，因为有级联
            for (int i = 0; i < cplJyxxList.size(); i++) {
                DataCrkywCpljyxxDefault crkywCpljyxxDefault = cplJyxxList.get(i);
                QueryWrapper<DataCrkywCpljyxxFlt001> cplJyxxFlt001QueryWrapper = new QueryWrapper<>();
                cplJyxxQueryWrapper.eq("unitid",unitId);
                cplJyxxQueryWrapper.eq("mainDataId",cplJyxxList.get(i).getDataId()); //主表主键Id
                cplJyxxQueryWrapper.isNull("updateTime"); //上传时间为空
                //得到从表数据
                crkywCpljyxxDefault.setFltList(dataCrkywCpljyxxFlt001Service.list(cplJyxxFlt001QueryWrapper));
            }
            data.put("dataCrkywCpljyxxDefault",cplJyxxList);
        }
        operatlog.append("dataCrkywCpljyxxDefault:").append(cplJyxxList.size()).append("条;");
        //成品粮入库
        QueryWrapper<DataCrkywCplrkDefault> cplrkQueryWrapper = new QueryWrapper<>();
        cplrkQueryWrapper.eq("unitid",unitId);
        cplrkQueryWrapper.isNull("updateTime"); //上传时间为空
        cplrkQueryWrapper.last(limit);
        List<DataCrkywCplrkDefault> cplrkList = dataCrkywCplrkDefaultService.list(cplrkQueryWrapper);
        if (cplrkList.size() > 0){
            data.put("dataCrkywCplrkDefault",cplrkList);
        }
        operatlog.append("dataCrkywCplrkDefault:").append(cplrkList.size()).append("条;");
        //成品粮分库保管账
        QueryWrapper<DataKcglCplfcbgzDefault> cplFcbgzQueryWrapper = new QueryWrapper<>();
        cplFcbgzQueryWrapper.eq("unitid",unitId);
        cplFcbgzQueryWrapper.isNull("updateTime"); //上传时间为空
        cplFcbgzQueryWrapper.last(limit);
        List<DataKcglCplfcbgzDefault> cplFcbgzList = dataKcglCplfcbgzDefaultService.list(cplFcbgzQueryWrapper);
        if (cplFcbgzList.size() > 0){
            data.put("dataKcglCplfcbgzDefault",cplFcbgzList);
        }
        operatlog.append("dataKcglCplfcbgzDefault:").append(cplFcbgzList.size()).append("条;");

        //成品粮库存变动
        QueryWrapper<DataKcglCplkcbdDefault> cplKcbdQueryWrapper = new QueryWrapper<>();
        cplKcbdQueryWrapper.eq("unitid",unitId);
        cplKcbdQueryWrapper.isNull("updateTime"); //上传时间为空
        cplKcbdQueryWrapper.last(limit);
        List<DataKcglCplkcbdDefault> cplKcbdList = dataKcglCplkcbdDefaultService.list(cplKcbdQueryWrapper);
        if (cplKcbdList.size() > 0){
            data.put("dataKcglCplkcbdDefault",cplKcbdList);
        }
        operatlog.append("dataKcglCplkcbdDefault:").append(cplKcbdList.size()).append("条;");

        //成品粮库存实物
        QueryWrapper<DataKcglCplkcswDefault> cplKcswQueryWrapper = new QueryWrapper<>();
        cplKcswQueryWrapper.eq("unitid",unitId);
        cplKcswQueryWrapper.isNull("updateTime"); //上传时间为空
        cplKcswQueryWrapper.last(limit);
        List<DataKcglCplkcswDefault> cplKcswList = dataKcglCplkcswDefaultService.list(cplKcswQueryWrapper);
        if (cplKcswList.size() > 0){
            data.put("dataKcglCplkcswDefault",cplKcswList);
        }
        operatlog.append("dataKcglCplkcswDefault:").append(cplKcswList.size()).append("条;");

        //成品粮期库存
        QueryWrapper<DataKcglCplqckcDefault> cplQckcQueryWrapper = new QueryWrapper<>();
        cplQckcQueryWrapper.eq("unitid",unitId);
        cplQckcQueryWrapper.isNull("updateTime"); //上传时间为空
        cplQckcQueryWrapper.last(limit);
        List<DataKcglCplqckcDefault> cplQckcList = dataKcglCplqckcDefaultService.list(cplQckcQueryWrapper);
        if (cplQckcList.size() > 0){
            data.put("dataKcglCplqckcDefault",cplQckcList);
        }
        operatlog.append("dataKcglCplqckcDefault:").append(cplQckcList.size()).append("条;");

        //成品粮形成库存
        QueryWrapper<DataKcglCplxckcDefault> cplXckcQueryWrapper = new QueryWrapper<>();
        cplXckcQueryWrapper.eq("unitid",unitId);
        cplXckcQueryWrapper.isNull("updateTime"); //上传时间为空
        cplXckcQueryWrapper.last(limit);
        List<DataKcglCplxckcDefault> cplXckcList = dataKcglCplxckcDefaultService.list(cplXckcQueryWrapper);
        if (cplXckcList.size() > 0){
            data.put("dataKcglCplxckcDefault",cplXckcList);
        }
        operatlog.append("dataKcglCplxckcDefault:").append(cplXckcList.size()).append("条;");

        //成品粮商品
        QueryWrapper<DataCrkywCplspDefault> cplSpQueryWrapper = new QueryWrapper<>();
        cplSpQueryWrapper.eq("unitid",unitId);
        cplSpQueryWrapper.isNull("updateTime"); //上传时间为空
        cplSpQueryWrapper.last(limit);
        List<DataCrkywCplspDefault> cplSpList = dataCrkywCplspDefaultService.list(cplSpQueryWrapper);
        if (cplSpList.size() > 0){
            data.put("dataCrkywCplspDefault",cplSpList);
        }
        operatlog.append("dataCrkywCplspDefault:").append(cplSpList.size()).append("条;");
        return operatlog;
    }

    /**
     * 修改上传时间
     * @param data
     * @param updateFailData
     */
    private void updateUplpadTimeData(JSONObject data,String updateFailData) {
        if(data.get("basicStorehouse")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("basicStorehouse")<0 ){ //上传成功数据
            //仓房
            List<BasicStorehouse>  basicStorehouseList =  data.getJSONArray("basicStorehouse").toJavaList(BasicStorehouse.class);
            for (int i = 0; i < basicStorehouseList.size(); i++) {
                BasicStorehouse entiy = basicStorehouseList.get(i);
                entiy.setUpdatetime(new Date());
            }
            basicStorehouseService.saveOrUpdateBatch(basicStorehouseList);
        }
        if(data.get("basicWarehouse")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("basicwarehouse")<0 ){ //上传成功数据
            //货位
            List<BasicWarehouse>  basicWarehouseList =  data.getJSONArray("basicWarehouse").toJavaList(BasicWarehouse.class);
            for (int i = 0; i < basicWarehouseList.size(); i++) {
                BasicWarehouse  entiy= basicWarehouseList.get(i);
                entiy.setUpdatetime(new Date());
            }
            basicWarehouseService.saveOrUpdateBatch(basicWarehouseList);
        }
        if(data.get("bizflowtrail")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("bizflowtrail")<0 ){ //上传成功数据
            //原粮轨迹表
            List<Bizflowtrail>  bizflowtrailList =  data.getJSONArray("bizflowtrail").toJavaList(Bizflowtrail.class);
            for (int i = 0; i < bizflowtrailList.size(); i++) {
                Bizflowtrail  entiy= bizflowtrailList.get(i);
                entiy.setUpdateTime(new Date());
            }
            bizflowtrailService.saveOrUpdateBatch(bizflowtrailList);
        }
        //原粮入库入门
        if(data.get("dataCrkywRmdjDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywRmdjDefault")<0 ){ //上传成功数据
            List<DataCrkywRmdjDefault>  rkdjDefaultList =  data.getJSONArray("dataCrkywRmdjDefault").toJavaList(DataCrkywRmdjDefault.class);
            for (int i = 0; i < rkdjDefaultList.size(); i++) {
                DataCrkywRmdjDefault  entiy= rkdjDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywRmdjDefaultService.saveOrUpdateBatch(rkdjDefaultList);
        }
        //原粮入库扦样
        if(data.get("dataCrkywQyxxDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywQyxxDefault")<0 ){ //上传成功数据
            List<DataCrkywQyxxDefault>  qyxxDefault =  data.getJSONArray("dataCrkywQyxxDefault").toJavaList(DataCrkywQyxxDefault.class);
            for (int i = 0; i < qyxxDefault.size(); i++) {
                DataCrkywQyxxDefault  entiy= qyxxDefault.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywQyxxDefaultService.saveOrUpdateBatch(qyxxDefault);
        }
        //原粮入库化验
        if(data.get("dataCrkywJyxxDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywJyxxDefault")<0 ){ //上传成功数据
            List<DataCrkywJyxxDefault>  jyxxDefaultList =  data.getJSONArray("dataCrkywJyxxDefault").toJavaList(DataCrkywJyxxDefault.class);
            for (int i = 0; i < jyxxDefaultList.size(); i++) {
                DataCrkywJyxxDefault  entiy= jyxxDefaultList.get(i);
                entiy.setUpdateTime(new Date());
                List<DataCrkywJyxxFlt001> dataCrkywJyxxFlt001s = entiy.getFltList();
                for (int j = 0; j <dataCrkywJyxxFlt001s.size(); j++) {
                    DataCrkywJyxxFlt001 jyxxFlt001 = dataCrkywJyxxFlt001s.get(i);
                    jyxxFlt001.setUpdateTime(new Date());
                }
                //保存主表
                dataCrkywJyxxDefaultService.saveOrUpdate(entiy);
                //保存从表
                dataCrkywJyxxFlt001Service.saveOrUpdateBatch(dataCrkywJyxxFlt001s);
            }
        }
        //原粮入库称毛重
        if(data.get("dataCrkywCmzDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCmzDefault")<0 ){ //上传成功数据
            List<DataCrkywCmzDefault>  cmzDefaultList =  data.getJSONArray("dataCrkywCmzDefault").toJavaList(DataCrkywCmzDefault.class);
            for (int i = 0; i < cmzDefaultList.size(); i++) {
                DataCrkywCmzDefault  entiy= cmzDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCmzDefaultService.saveOrUpdateBatch(cmzDefaultList);
        }
        //原粮入库称皮重
        if(data.get("dataCrkywCpzDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCpzDefault")<0 ){ //上传成功数据
            List<DataCrkywCpzDefault>  cpzDefaultList =  data.getJSONArray("dataCrkywCpzDefault").toJavaList(DataCrkywCpzDefault.class);
            for (int i = 0; i < cpzDefaultList.size(); i++) {
                DataCrkywCpzDefault  entiy= cpzDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCpzDefaultService.saveOrUpdateBatch(cpzDefaultList);
        }
        //原粮入库结算
        if(data.get("dataCrkywJsxxDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywJsxxDefault")<0 ){ //上传成功数据
            List<DataCrkywJsxxDefault>  jsxxDefaultList =  data.getJSONArray("dataCrkywJsxxDefault").toJavaList(DataCrkywJsxxDefault.class);
            for (int i = 0; i < jsxxDefaultList.size(); i++) {
                DataCrkywJsxxDefault  entiy= jsxxDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywJsxxDefaultService.saveOrUpdateBatch(jsxxDefaultList);
        }
        //原粮入库出门
        if(data.get("dataCrkywRkcmDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywRkcmDefault")<0 ){ //上传成功数据
            List<DataCrkywRkcmDefault>  rkcmDefaultList =  data.getJSONArray("dataCrkywRkcmDefault").toJavaList(DataCrkywRkcmDefault.class);
            for (int i = 0; i < rkcmDefaultList.size(); i++) {
                DataCrkywRkcmDefault  entiy= rkcmDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywRkcmDefaultService.saveOrUpdateBatch(rkcmDefaultList);
        }
        //原粮出库入门
        if(data.get("dataCrkywCkrmdjDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCkrmdjDefault")<0 ){ //上传成功数据
            List<DataCrkywCkrmdjDefault>  ckrmDefaultList =  data.getJSONArray("dataCrkywCkrmdjDefault").toJavaList(DataCrkywCkrmdjDefault.class);
            for (int i = 0; i < ckrmDefaultList.size(); i++) {
                DataCrkywCkrmdjDefault  entiy= ckrmDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCkrmdjDefaultService.saveOrUpdateBatch(ckrmDefaultList);
        }
        //原粮出库称皮重
        if(data.get("dataCrkywCkcpzDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCkcpzDefault")<0 ){ //上传成功数据
            List<DataCrkywCkcpzDefault>  ckcpzDefaultList =  data.getJSONArray("dataCrkywCkcpzDefault").toJavaList(DataCrkywCkcpzDefault.class);
            for (int i = 0; i < ckcpzDefaultList.size(); i++) {
                DataCrkywCkcpzDefault  entiy= ckcpzDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCkcpzDefaultService.saveOrUpdateBatch(ckcpzDefaultList);
        }
        //原粮出库称皮重
        if(data.get("dataCrkywCkcmzDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("datCrkywCkcmzDefault")<0 ){ //上传成功数据
            List<DataCrkywCkcmzDefault>  ckcmzDefaultList =  data.getJSONArray("dataCrkywCkcmzDefault").toJavaList(DataCrkywCkcmzDefault.class);
            for (int i = 0; i < ckcmzDefaultList.size(); i++) {
                DataCrkywCkcmzDefault  entiy= ckcmzDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCkcmzDefaultService.saveOrUpdateBatch(ckcmzDefaultList);
        }
        //原粮出库出门
        if(data.get("dataCrkywCkcmDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCkcmDefault")<0 ){ //上传成功数据
            List<DataCrkywCkcmDefault>  ckcmDefaultList =  data.getJSONArray("dataCrkywCkcmDefault").toJavaList(DataCrkywCkcmDefault.class);
            for (int i = 0; i < ckcmDefaultList.size(); i++) {
                DataCrkywCkcmDefault  entiy= ckcmDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCkcmDefaultService.saveOrUpdateBatch(ckcmDefaultList);
        }
        //通知单
        if(data.get("dataCrkywTzdxxDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywTzdxxDefault")<0 ){ //上传成功数据
            List<DataCrkywTzdxxDefault>  tzdDefaultList =  data.getJSONArray("dataCrkywTzdxxDefault").toJavaList(DataCrkywTzdxxDefault.class);
            for (int i = 0; i < tzdDefaultList.size(); i++) {
                DataCrkywTzdxxDefault  entiy= tzdDefaultList.get(i);
                entiy.setUpdateTime(new Date());
                List<DataCrkywTzdxxFlt001> dataCrkywTzdFlt001s = entiy.getFltList();
                for (int j = 0; j <dataCrkywTzdFlt001s.size(); j++) {
                    DataCrkywTzdxxFlt001 tzdFlt001 = dataCrkywTzdFlt001s.get(j);
                    tzdFlt001.setUpdateTime(new Date());
                }
                //保存主表
                dataCrkywTzdxxDefaultService.saveOrUpdate(entiy);
                //保存从表
                dataCrkywTzdxxFlt001Service.saveOrUpdateBatch(dataCrkywTzdFlt001s);
            }
        }

        //原粮库存管理-期初库存
        if(data.get("dataKcglQckcDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglQckcDefault")<0 ){ //上传成功数据
            List<DataKcglQckcDefault>  qckcDefaultList =  data.getJSONArray("dataKcglQckcDefault").toJavaList(DataKcglQckcDefault.class);
            for (int i = 0; i < qckcDefaultList.size(); i++) {
                DataKcglQckcDefault  entiy= qckcDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglQckcDefaultService.saveOrUpdateBatch(qckcDefaultList);
        }
        //原粮库存管理-库存变动
        if(data.get("dataKcglKcbdDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglKcbdDefault")<0 ){ //上传成功数据
            List<DataKcglKcbdDefault>  kcbdDefaultList =  data.getJSONArray("dataKcglKcbdDefault").toJavaList(DataKcglKcbdDefault.class);
            for (int i = 0; i < kcbdDefaultList.size(); i++) {
                DataKcglKcbdDefault  entiy= kcbdDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglKcbdDefaultService.saveOrUpdateBatch(kcbdDefaultList);
        }
        //原粮库存管理-形成库存
        if(data.get("dataKcglXckcDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglXckcDefault")<0 ){ //上传成功数据
            List<DataKcglXckcDefault>  xckcDefaultList =  data.getJSONArray("dataKcglXckcDefault").toJavaList(DataKcglXckcDefault.class);
            for (int i = 0; i < xckcDefaultList.size(); i++) {
                DataKcglXckcDefault  entiy= xckcDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglXckcDefaultService.saveOrUpdateBatch(xckcDefaultList);
        }
        //原粮库存管理-分库保管账
        if(data.get("dataKcglFcbgzDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglFcbgzDefault")<0 ){ //上传成功数据
            List<DataKcglFcbgzDefault>  fcbgzDefaultList =  data.getJSONArray("dataKcglFcbgzDefault").toJavaList(DataKcglFcbgzDefault.class);
            for (int i = 0; i < fcbgzDefaultList.size(); i++) {
                DataKcglFcbgzDefault  entiy= fcbgzDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglFcbgzDefaultService.saveOrUpdateBatch(fcbgzDefaultList);
        }
        //原粮库存管理-库存实物
        if(data.get("dataKcglKcswDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglKcswDefault")<0 ){ //上传成功数据
            List<DataKcglKcswDefault>  kcswDefaultList =  data.getJSONArray("dataKcglKcswDefault").toJavaList(DataKcglKcswDefault.class);
            for (int i = 0; i < kcswDefaultList.size(); i++) {
                DataKcglKcswDefault  entiy= kcswDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglKcswDefaultService.saveOrUpdateBatch(kcswDefaultList);
        }
        //成品粮入库
        if(data.get("dataCrkywCplckDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCplckDefault")<0 ){ //上传成功数据
            List<DataCrkywCplckDefault>  cpkckDefaultList =  data.getJSONArray("dataCrkywCplckDefault").toJavaList(DataCrkywCplckDefault.class);
            for (int i = 0; i < cpkckDefaultList.size(); i++) {
                DataCrkywCplckDefault  entiy= cpkckDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCplckDefaultService.saveOrUpdateBatch(cpkckDefaultList);
        }
        //成品粮抽样化验
        if(data.get("dataCrkywCpljyxxDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCpljyxxDefault")<0 ){ //上传成功数据
            List<DataCrkywCpljyxxDefault>  cplJyxxDefaultList =  data.getJSONArray("dataCrkywCpljyxxDefault").toJavaList(DataCrkywCpljyxxDefault.class);
            for (int i = 0; i < cplJyxxDefaultList.size(); i++) {
                DataCrkywCpljyxxDefault  entiy= cplJyxxDefaultList.get(i);
                entiy.setUpdateTime(new Date());
                List<DataCrkywCpljyxxFlt001> dataCrkywTzdFlt001s = entiy.getFltList();
                for (int j = 0; j <dataCrkywTzdFlt001s.size(); j++) {
                    DataCrkywCpljyxxFlt001 cplJyxxFlt001 = dataCrkywTzdFlt001s.get(j);
                    cplJyxxFlt001.setUpdateTime(new Date());
                }
                //保存主表
                dataCrkywCpljyxxDefaultService.saveOrUpdate(entiy);
                //保存从表
                dataCrkywCpljyxxFlt001Service.saveOrUpdateBatch(dataCrkywTzdFlt001s);
            }
        }
        //成品粮入库
        if(data.get("dataCrkywCplrkDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCplrkDefault")<0 ){ //上传成功数据
            List<DataCrkywCplrkDefault>  cplrkDefaultList =  data.getJSONArray("dataCrkywCplrkDefault").toJavaList(DataCrkywCplrkDefault.class);
            for (int i = 0; i < cplrkDefaultList.size(); i++) {
                DataCrkywCplrkDefault  entiy= cplrkDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCplrkDefaultService.saveOrUpdateBatch(cplrkDefaultList);
        }
        //成品粮分库保管账
        if(data.get("dataKcglCplfcbgzDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglCplfcbgzDefault")<0 ){ //上传成功数据
            List<DataKcglCplfcbgzDefault>  cplFcbgzDefaultList =  data.getJSONArray("dataKcglCplfcbgzDefault").toJavaList(DataKcglCplfcbgzDefault.class);
            for (int i = 0; i < cplFcbgzDefaultList.size(); i++) {
                DataKcglCplfcbgzDefault  entiy= cplFcbgzDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglCplfcbgzDefaultService.saveOrUpdateBatch(cplFcbgzDefaultList);
        }
        //成品粮库存变动
        if(data.get("dataKcglCplkcbdDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglCplkcbdDefault")<0 ){ //上传成功数据
            List<DataKcglCplkcbdDefault>  cplKcbdDefaultList =  data.getJSONArray("dataKcglCplkcbdDefault").toJavaList(DataKcglCplkcbdDefault.class);
            for (int i = 0; i < cplKcbdDefaultList.size(); i++) {
                DataKcglCplkcbdDefault  entiy= cplKcbdDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglCplkcbdDefaultService.saveOrUpdateBatch(cplKcbdDefaultList);
        }
        //成品粮库存实物
        if(data.get("dataKcglCplkcswDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglCplkcswDefault")<0 ){ //上传成功数据
            List<DataKcglCplkcswDefault>  cplkKcswDefaultList =  data.getJSONArray("dataKcglCplkcswDefault").toJavaList(DataKcglCplkcswDefault.class);
            for (int i = 0; i < cplkKcswDefaultList.size(); i++) {
                DataKcglCplkcswDefault  entiy= cplkKcswDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglCplkcswDefaultService.saveOrUpdateBatch(cplkKcswDefaultList);
        }
        //成品粮期初库存
        if(data.get("dataKcglCplqckcDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglCplqckcDefault")<0 ){ //上传成功数据
            List<DataKcglCplqckcDefault>  cplkQckcDefaultList =  data.getJSONArray("dataKcglCplqckcDefault").toJavaList(DataKcglCplqckcDefault.class);
            for (int i = 0; i < cplkQckcDefaultList.size(); i++) {
                DataKcglCplqckcDefault  entiy= cplkQckcDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglCplqckcDefaultService.saveOrUpdateBatch(cplkQckcDefaultList);
        }
        //成品粮形成库存
        if(data.get("dataKcglCplxckcDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataKcglCplxckcDefault")<0 ){ //上传成功数据
            List<DataKcglCplxckcDefault>  cplkXckcDefaultList =  data.getJSONArray("dataKcglCplxckcDefault").toJavaList(DataKcglCplxckcDefault.class);
            for (int i = 0; i < cplkXckcDefaultList.size(); i++) {
                DataKcglCplxckcDefault  entiy= cplkXckcDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataKcglCplxckcDefaultService.saveOrUpdateBatch(cplkXckcDefaultList);
        }
        //成品粮商品信息
        if(data.get("dataCrkywCplspDefault")!= null && StrUtil.isNotBlank(updateFailData) && updateFailData.indexOf("dataCrkywCplspDefault")<0 ){ //上传成功数据
            List<DataCrkywCplspDefault>  cplspDefaultList =  data.getJSONArray("dataCrkywCplspDefault").toJavaList(DataCrkywCplspDefault.class);
            for (int i = 0; i < cplspDefaultList.size(); i++) {
                DataCrkywCplspDefault  entiy= cplspDefaultList.get(i);
                entiy.setUpdateTime(new Date());
            }
            dataCrkywCplspDefaultService.saveOrUpdateBatch(cplspDefaultList);
        }
    }

    @Override
    public JSONObject recData(JSONObject jsonObject) {
        JSONObject returnJson = new JSONObject();
        returnJson.put("success",false);
        returnJson.put("message","接收数据失败");
        //成功上传数据表名
        StringBuilder updateFailData = new StringBuilder(1000);
        JSONObject data = jsonObject.getJSONObject("data");
        if (data.size() != 0){
            try {
                saveOrupdateUploadData(updateFailData, data);
            }catch (Exception e){
                e.printStackTrace();
                returnJson.put("message","接收数据失败:"+e.getMessage());
            }

        }
        if (updateFailData.length() <=0){
            returnJson.put("success",true);
            returnJson.put("updateFailData","-1");
            returnJson.put("message","接收数据成功");
        }else {
            returnJson.put("updateFailData",updateFailData.toString());
        }
        return returnJson;
    }


    private void saveOrupdateUploadData(StringBuilder updateFailData, JSONObject data) {
        //仓房
        if(data.get("basicStorehouse") != null){
            List<BasicStorehouse> basicStorehouseList =  data.getJSONArray("basicStorehouse").toJavaList(BasicStorehouse.class);
            if(!basicStorehouseService.saveOrUpdateBatch(basicStorehouseList)){ //保存失败
                updateFailData.append("basicStorehouse,");
            }
        }
        //货位
        if(data.get("basicWarehouse") != null) {
            List<BasicWarehouse> basicWarehouseList = data.getJSONArray("basicWarehouse").toJavaList(BasicWarehouse.class);
            if (!basicWarehouseService.saveOrUpdateBatch(basicWarehouseList)) { //保存失败
                updateFailData.append("basicWarehouse,");
            }
        }
        //流程轨迹表
        if(data.get("bizflowtrail") != null) {
            List<Bizflowtrail> bizflowtrailList = data.getJSONArray("bizflowtrail").toJavaList(Bizflowtrail.class);
            if (!bizflowtrailService.saveOrUpdateBatch(bizflowtrailList)) { //保存失败
                updateFailData.append("bizflowtrail,");
            }
        }
        //原粮入库入门
        if(data.get("dataCrkywRmdjDefault") != null) {
            List<DataCrkywRmdjDefault> rkdList = data.getJSONArray("dataCrkywRmdjDefault").toJavaList(DataCrkywRmdjDefault.class);
            if (!dataCrkywRmdjDefaultService.saveOrUpdateBatch(rkdList)) { //保存失败
                updateFailData.append("dataCrkywRmdjDefault,");
            }
        }
        //原粮入库扦样
        if(data.get("dataCrkywQyxxDefault") != null) {
            List<DataCrkywQyxxDefault> qyxxdList = data.getJSONArray("dataCrkywQyxxDefault").toJavaList(DataCrkywQyxxDefault.class);
            if (!dataCrkywQyxxDefaultService.saveOrUpdateBatch(qyxxdList)) { //保存失败
                updateFailData.append("dataCrkywQyxxDefault,");
            }
        }
        //原粮入库化验
        if(data.get("dataCrkywJyxxDefault") != null) {
            List<DataCrkywJyxxDefault> jyxxdList = data.getJSONArray("dataCrkywJyxxDefault").toJavaList(DataCrkywJyxxDefault.class);
            if (!dataCrkywJyxxDefaultService.saveOrUpdateBatch(jyxxdList)) { //保存失败
                updateFailData.append("dataCrkywJyxxDefault,");
            }else {
                List<DataCrkywJyxxFlt001> dataCrkywJyxxFlt001s = new ArrayList<>();
                for (int i = 0; i < jyxxdList.size(); i++) {
                    dataCrkywJyxxFlt001s.addAll(jyxxdList.get(i).getFltList());
                }
                if (dataCrkywJyxxFlt001s.size() > 0){
                    //保存从表
                    dataCrkywJyxxFlt001Service.saveOrUpdateBatch(dataCrkywJyxxFlt001s);
                }
            }
        }
        //原粮入库称毛重
        if(data.get("dataCrkywCmzDefault") != null) {
            List<DataCrkywCmzDefault> cmzList = data.getJSONArray("dataCrkywCmzDefault").toJavaList(DataCrkywCmzDefault.class);
            if (!dataCrkywCmzDefaultService.saveOrUpdateBatch(cmzList)) { //保存失败
                updateFailData.append("dataCrkywCmzDefault,");
            }
        }
        //原粮入库称皮重
        if(data.get("dataCrkywCpzDefault") != null) {
            List<DataCrkywCpzDefault> cpzList = data.getJSONArray("dataCrkywCpzDefault").toJavaList(DataCrkywCpzDefault.class);
            if (!dataCrkywCpzDefaultService.saveOrUpdateBatch(cpzList)) { //保存失败
                updateFailData.append("dataCrkywCpzDefault,");
            }
        }
        //原粮入库结算
        if(data.get("dataCrkywJsxxDefault") != null) {
            List<DataCrkywJsxxDefault> jsxxList = data.getJSONArray("dataCrkywJsxxDefault").toJavaList(DataCrkywJsxxDefault.class);
            if (!dataCrkywJsxxDefaultService.saveOrUpdateBatch(jsxxList)) { //保存失败
                updateFailData.append("dataCrkywJsxxDefault,");
            }
        }
        //原粮入库出门
        if(data.get("dataCrkywRkcmDefault") != null) {
            List<DataCrkywRkcmDefault> jsxxList = data.getJSONArray("dataCrkywRkcmDefault").toJavaList(DataCrkywRkcmDefault.class);
            if (!dataCrkywRkcmDefaultService.saveOrUpdateBatch(jsxxList)) { //保存失败
                updateFailData.append("dataCrkywRkcmDefault,");
            }
        }
        //原粮入库出门
        if(data.get("dataCrkywRkcmDefault") != null) {
            List<DataCrkywRkcmDefault> jsxxList = data.getJSONArray("dataCrkywRkcmDefault").toJavaList(DataCrkywRkcmDefault.class);
            if (!dataCrkywRkcmDefaultService.saveOrUpdateBatch(jsxxList)) { //保存失败
                updateFailData.append("dataCrkywRkcmDefault,");
            }
        }
        //原粮出库入门
        if(data.get("dataCrkywCkrmdjDefault") != null) {
            List<DataCrkywCkrmdjDefault> ckrmList = data.getJSONArray("dataCrkywCkrmdjDefault").toJavaList(DataCrkywCkrmdjDefault.class);
            if (!dataCrkywCkrmdjDefaultService.saveOrUpdateBatch(ckrmList)) { //保存失败
                updateFailData.append("dataCrkywCkrmdjDefault,");
            }
        }
        //原粮出库称皮重
        if(data.get("dataCrkywCkcpzDefault") != null) {
            List<DataCrkywCkcpzDefault> ckcpzList = data.getJSONArray("dataCrkywCkcpzDefault").toJavaList(DataCrkywCkcpzDefault.class);
            if (!dataCrkywCkcpzDefaultService.saveOrUpdateBatch(ckcpzList)) { //保存失败
                updateFailData.append("dataCrkywCkcpzDefault,");
            }
        }
        //原粮出库称皮重
        if(data.get("dataCrkywCkcmzDefault") != null) {
            List<DataCrkywCkcmzDefault> ckcmzList = data.getJSONArray("dataCrkywCkcmzDefault").toJavaList(DataCrkywCkcmzDefault.class);
            if (!dataCrkywCkcmzDefaultService.saveOrUpdateBatch(ckcmzList)) { //保存失败
                updateFailData.append("dataCrkywCkcmzDefault,");
            }
        }
        //原粮出库出门
        if(data.get("dataCrkywCkcmDefault") != null) {
            List<DataCrkywCkcmDefault> ckcmList = data.getJSONArray("dataCrkywCkcmDefault").toJavaList(DataCrkywCkcmDefault.class);
            if (!dataCrkywCkcmDefaultService.saveOrUpdateBatch(ckcmList)) { //保存失败
                updateFailData.append("dataCrkywCkcmDefault,");
            }
        }
        //通知单
        if(data.get("dataCrkywTzdxxDefault") != null) {
            List<DataCrkywTzdxxDefault> tzdList = data.getJSONArray("dataCrkywTzdxxDefault").toJavaList(DataCrkywTzdxxDefault.class);
            if (!dataCrkywTzdxxDefaultService.saveOrUpdateBatch(tzdList)) { //保存失败
                updateFailData.append("dataCrkywTzdxxDefault,");
            }else {
                List<DataCrkywTzdxxFlt001> dataCrkywTzdxxFlt001s = new ArrayList<>();
                for (int i = 0; i < tzdList.size(); i++) {
                    dataCrkywTzdxxFlt001s.addAll(tzdList.get(i).getFltList());
                }
                if (dataCrkywTzdxxFlt001s.size() > 0){
                    //保存从表
                    dataCrkywTzdxxFlt001Service.saveOrUpdateBatch(dataCrkywTzdxxFlt001s);
                }
            }
        }
        //库存管理-期初库存
        if(data.get("dataKcglQckcDefault") != null) {
            List<DataKcglQckcDefault> qckcList = data.getJSONArray("dataKcglQckcDefault").toJavaList(DataKcglQckcDefault.class);
            if (!dataKcglQckcDefaultService.saveOrUpdateBatch(qckcList)) { //保存失败
                updateFailData.append("dataKcglQckcDefault,");
            }
        }
        //库存管理-库存变动
        if(data.get("dataKcglKcbdDefault") != null) {
            List<DataKcglKcbdDefault> kcbdList = data.getJSONArray("dataKcglKcbdDefault").toJavaList(DataKcglKcbdDefault.class);
            if (!dataKcglKcbdDefaultService.saveOrUpdateBatch(kcbdList)) { //保存失败
                updateFailData.append("dataKcglKcbdDefault,");
            }
        }
        //库存管理-形成库存
        if(data.get("dataKcglXckcDefault") != null) {
            List<DataKcglXckcDefault> xckcList = data.getJSONArray("dataKcglXckcDefault").toJavaList(DataKcglXckcDefault.class);
            if (!dataKcglXckcDefaultService.saveOrUpdateBatch(xckcList)) { //保存失败
                updateFailData.append("dataKcglXckcDefault,");
            }
        }
        //库存管理-分库保管账
        if(data.get("dataKcglFcbgzDefault") != null) {
            List<DataKcglFcbgzDefault> fcbgzList = data.getJSONArray("dataKcglFcbgzDefault").toJavaList(DataKcglFcbgzDefault.class);
            if (!dataKcglFcbgzDefaultService.saveOrUpdateBatch(fcbgzList)) { //保存失败
                updateFailData.append("dataKcglFcbgzDefault,");
            }
        }
        //库存管理-库存实物
        if(data.get("dataKcglKcswDefault") != null) {
            List<DataKcglKcswDefault> fcbgzList = data.getJSONArray("dataKcglKcswDefault").toJavaList(DataKcglKcswDefault.class);
            if (!dataKcglKcswDefaultService.saveOrUpdateBatch(fcbgzList)) { //保存失败
                updateFailData.append("dataKcglKcswDefault,");
            }
        }
        //成品粮出库
        if(data.get("dataCrkywCplckDefault") != null) {
            List<DataCrkywCplckDefault> cplckList = data.getJSONArray("dataCrkywCplckDefault").toJavaList(DataCrkywCplckDefault.class);
            if (!dataCrkywCplckDefaultService.saveOrUpdateBatch(cplckList)) { //保存失败
                updateFailData.append("dataCrkywCplckDefault,");
            }
        }
        //成品粮检验信息
        if(data.get("dataCrkywCpljyxxDefault") != null) {
            List<DataCrkywCpljyxxDefault> cplJyxxList = data.getJSONArray("dataCrkywCpljyxxDefault").toJavaList(DataCrkywCpljyxxDefault.class);
            if (!dataCrkywCpljyxxDefaultService.saveOrUpdateBatch(cplJyxxList)) { //保存失败
                updateFailData.append("dataCrkywCpljyxxDefault,");
            }else {
                List<DataCrkywCpljyxxFlt001> crkywCpljyxxFlt001s = new ArrayList<>();
                for (int i = 0; i < cplJyxxList.size(); i++) {
                    crkywCpljyxxFlt001s.addAll(cplJyxxList.get(i).getFltList());
                }
                if (crkywCpljyxxFlt001s.size() > 0){
                    //保存从表
                    dataCrkywCpljyxxFlt001Service.saveOrUpdateBatch(crkywCpljyxxFlt001s);
                }
            }
        }
        //成品粮入库
        if(data.get("dataCrkywCplrkDefault") != null) {
            List<DataCrkywCplrkDefault> cplrkList = data.getJSONArray("dataCrkywCplrkDefault").toJavaList(DataCrkywCplrkDefault.class);
            if (!dataCrkywCplrkDefaultService.saveOrUpdateBatch(cplrkList)) { //保存失败
                updateFailData.append("dataCrkywCplrkDefault,");
            }
        }
        //成品粮分库保管账
        if(data.get("dataKcglCplfcbgzDefault") != null) {
            List<DataKcglCplfcbgzDefault> cplFcbgzList = data.getJSONArray("dataKcglCplfcbgzDefault").toJavaList(DataKcglCplfcbgzDefault.class);
            if (!dataKcglCplfcbgzDefaultService.saveOrUpdateBatch(cplFcbgzList)) { //保存失败
                updateFailData.append("dataKcglCplfcbgzDefault,");
            }
        }
        //成品粮库存变动
        if(data.get("dataKcglCplkcbdDefault") != null) {
            List<DataKcglCplkcbdDefault> cplKcbdList = data.getJSONArray("dataKcglCplkcbdDefault").toJavaList(DataKcglCplkcbdDefault.class);
            if (!dataKcglCplkcbdDefaultService.saveOrUpdateBatch(cplKcbdList)) { //保存失败
                updateFailData.append("dataKcglCplkcbdDefault,");
            }
        }
        //成品粮库存实物
        if(data.get("dataKcglCplkcswDefault") != null) {
            List<DataKcglCplkcswDefault> cplKcswList = data.getJSONArray("dataKcglCplkcswDefault").toJavaList(DataKcglCplkcswDefault.class);
            if (!dataKcglCplkcswDefaultService.saveOrUpdateBatch(cplKcswList)) { //保存失败
                updateFailData.append("dataKcglCplkcswDefault,");
            }
        }
        //成品粮期初库存
        if(data.get("dataKcglCplqckcDefault") != null) {
            List<DataKcglCplqckcDefault> cplQckcList = data.getJSONArray("dataKcglCplqckcDefault").toJavaList(DataKcglCplqckcDefault.class);
            if (!dataKcglCplqckcDefaultService.saveOrUpdateBatch(cplQckcList)) { //保存失败
                updateFailData.append("dataKcglCplqckcDefault,");
            }
        }
        //成品粮形成库存
        if(data.get("dataKcglCplxckcDefault") != null) {
            List<DataKcglCplxckcDefault> cplQckcList = data.getJSONArray("dataKcglCplxckcDefault").toJavaList(DataKcglCplxckcDefault.class);
            if (!dataKcglCplxckcDefaultService.saveOrUpdateBatch(cplQckcList)) { //保存失败
                updateFailData.append("dataKcglCplxckcDefault,");
            }
        }
        //成品粮商品
        if(data.get("dataCrkywCplspDefault") != null) {
            List<DataCrkywCplspDefault> cplSpList = data.getJSONArray("dataCrkywCplspDefault").toJavaList(DataCrkywCplspDefault.class);
            if (!dataCrkywCplspDefaultService.saveOrUpdateBatch(cplSpList)) { //保存失败
                updateFailData.append("dataCrkywCplspDefault,");
            }
        }

    }
    /**
     *
     * 从ddm跟新仓房
     * @return
     */

    @Override
    public JSONObject downChInfo() {
        String url =sysParamsService.getById("ddm.hwurl").getParamVal();
        JSONObject ddm = this.getDdm(url);
        String body = ddm.get("body").toString();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = (JSONObject) jsonObject.get("data");
        List<WareHouseDTOPageInfoResponse> dataList = data.getJSONArray("dataList").toJavaList(WareHouseDTOPageInfoResponse.class);
        Iterator<WareHouseDTOPageInfoResponse> it = dataList.iterator();
        while (it.hasNext()){
            WareHouseDTOPageInfoResponse next = it.next();
            BasicStorehouse basicStorehouse = new BasicStorehouse();
            basicStorehouse.setStorehouseId(next.getId());
            basicStorehouse.setDepotId(next.getDepotId());
            basicStorehouse.setDepotName(next.getDepotName());
            basicStorehouse.setOrgId(next.getOrgId().toString());
            basicStorehouse.setStorehouseCode(next.getStorehouseCode());
            basicStorehouse.setStorehouseName(next.getName());
            basicStorehouse.setStorehouseType(judge(next.getStorehouseType()));
            basicStorehouse.setStorehouseTypeMx(judge(next.getStorehouseTypeMx()));
            //basicStorehouse.setArchitectureType(next.get)
            //basicStorehouse.setStoreImg(next.getStoreImg());
            basicStorehouse.setGround(judge(next.getGround()));
            basicStorehouse.setWall(judge(next.getWall()));
            basicStorehouse.setRoof(judge(next.getRoof()));
            basicStorehouse.setHouse(judge(next.getHouse()));
            basicStorehouse.setDesignCapacity(next.getDesignCapacity());
            basicStorehouse.setKeepingWay(judge(next.getKeepingWay()));
            basicStorehouse.setGrainHeigth(next.getGrainHeigth());
            basicStorehouse.setStoreOutsideLength(next.getStoreOutsideLength());
            basicStorehouse.setStoreOutsideHeigth(next.getStoreOutsideHeigth());
            basicStorehouse.setStoreOutsideWidth(next.getStoreOutsideWidth());
            basicStorehouse.setStoreOutsideTopHeigth(next.getStoreOutsideTopHeigth());
            basicStorehouse.setOutsideSiloDiameter(next.getOutsideSiloDiameter());
            basicStorehouse.setOuterWallArea(next.getOuterWallArea());
            basicStorehouse.setDeductVolume(next.getDeductVolume());
            basicStorehouse.setLength(next.getLength());
            basicStorehouse.setHeigth(next.getHeigth());
            basicStorehouse.setWidth(next.getWidth());
            basicStorehouse.setGrainHeigth(next.getGrainHeigth());
            basicStorehouse.setSiloDiameter(next.getSiloDiameter());
            basicStorehouse.setBinVolume(next.getBinVolume());
            basicStorehouse.setGrainPileVolume(next.getGrainPileVolume());
            basicStorehouse.setHouseDoorNum(next.getHouseDoorNum());
            basicStorehouse.setHouseDootPosition(next.getHouseDootPosition());
            basicStorehouse.setHouseDoorHeigth(next.getHouseDoorHeigth());
            basicStorehouse.setHouseDoorWidth(next.getHouseDoorWidth());
            basicStorehouse.setDraftType(judge(next.getDraftType()));
            basicStorehouse.setHeatinsulationstepRoof(next.getHeatinsulationstepRoof());
            basicStorehouse.setHeatinsulationstepWindow(next.getHeatinsulationstepWindow());
            basicStorehouse.setGrbwcs(next.getGrbwcs());
            basicStorehouse.setNfszcc(StrUtil.isNotBlank(next.getNfszcc())?Integer.parseInt(judge(next.getNfszcc())):null);
            basicStorehouse.setYwfsfqfczz(StrUtil.isNotBlank(next.getYwfsfqfczz())?Integer.parseInt(judge(next.getYwfsfqfczz())):null);
            basicStorehouse.setYwfhfbfdss(StrUtil.isNotBlank(next.getYwfhfbfdss())?Integer.parseInt(judge(next.getYwfhfbfdss())):null);
            basicStorehouse.setYwlqjcss(StrUtil.isNotBlank(next.getYwlqjcss())?Integer.parseInt(judge(next.getYwlqjcss())):null);
            basicStorehouse.setYwjxtfss(StrUtil.isNotBlank(next.getYwjxtfss())?Integer.parseInt(judge(next.getYwjxtfss())):null);
            basicStorehouse.setNfhlxzsc(StrUtil.isNotBlank(next.getNfhlxzsc())?Integer.parseInt(judge(next.getNfhlxzsc())):null);
            basicStorehouse.setNffddyqtcl(StrUtil.isNotBlank(next.getNfhlxzsc())?Integer.parseInt(judge(next.getNfhlxzsc())):null);
            basicStorehouse.setNfcwcc(StrUtil.isNotBlank(next.getNfcwcc())?Integer.parseInt(judge(next.getNfcwcc())):null);
           // basicStorehouse.setActualCapacity(Integer.parseInt(next.get))实际吨容
            // getUseDate.setCompletionDate(next.getC)建成日期
            Date useDate = DateUtil.parse(StrUtil.isNotBlank(next.getUseDate())?next.getUseDate():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
            basicStorehouse.setUseDate(useDate);
            basicStorehouse.setStorehouseState(judge(next.getCurrentType()));
            basicStorehouse.setStoreWay(judge(next.getStoreWay()));
            basicStorehouse.setCurrentType(judge(next.getCurrentType()));
           // basicStorehouse.setDutyStoreman(next.getDutyStoreman().toString());
            basicStorehouse.setRemark(next.getRemark());
            basicStorehouse.setCreateName(next.getCreateUserId().toString());
            Date createdTime = DateUtil.parse(StrUtil.isNotBlank(next.getCreatedTime())?next.getCreatedTime():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
            basicStorehouse.setCreateDate(createdTime);
            Date modifiedTime = DateUtil.parse(StrUtil.isNotBlank(next.getModifiedTime())?next.getModifiedTime():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
            basicStorehouse.setUpdatetime(modifiedTime);
            basicStorehouse.setUpdateName(next.getModifyUserId().toString());
           // basicStorehouse.setWorkingStatus(next.getW)工作状态
            basicStorehouse.setDelFlag(StrUtil.isNotBlank(next.getDelFlag())?Integer.parseInt(judge(next.getDelFlag())):null);
            basicStorehouse.setLibraryType(judge(next.getLibraryType()));
            //basicStorehouse.setHwbm(next.getHW);货位编码
            basicStorehouseService.saveOrUpdate(basicStorehouse);
        }
        return null;
    }
    /**
     *
     * 从ddm跟新货位
     * @return
     */
    @Override
    public JSONObject downHwInfo() {
        String url =sysParamsService.getById("ddm.hwurl").getParamVal();
        JSONObject ddm = this.getDdm(url);
        String body = ddm.get("body").toString();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = (JSONObject) jsonObject.get("data");
        List<GoodsAllocationDTOPageInfoResponse> dataList = data.getJSONArray("dataList").toJavaList(GoodsAllocationDTOPageInfoResponse.class);
            Iterator<GoodsAllocationDTOPageInfoResponse> it = dataList.iterator();
            while (it.hasNext()){
                GoodsAllocationDTOPageInfoResponse next = it.next();
                BasicWarehouse basicStorehouse = new BasicWarehouse();
                Date createdTime = DateUtil.parse(StrUtil.isNotBlank(next.getCreatedTime())?next.getCreatedTime():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
                Date modifiedTime = DateUtil.parse(StrUtil.isNotBlank(next.getModifiedTime())?next.getModifiedTime():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
                Date useTime = null;
                if(StrUtil.isNotBlank(next.getWarehouseUseTime())){
                    useTime = DateUtil.parse(StrUtil.isNotBlank(next.getWarehouseUseTime())?next.getWarehouseUseTime():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
                }else{
                    useTime=new Date();
                }
                basicStorehouse.setDepotId(next.getDepotId());//部门ID
                basicStorehouse.setStorehouseId(next.getStorehouseId().toString());//仓房ID
                basicStorehouse.setCreateDate(createdTime);//创建时间
                basicStorehouse.setWarehouseCode(next.getWarehouseCode());//货位编码
                basicStorehouse.setWarehouseName(next.getName());//货位名称
                basicStorehouse.setDutyStoreman(next.getRemark());//备注
                basicStorehouse.setDepotName(next.getDepotName());
                basicStorehouse.setCrkStatus(judge(next.getCrkStatus()));
                basicStorehouse.setUpdatetime(modifiedTime);
                basicStorehouse.setContactNumber(next.getContactNumber()!=null?judge(next.getContactNumber().toString()):null);
                basicStorehouse.setDutyStoreman(next.getResponsibleUserId().toString());
                basicStorehouse.setWarehouseUseTime(useTime);
                basicStorehouse.setStoreWay(judge(next.getStoreWay()));
                basicStorehouse.setWidth(next.getWidth());
                basicStorehouse.setHeigth(next.getHeigth());
                basicStorehouse.setHwbm(next.getHwbm());
                basicStorehouse.setDelFlag(StrUtil.isNotBlank(next.getDelFlag())?Integer.parseInt(judge(next.getDelFlag())):null);
                basicStorehouse.setLibraryType(next.getLibraryType());
                basicStorehouse.setCreateName(next.getCreateUserId().toString());
                basicStorehouse.setOrgId(next.getOrgId().toString());
                basicStorehouse.setUpdateName(next.getModifyUserId().toString());
                basicStorehouse.setWarehouseCapacity(next.getWarehouseCapacity());
                basicStorehouse.setWarehouseStatus(next.getWarehouseStatus());
                basicStorehouse.setLength(next.getLength());
                basicStorehouse.setWarehouseId(next.getId());
            }

        return null;
    }

    /**
     * 从ddm调拔
     * @return
     */
    @Override
    public JSONObject downDbDInfo() {
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status","300"); //失败
            SysParams sysParams  = sysParamsService.getById("dbd.url");
            try {
                //上传数据
                JSONObject postData = new JSONObject();
                postData.put("pageNum", "1");
                postData.put("pageSize", "2000");
                postData.put("name", null); //调拔单编号
                postData.put("startDate", null);
                postData.put("endDate", null);


                RestTemplate client = new RestTemplate(new HttpsClientRequestFactory());
                JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();

                if (json.getString("code").equals("200")){
                    if (savetTzd(jsonObject, json)) return jsonObject;
                }
            }catch (Exception e){
                e.printStackTrace();
                jsonObject.put("message","更新调拔单数据失败："+e.getMessage());
                return jsonObject;
            }
            jsonObject.put("status","200"); //成功
            jsonObject.put("message","更新调拔单数据成功");
            return jsonObject;
        }
    }

    @Override
    public JSONObject downCgdInfo() {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status","300"); //失败
            SysParams sysParams  = sysParamsService.getById("cgd.url");
            try {
                //上传数据
                JSONObject postData = new JSONObject();
                postData.put("pageNum", "1");
                postData.put("pageSize", "2000");
                postData.put("startDate", null);
                postData.put("name", null); //采购单编号
                postData.put("endDate", null);


                RestTemplate client = new RestTemplate(new HttpsClientRequestFactory());
                JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();

                if (json.getString("code").equals("200")) {
                    if (savetTzd(jsonObject, json)) return jsonObject;
                }
            }catch (Exception e){
                e.printStackTrace();
                jsonObject.put("message","更新采购单数据失败："+e.getMessage());
                return jsonObject;
            }
            jsonObject.put("status","200"); //成功
            jsonObject.put("message","更新采购单数据成功");
            return jsonObject;
    }
    @Override
    public JSONObject downXsdInfo() {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status","300"); //失败
            SysParams sysParams  = sysParamsService.getById("xsd.url");
            try {
                //上传数据
                JSONObject postData = new JSONObject();
                postData.put("pageNum", "1");
                postData.put("name", null); //采购单编号
                postData.put("pageSize", "2000");
                postData.put("startDate", null);
                postData.put("endDate", null);


                RestTemplate client = new RestTemplate(new HttpsClientRequestFactory());
                JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();

                if (json.getString("code").equals("200")) {
                    if (savetTzd(jsonObject, json)) return jsonObject;
                }
            }catch (Exception e){
                e.printStackTrace();
                jsonObject.put("message","更新销售单数据失败："+e.getMessage());
                return jsonObject;
            }
            jsonObject.put("status","200"); //成功
            jsonObject.put("message","更新销售单数据成功");
            return jsonObject;
    }

    private boolean savetTzd(JSONObject jsonObject, JSONObject json) {
        JSONObject data = json.getJSONObject("data");
        //处理数据
        List<DataCrkywCpltzdxxDefault> saveTzd = new ArrayList<>();
        List<DataCrkywCpltzdxxFlt001> saveTzdFlt = new ArrayList<>();
        //json转化为对象
        if (data == null ){
            jsonObject.put("message","数据为0");
            return true;
        }
        List<DataCrkywCpltzdxxDefault> tzdList = data.getJSONArray("dataList").toJavaList(DataCrkywCpltzdxxDefault.class);
        for (int i = 0; i < tzdList.size(); i++) {
            DataCrkywCpltzdxxDefault  tzd = tzdList.get(i);
            if (dataCrkywCpltzdxxDefaultService.getById(tzd.getDataID()) == null){ //表示未新增
                String zylx = tzd.getCazylx();
                if (StringUtils.indexOf("3,5,7,9,10",zylx) >=0 ){ //成品粮入库流程
                    tzd.setFlowId("420801654D8F01A7D9FCE01B90A395E6"); //流程Id
                }
                if (StringUtils.indexOf("4,6,8,11",zylx) >=0){ //成品粮出库流程
                    tzd.setFlowId("284344D15DCA72D90AA43C24BD4F9B47"); //流程Id
                }
                List<DataCrkywCpltzdxxFlt001> fltList = tzd.getFltList();
                if(fltList.isEmpty() || fltList.size()<=0){
                    continue;
                }
                tzd.setWcsl(0.0); //完成数量
                tzd.setSysl(tzd.getSl());
                tzd.setFlowStatus(1); //流程状态
                String unitID = tzd.getUnitID();
                tzd.setSytelInfoID(unitID);
                tzd.setPeriodID("11");
                tzd.setDeptID(unitID); //部门id
                tzd.setUnitID(unitID); //
                tzd.setRoundID(0); //舍位id
                tzd.setKdr("自动");
                tzd.setKdsj(new Date());

                saveTzd.add(tzd); //添加主表数据

                String dataID = tzd.getDataID();

                if(!fltList.isEmpty() && fltList.size()>0){
                    for (int j = 0; j < fltList.size(); j++) {
                        DataCrkywCpltzdxxFlt001 dataCrkywCpltzdxxFlt001 = fltList.get(j);
                        dataCrkywCpltzdxxFlt001.setSytelInfoID(unitID);
                        dataCrkywCpltzdxxFlt001.setMainDataID(dataID);
                        dataCrkywCpltzdxxFlt001.setPeriodID("11");
                        dataCrkywCpltzdxxFlt001.setDeptID(unitID); //部门id
                        dataCrkywCpltzdxxFlt001.setUnitID(unitID); //
                        dataCrkywCpltzdxxFlt001.setRoundID(0); //舍位id
                        dataCrkywCpltzdxxFlt001.setWcsl(0.0); //完成数量
                        dataCrkywCpltzdxxFlt001.setSysl(dataCrkywCpltzdxxFlt001.getSl()); //剩余数量
                        dataCrkywCpltzdxxFlt001.setXz("952751"); //性质默认
                        //剩余数量=数量-完成数量（0）
                        dataCrkywCpltzdxxFlt001.setSysl(dataCrkywCpltzdxxFlt001.getSl()-dataCrkywCpltzdxxFlt001.getWcsl());
                        if (StringUtils.isBlank(dataCrkywCpltzdxxFlt001.getBuilder())){
                            dataCrkywCpltzdxxFlt001.setBuilder(unitID);
                            dataCrkywCpltzdxxFlt001.setEditor(unitID);
                        }
                    }
                    saveTzdFlt.addAll(fltList); //添加从表数据
                }
            }
        }
        if (!saveTzd.isEmpty() && !dataCrkywCpltzdxxDefaultService.saveOrUpdateBatch(saveTzd)) { //保存失败
            jsonObject.put("message","更新调拔单主表数据失败");
            return true;
        }else {
            if (!saveTzd.isEmpty() && !dataCrkywCpltzdxxFlt001Service.saveOrUpdateBatch(saveTzdFlt)) { //保存失败
                jsonObject.put("message","更新调拔单从表数据失败");
                return true;
            }
        }
        return false;
    }

    @Override
    public JSONObject downCgThhdInfo() {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status","300"); //失败
            SysParams sysParams  = sysParamsService.getById("cgThhd.url");
            try {
                //上传数据
                JSONObject postData = new JSONObject();
                postData.put("pageNum", "1");
                postData.put("pageSize", "2000");
                postData.put("startDate", null);
                postData.put("endDate", null);
                postData.put("name", null); //采购单编号


                RestTemplate client = new RestTemplate(new HttpsClientRequestFactory());
                JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();

                if (json.getString("code").equals("200")) {
                    if (savetTzd(jsonObject, json)) return jsonObject;
                }
            }catch (Exception e){
                e.printStackTrace();
                jsonObject.put("message","更新采购退换货单数据失败："+e.getMessage());
                return jsonObject;
            }
            jsonObject.put("status","200"); //成功
            jsonObject.put("message","更新采购退换货单数据成功");
            return jsonObject;
    }

    @Override
    public JSONObject downXsThhdInfo() {
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status","300"); //失败
            SysParams sysParams  = sysParamsService.getById("xsThhd.url");
            try {
                //上传数据
                JSONObject postData = new JSONObject();
                postData.put("pageSize", "2000");
                postData.put("pageNum", "1");
                postData.put("startDate", null);
                postData.put("endDate", null);
                postData.put("name", null); //采购单编号


                RestTemplate client = new RestTemplate(new HttpsClientRequestFactory());
                JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();

                if (json.getString("code").equals("200")) {
                    if (savetTzd(jsonObject, json)) return jsonObject;
                }
            }catch (Exception e){
                e.printStackTrace();
                jsonObject.put("message","更新销售退换货单数据失败："+e.getMessage());
                return jsonObject;
            }
            jsonObject.put("status","200"); //成功
            jsonObject.put("message","更新销售退换货单数据成功");
            return jsonObject;
        }
    }

    /**
     *
     * 从ddm跟新商品
     * @return
     */
    @Override
    public JSONObject downSpInfo() {

        String url =sysParamsService.getById("ddm.spurl").getParamVal();
        JSONObject ddm = this.getDdm(url);
        String body = ddm.get("body").toString();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = (JSONObject) jsonObject.get("data");
        List<MaterielDTOPageInfoResponse> dataList = data.getJSONArray("dataList").toJavaList(MaterielDTOPageInfoResponse.class);
        Iterator<MaterielDTOPageInfoResponse> it = dataList.iterator();
        while (it.hasNext()){
            DataCrkywCplspDefault dataCrkywCplspDefault = new DataCrkywCplspDefault();
            MaterielDTOPageInfoResponse next = it.next();
            dataCrkywCplspDefault.setDataId(next.getId());
            Date modifiedTime = DateUtil.parse(next.getModifiedTime(), "yyyy-MM-dd HH:mm:ss");
            dataCrkywCplspDefault.setUpdateTime(modifiedTime);
            dataCrkywCplspDefault.setBuilder(next.getCreateUserId()!=null?next.getCreateUserId().toString():null);
            dataCrkywCplspDefault.setBz(next.getDescription());
            Date createdTime = DateUtil.parse(next.getCreatedTime(), "yyyy-MM-dd HH:mm:ss");
            dataCrkywCplspDefault.setCjsj(createdTime);
            dataCrkywCplspDefault.setBzq(next.getGuaranteePeriod()!=null?next.getGuaranteePeriod().toString():null);
            dataCrkywCplspDefault.setDj(next.getMaterielModel()!=null?next.getMaterielModel().toString():null);
            dataCrkywCplspDefault.setGg(next.getMaterielModel()!=null?next.getMaterielModel().toString():null);
            dataCrkywCplspDefault.setJldw(next.getMaterielUnit()!=null?next.getMaterielUnit().toString():null);
            dataCrkywCplspDefault.setKcyjs(next.getMaxSafetyInventory()!=null?next.getMaxSafetyInventory().doubleValue():null);
            dataCrkywCplspDefault.setKcyjx(next.getMinSafetyInventory()!=null?next.getMinSafetyInventory().doubleValue():null);
            dataCrkywCplspDefault.setSccs(next.getEnterpriseId()!=null?next.getEnterpriseId().toString():null);
            dataCrkywCplspDefault.setSccsbh(next.getEnterpriseId()!=null?next.getEnterpriseId().toString():null);
            dataCrkywCplspDefault.setSjjlppbh(next.getMaterielBrand()!=null?next.getMaterielBrand().toString():null);
            dataCrkywCplspDefault.setSjjlpzbh(next.getCultivatedVarieties()!=null?next.getCultivatedVarieties().toString():null);
            dataCrkywCplspDefault.setSjjlspmcbh(next.getMaterielNum());
            dataCrkywCplspDefault.setUnitid(next.getTenantId()!=null?next.getTenantId().toString():null);
            dataCrkywCplspDefault.setDj(judge(next.getMaterielLevel()));
            dataCrkywCplspDefaultService.saveOrUpdate(dataCrkywCplspDefault);
        }
        return null;
    }

    /**
     * 从ddm跟新计量单位
     * @return
     */
    @Override
    public JSONObject downJldwInfo() {
        String url =sysParamsService.getById("ddm.jldwurl").getParamVal();
        JSONObject ddm = this.getDdm(url);
        String body = ddm.get("body").toString();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = (JSONObject) jsonObject.get("data");
        List<MeasurementUnitsDTOPageInfoResponse> dataList = data.getJSONArray("dataList").toJavaList(MeasurementUnitsDTOPageInfoResponse.class);
        Iterator<MeasurementUnitsDTOPageInfoResponse> it = dataList.iterator();
        while(it.hasNext()) {
            MeasurementUnitsDTOPageInfoResponse next = it.next();
            BasicEnum basicEnum = new BasicEnum();
            basicEnum.setEnumId(next.getId());
            basicEnum.setParentId("95274");
            basicEnum.setEnumName(next.getName());
            Date date = DateUtil.parse(next.getCreatedTime(), "yyyy-MM-dd HH:mm:ss");
            basicEnum.setCreateDate(date);
            basicEnum.setDelFlag(Integer.parseInt(next.getIsDeleted()));
            basicEnum.setCrearer((String) next.getCreateUserId());
            Date modifieddate = DateUtil.parse(next.getModifiedTime(), "yyyy-MM-dd HH:mm:ss");
            basicEnum.setUpdatetime(modifieddate);
            basicEnumService.saveOrUpdate(basicEnum);
        }

        return null;
    }

    /**
     * 从ddm跟新品种
     * @return
     */
    @Override
    public JSONObject downPzInfo() {
        String url =sysParamsService.getById("ddm.pzurl").getParamVal();
        JSONObject ddm = this.getDdm(url);
        String body = ddm.get("body").toString();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = (JSONObject) jsonObject.get("data");
        List<CultivatedVarietiesDTOPageInfoResponse> dataList = data.getJSONArray("dataList").toJavaList(CultivatedVarietiesDTOPageInfoResponse.class);
        Iterator<CultivatedVarietiesDTOPageInfoResponse> it = dataList.iterator();
        while(it.hasNext()) {
            CultivatedVarietiesDTOPageInfoResponse next = it.next();
            BasicEnum basicEnum = new BasicEnum();
            basicEnum.setEnumId(next.getId());
            basicEnum.setParentId("95272");
            basicEnum.setEnumName(next.getName());
            Date date = DateUtil.parse(next.getCreatedTime(), "yyyy-MM-dd HH:mm:ss");
            basicEnum.setCreateDate(date);
            basicEnum.setDelFlag(Integer.parseInt(next.getIsDeleted()));
            basicEnum.setCrearer((String) next.getCreateUserId());
            Date modifieddate = DateUtil.parse(next.getModifiedTime(), "yyyy-MM-dd HH:mm:ss");
            basicEnum.setUpdatetime(modifieddate);
            basicEnumService.saveOrUpdate(basicEnum);
        }
        return null;
    }

    /**
     * 从ddm跟新品牌
     * @return
     */
    @Override
    public JSONObject downPpInfo() {
        String url =sysParamsService.getById("ddm.spurl").getParamVal();
        JSONObject ddm = this.getDdm(url);
        String body = ddm.get("body").toString();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = (JSONObject) jsonObject.get("data");
        List<BrandDTOPageInfoResponse> dataList = data.getJSONArray("dataList").toJavaList(BrandDTOPageInfoResponse.class);
        Iterator<BrandDTOPageInfoResponse> it = dataList.iterator();
        while(it.hasNext()) {
            BrandDTOPageInfoResponse next = it.next();
            BasicEnum basicEnum = new BasicEnum();
            basicEnum.setEnumId(next.getId());
            basicEnum.setParentId("95271");
            basicEnum.setEnumName(next.getName());
            Date date = DateUtil.parse(next.getCreatedTime(), "yyyy-MM-dd HH:mm:ss");
            basicEnum.setCreateDate(date);
            basicEnum.setDelFlag(Integer.parseInt(next.getIsDeleted()));
            basicEnum.setCrearer((String) next.getCreateUserId());
            Date modifieddate = DateUtil.parse(next.getModifiedTime(), "yyyy-MM-dd HH:mm:ss");
            basicEnum.setUpdatetime(modifieddate);
            basicEnumService.saveOrUpdate(basicEnum);
        }
        return null;
    }

    @Override
    public JSONObject uploadSjzxData(String unitId) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status","300"); //失败
            SysParams sysParams = sysParamsService.getById("curUnit");
            if (StrUtil.isBlank(unitId)){
                if (sysParams == null ||StrUtil.isBlank(sysParams.getParamVal())) { //当前单位id
                    saveUploadLog(unitId,"",jsonObject.getString("message"));
                    jsonObject.put("message","未传入当前单位Id");
                    return jsonObject;
                }
                unitId = sysParams.getParamVal();
            }
            //得到是否上传数据
            sysParams = sysParamsService.getById("isStopUploadData");
            if (sysParams == null ||StrUtil.equals("1", sysParams.getParamVal())) { //表示停止上传数据
                jsonObject.put("message","停止上传数据");
                saveUploadLog(unitId,"",jsonObject.getString("message"));
                return jsonObject;
            }
            sysParams = sysParamsService.getById("upSJzxdata.url");
            if (sysParams == null || StrUtil.isBlank(sysParams.getParamVal())) { //上传数据地址
                jsonObject.put("message","未查询到上传数据地址");
                saveUploadLog(unitId,"",jsonObject.getString("message"));
                return jsonObject;
            }

            //将unitId放入json
            jsonObject.put("unitId",unitId);
            String limit = "limit 0,25";
            try {
                //解析JSON数据
                //{basicStorehouse:[{},{}],basicWarehouse[{},{}]}
                JSONArray datas = new JSONArray();
                StringBuilder operatlog = new StringBuilder("1000");
                operatlog =  getUpSjzxloadData(unitId, limit, datas, operatlog);

                if (datas.size() <=0){
                    jsonObject.put("message","未查询到上传数据");
                    saveUploadLog(unitId,"",jsonObject.getString("message"));
                    return jsonObject;
                }
                //上传数据
                JSONObject postData = new JSONObject();
                postData.put("platform", "inOutLib");
                postData.put("sign", "DD012479178D51FD33D06D7F3A4F0557");
                postData.put("data",JSONArray.toJSONStringWithDateFormat(datas,JSONObject.DEFFAULT_DATE_FORMAT));

                System.out.println(postData);
                RestTemplate client = new RestTemplate();
                JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();
                System.out.println(json);
                //更新上传时间
//                updateUplpadTimeData(data,retunrJson.getString("updateFailData"));
                if (!StrUtil.equals(json.getString("code"),"200")){
                    jsonObject.put("message",json.getString("message")+",此表数据未上传"+json.getString("updateFailData"));
                }else {
                    jsonObject.put("message","上传数据成功,"+operatlog);
                }
            }catch (Exception e){
                e.printStackTrace();
                jsonObject.put("message","上传数据失败："+e.getMessage());
            }

            return jsonObject;
    }

    /**
     * 根据单位Id删除上传数据中心数据
     *
     * @param unitId
     * @return
     */
    @Override
    public JSONObject deleteSjzxData(String unitId) {
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status","300"); //失败
            SysParams sysParams = sysParamsService.getById("curUnit");
            if (StrUtil.isBlank(unitId)){
                if (sysParams == null ||StrUtil.isBlank(sysParams.getParamVal())) { //当前单位id
                    jsonObject.put("message","未传入当前单位Id!");
                    saveUploadLog(unitId,"",jsonObject.getString("message"));
                    return jsonObject;
                }
                unitId = sysParams.getParamVal();
            }
            //得到是否上传数据
            sysParams = sysParamsService.getById("isStopUploadData");
            if (sysParams == null ||StrUtil.equals("1", sysParams.getParamVal())) { //表示停止上传数据
                jsonObject.put("message","停止上传数据");
                saveUploadLog(unitId,"",jsonObject.getString("message"));
                return jsonObject;
            }
            sysParams = sysParamsService.getById("upSJzxdata.url");
            if (sysParams == null || StrUtil.isBlank(sysParams.getParamVal())) { //上传数据地址
                jsonObject.put("message","未查询到上传数据地址");
                saveUploadLog(unitId,"",jsonObject.getString("message"));
                return jsonObject;
            }

            //将unitId放入json
            jsonObject.put("unitId",unitId);
            String limit = "limit 0,25";
            try {
                //解析JSON数据
                //{basicStorehouse:[{},{}],basicWarehouse[{},{}]}
                JSONArray datas = new JSONArray();
                StringBuilder operatlog = new StringBuilder("1000");
                operatlog =  delUpSjzxloadData(unitId, limit, datas, operatlog);

                if (datas.size() <=0){
                    jsonObject.put("message","未查询到上传数据");
                    saveUploadLog(unitId,"",jsonObject.getString("message"));
                    return jsonObject;
                }
                //上传数据
                JSONObject postData = new JSONObject();
                postData.put("platform", "inOutLib");
                postData.put("sign", "DD012479178D51FD33D06D7F3A4F0557");
                postData.put("data",JSONArray.toJSONStringWithDateFormat(datas,JSONObject.DEFFAULT_DATE_FORMAT));

                System.out.println(postData);
                RestTemplate client = new RestTemplate();
                JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();
                System.out.println(json);
                //更新上传时间
//                updateUplpadTimeData(data,retunrJson.getString("updateFailData"));
                if (!StrUtil.equals(json.getString("code"),"200")){
                    jsonObject.put("message",json.getString("message")+",此表数据未上传"+json.getString("updateFailData"));
                }else {
                    jsonObject.put("message","上传数据成功,"+operatlog);
                }
            }catch (Exception e){
                e.printStackTrace();
                jsonObject.put("message","上传数据失败："+e.getMessage());
            }

            return jsonObject;
        }
    }




    private StringBuilder delUpSjzxloadData(String unitId, String limit, JSONArray datas, StringBuilder operatlog) {{
        //成品粮入库
        QueryWrapper<DeleteDataRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("UPLOAD_TIME"); //上传时间为空
        queryWrapper.last(limit);
        List<DeleteDataRecord> list = deleteDataRecordService.list(queryWrapper);
        if (list.size() > 0){
            for (int i = 0; i <list.size() ; i++) {
                DeleteDataRecord deleteDataRecord = list.get(i);
                JSONObject data = new JSONObject();
                data.put("table",deleteDataRecord.getTableName());
                data.put("opt","del");
                data.put("dataInfo",deleteDataRecord.getDataid());
                datas.add(data);
            }
        }
        operatlog.append("delete_data_record:").append(list.size()).append("条;");
        return operatlog;
    }
    }

    private StringBuilder getUpSjzxloadData(String unitId, String limit, JSONArray datas, StringBuilder operatlog){
        //成品粮入库
        QueryWrapper<DataCrkywCplrkDefault> cplrkQueryWrapper = new QueryWrapper<>();
        cplrkQueryWrapper.eq("unitid",unitId);
        cplrkQueryWrapper.isNull("updateTime"); //上传时间为空
        cplrkQueryWrapper.last(limit);
        List<DataCrkywCplrkDefault> cplrkList = dataCrkywCplrkDefaultService.list(cplrkQueryWrapper);
        if (cplrkList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplrk_default");
            data.put("opt","add");
            data.put("dataInfo",cplrkList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplrk_default:").append(cplrkList.size()).append("条;");
        //成品粮入库从表
        QueryWrapper<DataCrkywCplrkFlt001> cplrkFltQueryWrapper = new QueryWrapper<>();
        cplrkQueryWrapper.eq("unitid",unitId);
        cplrkQueryWrapper.isNull("updateTime"); //上传时间为空
        cplrkQueryWrapper.last(limit);
        List<DataCrkywCplrkFlt001> cplrkFltList = dataCrkywCplrkFlt001Service.list(cplrkFltQueryWrapper);
        if (cplrkFltList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplrk_flt_001");
            data.put("opt","add");
            data.put("dataInfo",cplrkFltList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplrk_flt_001:").append(cplrkFltList.size()).append("条;");
        return operatlog;
    }
    /**
     * 判断是否是json字符串，是返回code
     * @param s
     * @return
     */
    public static  String  judge(String s){
        Integer k=0;
        boolean json = JSONUtil.isJson(s);
        if(json){
            cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(s);
            String code = jsonObject.getStr("code");
            return code;
        }else{
            return s;
        }

    }

    public JSONObject getDdm(String url){
        JSONObject jsonObject = new JSONObject();
        String accessKeyId =sysParamsService.getById("ddm.accessKeyId").getParamVal();
        String accessKeySecret = sysParamsService.getById("ddm.accessKeySecret").getParamVal();
        String userId = sysParamsService.getById("ddm.userId").getParamVal();
        Map<String, String> params=new HashMap<>();
        params.put("accessKeyId", accessKeyId);
        params.put("accessKeySecret",  accessKeySecret);
        params.put("userId", userId);
        params.put("$scopeField","modifiedTime");
        DateTime dateTime = DateUtil.offsetMonth(new Date(), -6);
        params.put("modifiedTime",dateTime.toString()+","+DateUtil.now());
        ddmPostCall ddmPostCall = new ddmPostCall();
        JSONObject ddm = ddmPostCall.callDmm(url, params);
        ResponseEntity<String> entity = (ResponseEntity<String>) ddm.get("entity");
        String body = entity.getBody();
        String statusCode = entity.getStatusCode().toString();
        jsonObject.put("body",body);
        jsonObject.put("statusCode",statusCode);
        return jsonObject;
    }
}
