yujian
2019-03-14 4aadf484e193995c23ee1d5bb1971a497d2f9a0d
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package com.yeshi.fanli.service.impl.config;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.config.SystemClientParamsDao;
import com.yeshi.fanli.dao.config.SystemHelpListDao;
import com.yeshi.fanli.dao.config.SystemSecondProblemDao;
import com.yeshi.fanli.dao.mybatis.SystemClientParamsMapper;
import com.yeshi.fanli.entity.admin.SystemClientParamsAdmin;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.entity.system.CustomerContent;
import com.yeshi.fanli.entity.system.CustomerName;
import com.yeshi.fanli.entity.system.SystemClientParams;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.config.SystemClientParamsService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class SystemClientParamsServiceImpl implements SystemClientParamsService {
 
    @Resource
    private SystemClientParamsDao systemClientParamsDao;
    
    @Resource
    private SystemClientParamsMapper systemClientParamsMapper;
    
    @Resource
    private SystemHelpListDao systemHelpListDao;
    
    @Resource
    private BusinessSystemService businessSystemService;
    
    @Resource
    private SystemSecondProblemDao systemSecondProblemDao;
    @Cacheable(value = "clientParamCache", key = "#id")
    public List<SystemClientParams> getSystemClientParamsBySystemId(long id) {
 
        return systemClientParamsDao.list("from SystemClientParams scp where scp.system.id=?",
                new Serializable[] { id });
    }
    
    @Cacheable(value = "clientParamCache", key = "#id+'-'+#version")
    public List<SystemClientParams> getSystemClientParamsBySystemId(long id,int version) {
 
        return systemClientParamsDao.list("from SystemClientParams scp where scp.system.id=?",
                new Serializable[] { id });
    }
 
    public List<SystemClientParamsAdmin> getAdminList(BusinessSystem system, String key) {
        List<SystemClientParams> list;
        List<SystemClientParamsAdmin> adminList = new ArrayList<SystemClientParamsAdmin>();
        if (system == null) {
            list = systemClientParamsDao.list("from SystemClientParams scp where scp.name like ? ",
                    new Serializable[] { "%" + key + "%" });
        } else {
            list = systemClientParamsDao.list(
                    "from SystemClientParams scp where scp.system.id = ? and scp.name like ? ",
                    new Serializable[] { system.getId(), "%" + key + "%" });
            StringBuffer hqlb = new StringBuffer("from SystemClientParams scp ");
            int ii = 0;
            if (list.size() > 0) {
                for (SystemClientParams scp : list) {
                    if (ii == 0) {
                        hqlb.append(" where ( scp.name = '" + scp.getName() + "'  and scp.key = '" + scp.getKey()
                                + "' and scp.value = '" + scp.getValue() + "' ) ");
                    } else {
                        hqlb.append(" or ( scp.name = '" + scp.getName() + "'  and scp.key = '" + scp.getKey()
                                + "' and scp.value = '" + scp.getValue() + "' ) ");
                    }
                    ii++;
                }
                list = systemClientParamsDao.list(hqlb.toString());
            }
        }
        
        SystemClientParamsAdmin scpa = null;
        for (SystemClientParams scp : list) {
            scpa = new SystemClientParamsAdmin(scp);
            if (adminList.contains(scpa)) {
                int indexof = adminList.indexOf(scpa);
                SystemClientParamsAdmin systemClientParamsAdmin = adminList.get(indexof);
                systemClientParamsAdmin.getSystemList().add(scp.getSystem());
            } else {
                scpa.getSystemList().add(scp.getSystem());
                adminList.add(scpa);
            }
        }
 
        return adminList;
    }
 
    @Transactional
    public void addSystemClientParamsList(SystemClientParams scp, List<BusinessSystem> list) throws Exception {
        SystemClientParams nscp = null;
        for (BusinessSystem system : list) {
            nscp = new SystemClientParams();
            nscp.setKey(scp.getKey());
            nscp.setName(scp.getName());
            nscp.setValue(scp.getValue());
            nscp.setSystem(system);
            nscp.setUpdatetime(java.lang.System.currentTimeMillis());
            systemClientParamsDao.create(nscp);
        }
    }
 
    @Transactional
    public void deleteSystemClientParams(final SystemClientParams scp, final BusinessSystem system) {
        systemClientParamsDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(
                        "delete from SystemClientParams scp where scp.name = ? and scp.key =? and scp.value = ? and scp.system.id = ?");
                query.setParameter(0, scp.getName());
                query.setParameter(1, scp.getKey());
                query.setParameter(2, scp.getValue());
                query.setParameter(3, system.getId());
                return query.executeUpdate();
            }
        });
    }
 
    public void addSystemClientParams(SystemClientParams scp, BusinessSystem system) {
        scp.setSystem(system);
        systemClientParamsDao.create(scp);
    }
 
    @Transactional
    public void deleteSystemClientParams(final SystemClientParams scp) {
        systemClientParamsDao.excute(new HibernateCallback() {
 
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(
                        "delete from SystemClientParams scp where scp.name=? and scp.key = ?  and scp.value = ? ");
                query.setParameter(0, scp.getName());
                query.setParameter(1, scp.getKey());
                query.setParameter(2, scp.getValue());
                return query.executeUpdate();
            }
        });
    }
 
    public SystemClientParamsAdmin getSystemClientParamsAdmin(final SystemClientParams scp) {
 
        return (SystemClientParamsAdmin) systemClientParamsDao.excute(new HibernateCallback<SystemClientParamsAdmin>() {
            public SystemClientParamsAdmin doInHibernate(Session session) throws HibernateException {
                Query query = session
                        .createQuery("from SystemClientParams s where s.key = ?");
                query.setParameter(0, scp.getKey());
                List<SystemClientParams> list = query.list();
                SystemClientParamsAdmin scpa = new SystemClientParamsAdmin(scp);
                for (SystemClientParams fscp : list) {
                    BusinessSystem system = fscp.getSystem();
                    if (scpa.getSystemList().contains(system)) {
                        continue;
                    }
                    scpa.getSystemList().add(fscp.getSystem());
                    scpa.setSystemClientParams(fscp);
                }
                return scpa;
            }
        });
 
    }
 
    @Transactional
    public void update(final SystemClientParams oscp, final SystemClientParams nscp) {
        systemClientParamsDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(
                        "update SystemClientParams s set s.name=? , s.key=? , s.value=? where s.name=? and s.key = ? ");
                query.setParameter(0, nscp.getName());
                query.setParameter(1, nscp.getKey());
                query.setParameter(2, nscp.getValue());
                query.setParameter(3, oscp.getName());
                query.setParameter(4, oscp.getKey());
                return query.executeUpdate();
            }
        });
    }
 
    @Override
    public SystemClientParams getSystemClientParamsBySystemAndKey(BusinessSystem system, String key) {
        List<SystemClientParams> list = systemClientParamsDao.list(
                "from SystemClientParams scp where scp.system.id=? and scp.key=?",
                new Serializable[] { system.getId(), key });
        if (list == null || list.size() == 0)
            return null;
        else
            return list.get(0);
    }
 
    @Override
    public Integer getCount(int type) {
        return systemClientParamsMapper.getCount(type);
    }
    
    @Override
    public List<CustomerName> customerNameList(int page, String key, int type) {
        int start = page * Constant.PAGE_SIZE;
        return systemHelpListDao.list(
 
            "from CustomerName cn where cn.type = ? and cn.name like ? order by cn.createTime asc", start,
            Constant.PAGE_SIZE, new Serializable[] {type, "%"+key+"%"});
    }
 
    @Override
    public void deleteHelp(long id) {
        systemHelpListDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) 
                                throws HibernateException {
                Query query = session.createQuery("delete from CustomerName cn where cn.id = ?");
                query.setParameter(0, id);
                query.executeUpdate();
                return null;
            }
        });
    }
    
 
    @Override
    public CustomerName getHelpCenter(int type) {
        return systemClientParamsMapper.getHelpCenter(type);
    }
 
    @Override
    public Integer getProblemCount(long id) {
        return systemClientParamsMapper.getProblemCount(id);
    }
 
    @Override
    public List<CustomerContent> getSecondProblemList(int index, String key, long id) {
        int start = index * Constant.PAGE_SIZE;
        return systemSecondProblemDao.list(
                "from CustomerContent cc where cc.cnId = ? and cc.title like ? order by cc.createTime asc", start,
                Constant.PAGE_SIZE, new Serializable[] {id, "%"+key+"%"});
    }
    
    
    @Override
    public void deleteSecondProblem(long id) {
        systemSecondProblemDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) 
                                throws HibernateException {
                Query query = session.createQuery("delete from CustomerContent cc where cc.id = ?");
                query.setParameter(0, id);
                query.executeUpdate();
                return null;
            }
        });
    }
 
    @Override
    public CustomerContent getCustomerContent(long id) {
        return systemSecondProblemDao.find(CustomerContent.class, id);
    }
 
    @Override
    public void updateAnswerDetail(CustomerContent customerContent) {
        CustomerContent find = systemSecondProblemDao.find(CustomerContent.class, customerContent.getId());
        if(find == null ){
            return;
        }
        find.setTitle(customerContent.getTitle());
        find.setContent(customerContent.getContent());
        systemSecondProblemDao.update(find);
    }
 
    @Override
    public void addFirstMenu(CustomerName customerName, int type) {
        customerName.setName(customerName.getName());
        customerName.setCreateTime(new Date());
        customerName.setType(type);
        systemHelpListDao.save(customerName);
    }
 
    @Override
    public void addSecondMenu(CustomerContent customerContent, long cnId) {
        customerContent.setCnId(cnId);
        customerContent.setTitle(customerContent.getTitle());
        customerContent.setContent(customerContent.getContent());
        customerContent.setCreateTime(new Date());
        systemSecondProblemDao.save(customerContent);
    }
 
    @Override
    public List<CustomerContent> contactCustomerService(String title) {
        return systemClientParamsMapper.contactCustomerService(title);
    }
 
    
    @Override
    public List<SystemClientParams> listQuery(int start, int count, String key, Long systemId) {
        
        List<SystemClientParams> list = null;
        
        if (systemId == null) {
            
            if (StringUtil.isNullOrEmpty(key)) {
                list = systemClientParamsDao.list("from SystemClientParams scp", start, count, null);
            } else {
                list = systemClientParamsDao.list("from SystemClientParams scp where (scp.name like ? or scp.key like ?) ",
                        start, count, new Serializable[] { "%" + key + "%" ,"%" + key + "%"});
            }
            
        } else {
            
            if (StringUtil.isNullOrEmpty(key)) {
                list = systemClientParamsDao.list("from SystemClientParams scp where scp.system.id = ? ",
                        start, count, new Serializable[] {systemId});
                
            } else {
                list = systemClientParamsDao.list(
                        "from SystemClientParams scp where scp.system.id = ? and (scp.name like ? or scp.key like ?)",
                        start, count, new Serializable[] {systemId, "%" + key + "%" ,"%" + key + "%"});
            }
        }
        
        return list;
    }
 
    
    @Override
    public long countQuery(String key, Long systemId) {
        
        if (systemId == null) {
            
            if (StringUtil.isNullOrEmpty(key)) {
                return systemClientParamsDao.getCount(" select count(id) from SystemClientParams scp");
            } else {
                return systemClientParamsDao.getCount(" select count(id) from SystemClientParams scp where (scp.name like ? or scp.key like ?) ",
                        new Serializable[] { "%" + key + "%" ,"%" + key + "%"});
            }
            
        } else {
            
            if (StringUtil.isNullOrEmpty(key)) {
                return systemClientParamsDao.getCount(" select count(id) from SystemClientParams scp where scp.system.id = ? ",
                         new Serializable[] {systemId});
                
            } else {
                return systemClientParamsDao.getCount(
                        " select count(id) from SystemClientParams scp where scp.system.id = ? and (scp.name like ? or scp.key like ?)",
                        new Serializable[] {systemId, "%" + key + "%" ,"%" + key + "%"});
            }
        }
        
    }
    
    
    @Transactional
    @Override
    public void saveAdd(SystemClientParams scp, List<Long> listId) throws Exception {
        
        if (listId == null || listId.size() == 0) {
            return;
        }
        
        SystemClientParams nscp = null;
        for (Long systemId : listId) {
            BusinessSystem system  = new BusinessSystem(systemId);
            nscp = new SystemClientParams();
            nscp.setKey(scp.getKey());
            nscp.setName(scp.getName());
            nscp.setValue(scp.getValue());
            nscp.setSystem(system);
            nscp.setUpdatetime(java.lang.System.currentTimeMillis());
            systemClientParamsDao.create(nscp);
        }
    }
    
    @Override
    public void update(SystemClientParams systemClientParams) {
        systemClientParamsDao.update(systemClientParams);
    }
    
    @Override
    public List<SystemClientParams> getById(long id) {
        return systemClientParamsDao.list("from SystemClientParams scp where scp.id=?",
                new Serializable[] { id });
    }
    
    @Transactional
    @Override
    public void deleteBatchByPrimaryKey(List<Long> ids) {
        for (Long id: ids) {
            systemClientParamsDao.excute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Query query = session.createQuery("delete from SystemClientParams scp where scp.id= ?");
                    query.setParameter(0, id);
                    return query.executeUpdate();
                }
            });
        }
    }
}