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

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.unis.crk.basic.entity.BasicEnum;
import com.unis.crk.basic.entity.SysParams;
import com.unis.crk.basic.service.*;
import com.unis.crk.business.kcgl.service.*;
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.service.*;
import com.unis.crk.common.mapper.DataCenterUploadMapper;
import com.unis.crk.common.service.IBizflowtrailService;
import com.unis.crk.common.service.IDataCRKtoYJCRKUploadService;
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.quality.entity.QualityBase;
import com.unis.crk.quality.entity.QualityCheckitem;
import com.unis.crk.quality.service.IQualityBaseService;
import com.unis.crk.quality.service.IQualityCheckitemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DataCRKtoYJCRKUploadServiceImpl implements IDataCRKtoYJCRKUploadService {
    @Resource
    private  DataCenterUploadMapper dataCenterUploadMapper;
    @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
    IDataCrkywZcDefaultService dataCrkywZcDefaultService;

    @Autowired
    IDataCrkywRkcmDefaultService dataCrkywRkcmDefaultService;
    @Autowired
    IDataCrkywTzdxxDefaultService dataCrkywTzdxxDefaultService;
    @Autowired
    IDataCrkywTzdxxFlt001Service dataCrkywTzdxxFlt001Service;
    @Autowired
    IDataCrkywCplspDefaultService dataCrkywCplspDefaultService;
    //原粮出库
    @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
    INventoryAdjustmentService nventoryAdjustmentService;
    //成品粮领用
    @Autowired
    IOllectionReviewService  ollectionReviewService;
    //成品粮商品拆分
    @Autowired
    ISettlementManagementService settlementManagementService;
    //标签打印
    @Autowired
    IDataCrkywCplbjdyDefaultService dataCrkywCplbjdyDefaultService;
    //成品粮出库
    @Autowired
    IDataCrkywCplckDefaultService dataCrkywCplckDefaultService;
    //成品粮出库从表
    @Autowired
    IDataCrkywCplckFlt001Service dataCrkywCplckFlt001Service;
    //成品粮评分
    @Autowired
    IDataCrkywCplpfDefaultService  dataCrkywCplpfDefaultService;
    //成品粮抽样质检主表
    @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
    IDataCrkywCplfjDefaultService dataCrkywCplfjDefaultService;

    //成品粮分拣从表
    @Autowired
    IDataCrkywCplfjFlt001Service dataCrkywCplfjFlt001Service;

    //附件表
    @Autowired
    IAttachesService attachesService;
    //成品粮通知单主表
    @Autowired
    IDataCrkywCpltzdxxDefaultService dataCrkywCpltzdxxDefaultService;
    //成品粮通知单从表
    @Autowired
    IDataCrkywCpltzdxxFlt001Service dataCrkywCpltzdxxFlt001Service;
    @Autowired
    IUpdataLogService updataLogService;
    //车辆调度
    @Autowired
    IDispatchEhicleManagementService dispatchEhicleManagementService;
    //人员调度
    @Autowired
    IDispatcPersonnelManagementService dispatcPersonnelManagementService;

    //质量标准
    @Autowired
    IQualityBaseService qualityBaseService;
    @Autowired
    IQualityCheckitemService qualityCheckitemService;

    /**
     * 政务外网出入库到应急出入库数据数据传输
     * @param unitId
     * @param topic
     * @return
     */
    @Override
    public JSONObject uploadCRKtoYJData(String unitId, String topic) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","300"); //失败
        SysParams sysParams = sysParamsService.getById("curUnit");
        //得到是否上传数据
        sysParams = sysParamsService.getById("isStopIssuedData");
        if (sysParams == null || StrUtil.equals("1", sysParams.getParamVal())) { //表示停止上传数据
            jsonObject.put("message","停止上传数据");
            updataLogService.saveUploadLog(unitId,"",jsonObject.getString("message"));
            return jsonObject;
        }
        sysParams = sysParamsService.getById("upSJzxdata.url");
        if (sysParams == null || StrUtil.isBlank(sysParams.getParamVal())) { //上传数据地址
            jsonObject.put("message","未查询到上传数据地址");
            updataLogService.saveUploadLog(unitId,"",jsonObject.getString("message"));
            return jsonObject;
        }

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

            if (datas.size() <=0){
                jsonObject.put("message","未查询到上传数据");
                updataLogService.saveUploadLog(unitId,"",jsonObject.getString("message"));
                return jsonObject;
            }
            //上传数据
            JSONObject data = new JSONObject();
            data.put("type","data");
            data.put("value",JSONArray.toJSONStringWithDateFormat(datas,JSONObject.DEFFAULT_DATE_FORMAT));
//            System.out.println(datas);

            JSONObject postData = new JSONObject();
            postData.put("platform", "inOutLib");
            if (StrUtil.isBlank(topic)){
                topic = "crktoyj";
            }
//            postData.put("topic", "inOutLib.all");
            postData.put("topic", "inOutLib."+topic);
            postData.put("sign", "DD012479178D51FD33D06D7F3A4F0557");
            postData.put("data",data);

            RestTemplate client = new RestTemplate();
            JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();
            System.out.println("上传数据返回值："+json);

        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("message","上传数据失败："+e.getMessage());
        }

        return jsonObject;
    }


    /**
     * 应急出入库到政务外网出入库数据传输
     * @param unitId
     * @param topic
     * @return
     */
    @Override
    public JSONObject uploadYJtoCRKData(String unitId, String topic) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","300"); //失败
        SysParams sysParams = sysParamsService.getById("curUnit");
        //得到是否上传数据
        sysParams = sysParamsService.getById("isStopIssuedData");
        if (sysParams == null || StrUtil.equals("1", sysParams.getParamVal())) { //表示停止上传数据
            jsonObject.put("message","停止上传数据");
            updataLogService.saveUploadLog(unitId,"",jsonObject.getString("message"));
            return jsonObject;
        }
        sysParams = sysParamsService.getById("upSJzxdata.url");
        if (sysParams == null || StrUtil.isBlank(sysParams.getParamVal())) { //上传数据地址
            jsonObject.put("message","未查询到上传数据地址");
            updataLogService.saveUploadLog(unitId,"",jsonObject.getString("message"));
            return jsonObject;
        }

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

            if (datas.size() <=0){
                jsonObject.put("message","未查询到上传数据");
                updataLogService.saveUploadLog(unitId,"",jsonObject.getString("message"));
                return jsonObject;
            }
            //上传数据
            JSONObject data = new JSONObject();
            data.put("type","data");
            data.put("value",JSONArray.toJSONStringWithDateFormat(datas,JSONObject.DEFFAULT_DATE_FORMAT));
//            System.out.println(datas);

            JSONObject postData = new JSONObject();
            postData.put("platform", "inOutLib");
            if (StrUtil.isBlank(topic)){
                topic = "yjtocrk";
            }
//            postData.put("topic", "inOutLib.all");
            postData.put("topic", "inOutLib."+topic);
            postData.put("sign", "DD012479178D51FD33D06D7F3A4F0557");
            postData.put("data",data);

            RestTemplate client = new RestTemplate();
            JSONObject json = client.postForEntity(sysParams.getParamVal(), postData, JSONObject.class).getBody();
            System.out.println("上传数据返回值："+json);

        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("message","上传数据失败："+e.getMessage());
        }

        return jsonObject;
    }

    /**
     * 上传政务外网出入库到应急出入库数据删除
     * @param unitId
     * @param topic
     * @return
     */
    @Override
    public JSONObject uploadCRKtoYJDDelete(String unitId, String topic) {
        return null;
    }

    /**
     * 应急出入库下载政务外网出入库数据
     * @param object
     * @param routingKey
     * @return
     */
    @Override
    public JSONObject downloadCRKtoYJData(String object, String routingKey) {
        return null;
    }

    /**
     * 政务外网出入库接收应急出入库数据
     * @param object
     * @param routingKey
     * @return
     */
    @Override
    public JSONObject downloadYJtoCRKData(String object, String routingKey) {
        JSONObject returnJSON = new JSONObject();
        returnJSON.put("success",false);
        if (StrUtil.isBlank(routingKey)){
            returnJSON.put("message","topic为空");
            return  returnJSON;
        }
        if (StrUtil.isBlank(object) ){
            returnJSON.put("message","业务数据为空");
            return  returnJSON;
        }
        String routerKey_1 = "lcapPatrol.yjtocrk";
        if (StringUtils.indexOf(routerKey_1,routingKey)<0){
            returnJSON.put("message","非应急数据");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));

        List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
        if(datas!=null && datas.size()>0) {
            for (int i = 0; i < datas.size(); i++) {
                JSONObject jsonObj = (JSONObject) datas.get(i);
                Map<String, Object> innerMap = jsonObj.getInnerMap();
                list.add(innerMap);
            }
        }
        List<QualityCheckitem> qualityd = new ArrayList<QualityCheckitem>();
        List<QualityBase> quality =new ArrayList<QualityBase>();



        return returnJSON;
    }
    private void saveData(List<Map<String, Object>> list){
        for (Map<String, Object> s:list) {
            String opt = s.get("opt").toString();
            String table = s.get("table").toString();
            String dataInfo = s.get("dataInfo").toString();
            if("quality_checkitem".equals(table)){
                List<QualityCheckitem> QualityCheckitems = JSONArray.parseArray(dataInfo).toJavaList(QualityCheckitem.class);
                boolean b = qualityCheckitemService.saveOrUpdateBatch(QualityCheckitems);
                if(b){
                    System.out.println("质检项跟新成功");
                }
            }
            if("quality_base".equals(table)){
                List<QualityBase> qualityBases = JSONArray.parseArray(dataInfo).toJavaList(QualityBase.class);
                boolean b = qualityBaseService.saveOrUpdateBatch(qualityBases);
                if(b){
                    System.out.println("质量标准跟新成功");
                }
            }
            if("data_crkyw_cpltzdxx_default".equals(table)){
                List<DataCrkywCpltzdxxDefault> dataCrkywCpltzdxx = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCpltzdxxDefault.class);

            }
            if("data_crkyw_cpltzdxx_flt_001".equals(table)){
                List<DataCrkywCpltzdxxFlt001> dataCrkywCpltzdxxFlt001 = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCpltzdxxFlt001.class);
                boolean b = dataCrkywCpltzdxxFlt001Service.saveOrUpdateBatch(dataCrkywCpltzdxxFlt001);
                if (b){
                    System.out.println();
                }

            }
            if("data_crkyw_cplrk_default".equals(table)){
                List<DataCrkywCplrkDefault> dataCrkywCplrk = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCplrkDefault.class);
                boolean b = dataCrkywCplrkDefaultService.saveOrUpdateBatch(dataCrkywCplrk);
                if(b){
                    System.out.println("");
                }

            }
            if("data_crkyw_cplrk_flt_001".equals(table)){
                List<DataCrkywCplrkFlt001> DataCrkywCplrkFlt001 = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCplrkFlt001.class);

            }
            if("data_crkyw_cplck_default".equals(table)){
                List<DataCrkywCplckDefault> DataCrkywCplck = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCplckDefault.class);

            }
            if("data_crkyw_cplck_flt_001".equals(table)){
                List<DataCrkywCplckFlt001> DataCrkywCplckFlt001 = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCplckFlt001.class);

            }
            if("data_crkyw_cpljyxx_default".equals(table)){
                List<DataCrkywCpljyxxDefault> DataCrkywCpljyxx = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCpljyxxDefault.class);

            }
            if("data_crkyw_cpljyxx_flt_001".equals(table)){
                List<DataCrkywCpljyxxFlt001> DataCrkywCpljyxxFlt001 = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCpljyxxFlt001.class);

            }
            if("data_crkyw_cplpf_default".equals(table)){
                List<DataCrkywCplpfDefault> DataCrkywCplpf = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCplpfDefault.class);

            }
            if("data_kcgl_cplkcsw_default".equals(table)){
                List<DataKcglCplkcswDefault> DataKcglCplkcsw = JSONArray.parseArray(dataInfo).toJavaList(DataKcglCplkcswDefault.class);

            }
            if("data_kcgl_cplxckc_default".equals(table)){
                List<DataKcglCplxckcDefault> DataKcglCplxckc = JSONArray.parseArray(dataInfo).toJavaList(DataKcglCplxckcDefault.class);

            }
            if("data_kcgl_cplfcbgz_default".equals(table)){
                List<DataKcglCplfcbgzDefault> DataKcglCplfcbgz = JSONArray.parseArray(dataInfo).toJavaList(DataKcglCplfcbgzDefault.class);

            }
            if("data_crkyw_cplsp_default".equals(table)){
                List<DataCrkywCplspDefault> DataCrkywCplsp = JSONArray.parseArray(dataInfo).toJavaList(DataCrkywCplspDefault.class);

            }
            if("basic_enum".equals(table)){
                List<BasicEnum> BasicEnum = JSONArray.parseArray(dataInfo).toJavaList(BasicEnum.class);

            }

        }
    }


    private StringBuilder getYJtoCRKData(String unitId, String limit, JSONArray datas, StringBuilder operatlog, Boolean isNull, String topic) {
        //成品订单主表
        QueryWrapper<DataCrkywCpltzdxxDefault> dataCrkywCpltzdxxQueryWrapper = new QueryWrapper<>();
        dataCrkywCpltzdxxQueryWrapper.eq("unitid",unitId);
        dataCrkywCpltzdxxQueryWrapper.orderByDesc("ModifyDate");
        dataCrkywCpltzdxxQueryWrapper.last(limit);
        List<DataCrkywCpltzdxxDefault> cpltzdxxList = dataCrkywCpltzdxxDefaultService.list(dataCrkywCpltzdxxQueryWrapper);
        if (cpltzdxxList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cpltzdxx_default");
            data.put("opt","add");
            data.put("dataInfo",cpltzdxxList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cpltzdxx_default").append(cpltzdxxList.size()).append("条;");


        //成品订单从表
        QueryWrapper<DataCrkywCpltzdxxFlt001> dataCrkywCpltzdxxFltQueryWrapper = new QueryWrapper<>();
        dataCrkywCpltzdxxFltQueryWrapper.eq("unitid",unitId);
        dataCrkywCpltzdxxFltQueryWrapper.orderByDesc("ModifyDate"); //上传时间为空
        dataCrkywCpltzdxxFltQueryWrapper.last(limit);
        List<DataCrkywCpltzdxxFlt001> cpltzdxxFlt = dataCrkywCpltzdxxFlt001Service.list(dataCrkywCpltzdxxFltQueryWrapper);
        if (cpltzdxxFlt.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cpltzdxx_flt_001");
            data.put("opt","add");
            data.put("dataInfo",cpltzdxxFlt);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cpltzdxx_flt_001").append(cpltzdxxFlt.size()).append("条;");

        //成品粮入库
        QueryWrapper<DataCrkywCplrkDefault> cplrkQueryWrapper = new QueryWrapper<>();
        cplrkQueryWrapper.eq("unitid",unitId);
        cplrkQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        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<>();
        cplrkFltQueryWrapper.eq("unitid",unitId);
        cplrkFltQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        cplrkFltQueryWrapper.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("条;");

        //成品粮出主表
        QueryWrapper<DataCrkywCplckDefault> dataCrkywCplckQueryWrapper = new QueryWrapper<>();
        dataCrkywCplckQueryWrapper.eq("unitid",unitId);
        dataCrkywCplckQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        dataCrkywCplckQueryWrapper.last(limit);
        List<DataCrkywCplckDefault> cplckList = dataCrkywCplckDefaultService.list(dataCrkywCplckQueryWrapper);
        if (cplckList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplck_default");
            data.put("opt","add");
            data.put("dataInfo",cplckList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplck_default").append(cplckList.size()).append("条;");


        //成品粮出库从表
        QueryWrapper<DataCrkywCplckFlt001> dataCrkywCplckFltQueryWrapper = new QueryWrapper<>();
        dataCrkywCplckFltQueryWrapper.eq("unitid",unitId);
        dataCrkywCplckFltQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        dataCrkywCplckFltQueryWrapper.last(limit);
        List<DataCrkywCplckFlt001> cplckFltList = dataCrkywCplckFlt001Service.list(dataCrkywCplckFltQueryWrapper);
        if (cplckFltList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplck_flt_001");
            data.put("opt","add");
            data.put("dataInfo",cplckFltList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplck_flt_001").append(cplckFltList.size()).append("条;");

        //成品粮质检主表
        QueryWrapper<DataCrkywCpljyxxDefault> jyxxDefaultQueryWrapper = new QueryWrapper<>();
        jyxxDefaultQueryWrapper.eq("unitid",unitId);
        jyxxDefaultQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        jyxxDefaultQueryWrapper.last(limit);
        List<DataCrkywCpljyxxDefault> cpljyxxList = dataCrkywCpljyxxDefaultService.list(jyxxDefaultQueryWrapper);
        if (cpljyxxList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cpljyxx_default");
            data.put("opt","add");
            data.put("dataInfo",cpljyxxList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cpljyxx_default").append(cpljyxxList.size()).append("条;");


        //成品粮质检从表
        QueryWrapper<DataCrkywCpljyxxFlt001> jyxxFltQueryWrapper = new QueryWrapper<>();
        jyxxFltQueryWrapper.eq("unitid",unitId);
        jyxxFltQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        jyxxFltQueryWrapper.last(limit);
        List<DataCrkywCpljyxxFlt001> cpljyxxFltList = dataCrkywCpljyxxFlt001Service.list(jyxxFltQueryWrapper);
        if (cpljyxxFltList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cpljyxx_flt_001");
            data.put("opt","add");
            data.put("dataInfo",cpljyxxFltList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cpljyxx_flt_001").append(cpljyxxFltList.size()).append("条;");

        //成品粮评分
        QueryWrapper<DataCrkywCplpfDefault> dataCrkywCplpfQueryWrapper = new QueryWrapper<>();
        dataCrkywCplpfQueryWrapper.eq("unitid",unitId);
        dataCrkywCplpfQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        dataCrkywCplpfQueryWrapper.last(limit);
        List<DataCrkywCplpfDefault> cplpfList = dataCrkywCplpfDefaultService.list(dataCrkywCplpfQueryWrapper);
        if (cplpfList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplpf_default");
            data.put("opt","add");
            data.put("dataInfo",cplpfList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplpf_default").append(cplpfList.size()).append("条;");
        return operatlog;
    }




    private StringBuilder getCRKtoYJData(String unitId, String limit, JSONArray datas, StringBuilder operatlog, Boolean isNull, String topic) {
        //成品订单主表
        QueryWrapper<DataCrkywCpltzdxxDefault> dataCrkywCpltzdxxQueryWrapper = new QueryWrapper<>();
        dataCrkywCpltzdxxQueryWrapper.eq("unitid",unitId);
        dataCrkywCpltzdxxQueryWrapper.orderByDesc("ModifyDate");
        dataCrkywCpltzdxxQueryWrapper.last(limit);
        List<DataCrkywCpltzdxxDefault> cpltzdxxList = dataCrkywCpltzdxxDefaultService.list(dataCrkywCpltzdxxQueryWrapper);
        if (cpltzdxxList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cpltzdxx_default");
            data.put("opt","add");
            data.put("dataInfo",cpltzdxxList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cpltzdxx_default").append(cpltzdxxList.size()).append("条;");


        //成品订单从表
        QueryWrapper<DataCrkywCpltzdxxFlt001> dataCrkywCpltzdxxFltQueryWrapper = new QueryWrapper<>();
        dataCrkywCpltzdxxFltQueryWrapper.eq("unitid",unitId);
        dataCrkywCpltzdxxFltQueryWrapper.orderByDesc("ModifyDate"); //上传时间为空
        dataCrkywCpltzdxxFltQueryWrapper.last(limit);
        List<DataCrkywCpltzdxxFlt001> cpltzdxxFlt = dataCrkywCpltzdxxFlt001Service.list(dataCrkywCpltzdxxFltQueryWrapper);
        if (cpltzdxxFlt.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cpltzdxx_flt_001");
            data.put("opt","add");
            data.put("dataInfo",cpltzdxxFlt);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cpltzdxx_flt_001").append(cpltzdxxFlt.size()).append("条;");

        //成品粮入库
        QueryWrapper<DataCrkywCplrkDefault> cplrkQueryWrapper = new QueryWrapper<>();
        cplrkQueryWrapper.eq("unitid",unitId);
        cplrkQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        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<>();
        cplrkFltQueryWrapper.eq("unitid",unitId);
        cplrkFltQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        cplrkFltQueryWrapper.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("条;");

        //成品粮出主表
        QueryWrapper<DataCrkywCplckDefault> dataCrkywCplckQueryWrapper = new QueryWrapper<>();
        dataCrkywCplckQueryWrapper.eq("unitid",unitId);
        dataCrkywCplckQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        dataCrkywCplckQueryWrapper.last(limit);
        List<DataCrkywCplckDefault> cplckList = dataCrkywCplckDefaultService.list(dataCrkywCplckQueryWrapper);
        if (cplckList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplck_default");
            data.put("opt","add");
            data.put("dataInfo",cplckList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplck_default").append(cplckList.size()).append("条;");


        //成品粮出库从表
        QueryWrapper<DataCrkywCplckFlt001> dataCrkywCplckFltQueryWrapper = new QueryWrapper<>();
        dataCrkywCplckFltQueryWrapper.eq("unitid",unitId);
        dataCrkywCplckFltQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        dataCrkywCplckFltQueryWrapper.last(limit);
        List<DataCrkywCplckFlt001> cplckFltList = dataCrkywCplckFlt001Service.list(dataCrkywCplckFltQueryWrapper);
        if (cplckFltList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplck_flt_001");
            data.put("opt","add");
            data.put("dataInfo",cplckFltList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplck_flt_001").append(cplckFltList.size()).append("条;");

        //成品粮质检主表
        QueryWrapper<DataCrkywCpljyxxDefault> jyxxDefaultQueryWrapper = new QueryWrapper<>();
        jyxxDefaultQueryWrapper.eq("unitid",unitId);
        jyxxDefaultQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        jyxxDefaultQueryWrapper.last(limit);
        List<DataCrkywCpljyxxDefault> cpljyxxList = dataCrkywCpljyxxDefaultService.list(jyxxDefaultQueryWrapper);
        if (cpljyxxList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cpljyxx_default");
            data.put("opt","add");
            data.put("dataInfo",cpljyxxList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cpljyxx_default").append(cpljyxxList.size()).append("条;");


        //成品粮质检从表
        QueryWrapper<DataCrkywCpljyxxFlt001> jyxxFltQueryWrapper = new QueryWrapper<>();
        jyxxFltQueryWrapper.eq("unitid",unitId);
        jyxxFltQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        jyxxFltQueryWrapper.last(limit);
        List<DataCrkywCpljyxxFlt001> cpljyxxFltList = dataCrkywCpljyxxFlt001Service.list(jyxxFltQueryWrapper);
        if (cpljyxxFltList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cpljyxx_flt_001");
            data.put("opt","add");
            data.put("dataInfo",cpljyxxFltList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cpljyxx_flt_001").append(cpljyxxFltList.size()).append("条;");

        //成品粮评分
        QueryWrapper<DataCrkywCplpfDefault> dataCrkywCplpfQueryWrapper = new QueryWrapper<>();
        dataCrkywCplpfQueryWrapper.eq("unitid",unitId);
        dataCrkywCplpfQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        dataCrkywCplpfQueryWrapper.last(limit);
        List<DataCrkywCplpfDefault> cplpfList = dataCrkywCplpfDefaultService.list(dataCrkywCplpfQueryWrapper);
        if (cplpfList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplpf_default");
            data.put("opt","add");
            data.put("dataInfo",cplpfList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplpf_default").append(cplpfList.size()).append("条;");

        //成品粮期初库存
        QueryWrapper<DataKcglCplqckcDefault> dataKcglCplqckcQueryWrapper = new QueryWrapper<>();
        dataKcglCplqckcQueryWrapper.eq("unitid",unitId);
        dataKcglCplqckcQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        dataKcglCplqckcQueryWrapper.last(limit);
        List<DataKcglCplqckcDefault> cplqckcList = dataKcglCplqckcDefaultService.list(dataKcglCplqckcQueryWrapper);
        if (cplqckcList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_kcgl_cplqckc_default");
            data.put("opt","add");
            data.put("dataInfo",cplqckcList);
            datas.add(data);
        }
        operatlog.append("data_kcgl_cplqckc_default").append(cplqckcList.size()).append("条;");

        //成品粮库存实物
        QueryWrapper<DataKcglCplkcswDefault> dataKcglCplkcswQueryWrapper = new QueryWrapper<>();
        dataKcglCplkcswQueryWrapper.eq("unitid",unitId);
        dataKcglCplkcswQueryWrapper.orderByDesc("modifyDate"); //上传时间为空
        dataKcglCplkcswQueryWrapper.last(limit);
        List<DataKcglCplkcswDefault> cplkcswList = dataKcglCplkcswDefaultService.list(dataKcglCplkcswQueryWrapper);
        if (cplkcswList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_kcgl_cplkcsw_default");
            data.put("opt","add");
            data.put("dataInfo",cplkcswList);
            datas.add(data);
        }
        operatlog.append("data_kcgl_cplkcsw_default").append(cplkcswList.size()).append("条;");
        //成品粮库形成库存
        QueryWrapper<DataKcglCplxckcDefault> dataKcglCplxckcQueryWrapper = new QueryWrapper<>();
        dataKcglCplxckcQueryWrapper.eq("unitid",unitId);
        dataKcglCplxckcQueryWrapper.orderByDesc("cjsj"); //上传时间为空
        dataKcglCplxckcQueryWrapper.last(limit);
        List<DataKcglCplxckcDefault> cplxckcList = dataKcglCplxckcDefaultService.list(dataKcglCplxckcQueryWrapper);
        if (cplxckcList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_kcgl_cplxckc_default");
            data.put("opt","add");
            data.put("dataInfo",cplxckcList);
            datas.add(data);
        }
        operatlog.append("data_kcgl_cplxckc_default").append(cplxckcList.size()).append("条;");

        //成品粮分仓保管账
        QueryWrapper<DataKcglCplfcbgzDefault> dataKcglCplfcbgzQueryWrapper = new QueryWrapper<>();
        dataKcglCplfcbgzQueryWrapper.eq("unitid",unitId);
        dataKcglCplxckcQueryWrapper.orderByDesc("cjsj"); //上传时间为空
        dataKcglCplfcbgzQueryWrapper.last(limit);
        List<DataKcglCplfcbgzDefault> cplfcbgzList = dataKcglCplfcbgzDefaultService.list(dataKcglCplfcbgzQueryWrapper);
        if (cplfcbgzList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_kcgl_cplfcbgz_default");
            data.put("opt","add");
            data.put("dataInfo",cplfcbgzList);
            datas.add(data);
        }
        operatlog.append("data_kcgl_cplfcbgz_default").append(cplfcbgzList.size()).append("条;");

        //成品商品信息
        QueryWrapper<DataCrkywCplspDefault> dataCrkywCplspQueryWrapper = new QueryWrapper<>();
        dataCrkywCplspQueryWrapper.eq("unitid",unitId);
        dataCrkywCplspQueryWrapper.orderByDesc("cjsj"); //上传时间为空
        dataCrkywCplspQueryWrapper.last(limit);
        List<DataCrkywCplspDefault> cplspList = dataCrkywCplspDefaultService.list(dataCrkywCplspQueryWrapper);
        if (cplspList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","data_crkyw_cplsp_default");
            data.put("opt","add");
            data.put("dataInfo",cplspList);
            datas.add(data);
        }
        operatlog.append("data_crkyw_cplsp_default").append(cplfcbgzList.size()).append("条;");

        //成品品种品牌
        QueryWrapper<BasicEnum> basicEnumQueryWrapper = new QueryWrapper<>();
        basicEnumQueryWrapper.in("parentId","95272","95271");
        basicEnumQueryWrapper.last(limit);
        List<BasicEnum> basicEnumList = basicEnumService.list(basicEnumQueryWrapper);
        if (basicEnumList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","basic_enum");
            data.put("opt","add");
            data.put("dataInfo",cplspList);
            datas.add(data);
        }
        operatlog.append("basic_enum").append(cplfcbgzList.size()).append("条;");


        //质量标准主表
        QueryWrapper<QualityBase> qualityBaseQueryWrapper = new QueryWrapper<>();
        basicEnumQueryWrapper.eq("UNITID",unitId);
        basicEnumQueryWrapper.last(limit);
        List<QualityBase> qualityBaseList = qualityBaseService.list(qualityBaseQueryWrapper);
        if (basicEnumList.size() > 0){
            JSONObject data = new JSONObject();
            data.put("table","quality_base");
            data.put("opt","add");
            data.put("dataInfo",cplspList);
            datas.add(data);
        }
        operatlog.append("quality_base").append(cplfcbgzList.size()).append("条;");
        Iterator<QualityBase> iterator = qualityBaseList.iterator();
        //质量标准检验项

        if(iterator.hasNext()){
            QualityBase next = iterator.next();
            QueryWrapper<QualityCheckitem> qualityCheckitemWrapper = new QueryWrapper<>();
            basicEnumQueryWrapper.eq("StandardID",next.getStandardID());
            basicEnumQueryWrapper.last(limit);
            List<QualityCheckitem> qualityCheckitemList = qualityCheckitemService.list(qualityCheckitemWrapper);
            if (basicEnumList.size() > 0){
                JSONObject data = new JSONObject();
                data.put("table","quality_checkitem");
                data.put("opt","add");
                data.put("dataInfo",cplspList);
                datas.add(data);
            }
            operatlog.append("quality_checkitem").append(cplfcbgzList.size()).append("条;");
        }



        return operatlog;
    }
}
