gdg
2021-01-29 f675672004af0ff9071d21546a0eee0a0c8f3092
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
package com.nanometer.smartlab.service;
 
import java.util.*;
 
import javax.annotation.Resource;
 
import com.google.common.collect.ImmutableBiMap;
import com.nanometer.smartlab.entity.*;
import com.nanometer.smartlab.entity.enumtype.ArrivalStatus;
import com.nanometer.smartlab.util.ExcelUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import com.nanometer.smartlab.dao.SysLaboratoryContainerDao;
import com.nanometer.smartlab.exception.AlarmCode;
import com.nanometer.smartlab.exception.AlarmException;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import com.nanometer.smartlab.util.IDUtils;
import com.nanometer.smartlab.util.MessageUtil;
 
/**
 * Created by cmower on 17/11/20.
 */
@Service("sysLaboratoryContainerService")
public class SysLaboratoryContainerServiceImpl implements SysLaboratoryContainerService {
 
    private static Logger logger = Logger.getLogger(SysLaboratoryContainerService.class);
 
    @Resource(name = "sysLaboratoryContainerDao")
    SysLaboratoryContainerDao sysLaboratoryContainerDao;
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysLaboratoryContainer> getSysLaboratoryContainerList(String laboratoryType, String laboratoryName,
            String laboratoryId,String project,String controllerName, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(laboratoryType)) {
                params.put("laboratoryType", laboratoryType);
            }
            if (StringUtils.isNotBlank(laboratoryName)) {
                params.put("laboratoryName", "%" + laboratoryName + "%");
            }
            if (StringUtils.isNotBlank(laboratoryId)) {
                params.put("laboratoryId", laboratoryId);
            }
            params.put("project", project);
            params.put("controllerName", controllerName);
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.sysLaboratoryContainerDao.getSysLaboratoryContainerList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysLaboratoryContainer> getSysLaboratoryContainerList(
            String laboratoryId) {
         return getSysLaboratoryContainerList(null,null,laboratoryId,null,null,null,null);
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public int getSysLaboratoryContainerTotalCount(String laboratoryType, String laboratoryName, String laboratoryId,String project,String controllerName) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(laboratoryType)) {
                params.put("laboratoryType", laboratoryType);
            }
            if (StringUtils.isNotBlank(laboratoryName)) {
                params.put("laboratoryName", "%" + laboratoryName + "%");
            }
            if (StringUtils.isNotBlank(laboratoryId)) {
                params.put("laboratoryId", laboratoryId);
            }
            params.put("project", project);
            params.put("controllerName", controllerName);
            return this.sysLaboratoryContainerDao.getSysLaboratoryContainerTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysLaboratoryContainer getSysLaboratoryContainer(String id) {
        try {
            return this.sysLaboratoryContainerDao.getSysLaboratoryContainer(id);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysLaboratoryContainer getSysLaboratoryContainerByContainerCode(String containerCode) {
        try {
            return this.sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode(containerCode);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysLaboratoryContainer insertSysLaboratoryContainer(SysLaboratoryContainer sysLaboratoryContainer) {
        try {
            if (sysLaboratoryContainer.getId() == null) {
                sysLaboratoryContainer.setId(IDUtils.uuid());
            }
            this.sysLaboratoryContainerDao.insertSysLaboratoryContainer(sysLaboratoryContainer);
            this.sysLaboratoryContainerDao.updateSLContainerUser(sysLaboratoryContainer);
            return sysLaboratoryContainer;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE,
                    MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateSysLaboratoryContainer(SysLaboratoryContainer sysLaboratoryContainer) {
        try {
            int row = this.sysLaboratoryContainerDao.updateSysLaboratoryContainer(sysLaboratoryContainer);
            this.sysLaboratoryContainerDao.updateSLContainerUser(sysLaboratoryContainer);
            return row != 0;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE,
                    MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteSysLaboratoryContainer(List<SysLaboratoryContainer> sysLaboratoryContainerList) {
        try {
            if (sysLaboratoryContainerList == null || sysLaboratoryContainerList.size() == 0) {
                return false;
            }
 
            List<String> ids = new ArrayList<String>();
            for (SysLaboratoryContainer sysLaboratoryContainer : sysLaboratoryContainerList) {
                ids.add(sysLaboratoryContainer.getId());
                this.sysLaboratoryContainerDao.updateSLContainerUser(sysLaboratoryContainer);
            }
 
            int row = this.sysLaboratoryContainerDao.deleteSysLaboratoryContainers(ids);
            return row != 0;
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean isSysLaboratoryContainerExist(String containerCode, String editId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("containerCode", containerCode);
            params.put("editId", editId);
 
            int count = this.sysLaboratoryContainerDao.getSysLaboratoryContainerTotalCount(params);
            return count > 0;
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    @Transactional(readOnly = true)
    public List<SysLaboratoryContainer> getSysLaboratoryContainerInfoList(String startTime, String endTime){
        Map<String, String> params = ImmutableBiMap.of("startTime",startTime,"endTime",endTime);
        return sysLaboratoryContainerDao.getSysLaboratoryContainerInfoList(params);
    }
 
    @Override
    public void insertSysReagentList(List<SysLaboratoryContainer> list) {
        for (SysLaboratoryContainer container :list
        ) {
            SysLaboratoryContainer containermpl = new SysLaboratoryContainer();
            containermpl = sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode(container.getContainerCode());
            if(containermpl!=null){
                container.setId(containermpl.getId());
                this.sysLaboratoryContainerDao.updateSysLaboratoryContainer(container);
            }else{
                if (containermpl == null) {
                    container.setId(IDUtils.uuid());
                }
                this.sysLaboratoryContainerDao.insertSysLaboratoryContainer(container);
            }
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateInfo(Float temp,Float humidity,Float voc1,String containerId,String flag) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("temp", temp);
            params.put("humidity", humidity);
            params.put("voc1", voc1);
            params.put("containerId", containerId);
            params.put("flag", flag);
            this.sysLaboratoryContainerDao.updateInfo(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public List<Map> getContainerPersonInCharge(String containerCode) {
        try {
            return sysLaboratoryContainerDao.getContainerPersonInCharge(containerCode);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public Map selectLocationByContainerCode(String containerCode) {
        try {
            return sysLaboratoryContainerDao.selectLocationByContainerCode(containerCode);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public List<Map> exportExcelList(String laboratoryType, String laboratoryName) {
        Map<String, String> params = new HashMap<>();
        params.put("type", laboratoryType);
        params.put("name", laboratoryName);
        return sysLaboratoryContainerDao.exportExcelList(params);
    }
 
    @Override
    public void export2Excel(List<Map> list) throws Exception {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("labType", "实验室类型");
        map.put("labName", "实验室名称");
        map.put("controlName", "主控名称");
        map.put("containerCode", "临时存储库条码");
        map.put("containerType", "临时存储库类型");
        map.put("infoCode", "临时存储库状态码");
        map.put("structure", "临时存储库结构");
        map.put("name", "临时存储库名称");
        ExcelUtils.export2Excel(list,"实验室临时存储库管理",map);
 
    }
 
    @Override
    public List<String> selectProjectsByContainerCode(String containerCode) {
        String projects = sysLaboratoryContainerDao.selectProjectsByContainerCode(containerCode);
        if  (projects != null){
            return Arrays.asList(projects.split(","));
        }
        return null;
    }
 
}