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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
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.unis.crk.basic.entity.*;
import com.unis.crk.basic.service.*;
import com.unis.crk.business.kcgl.entity.DataKcglKcbdDefault;
import com.unis.crk.business.kcgl.service.IDataKcglKcbdDefaultService;
import com.unis.crk.common.entity.vo.*;
import com.unis.crk.business.qcrk.entity.DataCrkywTzdxxDefault;
import com.unis.crk.business.qcrk.service.IDataCrkywTzdxxDefaultService;
import com.unis.crk.business.qcrk.service.IDataCrkywTzdxxFlt001Service;
import com.unis.crk.common.service.IDataCenterDownloadService;
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.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DataCenterDownloadServiceImpl implements IDataCenterDownloadService {
    //系统参数
    @Autowired
    private ISysParamsService sysParamsService;
    //成品粮商品信息
    @Autowired
    IDataCrkywCplspDefaultService dataCrkywCplspDefaultService;
    //成品粮通知单主表
    @Autowired
    IDataCrkywCpltzdxxDefaultService dataCrkywCpltzdxxDefaultService;
    //成品粮通知单从表
    @Autowired
    IDataCrkywCpltzdxxFlt001Service dataCrkywCpltzdxxFlt001Service;
    //原粮通知单主表
    @Autowired
    IDataCrkywTzdxxDefaultService dataCrkywTzdxxDefaultService;
    //原粮通知单从表
    @Autowired
    IDataCrkywTzdxxFlt001Service dataCrkywTzdxxFlt001Service;
    //人员信息
    @Autowired
    IDispatcPersonnelManagementService dispatcPersonnelManagementService;
    //车辆信息
    @Autowired
    IDispatchEhicleManagementService dispatchEhicleManagementService;
    //枚举
    @Autowired
    IBasicEnumService basicEnumService;
    //单位信息
    @Autowired
    IOrgInfoService orgInfoService;
    //仓房信息
    @Autowired
    IBasicStorehouseService basicStorehouseService;
    //货位信息
    @Autowired
    IBasicWarehouseService basicWarehouseService;
    //成品粮库存变动
    @Autowired
    IDataKcglCplkcbdDefaultService  dataKcglCplkcbdDefaultService;
    //原粮库存变动
    @Autowired
    IDataKcglKcbdDefaultService dataKcglKcbdDefaultService;
    @Autowired
    IDataCrkywGysDefaultService dataCrkywGysDefaultService;
    @Autowired
    IDataCrkywCplzjjgDefaultService dataCrkywCplzjjgDefaultService;
    //成品粮入库子表
    @Autowired
    IDataCrkywCplrkFlt001Service dataCrkywCplrkFlt001Service;
    @Autowired
    private IQualityBaseService qualityBaseService;

    @Autowired
    private IQualityCheckitemService qualityCheckitemService;
    //电商售后订单
    @Autowired
    private  ILiAfterSaleService  iLiAfterSaleService;
    //电商发货订单子表
    @Autowired
    private  ILiOrderItemService iLiOrderItemService;
    //电商发货订单
    @Autowired
    private  ILiOrderService iLiOrderService;
    public JSONObject savetTzd(String object,String routerKey) {

        JSONObject returnJSON = new JSONObject();
        returnJSON.put("success", false);
        if (StrUtil.isBlank(routerKey)) {
            returnJSON.put("message", "topic为空");
            return returnJSON;
        }
        if (StrUtil.isBlank(object)) {
            returnJSON.put("message", "业务数据为空");
            return returnJSON;
        }
        String routerKey_ = "lcapPatrol.PurchaseOrder,lcapPatrol.SalesOrder,lcapPatrol.SalesChangeOrder,lcapPatrol.PurchaseChangeOrder,lcapPatrol.PurchaseExchangeOrder,lcapPatrol.SalesExchangeOrder";
        if (StringUtils.indexOf(routerKey_, routerKey) < 0) {
            returnJSON.put("message", "topic非订单业务");
            return returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        //根据操作类型判断是删除还是增改
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            DataCrkywCpltzdxxDefault oneData = dataCrkywCpltzdxxDefaultService.getById(dataInfo);
            if(Optional.ofNullable(oneData).isPresent()){
                QueryWrapper<DataCrkywCpltzdxxFlt001> dataCrkywCpltzdxxFlt001QueryWrapper = new QueryWrapper<>();
                dataCrkywCpltzdxxFlt001QueryWrapper.eq("MainDataID", dataInfo);
                boolean remove = dataCrkywCpltzdxxFlt001Service.remove(dataCrkywCpltzdxxFlt001QueryWrapper);
                boolean remove2 =dataCrkywCpltzdxxDefaultService.removeById(dataInfo);
                if(remove && remove2){
                    returnJSON.put("message", "删除订单成功");
                }else{
                    returnJSON.put("message", "删除订单失败");
                }
            }else{
                returnJSON.put("message", "未查询到订单数据:"+dataInfo);
            }
        } else{
                //处理数据
            List<DataCrkywCpltzdxxDefault> saveTzd = new ArrayList<>();
            List<DataCrkywCpltzdxxFlt001> saveTzdFlt = new ArrayList<>();
            List<DataCrkywCpltzdxxDefault> tzdList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(DataCrkywCpltzdxxDefault.class);
            for (int i = 0; i < tzdList.size(); i++) {
                DataCrkywCpltzdxxDefault tzd = tzdList.get(i);
                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
                }
                tzd.setCazylx(zylx);
                if (dataCrkywCpltzdxxDefaultService.getById(tzd.getDataID()) == null) { //表示未新增
                    String dataID = tzd.getDataID();
                    tzd.setWcsl(0.0); //完成数量
                    Double sl = tzd.getSl();
                    if (sl == null) {
                        sl = tzd.getSysl();
                    }
                    tzd.setSysl(sl);
                    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.getBuilder()); //使创建者名称
                }
                saveTzd.add(tzd); //添加主表数据
            }
            List<DataCrkywCpltzdxxFlt001> fltList = datas.getJSONObject(1).getJSONArray("dataInfo").toJavaList(DataCrkywCpltzdxxFlt001.class);

            if (!fltList.isEmpty() && fltList.size() > 0) {
                for (int j = 0; j < fltList.size(); j++) {
                    DataCrkywCpltzdxxFlt001 dataCrkywCpltzdxxFlt001 = fltList.get(j);
                    if (dataCrkywCpltzdxxFlt001Service.getById(dataCrkywCpltzdxxFlt001.getDataID()) == null) { //表示未新增
                        String unitID = dataCrkywCpltzdxxFlt001.getUnitID();
                        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.setSl(dataCrkywCpltzdxxFlt001.getJs() * Double.parseDouble(dataCrkywCpltzdxxFlt001.getGg())); //完成数量
                        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.add(dataCrkywCpltzdxxFlt001); //添加从表数据
                }
            }
            if (!saveTzd.isEmpty() && !dataCrkywCpltzdxxDefaultService.saveOrUpdateBatch(saveTzd)) { //保存失败
                returnJSON.put("message", "更新订单主表数据失败");
            } else {
                if (!saveTzdFlt.isEmpty() && !dataCrkywCpltzdxxFlt001Service.saveOrUpdateBatch(saveTzdFlt)) { //保存失败
                    returnJSON.put("message", "更新订单从表数据失败");
                }
            }
            return returnJSON;
        }
        return returnJSON;
    }

    /**
     * 接收ddm车辆信息
     *
     * @param object
     * @param routingKey
     * @return
     */
    @Override
    public JSONObject saveClxx(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_ = "lcapPatrol.vehicle";
            if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
                returnJSON.put("message","topic非车辆信息业务");
                return  returnJSON;
            }
            JSONObject josn = JSONObject.parseObject(object);
            JSONArray datas = JSONArray.parseArray(josn.getString("value"));
            System.out.println(datas);
            //处理数据
            String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
                String dataInfo = datas.getJSONObject(0).getString("dataInfo");
                DispatchEhicleManagement oneById = dispatchEhicleManagementService.getOneById(dataInfo);
                if(Optional.ofNullable(oneById).isPresent()){
                    boolean remove =dispatchEhicleManagementService.removeById(dataInfo);
                        if(remove){
                            returnJSON.put("message", "删除车辆成功");
                        }else{
                            returnJSON.put("message", "删除车辆失败");
                        }
                }else{
                    returnJSON.put("message", "未查询到车辆数据:"+dataInfo);
                }
        } else {
                List<DispatchEhicleManagement> saveCxll = new ArrayList<>();
                List<DispatchEhicleManagement> clxxList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(DispatchEhicleManagement.class);
                JSONArray jsonArray = datas.getJSONObject(0).getJSONArray("dataInfo");
                for (int i = 0; i < clxxList.size(); i++) {
                    DispatchEhicleManagement clxx = clxxList.get(i);
                        JSONObject clxx_json = jsonArray.getJSONObject(i);
                        clxx.setDataId(clxx_json.getOrDefault("id", "").toString());
                        String unitID = clxx_json.getString("station");
                        clxx.setUnitid(unitID); //
                        clxx.setBuilder(clxx_json.getString("createUserId")); // 创建人
                        clxx.setEditor(clxx_json.getString("modifyUserId")); // 修改人
                        clxx.setBuilderDate(clxx_json.getDate("createdTime")); //创建时间
                        clxx.setModifyDate(clxx_json.getDate("modifiedTime")); //修改时间
                        clxx.setLicensePlateNumber(clxx_json.getString("name")); //车牌号
                        clxx.setEhicleType(clxx_json.getString("vehicleType")); //车辆类型
                        clxx.setEhicleBrand(clxx_json.getString("vehicleBrand")); //车辆品牌
                        clxx.setLicensePlateModel(clxx_json.getString("vehicleModel")); //车牌型号
                        clxx.setDateOfProduction(clxx_json.getDate("manufactureDate")); //出厂日期
                        clxx.setPurchaseTime(clxx_json.getDate("purchaseDate")); //购入时间
                        clxx.setPurchaseUnit(clxx_json.getString("purchasedCompany")); //购入单位
                        clxx.setPurchasePrice(clxx_json.getDouble("buyingPrice")); //购买价格
                        clxx.setInitialKilometers(clxx_json.getDouble("InitialKilometres")); //初始公里数
                        clxx.setCurrentState(clxx_json.getString("status")); //当前状态
                        clxx.setFuelConsumption(clxx_json.getDouble("oilConsumption")); //耗油量
                        clxx.setEngineNumber(clxx_json.getString("engineNumber")); //发动机号
                        clxx.setVehicleLoad(clxx_json.getOrDefault("deadWeight","").toString()); //载重
                        clxx.setSeat(clxx_json.getInteger("seating")); //座位
                        clxx.setAnnualReviewDate(clxx_json.getDate("annualReviewDate")); //年审时间
                        clxx.setAnnualReviewItems(clxx_json.getString("annualReviewProject")); //年审项目
                        clxx.setAnnualReview(clxx_json.getString("annualReviewResults")); //年审情况
                        clxx.setDateOfNextAnnualReview(null); //下次年审时间
                        clxx.setInsuredTime(clxx_json.getDate("insuranceDate")); //入保时间
                        clxx.setInsuranceCompany(clxx_json.getString("insurer")); //保险公司
                        clxx.setInsuranceCoverage(clxx_json.getString("insuranceSituation")); //投保情况
                        clxx.setDateOfIssue(clxx_json.getDate("releaseDate")); //出保日期
                        clxx.setFrontPhoto(clxx_json.getString("frontPicture")); //正面照片url
                        clxx.setProfilePhoto(clxx_json.getString("sidePicture")); //侧面照片url
                        clxx.setDriver(clxx_json.getString("driver")); //驾驶人员

                    saveCxll.add(clxx); //添加主表数据
                }
                if (!saveCxll.isEmpty() && !dispatchEhicleManagementService.saveOrUpdateBatch(saveCxll)) { //保存失败
                    returnJSON.put("message", "更新车辆信息数据失败");
                }
            }
            return returnJSON;
    }

    /**
     * 接收ddm人员信息
     *
     * @param object
     * @param routingKey
     * @return
     */
    @Override
    public JSONObject savetRyxx(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_ = "lcapPatrol.logisticsPersonnelInformation";
            if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
                returnJSON.put("message","topic非人员信息业务");
                return  returnJSON;
            }
            JSONObject josn = JSONObject.parseObject(object);
            JSONArray datas = JSONArray.parseArray(josn.getString("value"));
            System.out.println(datas);
            //处理数据
             String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
                String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            DispatcPersonnelManagement byId = dispatcPersonnelManagementService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =dispatcPersonnelManagementService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除人员成功");
                }else{
                    returnJSON.put("message", "删除人员失败");
                }
            }else{
                returnJSON.put("message", "未查询到人员数据:"+dataInfo);
            }
        } else {
                List<DispatcPersonnelManagement> saveRyxx = new ArrayList<>();
                List<DispatcPersonnelManagement> ryxxList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(DispatcPersonnelManagement.class);
                JSONArray jsonArray = datas.getJSONObject(0).getJSONArray("dataInfo");
                for (int i = 0; i < ryxxList.size(); i++) {
                    DispatcPersonnelManagement ryxx = ryxxList.get(i);
                    JSONObject ryxx_json = jsonArray.getJSONObject(i);
                    ryxx.setDataId(ryxx_json.getOrDefault("id", "").toString());
                    String unitID = ryxx_json.getOrDefault("station", "").toString();
                    ryxx.setUnitid(unitID); //
                    ryxx.setBuilder(ryxx_json.getString("createUserId")); // 创建人
                    ryxx.setEditor(ryxx_json.getString("modifyUserId")); // 修改人
                    ryxx.setBuilderDate(ryxx_json.getDate("createdTime")); //创建时间
                    ryxx.setModifyDate(ryxx_json.getDate("modifiedTime")); //修改时间
                    ryxx.setPersonName(ryxx_json.getString("name")); //姓名
                    //性别
                    ryxx.setSex(ryxx_json.getString("gender"));
                    //健康状况
                    ryxx.setHealth(ryxx_json.getString("health"));
                    //民族
                    ryxx.setNation(ryxx_json.getString("nationality"));
                    ryxx.setDateOfBirth(ryxx_json.getDate("birth")); //生日
                    ryxx.setAge(ryxx_json.getInteger("age")); //年龄
                    ryxx.setNativePlace(ryxx_json.getString("hometown")); //籍贯
                    //文化程度
                    ryxx.setDegreeOfEducation(ryxx_json.getString("educationalLevel"));
                    ryxx.setIdNumber(ryxx_json.getString("IDnumber")); //身份证
                    ryxx.setContactNumber(ryxx_json.getString("telephone"));//联系电话
                    ryxx.setContactAddress(ryxx_json.getString("address"));//联系地址
                    ryxx.setLicenseType(ryxx_json.getString("driverLicenseType"));//驾照类型
                    ryxx.setDriversLicenseNumber(ryxx_json.getString("driverLicenseNumber"));//驾照号码
                    ryxx.setInitialCollectionTime(ryxx_json.getDate("FirstPickTime"));//FirstPickTime
                    ryxx.setQuasiDriving(ryxx_json.getString("DrivingReadyModel"));//准驾车型
                    ryxx.setRiverPhotos(ryxx_json.getString("photo"));//司机照片url

                    saveRyxx.add(ryxx); //添加主表数据
                }

                if (!saveRyxx.isEmpty() && !dispatcPersonnelManagementService.saveOrUpdateBatch(saveRyxx)) { //保存失败
                    returnJSON.put("message", "更新人员信息数据失败");
                    return returnJSON;
                }
            }
            return returnJSON;
    }


    @Override
    public JSONObject savetjldw(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_ = "lcapPatrol.MeasurementUnits";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic非计量单位信息业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        //处理数据
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
                String dataInfo = datas.getJSONObject(0).getString("dataInfo");
                BasicEnum byId = basicEnumService.getById(dataInfo);
                    if(Optional.ofNullable(byId).isPresent()){
                        boolean remove =basicEnumService.removeById(dataInfo);
                            if(remove){
                                returnJSON.put("message", "删除计量单位成功");
                            }else{
                                returnJSON.put("message", "删除计量单位失败");
                            }
                    }else {
                        returnJSON.put("message", "未查询到计量单位数据:"+dataInfo);
                    }
        } else {
                List<BasicEnum> saveJldw = new ArrayList<>();
                List<MeasurementUnits> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(MeasurementUnits.class);
                for (int i = 0; i < dataList.size(); i++) {
                    MeasurementUnits baseicEnumVo = dataList.get(i);
                    //if (basicEnumService.getById(baseicEnumVo.getId()) == null){ //表示新增
                    BasicEnum baseicEnum = new BasicEnum();
                    MeasurementUnits basicEnum = new MeasurementUnits();
                    baseicEnum.setEnumId(baseicEnumVo.getCode());//使用计量单位编号
                    baseicEnum.setEnumCode(baseicEnumVo.getCode());
                    baseicEnum.setCrearer(baseicEnumVo.getCreateUserId());
                    baseicEnum.setEnumName(baseicEnumVo.getName());
                    baseicEnum.setUpdatetime(baseicEnumVo.getModifiedTime());
                    baseicEnum.setParentId("95274");
                    baseicEnum.setDelFlag(1);
                    baseicEnum.setCreateDate(baseicEnumVo.getCreatedTime());
                    saveJldw.add(baseicEnum);
                    // }
                }

                if (!saveJldw.isEmpty() && !basicEnumService.saveOrUpdateBatch(saveJldw)) { //保存失败
                    returnJSON.put("message", "更新计量单位");
                    return returnJSON;
                }
            }
        return returnJSON;
    }
    @Override
    public JSONObject savetpz(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_ = "lcapPatrol.CultivatedVarieties";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic非品种信息业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            BasicEnum byId = basicEnumService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =basicEnumService.removeById(dataInfo);
                    if(remove){
                        returnJSON.put("message", "删除品种成功");
                    }else{
                        returnJSON.put("message", "删除品种失败");
                    }
            }else {
                returnJSON.put("message", "未查询到品种数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<BasicEnum> savePz = new ArrayList<>();
            List<CultivatedVarieties> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(CultivatedVarieties.class);
            for (int i = 0; i < dataList.size(); i++) {
                CultivatedVarieties baseicEnumVo = dataList.get(i);
                //if (basicEnumService.getById(baseicEnumVo.getId()) == null){ //表示新增
                BasicEnum baseicEnum = new BasicEnum();
                baseicEnum.setEnumId(baseicEnumVo.getPzbm());//使用品种编号
                baseicEnum.setEnumCode(baseicEnumVo.getPzbm());
                baseicEnum.setCrearer(baseicEnumVo.getCreateUserId());
                baseicEnum.setEnumName(baseicEnumVo.getName());
                baseicEnum.setUpdatetime(baseicEnumVo.getModifiedTime());
                baseicEnum.setParentId("95272");
                if ("true".equals(baseicEnumVo.getQyzt())) {
                    baseicEnum.setDelFlag(1);
                } else {
                    baseicEnum.setDelFlag(2);
                }
                baseicEnum.setCreateDate(baseicEnumVo.getCreatedTime());
                savePz.add(baseicEnum);
                // }
            }

            if (!savePz.isEmpty() && !basicEnumService.saveOrUpdateBatch(savePz)) { //保存失败
                returnJSON.put("message", "更新品种");
                return returnJSON;
            }
        }
        return returnJSON;
    }

    @Override
    public JSONObject savetpp(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_ = "lcapPatrol.BrandZpt";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic品牌信息业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            BasicEnum byId = basicEnumService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =basicEnumService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除品牌成功");
                }else{
                    returnJSON.put("message", "删除品牌失败");
                }
            }else {
                returnJSON.put("message", "未查询到品牌数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<BasicEnum> savePp = new ArrayList<>();
            List<BrandZpt> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(BrandZpt.class);
            for (int i = 0; i < dataList.size(); i++) {
                BrandZpt baseicEnumVo = dataList.get(i);
                // if (basicEnumService.getById(baseicEnumVo.getId()) == null){ //表示新增
                BasicEnum baseicEnum = new BasicEnum();
                MeasurementUnits basicEnum = new MeasurementUnits();
                baseicEnum.setEnumId(baseicEnumVo.getPpbm()); //使用品牌编号
                baseicEnum.setEnumCode(baseicEnumVo.getPpbm()); //使用品牌编号
                baseicEnum.setCrearer(baseicEnumVo.getCreateUserId());
                baseicEnum.setEnumName(baseicEnumVo.getName());
                baseicEnum.setUpdatetime(baseicEnumVo.getModifiedTime());
                baseicEnum.setParentId("95271");
                if ("true".equals(baseicEnumVo.getQyzt())) {
                    baseicEnum.setDelFlag(1);
                } else {
                    baseicEnum.setDelFlag(2);
                }

                baseicEnum.setCreateDate(baseicEnumVo.getCreatedTime());
                savePp.add(baseicEnum);

                //}
            }

            if (!savePp.isEmpty() && !basicEnumService.saveOrUpdateBatch(savePp)) { //保存失败
                returnJSON.put("message", "更新品牌");
                return returnJSON;
            }
        }
        return returnJSON;
    }

    @Override
    public JSONObject savetsdwxx(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_ = "lcapPatrol.SupplyStationSpt";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic市站点信息业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            OrgInfo byId = orgInfoService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =orgInfoService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除单位成功");
                }else{
                    returnJSON.put("message", "删除单位失败");
                }
            }else {
                returnJSON.put("message", "未查询到单位数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<OrgInfo> saveSdwxx = new ArrayList<>();
            List<OrginfoDdmVo> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(OrginfoDdmVo.class);
            for (int i = 0; i < dataList.size(); i++) {
                OrginfoDdmVo orginfoDdmVo = dataList.get(i);
                OrgInfo orgInfo = new OrgInfo();
                orgInfo.setOrgId(orginfoDdmVo.getUnitID());//单位id
                orgInfo.setOrgName(orginfoDdmVo.getName());//名称
                orgInfo.setShortName(orginfoDdmVo.getShortName());//简称
                orgInfo.setOrgCode(orginfoDdmVo.getOrgCode());//组织编码
                orgInfo.setOrgClassId("5317");//组织类别
                orgInfo.setOrgClassIdName(orginfoDdmVo.getCompanyNature());//组织类别名称
                orgInfo.setCompanyNature(orginfoDdmVo.getCompanyNature());//企业性质
                orgInfo.setBusiType(orginfoDdmVo.getMainFunctions());//业务类型
                orgInfo.setAddress(orginfoDdmVo.getAddress());//地址
                orgInfo.setContact(orginfoDdmVo.getContactPhone());//电话
                //orgInfo.setFax()//传真
                orgInfo.setChargePerson(orginfoDdmVo.getContacts());//联系人
                orgInfo.setLongitude(orginfoDdmVo.getLongitude());//经度
                orgInfo.setLatitude(orginfoDdmVo.getLatitude());//纬度
                //orgInfo.setRegistCapital();//注册资本
                //orgInfo.setWorkNumber()//在职人数
                orgInfo.setAreaCode(Integer.valueOf(orginfoDdmVo.getAreaCode()));//行政区代码
                orgInfo.setAreaName(orginfoDdmVo.getAreaName());//行政区划名称
                orgInfo.setPost(Integer.valueOf(orginfoDdmVo.getPost()));//邮编
                orgInfo.setDepotClass(337);//库点类别
                orgInfo.setCompleteDate(orginfoDdmVo.getFoundingTime());//建成日期
                orgInfo.setUseDate(orginfoDdmVo.getCorporationTime());//启用日期
                orgInfo.setCompanyOrgCode(orginfoDdmVo.getCreditCode());//组织机构代码
                orgInfo.setCreditCode(orginfoDdmVo.getCreditCode());//社会信用代码
                orgInfo.setRegistDate(orginfoDdmVo.getFoundingTime());//注册日期
                orgInfo.setParentId("0");//父级ID
                orgInfo.setMailBox(orginfoDdmVo.getMailBox());//邮箱
                orgInfo.setOfficialWebsite(orginfoDdmVo.getOfficePhone());//官网地址
                orgInfo.setSubset(Boolean.TRUE);//是否有子集单位
                orgInfo.setLegalPerson(orginfoDdmVo.getContacts());//法人
                orgInfo.setOfficePhone(orginfoDdmVo.getContactPhone());//联系电话
                orgInfo.setLevelCode("0"); //级次
                saveSdwxx.add(orgInfo);

                //}
            }

            if (!saveSdwxx.isEmpty() && !orgInfoService.saveOrUpdateBatch(saveSdwxx)) { //保存失败
                returnJSON.put("message", "更新市站点信息");
                return returnJSON;
            }
        }
        return returnJSON;
    }

    @Override
    public JSONObject savetzdwxx(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_ = "lcapPatrol.SupplyStationZpt";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic市站点信息业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            OrgInfo byId = orgInfoService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =orgInfoService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除单位成功");
                }else{
                    returnJSON.put("message", "删除单位失败");
                }
            }else {
                returnJSON.put("message", "未查询到单位数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<OrgInfo> saveSdwxx = new ArrayList<>();
            List<OrginfoDdmVo> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(OrginfoDdmVo.class);
            for (int i = 0; i < dataList.size(); i++) {
                OrginfoDdmVo orginfoDdmVo = dataList.get(i);
                OrgInfo orgInfo = new OrgInfo();
                orgInfo.setOrgId(orginfoDdmVo.getUnitID());//单位id
                orgInfo.setOrgName(orginfoDdmVo.getName());//名称
                orgInfo.setShortName(orginfoDdmVo.getShortName());//简称
                orgInfo.setOrgCode(orginfoDdmVo.getOrgCode());//组织编码
                orgInfo.setOrgClassId("5318");//组织类别
                orgInfo.setCompanyNature(orginfoDdmVo.getCompanyNature());//企业性质
                orgInfo.setBusiType(orginfoDdmVo.getMainFunctions());//业务类型
                orgInfo.setAddress(orginfoDdmVo.getAddress());//地址
                orgInfo.setContact(orginfoDdmVo.getContactPhone());//电话
                //orgInfo.setFax()//传真
                orgInfo.setChargePerson(orginfoDdmVo.getContacts());//联系人
                orgInfo.setLongitude(orginfoDdmVo.getLongitude());//经度
                orgInfo.setLatitude(orginfoDdmVo.getLatitude());//纬度
                //orgInfo.setRegistCapital();//注册资本
                //orgInfo.setWorkNumber()//在职人数
                orgInfo.setAreaCode(Integer.valueOf(orginfoDdmVo.getAreaCode()));//行政区代码
                orgInfo.setAreaName(orginfoDdmVo.getAreaName());//行政区划名称
                orgInfo.setPost(Integer.valueOf(orginfoDdmVo.getPost()));//邮编
                orgInfo.setDepotClass(337);//库点类别
                orgInfo.setCompleteDate(orginfoDdmVo.getFoundingTime());//建成日期
                orgInfo.setUseDate(orginfoDdmVo.getCorporationTime());//启用日期
                orgInfo.setCompanyOrgCode(orginfoDdmVo.getCreditCode());//组织机构代码
                orgInfo.setCreditCode(orginfoDdmVo.getCreditCode());//社会信用代码
                orgInfo.setRegistDate(orginfoDdmVo.getFoundingTime());//注册日期
                orgInfo.setParentId("00");//父级ID
                orgInfo.setMailBox(orginfoDdmVo.getMailBox());//邮箱
                orgInfo.setOfficialWebsite(orginfoDdmVo.getOfficePhone());//官网地址
                orgInfo.setSubset(Boolean.FALSE);//是否有子集单位
                orgInfo.setLegalPerson(orginfoDdmVo.getContacts());//法人
                orgInfo.setOfficePhone(orginfoDdmVo.getContactPhone());//联系电话
                saveSdwxx.add(orgInfo);

            }
            if (!saveSdwxx.isEmpty() && !orgInfoService.saveOrUpdateBatch(saveSdwxx)) { //保存失败
                returnJSON.put("message", "更新站点信息");
                return returnJSON;
            }
        }
        return returnJSON;
    }


    /**
     * 接收ddm原粮通知单
     *
     * @param object
     * @param routingKey
     * @return
     */
    @Override
    public JSONObject savetYlTzd(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_ = "lcapPatrol.WarehousingNotice,lcapPatrol.OutboundNotice";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic非原粮通知单业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            DataCrkywTzdxxDefault byId = dataCrkywTzdxxDefaultService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =dataCrkywTzdxxDefaultService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除原粮通知单成功");
                }else{
                    returnJSON.put("message", "删除原粮通知单失败");
                }
            }else {
                returnJSON.put("message", "未查询到原粮通知单数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<DataCrkywTzdxxDefault> saveTzd = new ArrayList<>();
            List<DataCrkywTzdxxDefault> tzdList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(DataCrkywTzdxxDefault.class);
            for (int i = 0; i < tzdList.size(); i++) {
                DataCrkywTzdxxDefault tzd = tzdList.get(i);
                if (dataCrkywTzdxxDefaultService.getById(tzd.getDataID()) == null) { //表示未新增
                    String zylx = tzd.getCczyfl();
                    tzd.setSytelInfoID("2c9180856f7a0b21016f7e7c54e21cc1"); //样式id
                    if (routingKey.equals("lcapPatrol.WarehousingNotice")) { //粮油入库
                        tzd.setFlowId("A7B1FDB54DE7B32D6F8067FEFE2BF52F"); //流程Id
                    }
                    if (routingKey.equals("lcapPatrol.OutboundNotice")) { //粮油出库
                        tzd.setFlowId("6FD0418CD28A2B3F3E7E9EC0D7429461"); //流程Id
                    }
                    tzd.setWcsl(0.0); //完成数量
                    tzd.setLsxz("6868"); // 民营企业商品粮,默认
                    tzd.setSysl(tzd.getSl());
                    tzd.setFlowStatus(1); //流程状态
                    String unitID = tzd.getUnitID();
                    tzd.setPeriodID("11");
                    tzd.setDeptID(unitID); //部门id
                    tzd.setUnitID(unitID); //
                    if (StrUtil.isBlank(tzd.getBuilder())) {
                        tzd.setBuilder(unitID); //
                        tzd.setEditor(unitID); //
                    } else {
                        tzd.setKdr(tzd.getBuilder());
                        tzd.setEditor(tzd.getBuilder());
                    }
                    tzd.setRoundID(0); //舍位id
                    tzd.setKdsj(new Date());
//                    tzd.setjy("3146");// 检验结果
//                    tzd.setJsfs("2c998b8c668013d701668512c12a0094"); //结算方式
                    if (StringUtils.isBlank(tzd.getRukushijian()))
                        tzd.setRukushijian(DateUtil.now());
//                    if (tzd.getFrrq() == null)
//                        tzd.setFrrq(DateUtil.date());
                    if (StringUtils.isBlank(tzd.getZxbm()))
                        tzd.setZxbm(tzd.getKdr());
                    tzd.setFlowStatus(1); //流程状态

                    saveTzd.add(tzd); //添加主表数据
                }
            }
            if (!saveTzd.isEmpty() && !dataCrkywTzdxxDefaultService.saveOrUpdateBatch(saveTzd)) { //保存失败
                returnJSON.put("message", "更新原粮出入库数据失败");
                return returnJSON;
            }
        }
        return returnJSON;
    }

    @Override
    public JSONObject savesp(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_ = "lcapPatrol.Materiel";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic商品业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            DataCrkywCplspDefault byId = dataCrkywCplspDefaultService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =dataCrkywCplspDefaultService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除商品成功");
                }else{
                    returnJSON.put("message", "删除商品失败");
                }
            }else {
                returnJSON.put("message", "未查询到商品数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<DataCrkywCplspDefault> saveSp = new ArrayList<>();
            List<MaterielVo> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(MaterielVo.class);
            for (int i = 0; i < dataList.size(); i++) {
                MaterielVo materielVo = dataList.get(i);
                // if (basicEnumService.getById(baseicEnumVo.getId()) == null){ //表示新增
                DataCrkywCplspDefault sp = new DataCrkywCplspDefault();
                sp.setDataId(materielVo.getId());//id
                sp.setUnitid(materielVo.getStation());//站点id
                sp.setDeptId(materielVo.getStation());//站点id
                sp.setBuilder(materielVo.getCreateUserId());//创建人
                sp.setCjsj(materielVo.getCreatedTime());//创建时间
                sp.setEditor(materielVo.getModifyUserId());//编辑人
                sp.setModifyDate(materielVo.getModifiedTime());//修改时间
                sp.setBzq(materielVo.getGuaranteePeriod());//保质期
                sp.setSjjlpzbh(materielVo.getCultivatedVarieties());//品种编号
                sp.setSjjlppbh(materielVo.getMaterielBrand());//品牌编号
                sp.setSjjlspmc(materielVo.getName()); //商品名称
                sp.setSjjlspmcbh(materielVo.getMaterielNum()); //商品编号
                sp.setDj(materielVo.getMaterielLevel());//商品等级
                sp.setGg(materielVo.getMaterielModel());
                sp.setJldw(materielVo.getMaterielUnit());
                sp.setKcyjs(materielVo.getMaxSafetyInventory()); //库存预警-下限
                sp.setKcyjx(materielVo.getMinSafetyInventory()); //库存预警-上限
                sp.setTqyjts(materielVo.getEarlyWarningDays()); //提前预警天数
                //设置是否启用
                sp.setIsEnable(StrUtil.equals("Y", materielVo.getAvailable()) ? "1" : "0");
                sp.setTccg("0"); //默认为非统筹采购
                if (StrUtil.equals("00", materielVo.getStation())) { //顶级单位
                    sp.setTccg("1"); //统筹采购
                }
/*            sp.setTqyjts(materielVo.get);
            //sp.setSccs();
            //sp.setSccsbh();
            sp.setDj(materielVo.getMaterielLevel());
            sp.setTccg();
            sp.setZgcs();
            sp.setZgcw();*/
                saveSp.add(sp);
            }
            if (!saveSp.isEmpty() && !dataCrkywCplspDefaultService.saveOrUpdateBatch(saveSp)) { //保存失败
                returnJSON.put("message", "更新商品");
                return returnJSON;
            }
        }
        return returnJSON;
    }

    @Override
    public JSONObject saveCf(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_ = "lcapPatrol.WareHouse";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic仓房信息业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
//        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            BasicStorehouse byId = basicStorehouseService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =basicStorehouseService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除仓房成功");
                }else{
                    returnJSON.put("message", "删除仓房失败");
                }
            }else {
                returnJSON.put("message", "未查询到仓房数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<BasicStorehouse> saveCf = new ArrayList<>();
            List<BasicWarehouse> saveHw = new ArrayList<>();
            List<WareHouseDdmVo> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(WareHouseDdmVo.class);
            for (int i = 0; i < dataList.size(); i++) {
                WareHouseDdmVo next = dataList.get(i);
                BasicStorehouse basicStorehouse = new BasicStorehouse();
                basicStorehouse.setStorehouseId(next.getStorehouseId());
                basicStorehouse.setDepotId(next.getOrgId());
                basicStorehouse.setOrgId(next.getOrgId());
                basicStorehouse.setStorehouseCode(next.getStorehouseCode());
                basicStorehouse.setStorehouseName(next.getStorehouseName());
                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(next.getNfszcc());
                basicStorehouse.setYwfsfqfczz(next.getYwfsfqfczz());
                basicStorehouse.setYwfhfbfdss(next.getYwfhfbfdss());
                basicStorehouse.setYwlqjcss(next.getYwlqjcss());
                basicStorehouse.setYwjxtfss(next.getYwjxtfss());
                basicStorehouse.setNfhlxzsc(next.getNfhlxzsc());
                basicStorehouse.setNffddyqtcl(next.getNfhlxzsc());
                basicStorehouse.setNfcwcc(next.getNfcwcc());
                basicStorehouse.setActualCapacity(next.getActualCapacity()); //实际仓容(t)
                // getUseDate.setCompletionDate(next.getC)建成日期
                // Date useDate = DateUtil.parse(StrUtil.isNotBlank(next.getUseDate())?next.getUseDate():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
                basicStorehouse.setUseDate(next.getUseDate());
                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.getCreateName());
                //     Date createdTime = DateUtil.parse(StrUtil.isNotBlank(next.getCreatedTime())?next.getCreatedTime():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
                basicStorehouse.setCreateDate(next.getCreatedTime());
                //   Date modifiedTime = DateUtil.parse(StrUtil.isNotBlank(next.getModifiedTime())?next.getModifiedTime():DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
                basicStorehouse.setUpdatetime(next.getModifiedTime());
                basicStorehouse.setUpdateName(next.getCreateName());
                // basicStorehouse.setWorkingStatus(next.getW)工作状态
                basicStorehouse.setDelFlag(next.getDelFlag() != null ? next.getDelFlag() : 1);
                basicStorehouse.setLibraryType(judge(next.getLibraryType()));
                basicStorehouse.setCclx(next.getCclx());

                //basicStorehouse.setHwbm(next.getHW);货位编码
                //得到油罐信息
                String type = next.getType(); //类型
                basicStorehouse.setType(type);
                if (StringUtils.equals("02", type)) { //表示是油罐
                    basicStorehouse.setStorehouseId(next.getStorehouseId());
                    basicStorehouse.setDepotId(next.getOrgId());
                    basicStorehouse.setOrgId(next.getOrgId());
                    basicStorehouse.setStorehouseCode(next.getStorehouseCode());
                    basicStorehouse.setStorehouseName(next.getStorehouseName());
                    basicStorehouse.setOutOilHeight(next.getOutOilHeight()); //出油口高度(m)
                    basicStorehouse.setDesignedStorageHeight(next.getDesignedStorageHeight()); //设计储油高度(m)
                    basicStorehouse.setBottomArea(next.getBottomArea()); // 底面积(㎡)
                    basicStorehouse.setTankStatus(next.getTankStatus()); // 油罐状态
                    basicStorehouse.setWeldingMethod(next.getWeldingMethod()); //焊接方式
                    basicStorehouse.setVerificationMethod(next.getVerificationMethod()); //检定方式
                    basicStorehouse.setHeigth(next.getHeigth()); //高度(m)
                    basicStorehouse.setDiameter(next.getDiameter()); //直径(m)
                    basicStorehouse.setYwjrzz(next.getYwjrzz()); //有无加热装置
                    basicStorehouse.setTankType(next.getTankType()); //油罐类型
                    basicStorehouse.setFssssfwh(next.getFssssfwh()); //附属设施是否完好
                    basicStorehouse.setActualCapacity(next.getActualCapacity()); //实际仓容(t)
                    //生成货位数据
                    BasicWarehouse basicWarehouse = new BasicWarehouse();
                    basicWarehouse.setDepotId(next.getOrgId());//部门ID
                    basicWarehouse.setWarehouseId(next.getStorehouseId());//货位id
                    basicWarehouse.setStorehouseId(next.getStorehouseId());//仓房ID
                    basicWarehouse.setCreateDate(next.getCreateDate());//创建时间
                    basicWarehouse.setWarehouseCode(next.getStorehouseCode());//货位编码
                    basicWarehouse.setWarehouseName(next.getStorehouseName());//货位名称
                    basicWarehouse.setDutyStoreman(next.getRemark());//备注
                    basicWarehouse.setDepotName(next.getDepotName());
                    basicWarehouse.setCrkStatus("0");
                    basicWarehouse.setContactNumber(null);
                    basicWarehouse.setDutyStoreman(next.getCreateUserId());
                    basicWarehouse.setWarehouseUseTime(next.getCreateDate());
                    basicWarehouse.setStoreWay(judge(next.getStoreWay()));
                    basicWarehouse.setWidth(next.getWidth());
                    basicWarehouse.setHeigth(next.getHeigth());
                    basicWarehouse.setHwbm(next.getHwbm());
                    basicWarehouse.setDelFlag(next.getDelFlag() != null ? next.getDelFlag() : 1);
                    basicWarehouse.setLibraryType(next.getLibraryType());
                    basicWarehouse.setCreateName(next.getCreateUserId());
                    basicWarehouse.setOrgId(next.getOrgId());
                    basicWarehouse.setUpdateName(next.getModifyUserId());
                    basicWarehouse.setWarehouseCapacity(next.getActualCapacity());
                    basicWarehouse.setWarehouseStatus(next.getTankStatus());
                    basicWarehouse.setLength(next.getLength());
                    saveHw.add(basicWarehouse);
                }
                saveCf.add(basicStorehouse);

            }

            if (!saveCf.isEmpty() && !basicStorehouseService.saveOrUpdateBatch(saveCf)) { //保存失败
                returnJSON.put("message", "更新仓房");
                return returnJSON;
            }
            if (!saveHw.isEmpty() && !basicWarehouseService.saveOrUpdateBatch(saveHw)) { //保存失败
                returnJSON.put("message", "更新货位");
                return returnJSON;
            }
        }

        return returnJSON;

    }

    @Override
    public JSONObject saveHw(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_ = "lcapPatrol.goodsAllocation";
        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic货位信息业务");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            BasicWarehouse byId = basicWarehouseService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =basicWarehouseService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除货位成功");
                }else{
                    returnJSON.put("message", "删除货位失败");
                }
            }else {
                returnJSON.put("message", "未查询到货位数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<BasicWarehouse> saveHw = new ArrayList<>();
            List<GoodsAllocationDdmVo> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(GoodsAllocationDdmVo.class);
            for (int i = 0; i < dataList.size(); i++) {
                GoodsAllocationDdmVo next = dataList.get(i);
                BasicWarehouse basicWarehouse = new BasicWarehouse();
                basicWarehouse.setDepotId(next.getUnitId());//部门ID
                basicWarehouse.setStorehouseId(next.getStorehouseId());//仓房ID
                basicWarehouse.setCreateDate(next.getCreateDate());//创建时间
                basicWarehouse.setWarehouseCode(next.getWarehouseCode());//货位编码
                basicWarehouse.setWarehouseName(next.getName());//货位名称
                basicWarehouse.setDutyStoreman(next.getRemark());//备注
                basicWarehouse.setDepotName(next.getDepotName());
                basicWarehouse.setCrkStatus(judge(next.getCrkStatus()));
                basicWarehouse.setContactNumber(next.getContactNumber() != null ? judge(next.getContactNumber()) : null);
                basicWarehouse.setDutyStoreman(next.getCreateUserId());
                basicWarehouse.setWarehouseUseTime(next.getWarehouseUseTime());
                basicWarehouse.setStoreWay(judge(next.getStoreWay()));
                basicWarehouse.setWidth(next.getWidth());
                basicWarehouse.setHeigth(next.getHeigth());
                basicWarehouse.setHwbm(next.getHwbm());
                basicWarehouse.setDelFlag(next.getDelFlag() != null ? next.getDelFlag() : 1);
                basicWarehouse.setLibraryType(next.getLibraryType());
                basicWarehouse.setCreateName(next.getCreateUserId());
                basicWarehouse.setOrgId(next.getUnitId());
                basicWarehouse.setUpdateName(next.getModifyUserId());
                basicWarehouse.setWarehouseCapacity(next.getWarehouseCapacity());
                basicWarehouse.setWarehouseStatus(next.getWarehouseStatus());
                basicWarehouse.setLength(next.getLength());
                basicWarehouse.setWarehouseId(next.getId());
                saveHw.add(basicWarehouse);
            }

            if (!saveHw.isEmpty() && !basicWarehouseService.saveOrUpdateBatch(saveHw)) { //保存失败
                returnJSON.put("message", "更新货位");
                return returnJSON;
            }
        }
        return returnJSON;

    }
    /**
     * 接收用户中心用户中心
     *
     * @param jsonObject
     * @return
     */
    @Override
    public JSONObject downUserInfos(JSONObject jsonObject) {
        JSONObject returnJson = new JSONObject();
        returnJson.put("suceess","ture");
        returnJson.put("message","保存成功");
        System.out.println(jsonObject);
        return returnJson;
    }

    /**
     * 生产加工结果，成品粮库存变动，原粮库存变动
     * @param object
     * @param routingKey
     * @return
     */
    @Override
    public JSONObject saveScjg(String object, String routingKey) throws Exception {
        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_ = "lcapPatrol.ProductionResult";
/*        if (StringUtils.indexOf(routerKey_,routingKey) < 0 ){
            returnJSON.put("message","topic生产加工");
            return  returnJSON;
        }*/
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        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<ProductionResultVo> pro =new ArrayList<ProductionResultVo>();
        List<RawMaterialResultVo> maw =new ArrayList<RawMaterialResultVo>();
        for (Map<String, Object> s:list) {
            String opt = s.get("opt").toString();
            String table = s.get("table").toString();
            if("DATA_CP_PRODUCTIONRESULT".equals(table)){
                if("del".equals(opt)){
                    String dataInfo = s.get("dataInfo").toString();
                    DataKcglCplkcbdDefault byId = dataKcglCplkcbdDefaultService.getById(dataInfo);
                    cn.hutool.json.JSONObject jsonObject = dataKcglCplkcbdDefaultService.deleteKcDb(dataInfo);
                    if(Optional.ofNullable(byId).isPresent()){
                        cn.hutool.json.JSONObject jsonObject1 = dataKcglCplkcbdDefaultService.deleteKcDb(dataInfo);
                        if("删除成功".equals(jsonObject1.get("msg"))){
                            returnJSON.put("message", "删除生产加工库存变动成功");
                        }else{
                            returnJSON.put("message", "删除生产加工库存变动失败");
                        }
                        QueryWrapper<DataCrkywCplrkFlt001> Wrapper = new QueryWrapper<>();
                        Wrapper.eq("bz",dataInfo);
                        dataCrkywCplrkFlt001Service.remove(Wrapper);
                    }else {
                        returnJSON.put("message", "未查询到库存数据:"+dataInfo);
                    }
                }else{
                    String dataInfo = s.get("dataInfo").toString();
                    List<ProductionResultVo> productionResultVos = JSONArray.parseArray(dataInfo).toJavaList(ProductionResultVo.class);
                    for (ProductionResultVo pr:productionResultVos) {
                        pro.add(pr);
                    }
                }
            }else if("DATA_CP_RAWMATERIALRESULT".equals(table)){
                if("del".equals(opt)){
                    String dataInfo = s.get("dataInfo").toString();
                    DataKcglKcbdDefault byId = dataKcglKcbdDefaultService.getById(dataInfo);
                    if(Optional.ofNullable(byId).isPresent()){
                    cn.hutool.json.JSONObject delete = dataKcglKcbdDefaultService.delete(dataInfo);
                                if("删除成功".equals(delete.get("msg"))){
                                    returnJSON.put("message", "删除生产加工库存变动成功");
                                }else{
                                    returnJSON.put("message", "删除生产加工库存变动失败");
                                }
                         }else {
                            returnJSON.put("message", "未查询到库存数据:"+dataInfo);
                                }

                }else{
                    String dataInfo = s.get("dataInfo").toString();
                    List<RawMaterialResultVo> rawMaterialResultVos = JSONArray.parseArray(dataInfo).toJavaList(RawMaterialResultVo.class);
                    for (RawMaterialResultVo raw:rawMaterialResultVos) {
                        maw.add(raw);
                    }

                }
            }

        }



        System.out.println(datas);
            List<DataKcglCplkcbdDefault> cplkcbd = new ArrayList<>();
            List<DataKcglKcbdDefault> kcbd = new ArrayList<>();
        List<DataCrkywCplrkFlt001> rkflt001 = new ArrayList<>();
            if(pro!=null && pro.size()>0){
           /* List<ProductionResultVo> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(ProductionResultVo.class);
            List<RawMaterialResultVo> MaterialList = datas.getJSONObject(1).getJSONArray("dataInfo").toJavaList(RawMaterialResultVo.class);*/
                for (int i = 0; i < pro.size(); i++) {
                    DataKcglCplkcbdDefault dataKcglCplkcbdDefault = new DataKcglCplkcbdDefault();
                    ProductionResultVo next = pro.get(i);
                    String dataid = next.getId();
                    String unitid = next.getStation();
                    dataKcglCplkcbdDefault.setDataId(dataid);
                    dataKcglCplkcbdDefault.setBdfs("16");
                    dataKcglCplkcbdDefault.setUnitid(unitid);
                    dataKcglCplkcbdDefault.setCh(next.getWareHouse());
                    dataKcglCplkcbdDefault.setHwh(next.getGoodsAllocation());
                    dataKcglCplkcbdDefault.setBuilder(next.getCreateUserId());
                    dataKcglCplkcbdDefault.setEditor(next.getModifyUserId());
                    dataKcglCplkcbdDefault.setModifyDate(next.getModifiedTime());
                    dataKcglCplkcbdDefault.setBdhjs(next.getQuantity()+next.getInventoryQuantity()); //变动后件数
                    dataKcglCplkcbdDefault.setBdhsl((next.getInventoryQuantity() + next.getQuantity()) * Double.parseDouble(next.getMaterielModel()));//变动后件数
                    dataKcglCplkcbdDefault.setBdhxz("952751");
                    //获取商品信息
                    ArrayList<String> strings = new ArrayList<>();
                    strings.add(next.getMaterielNum());
                    List<DataCrkywCplspDefault> bySpbhs = dataCrkywCplspDefaultService.getBySpbhs(strings);
                    if(bySpbhs !=null && bySpbhs.size()>0) {
                        DataCrkywCplspDefault dataCrkywCplspDefault = bySpbhs.get(0);
                        if (dataCrkywCplspDefault.getDj() != null) {
                        dataKcglCplkcbdDefault.setDj(dataCrkywCplspDefault.getDj());
                        dataKcglCplkcbdDefault.setBdje(Double.parseDouble(dataCrkywCplspDefault.getDj()) * next.getQuantity());
                        }
                    }
                    dataKcglCplkcbdDefault.setBdqjs(next.getInventoryQuantity());//变动件数
                    dataKcglCplkcbdDefault.setBdjs(next.getQuantity());
                    dataKcglCplkcbdDefault.setBdqsl(next.getInventoryQuantity()*Double.parseDouble(next.getMaterielModel()));//变动前数量
                    dataKcglCplkcbdDefault.setBdsl(next.getWeight());
                    dataKcglCplkcbdDefault.setBzq(next.getGuaranteePeriod());
                    dataKcglCplkcbdDefault.setGg(next.getMaterielModel());
                    dataKcglCplkcbdDefault.setJldw(next.getMaterielUnit());
                    dataKcglCplkcbdDefault.setScpc(next.getBatch());
                    dataKcglCplkcbdDefault.setSjjlppbh(next.getMaterielBrand());
                    dataKcglCplkcbdDefault.setSjjlpzbh(next.getCultivatedVarieties());
                    dataKcglCplkcbdDefault.setSjjlspmc(next.getMateriel());
                    dataKcglCplkcbdDefault.setSjjlspmcbh(next.getMaterielNum());
                    dataKcglCplkcbdDefault.setBz("成品粮生产加工入库");
                    dataKcglCplkcbdDefault.setScrq(next.getManufactureDate());
                    dataKcglCplkcbdDefault.setXz("952751");
                    dataKcglCplkcbdDefault.setYwsj(next.getCreatedTime());
                    dataKcglCplkcbdDefault.setBddh(next.getName());
                    dataKcglCplkcbdDefault.setBdhxz("952751");

                    cplkcbd.add(dataKcglCplkcbdDefault);
                    //新增修改时先删除
                    QueryWrapper<DataCrkywCplrkFlt001> Wrapper = new QueryWrapper<>();
                    Wrapper.eq("bz",dataid);
                    dataCrkywCplrkFlt001Service.remove(Wrapper);

                    //同时生成入库子表
                    DataCrkywCplrkFlt001 dataCrkywCplrkFlt001 = new DataCrkywCplrkFlt001();
                    dataCrkywCplrkFlt001.setMainDataId(next.getName()); //主数据id
                    SysParams byId = sysParamsService.getById("sy.url");
                    String paramVal = byId.getParamVal();
                    String syurl=paramVal+"/"+unitid+"/"+next.getWareHouse()+"/"+next.getGoodsAllocation()+"/"+next.getMaterielNum()+"/"+next.getName()+"/"+next.getBatch();
                    dataCrkywCplrkFlt001.setSyurl(syurl);//溯源地址
                    dataCrkywCplrkFlt001.setJldw(next.getMaterielUnit()); //计量单位
                    dataCrkywCplrkFlt001.setGg(next.getMaterielModel()); //规格
                    dataCrkywCplrkFlt001.setJs(next.getQuantity()); //件数
                    dataCrkywCplrkFlt001.setSjjlppbh(next.getMaterielBrand());//品牌
                    dataCrkywCplrkFlt001.setSjjlpzbh(next.getCultivatedVarieties());//品种
                    dataCrkywCplrkFlt001.setSjjlspmc(next.getMateriel());//商品名称
                    dataCrkywCplrkFlt001.setBzq(next.getGuaranteePeriod());//保质期
                    dataCrkywCplrkFlt001.setScrq(next.getManufactureDate());//生产日期
                    dataCrkywCplrkFlt001.setScpc(next.getBatch());//生产批次
                    dataCrkywCplrkFlt001.setCjsj(next.getCreatedTime());//创建时间
                    dataCrkywCplrkFlt001.setBz(dataid);
                    rkflt001.add(dataCrkywCplrkFlt001);
                    if(maw!=null && maw.size()>0){
                        for (int K = 0; K < maw.size(); K++) {
                            RawMaterialResultVo next1 = maw.get(K);
                            DataKcglKcbdDefault dataKcglKcbdDefault = new DataKcglKcbdDefault();
                            if (dataid.equals(next1.getProductionResult())) {
                                dataKcglKcbdDefault.setDataID(next1.getId());
                                dataKcglKcbdDefault.setUnitID(unitid);
                                dataKcglKcbdDefault.setPeriodID("4028d0f25fb8ee9a015fbd6f770909ba");
                                dataKcglKcbdDefault.setRoundID(0);
                                dataKcglKcbdDefault.setBuilder(next.getCreateUserId());
                                dataKcglKcbdDefault.setBuildDate(next.getCreatedTime());
                                dataKcglKcbdDefault.setEditor(next.getModifyUserId());
                                dataKcglKcbdDefault.setDeptID(unitid);
                                dataKcglKcbdDefault.setSytelInfoID("2c90808b66fc54ea0166fcaa24840c9e");
                                dataKcglKcbdDefault.setBgy(next.getCreateUserId());

                                dataKcglKcbdDefault.setYrcjysl(0.0);
                                dataKcglKcbdDefault.setJysl(next1.getGrainsInventory() - next1.getActualWeigh());
                                dataKcglKcbdDefault.setYrcqcsl(0.0);
                                dataKcglKcbdDefault.setQcsl(next1.getGrainsInventory());
                                dataKcglKcbdDefault.setBdhxz("6868");
                                dataKcglKcbdDefault.setHwid(next1.getSupplyStation());
                                dataKcglKcbdDefault.setCfid(next1.getWareHouse());
                                dataKcglKcbdDefault.setBdsl(Double.parseDouble(next1.getActualWeigh()+""));
                                dataKcglKcbdDefault.setYwjssj(next1.getCreatedTime());
                                dataKcglKcbdDefault.setScrq(next1.getProductiveYear());
                                dataKcglKcbdDefault.setDj(next1.getGrainLevel());
                                dataKcglKcbdDefault.setPz(next1.getGrainVarieties());
                                dataKcglKcbdDefault.setBdfs("2c998b8c668013d70166852fa9ec00c2");
                                dataKcglKcbdDefault.setBddh(next.getName());
                                dataKcglKcbdDefault.setMxpz(next1.getDetailedVariety());
                                dataKcglKcbdDefault.setCd(next1.getProducingArea());
                                kcbd.add(dataKcglKcbdDefault);
                            } else {
                                continue;
                            }
                        }
                    }
                }
            }
            if (!cplkcbd.isEmpty() && !kcbd.isEmpty() && !rkflt001.isEmpty()) { //保存失败

                for (DataKcglKcbdDefault kc : kcbd) {
                    try {
                        this.dataKcglKcbdDefaultService.saveKcbd(kc);
                    } catch (Exception e) {
                        e.printStackTrace();
                        break;
                    }
                }
                try {
                    dataKcglCplkcbdDefaultService.saveList(cplkcbd);
                    dataCrkywCplrkFlt001Service.saveBatch(rkflt001);
                } catch (Exception e) {
                    e.printStackTrace();
                }



            }

        return returnJSON;
    }

    @Override
    public JSONObject saveYcr(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.TransformInGoodsDetail";
        if (StringUtils.indexOf(routerKey_1,routingKey)<0){
            returnJSON.put("message","topic性质转换");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            DataKcglCplkcbdDefault byId = dataKcglCplkcbdDefaultService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                cn.hutool.json.JSONObject jsonObject = null;
                try {
                    jsonObject = dataKcglCplkcbdDefaultService.deleteKcDb(dataInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String msg = jsonObject.get("msg").toString();
                if(msg.equals("删除成功")){
                    returnJSON.put("message", "删除成品粮移仓成功");
                }else{
                    returnJSON.put("message", "删除成品粮移仓失败");
                }
            }else {
                returnJSON.put("message", "未查询到成品粮移仓数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<DataKcglCplkcbdDefault> saveYc = new ArrayList<>();
            List<DataKcglCplkcbdDefault> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(DataKcglCplkcbdDefault.class);
            System.out.println(dataList.toString());

            for (int i = 0; i < dataList.size(); i++) {
                DataKcglCplkcbdDefault next = dataList.get(i);
                next.setXz("952751");
                next.setBdhxz("952751");
                next.setBdfs("14");
                System.out.println(next.toString());
                saveYc.add(next);
            }
            if (!saveYc.isEmpty()) {
                try {
                    dataKcglCplkcbdDefaultService.saveListDbs(saveYc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return returnJSON;
    }
    @Override
    public JSONObject saveYcc(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_2 = "lcapPatrol.TransformOutGoodsDetail";
        if (StringUtils.indexOf(routerKey_2,routingKey)<0){
            returnJSON.put("message","topic性质转换");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        System.out.println(datas);
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            DataKcglCplkcbdDefault byId = dataKcglCplkcbdDefaultService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                cn.hutool.json.JSONObject jsonObject = null;
                try {
                    jsonObject = dataKcglCplkcbdDefaultService.deleteKcDb(dataInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String msg = jsonObject.get("msg").toString();
                if(msg.equals("删除成功")){
                    returnJSON.put("message", "删除成品粮移仓成功");
                }else{
                    returnJSON.put("message", "删除成品粮移仓失败");
                }
            }else {
                returnJSON.put("message", "未查询到成品粮移仓数据:"+dataInfo);
            }
        } else {
            //处理数据
            List<DataKcglCplkcbdDefault> saveYc = new ArrayList<>();
            List<DataKcglCplkcbdDefault> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(DataKcglCplkcbdDefault.class);
            System.out.println(dataList.toString());

            for (int i = 0; i < dataList.size(); i++) {
                DataKcglCplkcbdDefault next = dataList.get(i);
                next.setXz("952751");
                next.setBdhxz("952751");
                next.setBdfs("14");
                System.out.println(next.toString());
                saveYc.add(next);
            }
            if (!saveYc.isEmpty()) {
                try {
                    dataKcglCplkcbdDefaultService.saveListDbs(saveYc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return returnJSON;
    }
// dataCrkywGysDefaultService; dataCrkywCplzjjgDefaultService
    @Override
    public JSONObject saveGys(String object, String routingKey) throws Exception {
        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.Supplier";
        if (StringUtils.indexOf(routerKey_1,routingKey)<0){
            returnJSON.put("message","非供应商数据");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            DataCrkywGysDefault byId = dataCrkywGysDefaultService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =dataCrkywGysDefaultService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除供应商成功");
                }else{
                    returnJSON.put("message", "删除供应商失败");
                }
            }else {
                returnJSON.put("message", "未查询到供应商数据:"+dataInfo);
            }
        } else {
//        System.out.println(datas);
            //处理数据
            List<DataCrkywGysDefault> saveGys = new ArrayList<>();
            List<DataCrkywGysDefault> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(DataCrkywGysDefault.class);
            System.out.println(dataList.toString());

            for (int i = 0; i < dataList.size(); i++) {
                DataCrkywGysDefault next = dataList.get(i);
                next.setStation(next.getUnitid());
                saveGys.add(next);
            }
            if (!saveGys.isEmpty()) {
                try {
                    dataCrkywGysDefaultService.saveOrUpdateBatch(saveGys);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return returnJSON;
    }

    @Override
    public JSONObject saveZjjg(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.QualityInspectionOrganization";
        if (StringUtils.indexOf(routerKey_1,routingKey)<0){
            returnJSON.put("message","非质检机构");
            return  returnJSON;
        }
        JSONObject josn = JSONObject.parseObject(object);
        JSONArray datas = JSONArray.parseArray(josn.getString("value"));
        String operationTyep = datas.getJSONObject(0).getString("opt");
        if ("del".equals(operationTyep)) {
            String dataInfo = datas.getJSONObject(0).getString("dataInfo");
            DataCrkywCplzjjgDefault byId = dataCrkywCplzjjgDefaultService.getById(dataInfo);
            if(Optional.ofNullable(byId).isPresent()){
                boolean remove =dataCrkywCplzjjgDefaultService.removeById(dataInfo);
                if(remove){
                    returnJSON.put("message", "删除质检机构成功");
                }else{
                    returnJSON.put("message", "删除质检机构失败");
                }
            }else {
                returnJSON.put("message", "未查询到质检机构数据:"+dataInfo);
            }
        } else {
//        System.out.println(datas);
            //处理数据
            List<DataCrkywCplzjjgDefault> saveZjjg = new ArrayList<>();
            List<DataCrkywCplzjjgDefault> dataList = datas.getJSONObject(0).getJSONArray("dataInfo").toJavaList(DataCrkywCplzjjgDefault.class);
            System.out.println(dataList.toString());

            for (int i = 0; i < dataList.size(); i++) {
                DataCrkywCplzjjgDefault next = dataList.get(i);
                next.setStation(next.getUnitid());
                saveZjjg.add(next);
            }
            if (!saveZjjg.isEmpty()) {
                try {
                    dataCrkywCplzjjgDefaultService.saveOrUpdateBatch(saveZjjg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
            return returnJSON;
    }



    @Override
    public JSONObject saveZlbz(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.QualityStandard";
        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>();
        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("del".equals(opt)){
                if("DATA_CP_QUALITYSTANDARD".equals(table)){
                    qualityBaseService.removeById(dataInfo);
                }
                if("DATA_CP_QUALITYSTANDARDDETAILS".equals(table)){
                    qualityCheckitemService.removeById(dataInfo);
                }
            }else if("add".equals(opt)){
                if("DATA_CP_QUALITYSTANDARD".equals(table)){
                    List<QualityStandardVo> productionResultVos = JSONArray.parseArray(dataInfo).toJavaList(QualityStandardVo.class);
                    for (QualityStandardVo q: productionResultVos) {
                        QualityBase qualityBase = new QualityBase();
                        qualityBase.setCreater(q.getCreateUserId());
                        qualityBase.setCreateTime(q.getCreatedTime());
                        qualityBase.setUnitid(q.getUnitId());
                        qualityBase.setStandardID(q.getId());
                        if("01".equals(q.getStandardStatus())){
                        qualityBase.setIfStarted(1);//启用
                        }else{
                            qualityBase.setIfStarted(0);
                        }
                        if("01".equals(q.getClassification())){
                            qualityBase.setIsCpl("1");//成品粮
                        }else{
                            qualityBase.setIsCpl("0");
                        }
                        qualityBase.setFileName(q.getName());

                        String applicableVarieties = q.getApplicableVarieties();
                        String pz=null;
                        if("01".equals(applicableVarieties)){
                            pz="952721";
                        }else if("02".equals(applicableVarieties)){
                            pz="952722";
                        }else if("03".equals(applicableVarieties)){
                            pz="952723";
                        }else if("04".equals(applicableVarieties)){
                            pz="3166";
                        }else if("05".equals(applicableVarieties)){
                            pz="2777";
                        }else if("06".equals(applicableVarieties)){
                            pz="3165";
                        }else if("07".equals(applicableVarieties)){
                            pz="3164";
                        }
                        BasicEnum byId = basicEnumService.getById(pz);
                        if (ObjectUtil.isNotNull(byId)){
                            qualityBase.setFoodType(byId.getEnumCode());
                            qualityBase.setFoodName(byId.getEnumName());
                        }

                        quality.add(qualityBase);
                    }

                }
                if("DATA_CP_QUALITYSTANDARDDETAILS".equals(table)){
                    List<QualityStandardDetailsVo> productionResultVos = JSONArray.parseArray(dataInfo).toJavaList(QualityStandardDetailsVo.class);
                    for (QualityStandardDetailsVo qd: productionResultVos) {
                        QualityCheckitem qualityCheckitem = new QualityCheckitem();
                        qualityCheckitem.setCheckItemID(qd.getId());
                        BasicEnum byId = basicEnumService.getById(qd.getCrkId());
                        if (ObjectUtil.isNotNull(byId)){
                            qualityCheckitem.setCheckCode(byId.getEnumCode());
                        }
                        qualityCheckitem.setDdmid(qd.getId());
                        qualityCheckitem.setStandardID(qd.getQualityStandard());
                        qualityCheckitem.setFoodGrade(qd.getQualityGrade());
                        qualityCheckitem.setCheckValue(qd.getStandard());
                        qualityd.add(qualityCheckitem);
                    }

                }

            }

        }
        if(quality.size()>0){
            boolean b = qualityBaseService.saveOrUpdateBatch(quality);
        }
        if(qualityd.size()>0){
            boolean b1 = qualityCheckitemService.saveOrUpdateBatch(qualityd);

        }

        return returnJSON;
    }

    @Override
    public JSONObject storeConfirm(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.storeConfirm";
        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);
            }
        }else{
            returnJSON.put("message","无退货订单数据");
            return  returnJSON;
        }
        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 ("li_after_sale".equals(table)) {
                List<LiAfterSale> liAfterSale = JSONArray.parseArray(dataInfo).toJavaList(LiAfterSale.class);
                for (LiAfterSale q: liAfterSale) {
                    q.setIsFh("1");
                    iLiAfterSaleService.saveOrUpdate(q);

                }
            }
        }
        return null;
    }

    @Override
    public JSONObject delivery(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.delivery";
        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);
            }
        }else {
            returnJSON.put("message","发货订单数据");
            return  returnJSON;
        }
        for (Map<String, Object> s:list) {
            String opt = s.get("opt").toString();
            String table = s.get("table").toString();
            String dataInfo = s.get("dataInfo").toString();
            String goodsName="";
            if ("li_order_item".equals(table)) {
                List<LiOrderItem> liOrderItem = JSONArray.parseArray(dataInfo).toJavaList(LiOrderItem.class);
                for (LiOrderItem q: liOrderItem) {
                    iLiOrderItemService.saveOrUpdate(q);
                    goodsName+=q.getGoodsName();
                }
            }
            if ("li_order".equals(table)) {
                List<LiOrder> liOrder = JSONArray.parseArray(dataInfo).toJavaList(LiOrder.class);
                for (LiOrder q: liOrder) {
                    q.setGoodsNames(goodsName);
                    q.setIsFh("1");
                    iLiOrderService.saveOrUpdate(q);

                }
            }
        }
        return null;
    }

    private String getEnumIdByName(String enumName,String panetnId) {
        QueryWrapper<BasicEnum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enumName",enumName);
        queryWrapper.eq("parentId",panetnId);
        queryWrapper.last("limit 1");
        BasicEnum basicEnum = basicEnumService.getOne(queryWrapper);
        if (basicEnum != null){
           return basicEnum.getEnumId();
        }
        return enumName;
    }

    /**
     * 判断是否是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;
        }

    }
}
