cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
323
324
325
326
327
328
package cn.ksource.web.service.cmdb.impl;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
 
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import cn.ksource.core.dao.BaseDao;
import cn.ksource.core.dao.SqlParameter;
import cn.ksource.core.util.ConvertUtil;
import cn.ksource.core.util.JsonUtil;
import cn.ksource.web.Constants;
import cn.ksource.web.service.cmdb.CICategory;
import cn.ksource.web.service.cmdb.CIPropertity;
import cn.ksource.web.service.cmdb.CIPropertityMetadata;
import cn.ksource.web.service.cmdb.CIPropertityService;
import cn.ksource.web.service.cmdb.DSLCategory;
import cn.ksource.web.service.cmdb.SUBCIPropertity;
import cn.ksource.web.service.cmdb.SUBCIPropertityMetadata;
import cn.ksource.web.service.cmdb.SequenceService;
 
@Service
public class CIPropertityServiceImpl implements CIPropertityService {
 
    @Autowired
    private BaseDao baseDao;
    
    @Autowired
    private SequenceService sequenceService;
    
    @Override
    public List<Map> getMetadataByTableName(String tableName) {
        //TODO 需要加入缓存处理
        String sql = "select * from CMDB_CI_EXTEND_COLUMN where TABLENAME=:TABLENAME and STATE=1 order by ORDERNUM";
        List<Map> list =  baseDao.queryForList(sql,new SqlParameter("TABLENAME",tableName));
        for (Map map : list) {
            if (Constants.CMDB_CI_EXTEND_COLUMN_DATATYPE_SINGLE_SELECT == ConvertUtil.obj2Integer(map.get("DATATYPE"))) {
                map.put("_SELECTVALUE", map.get("SELECTVALUE") == null ? new HashMap() : JsonUtil.json2Map(map.get("SELECTVALUE").toString()));
            } else if (Constants.CMDB_CI_EXTEND_COLUMN_DATATYPE_MULTI_SELECT == ConvertUtil.obj2Integer(map.get("DATATYPE"))) {
                if (map.get("SELECTVALUE") != null) {
                    Map resultMap = JsonUtil.json2Map(map.get("SELECTVALUE").toString());
                    List<Map> resultList = new LinkedList<Map>();
                    for (Iterator iterator = resultMap.keySet().iterator(); iterator.hasNext();) {
                        String key = (String) iterator.next();
                        Map temMap = new HashMap();
                        temMap.put("KEY", key);
                        temMap.put("VALUE", resultMap.get(key));
                        resultList.add(temMap);
                    }
                    map.put("_SELECTVALUE",resultList);
                    map.put("_SELECTVALUE_JSON", JsonUtil.list2Json(resultList));
                } else {
                    map.put("_SELECTVALUE", new ArrayList());
                    map.put("_SELECTVALUE_JSON", "[]");
                }
            }
        }
        return list;
    }
    
    @Override
    public CIPropertity getCIPropertity(String ci_id) {
        String sql = "select * from CMDB_CI_BASE where id=:id";
        Map basePropertity = baseDao.queryForMap(sql,new SqlParameter("id",ci_id));
        
        //所属层级
        int level = ConvertUtil.obj2Int(basePropertity.get("LEVELNUMBER"));
        
        CIPropertity propertity = new CIPropertity();
        //基本属性
        propertity.setBasePropertity(basePropertity);
        //通用属性
        propertity.setCommonPropertity(baseDao.queryForMap("select * from CMDB_CI_BASE_EXTEND where id=:id",new SqlParameter("id",ci_id)));
        
        //分类信息
        propertity.setCategory(getCICategory(basePropertity.get("THELEVELID").toString()));
        //元数据信息
        propertity.setMeta(getCIPropertityMetadata(propertity.getCategory().getCategory().get("TABLE_NAME").toString()));
        
        //一级属性
        propertity.setFirstLevelPropertity(baseDao.queryForMap("select * from "+ propertity.getCategory().getFirstCategory().get("TABLE_NAME") +" where id=:id",new SqlParameter("id",ci_id)));
        //二级属性
        propertity.setSecondLevelPropertity(baseDao.queryForMap("select * from "+ propertity.getCategory().getSecondCategory().get("TABLE_NAME")  +" where id=:id",new SqlParameter("id",ci_id)));
        
        if (level == 3) {
            //三级属性
            propertity.setThirdLevelPropertity(baseDao.queryForMap("select * from "+ propertity.getCategory().getThirdCategory().get("TABLE_NAME") +" where id=:id",new SqlParameter("id",ci_id)));
        } 
        return propertity;
    }
    
    public SUBCIPropertity getSubCIPropertity(String ci_id) {
        String sql = "select * from CMDB_CI_BASE where id=:id";
        Map basePropertity = baseDao.queryForMap(sql,new SqlParameter("id",ci_id));
        
        SUBCIPropertity propertity = new SUBCIPropertity();
        //基本属性
        propertity.setBasePropertity(basePropertity);
        //通用属性
        propertity.setCommonPropertity(baseDao.queryForMap("select * from CMDB_CI_SUB_BASE_EXTEND where id=:id",new SqlParameter("id",ci_id)));
        
        //分类信息
        propertity.setCategory(getCICategory(basePropertity.get("THELEVELID").toString()));
        //元数据信息
        propertity.setMeta(getSubCIPropertityMetadata(propertity.getCategory().getCategory().get("TABLE_NAME").toString()));
        
        //一级属性
        propertity.setCustomPropertity(baseDao.queryForMap("select * from "+ propertity.getCategory().getFirstCategory().get("TABLE_NAME") +" where id=:id",new SqlParameter("id",ci_id)));
        
        return propertity;
    }
    
 
    @Override
    public Map getCategoryById(String categoryid) {
        //TODO 需要加入缓存处理
        String sql = "select * from CMDB_CI_CATEGORY where id=:id";
        return baseDao.queryForMap(sql,new SqlParameter("id",categoryid));
    }
    
    @Override
    public Map getDslCategoryById(String categoryid) {
        //TODO 需要加入缓存处理
        String sql = "select * from DSL_CATEGORY where id=:id";
        return baseDao.queryForMap(sql,new SqlParameter("id",categoryid));
    }
 
    @Override
    public CIPropertityMetadata getCIPropertityMetadata(String tablename) {
        
        if (StringUtils.isBlank(tablename) || StringUtils.equalsIgnoreCase(tablename, Constants.CMDB_CI_BASE_EXTEND_TABLE)) {
            CIPropertityMetadata meta = new CIPropertityMetadata();
            meta.setLevel(0);
            meta.setCommonPropertityMetadata(getMetadataByTableName(Constants.CMDB_CI_BASE_EXTEND_TABLE));
            return meta;
        }
        
        Map category = baseDao.queryForMap("select * from CMDB_CI_CATEGORY where TABLE_NAME=:TABLE_NAME",new SqlParameter("TABLE_NAME",tablename));
        CIPropertityMetadata meta = new CIPropertityMetadata();
        //级别
        meta.setLevel(ConvertUtil.obj2Int(category.get("JB")));
        meta.setCatetory(category);
        meta.setCommonPropertityMetadata(getMetadataByTableName(Constants.CMDB_CI_BASE_EXTEND_TABLE));
        
        if (meta.getLevel() == 1) {
            meta.setFirstLevelPropertityMetadata(getMetadataByTableName(category.get("TABLE_NAME").toString()));
        } else if (meta.getLevel() == 2) {
            meta.setSecondLevelPropertityMetadata(getMetadataByTableName(category.get("TABLE_NAME").toString()));
            Map firstCategory = getCategoryById(category.get("PID").toString());
            meta.setFirstLevelPropertityMetadata(getMetadataByTableName(firstCategory.get("TABLE_NAME").toString()));
        } else {
            meta.setThirdLevelPropertityMetadata(getMetadataByTableName(category.get("TABLE_NAME").toString()));
            Map secondCategory = getCategoryById(category.get("PID").toString());
            meta.setSecondLevelPropertityMetadata(getMetadataByTableName(secondCategory.get("TABLE_NAME").toString()));
            Map firstCategory = getCategoryById(secondCategory.get("PID").toString());
            meta.setFirstLevelPropertityMetadata(getMetadataByTableName(firstCategory.get("TABLE_NAME").toString()));
        }
        
        return meta;
    }
    
    public SUBCIPropertityMetadata getSubCIPropertityMetadata(String tablename){
        
        if (StringUtils.isBlank(tablename) || StringUtils.equalsIgnoreCase(tablename, Constants.CMDB_SUB_CI_BASE_EXTEND_TABLE)) {
            SUBCIPropertityMetadata meta = new SUBCIPropertityMetadata();
            meta.setCommonPropertityMetadata(getMetadataByTableName(Constants.CMDB_SUB_CI_BASE_EXTEND_TABLE));
            return meta;
        }
        
        Map category = baseDao.queryForMap("select * from CMDB_CI_CATEGORY where TABLE_NAME=:TABLE_NAME",new SqlParameter("TABLE_NAME",tablename));
        SUBCIPropertityMetadata meta = new SUBCIPropertityMetadata();
        //级别
        meta.setCatetory(category);
        meta.setCommonPropertityMetadata(getMetadataByTableName(Constants.CMDB_SUB_CI_BASE_EXTEND_TABLE));
        
        meta.setCustomPropertityMetadata(getMetadataByTableName(category.get("TABLE_NAME").toString()));
        
        return meta;
    }
 
    @Override
    public CICategory getCICategory(String categoryid) {
        Map base = getCategoryById(categoryid);
        CICategory category = new CICategory();
        category.setCategory(base);
        category.setLevel(ConvertUtil.obj2Integer(base.get("JB")));
        //一级
        if (category.getLevel() == 1) {
            category.setFirstCategory(base);
        } else if(category.getLevel() == 2) {
            category.setSecondCategory(base);
            category.setFirstCategory(getCategoryById(base.get("PID").toString()));
        } else {
            category.setThirdCategory(base);
            category.setSecondCategory(getCategoryById(base.get("PID").toString()));
            category.setFirstCategory(getCategoryById(category.getSecondCategory().get("PID").toString()));
        }
        return category;
    }
    
    
    @Override
    public DSLCategory getDSLCategory(String categoryid) {
        Map base = getDslCategoryById(categoryid);
        DSLCategory category = new DSLCategory();
        category.setCategory(base);
        category.setLevel(ConvertUtil.obj2Integer(base.get("LEVEL")));
        //一级
        if (category.getLevel() == 1) {
            category.setFirstCategory(base);
        } else if(category.getLevel() == 2) {
            category.setSecondCategory(base);
            category.setFirstCategory(getDslCategoryById(base.get("P_ID").toString()));
        } else {
            category.setThirdCategory(base);
            category.setSecondCategory(getDslCategoryById(base.get("P_ID").toString()));
            category.setFirstCategory(getDslCategoryById(category.getSecondCategory().get("P_ID").toString()));
        }
        return category;
    }
 
    public void copyData(String tableName,String source_id,List<String> target_ids){
        Map map = baseDao.queryForMap("select * from " + tableName +" where id=:id",new SqlParameter("id",source_id));
        if (map.isEmpty()) {
            return;
        }
        String sql = "insert into " + tableName + "(";
        String values = " values(";
        for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();
            sql += key + ",";
            values += ":"+key+",";
        }
        sql = StringUtils.removeEnd(sql, ",")+")" + StringUtils.removeEnd(values, ",") + ")";
        List<SqlParameter> list = new LinkedList<SqlParameter>();
        
        for (String targetid : target_ids) {
            SqlParameter param = new SqlParameter();
            param.putAll(map);
            param.put("ID", targetid);
            list.add(param);
        }
        baseDao.executeBatch(sql, list);
    }
    
    
    
    /*public void execCopyCI(String ci_id,int count,HttpServletRequest request){
        CMDB_CI_BASE base = new CMDB_CI_BASE(ci_id).getInstanceById();
        CICategory category = getCICategory(base.getThelevelid());
        String extendTableName = Constants.CMDB_CI_BASE_EXTEND_TABLE;
        String secondTableName = category.getSecondCategory().get("TABLE_NAME").toString();
        String thirdTableName = category.getLevel() == 3 ? category.getThirdCategory().get("TABLE_NAME").toString() :"";
        List<String> list = new LinkedList<String>();
        for (int i = 1; i < count; i++) {
            base.setId(StringUtil.getUUID());
            base.setSeachcode(sequenceService.getCICategorySequence(base.getThelevelid()));
            sequenceService.nextCICategorySequence(base.getThelevelid());
            base.setMembercode(String.valueOf(sequenceService.getMemberSequence(base.getMemberid())));
            sequenceService.nextMemberSequence(base.getMemberid());
            base.insert();
            
            //生成二维码
            TwoDimensionCode handler = new TwoDimensionCode();
            String searchCode = base.getSeachcode();
            String url = Constants.QRCODE_URL + searchCode;
            
            String logoUrl = WebUtil.getRealPath(request, "/static/logo.jpg");
            handler.encoderQRCode(url, WebUtil.getRealPath(request, "/upload/qrCode/"+searchCode+".png"), "png",logoUrl);
 
            list.add(base.getId());
        }
        copyData(extendTableName,ci_id,list);
        copyData(secondTableName,ci_id,list);
        copyData(thirdTableName,ci_id,list);
        
    }*/
 
    /**
     * 不需要比对的列
     * @return
     */
    public Map noCompairedColumns() {
        Map map = new HashMap();
        map.put("ID", "编号");
        map.put("FLOW_ID", "流程编号");
        map.put("SEACHCODE", "搜索码");
        map.put("MEMBERCODE", "内部编号");
        map.put("MEMBERID", "客户编号");
        map.put("MEMBERNAME", "客户名称");
        map.put("LEVELNUMBER", "层级序号");
        map.put("THELEVELID", "层级编号");
        map.put("FIRSTLEVELID", "一级编号");
        map.put("SECONDLEVELID", "二级编号");
        map.put("THIRDLEVELID", "三级编号");
        map.put("USINGSTATE", "使用状态");
        map.put("CREATETIME", "创建时间");
        map.put("VERSION", "版本");
        map.put("DISK_ID", "磁盘序列号");
        map.put("LEIX", "类型");
        map.put("PCI_ID", "父CI编号");
        
        return map;
    }
    
    /**
     * 不需要显示的列
     * @return
     */
    public Map noShowColumns() {
        Map map = new HashMap();
        map.put("LASTUPDATETIME", "最后更新时间");
        map.put("SERVICE_PROVIDERS_ID", "服务商编号");
        map.put("SYSTEMINTEGRATIONID", "集成商编号");
        map.put("MAINUFACTURERID", "厂商编号");
        map.put("DEPT_ID", "负责部门编号");
        map.put("SERVICE_PROVIDERS_NAME", "服务商名称");
        
        return map;
    }
}