package com.changhong.epc.admin.service.site.impl; import com.changhong.epc.admin.mapper.centen.site.CenterMasterValueMapper; import com.changhong.epc.admin.service.site.CenterMasterValueService; import com.changhong.epc.bean.tenant.master.MasterValue; import com.changhong.epc.bean.tenant.sunCloud.MasterElementExample; import com.changhong.epc.bean.tenant.sunCloud.PushMasDataValue; import com.changhong.epc.constter.tenant.SunCloudConst; import com.iemsoft.framework.cloud.core.exception.IEMRestException; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.ModelMap; import javax.annotation.Resource; import java.util.*; @Slf4j @Service public class CenterMasterValueServiceImpl implements CenterMasterValueService, SunCloudConst{ @Resource CenterMasterValueMapper centerMasterValueMapper; /* * 下标key */ public static final String INDEX_KEY = "index"; /* * 删除标记 */ public static final String DEL_FLAG = "delFlag"; /* * 行号 */ public static final String ROW_NUM = "rowNum"; public static final String MAS_DATA_DEF_CODE = "masDataDefCode"; private Integer tenantId; @Transactional @Override public void pushMasDataValue(PushMasDataValue pushMasDataValue) { List> errData = new ArrayList<>(); String type = pushMasDataValue.getType(); this.tenantId = pushMasDataValue.getTenantId(); if(StringUtils.isNotBlank(type)){ List> dataList = pushMasDataValue.getData(); String masDataDefCode = pushMasDataValue.getMasDataDefCode(); if(CollectionUtils.isNotEmpty(dataList)){ switch (type) { /* 增加 */ case TYPE_INSERT: insert(masDataDefCode, dataList, errData); break; /* 修改 */ case TYPE_UPDATE: update(masDataDefCode, dataList, errData); break; /* 删除 */ case TYPE_DELETE: beginDelete(masDataDefCode, dataList); break; default: throw new IEMRestException("C0018"); } } } } private void insert(String mdCode, List> dataList, List> errData){ if(ObjectUtil.empty(dataList) || ObjectUtil.empty(errData)){ return; } if(dataList.size() == 1){ if(beginInsert(mdCode, dataList.get(0)) == Integer.MIN_VALUE){ errData.add(dataList.get(0)); } }else{ for (Map map : dataList) { if(beginInsert(mdCode, map) == Integer.MIN_VALUE){ errData.add(map); } } } } private void update(String mdCode, List> dataList, List> errData){ if(ObjectUtil.empty(dataList) || ObjectUtil.empty(errData)){ return; } for (Map map : dataList) { if(beginUpdate(mdCode, map) == Integer.MIN_VALUE){ errData.add(map); } } } /** * 开始添加 * @Title: insert * @param @param data * @param @param userInfo * @param @return 设定文件 * @return int 返回类型 * @throws */ private int beginInsert(String mdCode, Map data){ log.error(String.format("主数据同步ing...%s:%s", mdCode, data.toString())); /** * 验证是否存在 */ // ValidateTool.assertException( // StringUtils.isBlank(Objects.toString(data.get(ROW_NUM), "")) || // masterValueMapper.isEmpty(mdCode, data) > 0, Const.C_100008); Map config = new ModelMap(DEL_FLAG, data.get(DEL_FLAG)) .addAttribute(ROW_NUM, data.get(ROW_NUM)) .addAttribute(MAS_DATA_DEF_CODE, mdCode); data.remove(DEL_FLAG); data.remove(ROW_NUM); /** * 添加主数据 */ // Integer mdId = masterDefineMapper.selectIdByCode(mdCode); MasterValue precord = null; for (Map.Entry val : data.entrySet()) { // ValidateTool.assertException( // masterValueMapper.isEmpty(mdCode//define_code --> master_value 表中对应字段 // ,val.getKey()//ele_code // ,NumberUtils.toInt(Objects.toString(config.get(ROW_NUM), ""), -1)//row_no // ,this.tenantId.toString()) > 0);//租户id MasterElementExample mee = new MasterElementExample(); MasterElementExample.Criteria c = mee.createCriteria(); // c.andMeMasterIdEqualTo(mdId); precord = new MasterValue(); precord.setDefineCode(mdCode); precord.setEleCode(val.getKey()); //默认递增 precord.setEleValue(Objects.toString(val.getValue(), "")); precord.setRowNo(NumberUtils.toInt(Objects.toString(config.get(ROW_NUM), ""), -1)); precord.setDelFlag(NumberUtils.toInt(Objects.toString(config.get(DEL_FLAG), ""), 0)); precord.setDataStart(0); precord.setInsP("-1"); precord.setUpdP("-1"); // System.err.println(ThreadData.get(Keys.TENANT_ID)); precord.setTenantId("999"); // precord.setTenantId(ThreadData.get(Keys.TENANT_ID).toString()); precord.setInsT(new Date()); precord.setUpdT(new Date()); centerMasterValueMapper.insert(precord); } return 1; }; /** * 开始修改 * @Title: update * @return int * @throws */ private int beginUpdate(String mdCode, Map data){ Map config = new ModelMap(DEL_FLAG, data.get(DEL_FLAG)) .addAttribute(ROW_NUM, data.get(ROW_NUM)) .addAttribute(MAS_DATA_DEF_CODE, mdCode); data.remove(DEL_FLAG); data.remove(ROW_NUM); int count = centerMasterValueMapper.deleteValue(config, data); /** * 添加主数据 */ MasterValue precord = null; for (Map.Entry val : data.entrySet()) { precord = new MasterValue(); precord.setDefineCode(mdCode); precord.setEleCode(val.getKey()); //默认递增 precord.setEleValue(Objects.toString(val.getValue(), "")); precord.setRowNo(NumberUtils.toInt(Objects.toString(config.get(ROW_NUM), ""), -1)); precord.setDelFlag(NumberUtils.toInt(Objects.toString(config.get(DEL_FLAG), ""), 0)); precord.setDataStart(0); precord.setInsP("-1"); precord.setUpdP("-1"); precord.setTenantId(this.tenantId.toString()); precord.setInsT(new Date()); precord.setUpdT(new Date()); count += centerMasterValueMapper.insert(precord); } return count; }; /** * 开始删除 * @Title: delete * @param @param data * @param @param userInfo * @param @return 设定文件 * @return int 返回类型 * @throws */ private int beginDelete(String mdCode, List> dataList){ return centerMasterValueMapper.updateStart(mdCode, dataList); }; }