zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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;
            }
        }
    }
 
 
 
}