zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1 package com.changhong.epc.admin.service.master.impl;
Z 2
3 import com.changhong.epc.admin.mapper.centen.corres.CorresElFieldMapper;
4 import com.changhong.epc.admin.mapper.centen.master.MasterDefineMapper;
5 import com.changhong.epc.admin.mapper.centen.master.MasterElementMapper;
6 import com.changhong.epc.admin.mapper.centen.master.MasterValueMapper;
7 import com.changhong.epc.admin.mapper.centen.norm.MasterFtpMapper;
8 import com.changhong.epc.admin.mapper.centen.site.MasterDefaultMappingMapper;
9 import com.changhong.epc.admin.mapper.centen.site.MasterElementMappingMapper;
10 import com.changhong.epc.bean.admin.MasterDefaultMapping;
11 import com.changhong.epc.bean.admin.MasterElementMapping;
12 import com.changhong.epc.bean.tenant.master.MasterDefine;
13 import com.changhong.epc.bean.tenant.master.extend.*;
14 import com.changhong.epc.bean.tenant.sunCloud.MasterFtp;
15 import com.changhong.epc.config.service.MasterDefineService;
16 import com.iemsoft.framework.cloud.core.thread.Keys;
17 import com.iemsoft.framework.cloud.core.thread.ThreadData;
18 import com.iemsoft.framework.cloud.core.tools.JSONTool;
19 import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
20 import org.springframework.stereotype.Service;
21 import org.springframework.transaction.annotation.Transactional;
22
23 import javax.annotation.Resource;
24 import java.util.*;
25
26 @Service
27 public class MasterDefineServiceImpl extends MasterDefineService {
28
29     @Resource
30     private MasterDefineMapper masterDefineMapper;
31
32     @Resource
33     private MasterElementMapper masterElementMapper;
34
35     @Resource
36     private MasterValueMapper masterValueMapper;
37
38     @Resource
39     private MasterElementMappingMapper masterElementMappingMapper;
40
41     @Resource
42     private MasterDefaultMappingMapper masterDefaultMappingMapper;
43
44     @Resource
45     private CorresElFieldMapper corresElFieldMapper;
46
47     @Resource
48     private MasterFtpMapper masterFtp;
49
50     private static String TABLENAME = "epc_master_value_";
51
52     //判断二维表常量
53     private static String MASTERFLAG = "masterFlag";
54
55     //创建表的字段添加英文
56     private static String ELEMENTCOLUMNSNAME = "col_";
57
58     //ROW_NO 二维表字段
59     private static String ROWNO = "ROW_NO";
60
61     //define_code 二维表字段
62     private static String DEFINECODE = "define_code";
63
64     //判断标记
65     private static String FLAG = "flag";
66
67     private static String TYPE = "10";
68
69
70     public List<MasterDefine> selectByQuery(String type) {
71         return masterDefineMapper.selectByQuery(null);
72     }
73
74     public List selectAllMaster(MasterDefine map) {
75         return masterDefineMapper.selectOrName(map);
76     }
77
78
79     @Override
80     public List<MasterDefinesExtend> selectEle(MasterDefine masterDefine) {
81
82         return masterDefineMapper.selectEle(masterDefine);
83     }
84
85     @Override
86     public List<MasterDefinesExtend> selectName(MasterDefine master) {
87
88         return masterDefineMapper.selectName(master);
89     }
90
91     @Transactional
92     public int addMasterFtp(MasterFtp ftp) {
93         ftp.initParam();
94         return masterFtp.insert(ftp);
95     }
96
97     @Transactional
98     public int updateMasterFtp(MasterFtp ftp) {
99         return masterFtp.updateByPrimaryKey(ftp);
100     }
101
102
103     public List<MasterFtp> findMasterFtp() {
104         MasterFtp ftp = new MasterFtp();
105         ftp.setDataStart(0);
106         List<MasterFtp> ftps = masterFtp.select(ftp);
107         Collections.reverse(ftps);
108         return ftps;
109     }
110     //判断增删改
111     public int cudThreeTables(MasterList masterList) {
112         /**
113          * 公用数据
114          * */
115         //累计修改数据条数
116         int count = 0;
117         //定义code
118         String code = masterList.getCode();
119         //传递过来的元素集合
120         List<GeneralMasterElement> materEle = masterList.getEleList();
121         if(ObjectUtil.empty(materEle)){
122             materEle = new ArrayList<>();
123         }
124         //传递过来的value集合
125         List<GeneralMasterValue> masterVal = masterList.getValList();
126         if(ObjectUtil.empty(masterVal)){
127             masterVal = new ArrayList<>();
128         }
129         //查判断是否为二维表的对象
130         MasterDefaultMapping masterDefaultMapping = masterDefaultMappingMapper.selectMaster(code);
131         /**
132          * 主数据增加,查询原先有的话不改变,没有添加。
133          * */
134         if (Objects.nonNull(code)) {
135             //增加主表define数据
136             GeneralMasterDefine masterDefine = JSONTool.toObj(JSONTool.toJson(masterList), GeneralMasterDefine.class);
137             //添加之前根据code查询数据是否存在
138             String codeOld = masterDefineMapper.selectMasterDefineCodeByCode(code);
139             if (Objects.isNull(codeOld)) {
140                 masterDefine.initParam();
141                 masterDefine.setDelFlag(0);
142                 count += masterDefineMapper.insert(masterDefine);
143             }
144             //查询常量表,如果符合条件则插入default_mapping表
145             String codes = corresElFieldMapper.getCodeByMeaning(MASTERFLAG);
146             if(Objects.nonNull(codes)){
147                 String codeL[] = codes.split(",");
148                 for (String co : codeL) {
149                     //如果code存在常量表中,并且二维表关联为空
150                     if (co.equals(code) && Objects.isNull(masterDefaultMapping)) {
151                         //创建二维表
152                         List<String> column = new ArrayList<String>();
153                         for (GeneralMasterElement gme : materEle) {
154                             column.add(ELEMENTCOLUMNSNAME + gme.getCode());
155                         }
156                         masterValueMapper.createValueTable(TABLENAME + code, column);
157                         //插入二维表关联
158                         MasterDefaultMapping masterDefaultMapping2 = new MasterDefaultMapping();
159                         masterDefaultMapping2.setMdCode(code);
160                         masterDefaultMapping2.setTableName(TABLENAME + code);
161                         masterDefaultMapping2.setType(TYPE);
162                         masterDefaultMapping2.initParam();
163                         count += masterDefaultMappingMapper.insertDefaultMappingToId(masterDefaultMapping2);
164                         //插入二维表字段映射表(epc_master_element_mapping)
165                         Integer mId = masterDefaultMapping2.getId();
166                         if(ObjectUtil.notEmpty(materEle)) {
167                             for (GeneralMasterElement gm : materEle) {
168                                 MasterElementMapping eleMapping = new MasterElementMapping();
169                                 eleMapping.setMId(mId);
170                                 eleMapping.setMeCode(gm.getCode());
171                                 eleMapping.setTableField(ELEMENTCOLUMNSNAME + gm.getCode());
172                                 eleMapping.initParam();
173                                 count += masterElementMappingMapper.insert(eleMapping);
174                             }
175                         }
176                         break;
177                     }
178                 }
179             }
180
181         }
182         /**
183          * 元素增加/更新
184          * */
185         //数据库已有元素
186         List<GeneralMasterElement> materEleOld = masterElementMapper.findMasterEleByCode(code);
187         if (Objects.isNull(materEleOld) || materEleOld.size() == 0) {
188             for (GeneralMasterElement gme : materEle) {
189                 gme.setDelFlag(0);
190                 count += masterElementMapper.insert(gme);
191             }
192         } else {
193             boolean flag = false;
194             for (GeneralMasterElement gme : materEle) {
195                 for (GeneralMasterElement gmeOld : materEleOld) {
196                     if (gmeOld.getCode().equals(gme.getCode()) && gmeOld.getDefineCode().equals(gme.getDefineCode())) {
197                         gme.setId(gmeOld.getId());
198                         gme.setAlias(gmeOld.getAlias());
199                         gme.setDelFlag(0);
200                         gme.initParam();
201                         flag = true;
202                         break;
203                     } else {
204                         flag = false;
205                     }
206                 }
207                 if (flag) {
208                     count += masterElementMapper.updateByPrimaryKey(gme);
209                 } else {
210                     count += masterElementMapper.insert(gme);
211                     //插入elementMapping表
212                     MasterElementMapping masterElementMapping;
213                     //如果二维表名字不为空,进行更新
214                     if (Objects.nonNull(masterDefaultMapping)) {
215                         //二维表表名
216                         String tableName = masterDefaultMapping.getTableName();
217                         masterElementMapping = new MasterElementMapping();
218                         //进行更新操作(eleMapping表),(已有二维表的结构)
219                         masterElementMapping.setMId(masterDefaultMapping.getId());
220                         masterElementMapping.setMeCode(gme.getCode());
221                         masterElementMapping.setTableField(ELEMENTCOLUMNSNAME + gme.getCode());
222                         masterElementMapping.initParam();
223                         count += masterElementMappingMapper.insert(masterElementMapping);
224                         //更改二维表结构
225                         count += masterValueMapper.addTabelColumns(tableName, ELEMENTCOLUMNSNAME + gme.getCode());
226                     }
227                 }
228             }
229         }
230         /**
231          * value表的增加,更新,删除
232          * */
233         //二维映射表不为空直接插入数据
234         MasterDefaultMapping masterDefaultMapping2 = masterDefaultMappingMapper.selectMaster(code);
235         if (Objects.nonNull(masterDefaultMapping2)) {
236             this.insertToValueTable2(masterVal, code, masterDefaultMapping2.getTableName());
237         } else {
238             this.insertToValueTable(masterVal);
239         }
240         return count;
241     }
242
243     public void insertToValueTable2(List<GeneralMasterValue> masterVal, String code, String tableName) {
244
245         int count = 0;
246         List<Map<String, String>> valueListMap = new ArrayList<Map<String, String>>();
247         for (int i = 0; i < masterVal.size(); i = count) {
248             //二维表一行的值
249             Map<String, String> m = new HashMap<String, String>();
250             m.put(ROWNO, masterVal.get(i).getRowNo().toString());
251             m.put(ELEMENTCOLUMNSNAME + masterVal.get(i).getEleCode(), masterVal.get(i).getEleValue());
252             m.put(FLAG, masterVal.get(i).getFlag());
253             m.put(DEFINECODE, code);
254             for (int j = i + 1; j < masterVal.size(); j++) {
255                 if (masterVal.get(i).getRowNo().equals(masterVal.get(j).getRowNo())) {
256                     count++;
257                     m.put(ELEMENTCOLUMNSNAME + masterVal.get(j).getEleCode(), masterVal.get(j).getEleValue());
258                 }
259             }
260             count++;
261             valueListMap.add(m);
262         }
263         valueListMap.stream().forEach(o -> {
264                     switch (o.get(FLAG)) {
265                         case "1":
266                             //增加
267                             o.remove(FLAG);
268                             masterValueMapper.insertOtherValue(o, tableName);
269                             break;
270                         case "2":
271                             //更新
272                             o.remove(FLAG);
273                             List<Map<String, Object>> old = masterValueMapper.selectTwoByRowNo(tableName, o.get(ROWNO));
274                             if (Objects.isNull(old) || old.size() == 0) {
275                                 masterValueMapper.insertOtherValue(o, tableName);
276                             } else {
277                                 masterValueMapper.updateOtherValue(o, tableName, o.get(DEFINECODE), o.get(ROWNO));
278                             }
279                             break;
280                         case "3":
281                             //删除
282                             o.remove(FLAG);
283                             masterValueMapper.deleteOtherValue(tableName, o.get(DEFINECODE), o.get(ROWNO));
284                             break;
285                     }
286                 }
287         );
288     }
289     //1维表赋值
290     public void insertToValueTable(List<GeneralMasterValue> masterVal){
291         if(ObjectUtil.empty(masterVal)){
292             return;
293         }
294         for (GeneralMasterValue gv : masterVal) {
295             String valFlag = gv.getFlag();
296             switch (valFlag) {
297                 case "1":
298                     gv.initParam();
299                     masterValueMapper.insert(gv);
300                     break;
301                 case "2":
302                     int old = masterValueMapper.selectByRowNo(gv.getRowNo(), gv.getEleCode(), gv.getDefineCode());
303                     if (old == 0) {
304                         gv.initParam();
305                         masterValueMapper.insert(gv);
306                     } else {
307                         gv.setUpdP((String) ThreadData.get(Keys.USER_ID));
308                         gv.setUpdT(new Date());
309                         masterValueMapper.updateMasterVal(gv);
310                     }
311                     break;
312                 case "3":
313                     masterValueMapper.delMasterValue(gv);
314                     break;
315                 default: break;
316             }
317         }
318     }
319
320
321
322 }