package com.changhong.epc.admin.service.master.impl;
|
|
import com.changhong.epc.admin.mapper.centen.corres.CorresElFieldMapper;
|
import com.changhong.epc.admin.mapper.centen.master.MasterDefineMapper;
|
import com.changhong.epc.admin.mapper.centen.master.MasterElementMapper;
|
import com.changhong.epc.admin.mapper.centen.master.MasterValueMapper;
|
import com.changhong.epc.admin.mapper.centen.norm.MasterFtpMapper;
|
import com.changhong.epc.admin.mapper.centen.site.MasterDefaultMappingMapper;
|
import com.changhong.epc.admin.mapper.centen.site.MasterElementMappingMapper;
|
import com.changhong.epc.bean.admin.MasterDefaultMapping;
|
import com.changhong.epc.bean.admin.MasterElementMapping;
|
import com.changhong.epc.bean.tenant.master.MasterDefine;
|
import com.changhong.epc.bean.tenant.master.extend.*;
|
import com.changhong.epc.bean.tenant.sunCloud.MasterFtp;
|
import com.changhong.epc.config.service.MasterDefineService;
|
import com.iemsoft.framework.cloud.core.thread.Keys;
|
import com.iemsoft.framework.cloud.core.thread.ThreadData;
|
import com.iemsoft.framework.cloud.core.tools.JSONTool;
|
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.util.*;
|
|
@Service
|
public class MasterDefineServiceImpl extends MasterDefineService {
|
|
@Resource
|
private MasterDefineMapper masterDefineMapper;
|
|
@Resource
|
private MasterElementMapper masterElementMapper;
|
|
@Resource
|
private MasterValueMapper masterValueMapper;
|
|
@Resource
|
private MasterElementMappingMapper masterElementMappingMapper;
|
|
@Resource
|
private MasterDefaultMappingMapper masterDefaultMappingMapper;
|
|
@Resource
|
private CorresElFieldMapper corresElFieldMapper;
|
|
@Resource
|
private MasterFtpMapper masterFtp;
|
|
private static String TABLENAME = "epc_master_value_";
|
|
//判断二维表常量
|
private static String MASTERFLAG = "masterFlag";
|
|
//创建表的字段添加英文
|
private static String ELEMENTCOLUMNSNAME = "col_";
|
|
//ROW_NO 二维表字段
|
private static String ROWNO = "ROW_NO";
|
|
//define_code 二维表字段
|
private static String DEFINECODE = "define_code";
|
|
//判断标记
|
private static String FLAG = "flag";
|
|
private static String TYPE = "10";
|
|
|
public List<MasterDefine> selectByQuery(String type) {
|
return masterDefineMapper.selectByQuery(null);
|
}
|
|
public List selectAllMaster(MasterDefine map) {
|
return masterDefineMapper.selectOrName(map);
|
}
|
|
|
@Override
|
public List<MasterDefinesExtend> selectEle(MasterDefine masterDefine) {
|
|
return masterDefineMapper.selectEle(masterDefine);
|
}
|
|
@Override
|
public List<MasterDefinesExtend> selectName(MasterDefine master) {
|
|
return masterDefineMapper.selectName(master);
|
}
|
|
@Transactional
|
public int addMasterFtp(MasterFtp ftp) {
|
ftp.initParam();
|
return masterFtp.insert(ftp);
|
}
|
|
@Transactional
|
public int updateMasterFtp(MasterFtp ftp) {
|
return masterFtp.updateByPrimaryKey(ftp);
|
}
|
|
|
public List<MasterFtp> findMasterFtp() {
|
MasterFtp ftp = new MasterFtp();
|
ftp.setDataStart(0);
|
List<MasterFtp> ftps = masterFtp.select(ftp);
|
Collections.reverse(ftps);
|
return ftps;
|
}
|
//判断增删改
|
public int cudThreeTables(MasterList masterList) {
|
/**
|
* 公用数据
|
* */
|
//累计修改数据条数
|
int count = 0;
|
//定义code
|
String code = masterList.getCode();
|
//传递过来的元素集合
|
List<GeneralMasterElement> materEle = masterList.getEleList();
|
if(ObjectUtil.empty(materEle)){
|
materEle = new ArrayList<>();
|
}
|
//传递过来的value集合
|
List<GeneralMasterValue> masterVal = masterList.getValList();
|
if(ObjectUtil.empty(masterVal)){
|
masterVal = new ArrayList<>();
|
}
|
//查判断是否为二维表的对象
|
MasterDefaultMapping masterDefaultMapping = masterDefaultMappingMapper.selectMaster(code);
|
/**
|
* 主数据增加,查询原先有的话不改变,没有添加。
|
* */
|
if (Objects.nonNull(code)) {
|
//增加主表define数据
|
GeneralMasterDefine masterDefine = JSONTool.toObj(JSONTool.toJson(masterList), GeneralMasterDefine.class);
|
//添加之前根据code查询数据是否存在
|
String codeOld = masterDefineMapper.selectMasterDefineCodeByCode(code);
|
if (Objects.isNull(codeOld)) {
|
masterDefine.initParam();
|
masterDefine.setDelFlag(0);
|
count += masterDefineMapper.insert(masterDefine);
|
}
|
//查询常量表,如果符合条件则插入default_mapping表
|
String codes = corresElFieldMapper.getCodeByMeaning(MASTERFLAG);
|
if(Objects.nonNull(codes)){
|
String codeL[] = codes.split(",");
|
for (String co : codeL) {
|
//如果code存在常量表中,并且二维表关联为空
|
if (co.equals(code) && Objects.isNull(masterDefaultMapping)) {
|
//创建二维表
|
List<String> column = new ArrayList<String>();
|
for (GeneralMasterElement gme : materEle) {
|
column.add(ELEMENTCOLUMNSNAME + gme.getCode());
|
}
|
masterValueMapper.createValueTable(TABLENAME + code, column);
|
//插入二维表关联
|
MasterDefaultMapping masterDefaultMapping2 = new MasterDefaultMapping();
|
masterDefaultMapping2.setMdCode(code);
|
masterDefaultMapping2.setTableName(TABLENAME + code);
|
masterDefaultMapping2.setType(TYPE);
|
masterDefaultMapping2.initParam();
|
count += masterDefaultMappingMapper.insertDefaultMappingToId(masterDefaultMapping2);
|
//插入二维表字段映射表(epc_master_element_mapping)
|
Integer mId = masterDefaultMapping2.getId();
|
if(ObjectUtil.notEmpty(materEle)) {
|
for (GeneralMasterElement gm : materEle) {
|
MasterElementMapping eleMapping = new MasterElementMapping();
|
eleMapping.setMId(mId);
|
eleMapping.setMeCode(gm.getCode());
|
eleMapping.setTableField(ELEMENTCOLUMNSNAME + gm.getCode());
|
eleMapping.initParam();
|
count += masterElementMappingMapper.insert(eleMapping);
|
}
|
}
|
break;
|
}
|
}
|
}
|
|
}
|
/**
|
* 元素增加/更新
|
* */
|
//数据库已有元素
|
List<GeneralMasterElement> materEleOld = masterElementMapper.findMasterEleByCode(code);
|
if (Objects.isNull(materEleOld) || materEleOld.size() == 0) {
|
for (GeneralMasterElement gme : materEle) {
|
gme.setDelFlag(0);
|
count += masterElementMapper.insert(gme);
|
}
|
} else {
|
boolean flag = false;
|
for (GeneralMasterElement gme : materEle) {
|
for (GeneralMasterElement gmeOld : materEleOld) {
|
if (gmeOld.getCode().equals(gme.getCode()) && gmeOld.getDefineCode().equals(gme.getDefineCode())) {
|
gme.setId(gmeOld.getId());
|
gme.setAlias(gmeOld.getAlias());
|
gme.setDelFlag(0);
|
gme.initParam();
|
flag = true;
|
break;
|
} else {
|
flag = false;
|
}
|
}
|
if (flag) {
|
count += masterElementMapper.updateByPrimaryKey(gme);
|
} else {
|
count += masterElementMapper.insert(gme);
|
//插入elementMapping表
|
MasterElementMapping masterElementMapping;
|
//如果二维表名字不为空,进行更新
|
if (Objects.nonNull(masterDefaultMapping)) {
|
//二维表表名
|
String tableName = masterDefaultMapping.getTableName();
|
masterElementMapping = new MasterElementMapping();
|
//进行更新操作(eleMapping表),(已有二维表的结构)
|
masterElementMapping.setMId(masterDefaultMapping.getId());
|
masterElementMapping.setMeCode(gme.getCode());
|
masterElementMapping.setTableField(ELEMENTCOLUMNSNAME + gme.getCode());
|
masterElementMapping.initParam();
|
count += masterElementMappingMapper.insert(masterElementMapping);
|
//更改二维表结构
|
count += masterValueMapper.addTabelColumns(tableName, ELEMENTCOLUMNSNAME + gme.getCode());
|
}
|
}
|
}
|
}
|
/**
|
* value表的增加,更新,删除
|
* */
|
//二维映射表不为空直接插入数据
|
MasterDefaultMapping masterDefaultMapping2 = masterDefaultMappingMapper.selectMaster(code);
|
if (Objects.nonNull(masterDefaultMapping2)) {
|
this.insertToValueTable2(masterVal, code, masterDefaultMapping2.getTableName());
|
} else {
|
this.insertToValueTable(masterVal);
|
}
|
return count;
|
}
|
|
public void insertToValueTable2(List<GeneralMasterValue> masterVal, String code, String tableName) {
|
|
int count = 0;
|
List<Map<String, String>> valueListMap = new ArrayList<Map<String, String>>();
|
for (int i = 0; i < masterVal.size(); i = count) {
|
//二维表一行的值
|
Map<String, String> m = new HashMap<String, String>();
|
m.put(ROWNO, masterVal.get(i).getRowNo().toString());
|
m.put(ELEMENTCOLUMNSNAME + masterVal.get(i).getEleCode(), masterVal.get(i).getEleValue());
|
m.put(FLAG, masterVal.get(i).getFlag());
|
m.put(DEFINECODE, code);
|
for (int j = i + 1; j < masterVal.size(); j++) {
|
if (masterVal.get(i).getRowNo().equals(masterVal.get(j).getRowNo())) {
|
count++;
|
m.put(ELEMENTCOLUMNSNAME + masterVal.get(j).getEleCode(), masterVal.get(j).getEleValue());
|
}
|
}
|
count++;
|
valueListMap.add(m);
|
}
|
valueListMap.stream().forEach(o -> {
|
switch (o.get(FLAG)) {
|
case "1":
|
//增加
|
o.remove(FLAG);
|
masterValueMapper.insertOtherValue(o, tableName);
|
break;
|
case "2":
|
//更新
|
o.remove(FLAG);
|
List<Map<String, Object>> old = masterValueMapper.selectTwoByRowNo(tableName, o.get(ROWNO));
|
if (Objects.isNull(old) || old.size() == 0) {
|
masterValueMapper.insertOtherValue(o, tableName);
|
} else {
|
masterValueMapper.updateOtherValue(o, tableName, o.get(DEFINECODE), o.get(ROWNO));
|
}
|
break;
|
case "3":
|
//删除
|
o.remove(FLAG);
|
masterValueMapper.deleteOtherValue(tableName, o.get(DEFINECODE), o.get(ROWNO));
|
break;
|
}
|
}
|
);
|
}
|
//1维表赋值
|
public void insertToValueTable(List<GeneralMasterValue> masterVal){
|
if(ObjectUtil.empty(masterVal)){
|
return;
|
}
|
for (GeneralMasterValue gv : masterVal) {
|
String valFlag = gv.getFlag();
|
switch (valFlag) {
|
case "1":
|
gv.initParam();
|
masterValueMapper.insert(gv);
|
break;
|
case "2":
|
int old = masterValueMapper.selectByRowNo(gv.getRowNo(), gv.getEleCode(), gv.getDefineCode());
|
if (old == 0) {
|
gv.initParam();
|
masterValueMapper.insert(gv);
|
} else {
|
gv.setUpdP((String) ThreadData.get(Keys.USER_ID));
|
gv.setUpdT(new Date());
|
masterValueMapper.updateMasterVal(gv);
|
}
|
break;
|
case "3":
|
masterValueMapper.delMasterValue(gv);
|
break;
|
default: break;
|
}
|
}
|
}
|
|
|
|
}
|