cy
2022-06-23 da30b02214ab3a35a3b2ac163afce87e70a39ff4
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
package cn.ksource.web.facade.tj;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Service;
 
import cn.ksource.core.dao.BaseDao;
import cn.ksource.core.util.ConvertUtil;
import cn.ksource.core.util.JsonUtil;
import cn.ksource.core.util.StringUtil;
import cn.ksource.core.util.TreeUtil;
import cn.ksource.core.web.WebUtil;
@Service
public class CiStatisFacadeImpl implements CiStatisFacade{
    @Resource
    private BaseDao baseDao;
 
    public Map getCiTypeChart(String cusId,String lv1Id,String lv2Id,String queryType){
        if(StringUtil.isBlank(queryType)){
            queryType = "CHART";
        }
        
        
        String level = "1";
        String where_c = "";
        String where_q = "";
        
        String column = "lv1_id";
        if(StringUtil.isNotBlank(lv1Id)){
            if(StringUtil.isBlank(lv2Id)){
                column = "lv2_id";
                where_q = " and lv1_id =:lv1Id ";
                level = "2";
                where_c = " and PID =:lv1Id ";
                
            }else{
                column = "lv3_id";
                where_q = " and lv2_id =:lv2Id ";
                level = "3";
                where_c = " and PID =:lv2Id ";
            }
        }
        
        Map param = new HashMap();
        param.put("cusId", cusId);
        param.put("level", level);
        param.put("lv1Id", lv1Id);
        param.put("lv2Id", lv2Id);
        
        StringBuilder sql = new StringBuilder();
        String realSql;
        sql.append(" select count(id) num,$column id from cmdb_ci_base  ");
        sql.append(" where state=1 and CUS_ID = :cusId  $where ");
        sql.append(" group by $column  ");
        realSql = sql.toString().replace("$column", column);
        realSql = realSql.replace("$where", where_q);
        
        List<Map> queryList = baseDao.queryForList(realSql,param);
        Map queryMap = new HashMap();
        if(queryList!=null&&queryList.size()>0){
            for(Map map:queryList){
                queryMap.put(map.get("id").toString(), map.get("num"));
            }
        }
        
        sql.setLength(0);
        sql.append(" select id,name from cmdb_ci_category where type=1   ");
        sql.append(" and lv=:level  $where and state=1 and CATE_TYPE=1  order by SERIAL asc ");
        realSql = sql.toString().replace("$where", where_c);
        List<Map> categoryList = baseDao.queryForList(realSql,param);
        
        
        List<List<String>> series = new ArrayList<List<String>>();
        List<Map> tableData = new ArrayList<Map>();
        if(queryType.equals("CHART")){
            for(Map map:categoryList){
                List tempList = new ArrayList();
                tempList.add(map.get("name").toString());
                if(queryMap.get(map.get("id").toString())!=null){
                    tempList.add(queryMap.get(map.get("id").toString()));
                }else{
                    tempList.add(0);
                }
                series.add(tempList);
            }
        }else{
            for(Map map:categoryList){
                Map tempMap = new HashMap();
                tempMap.put("name", map.get("name").toString());
                if(queryMap.get(map.get("id").toString())!=null){
                    tempMap.put("num", queryMap.get(map.get("id").toString()));
                }else{
                    tempMap.put("num", "0");
                }
                tableData.add(tempMap);
            }
        }
        
        System.out.println("----------"+JsonUtil.list2Json(tableData));
        Map resMap = new HashMap();
        resMap.put("series", series);
        resMap.put("tableData", tableData);
        return resMap;
    }
    
    public Map getCiCountChart(String cusId){
        Map param = new HashMap();
        param.put("cusId", cusId);
        
        StringBuilder sql = new StringBuilder();
        sql.append(" select total.lv3_id id,total.total_num," +
                "IFNULL(normal.nor_num,'-') nor_num,IFNULL(abandon.abd_num,'-') abd_num," +
                "IFNULL(new.new_num,'-') new_num from  ");
        sql.append(" ( ");
        sql.append("     select count(id) total_num,lv3_id  ");
        sql.append("      from cmdb_ci_base where cus_ID = :cusId ");
        sql.append("      group by lv3_id  ");
        sql.append("  ) total ");
        sql.append("  LEFT JOIN ");
        sql.append("  ( ");
        sql.append("      select count(id) nor_num,lv3_id  ");
        sql.append("      from  cmdb_ci_base where state = 1 and cus_ID = :cusId  ");
        sql.append("      group by lv3_id  ");
        sql.append("  ) normal ");
        sql.append("  on total.lv3_id = normal.lv3_id ");
        sql.append("  LEFT JOIN ");
        sql.append("  ( ");
        sql.append("      select count(id) abd_num,lv3_id  ");
        sql.append("      from  cmdb_ci_base where state = 2 and cus_ID = :cusId   ");
        sql.append("      group by lv3_id ");
        sql.append("  ) abandon  ");
        sql.append("  on total.lv3_id = abandon.lv3_id ");
        sql.append("  LEFT JOIN ");
        sql.append("  ( ");
        sql.append("      select count(id) new_num,lv3_id   ");
        sql.append("      from  cmdb_ci_base where state = 3 and cus_ID = :cusId   ");
        sql.append("      group by lv3_id ");
        sql.append("  ) new  ");
        sql.append("  on total.lv3_id = new.lv3_id ");
        List<Map> queryList = baseDao.queryForList(sql.toString(),param);
        
        
        sql.setLength(0);
        sql.append(" select id,name text,pid parentId,serial sortId ");
        sql.append(" from cmdb_ci_category  ");
        sql.append(" where type=1 and state=1  and CATE_TYPE=1 ");
        List<Map> categoryList = baseDao.queryForList(sql.toString(),param);
        TreeUtil treeUtil = new TreeUtil();
        Map rootMap = treeUtil.createTree(categoryList,queryList);
        
        Map resMap = new HashMap();
        resMap.put("rootMap", rootMap);
        return resMap;
    }
    
    public List getCiCategoryList(String lv1Id){
        StringBuilder sql = new StringBuilder();
        Map param = new HashMap();
        param.put("lv1Id", lv1Id);
        
        sql.append(" select id,name from cmdb_ci_category ");
        sql.append(" where type=1  and CATE_TYPE=1");
        if(StringUtil.isBlank(lv1Id)){
            sql.append(" and lv =1 ");
        }else{
            sql.append(" and lv =2 and pid = :lv1Id ");
        }
        sql.append(" and state=1 order by SERIAL ");
        return baseDao.queryForList(sql.toString(),param);
    }
    
    
    public Map getCiMainufacturerChart(String cusId,String queryType){
        
        String column;
        if(queryType.equals("1")){
            column = "MFT_ID";
        }else{
            column = "IGT_ID";
        }
        
        Map param = new HashMap();
        param.put("cusId", cusId);
        param.put("queryType", queryType);
        
        
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,MANUFACTURERNAME name from CMDB_MAINUFACTURER ");
        sql.append(" where customer_id = :cusId and state=1 and type=:queryType ");
        List<Map> lebalList = baseDao.queryForList(sql.toString(),param);        
        
        sql.setLength(0);
        sql.append(" select id,name text,pid parentId,serial sortId ");
        sql.append(" from cmdb_ci_category  ");
        sql.append(" where cate_type=1 and state=1  ");
        List<Map> categoryList = baseDao.queryForList(sql.toString(),param);
                
        sql.setLength(0);
        sql.append(" select CONCAT(lv3_id,'_',$column) id,count(ID) num from cmdb_ci_base  ");
        sql.append(" where lv3_id is not null ");
        sql.append(" and $column is not NULL and $column<>'' ");
        sql.append(" and cus_ID = :cusId ");
        sql.append(" group by lv3_id,$column ");
        String realSql = sql.toString().replace("$column", column);
        List<Map> queryList = baseDao.queryForList(realSql,param);
        
        TreeUtil treeUtil = new TreeUtil();
        Map rootMap = treeUtil.createTree(categoryList,queryList,lebalList);
        
        Map resMap = new HashMap();
        resMap.put("rootMap", rootMap);
        resMap.put("lebalList", lebalList);
        return resMap;
    }
    @SuppressWarnings("unchecked")
    @Override
    public List queryCiListData(HttpServletRequest request) {
        String cusId = request.getParameter("cusId");
        String subId = request.getParameter("subId");
        String categoryId = request.getParameter("categoryId");
        String subChildId = request.getParameter("subChildId");
        String sjState=request.getParameter("sjState");
        Map param = new HashMap();
        param.put("cusId", cusId);
        param.put("categoryId", categoryId);
        param.put("subId", subId);
        param.put("subChildId", subChildId);
        param.put("sjState", sjState);
        
        
        //开始查询
        StringBuilder sql = new StringBuilder();
        sql.append(" select c.*,CONCAT(lv1.NAME,'->',lv2.name,'->',lv3.NAME) CATENAME,c.ID,c.CINAME,c.POSITION ");
        sql.append(" from CMDB_CI_BASE c inner JOIN cmdb_ci_category lv3 on c.LV3_ID = lv3.ID ");
        if (StringUtil.notEmpty(cusId)) {
            sql.append(" AND c.CUS_ID = :cusId ");
        }
        if (StringUtil.notEmpty(subChildId)) {
            sql.append(" AND c.SUB_CUS_ID = :subChildId ");
        }else if(StringUtil.notEmpty(subId)) {
            String sqlb="select id from SC_PARTNER_CUSTOMER_SUB where p_id=:subId and MY_LEVEL=2 and DEL_FLAG=1";
            List<Map> list1=baseDao.queryForList(sqlb, param);
            if(list1.size()>0){
                String str="";
                for(Map map:list1){
                    str+=ConvertUtil.obj2StrBlank(map.get("id"))+",";
                }
                str=str+subId;
                param.put("subId", Arrays.asList(str.split(",")));
                sql.append(" AND c.SUB_CUS_ID in( :subId) ");
            }
            
        }
        if (StringUtil.isNotBlank(categoryId)) {
            sql.append(" and c.LV3_ID = :categoryId ");
        }
        //审计状态
        if(StringUtil.isNotBlank(ConvertUtil.obj2StrBlank(param.get("sjState")))){
            sql.append(" and c.sj_state = :sjState ");
        }
        sql.append(" inner JOIN cmdb_ci_category lv2 on c.LV2_ID = lv2.ID ");
        sql.append(" inner JOIN cmdb_ci_category lv1 on c.LV1_ID = lv1.ID ");
        sql.append(" where 1 = 1 ");
 
        return baseDao.queryForList(sql.toString(), param);
    }
}