add
gdg
2020-10-30 9222b2db700cd25e46b67c2a3c05fffd3817cc64
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
package com.nanometer.smartlab.service;
 
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
import javax.annotation.Resource;
import javax.ejb.EJB;
 
import com.nanometer.smartlab.dao.SysLaboratoryContainerDao;
import com.nanometer.smartlab.dao.SysWarehouseContainerDao;
import com.nanometer.smartlab.entity.*;
import com.nanometer.smartlab.util.Constants;
import com.nanometer.smartlab.util.Utils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
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.OpeUseFlowDao;
import com.nanometer.smartlab.entity.enumtype.ArrivalStatus;
import com.nanometer.smartlab.entity.enumtype.SeeFlag;
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;
import org.springframework.util.Assert;
 
/**
 * Created by johnny on 17/12/16.
 */
@Service("OpeUseFlowService")
public class OpeUseFlowServiceImpl implements OpeUseFlowService {
 
    private static Logger logger = Logger.getLogger(OpeUseFlowService.class);
 
    @Resource(name = "opeUseFlowDao")
    OpeUseFlowDao opeUseFlowDao;
    @Resource
    private SysLaboratoryContainerDao sysLaboratoryContainerDao;
    @Resource
    private SysWarehouseContainerDao sysWarehouseContainerDao;
 
    @Resource
    private SysUserService sysUserService;
 
 
    @Transactional(propagation = Propagation.REQUIRED)
    public OpeUseFlow insertOpeUseFlow(OpeUseFlow opeUseFlow) {
        try {
            if (opeUseFlow.getId() == null) {
                opeUseFlow.setId(IDUtils.uuid());
            }
 
            if(opeUseFlow.getCreateTime()==null){
                opeUseFlow.setCreateTime(new Timestamp(System.currentTimeMillis()));
            }
 
            this.opeUseFlowDao.insertOpeUseFlow(opeUseFlow);
            return opeUseFlow;
        } 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 List<OpeUseFlow> getOpeUseFlowList(String reagentId, String reagentCode, Integer status, String userId,
                                              Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
 
            params.put("reagentId", reagentId);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("status", status);
 
            addParamByUserId(userId, params);
 
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeUseFlowDao.getOpeUseFlowList(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 int getOpeUseFlowTotalCount(String reagentId, String reagentCode, Integer status, String userId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentId", reagentId);
 
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
 
            addParamByUserId(userId, params);
 
            params.put("status", status);
            return this.opeUseFlowDao.getOpeUseFlowTotalCount(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<OpeUseFlow> getOpeUseFlowListByName(String houseName,String reagentId, String reagentCode, Integer status, String userId,
                                                    Date startDate,Date endDate,Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
 
            params.put("reagentName", reagentId);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
 
            SimpleDateFormat sdfstart = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            SimpleDateFormat sdfend = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
            Date now = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            cal.add(Calendar.DATE, -7);
            Date startTime = cal.getTime();
 
            if (null != startDate){
                try {
                    params.put("startDate",sdfstart.format(startDate));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                params.put("startDate",sdfstart.format(startTime));
            }
 
            if (null != endDate){
                try {
                    params.put("endDate",sdfend.format(endDate));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                params.put("endDate",sdfend.format(now));
            }
 
            params.put("status", status);
            params.put("houseName", houseName);
            addParamByUserId(userId, params);
 
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeUseFlowDao.getOpeUseFlowList(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 int getOpeUseFlowTotalCountByName(String houseName,String reagentId, String reagentCode, Integer status, String userId,Date startDate,Date endDate) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentName", reagentId);
 
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
 
            SimpleDateFormat sdfstart = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            SimpleDateFormat sdfend = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
            Date now = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            cal.add(Calendar.DATE, -7);
            Date startTime = cal.getTime();
 
            if (null != startDate){
                try {
                    params.put("startDate",sdfstart.format(startDate));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                params.put("startDate",sdfstart.format(startTime));
            }
 
            if (null != endDate){
                try {
                    params.put("endDate",sdfend.format(endDate));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                params.put("endDate",sdfend.format(now));
            }
 
            params.put("houseName", houseName);
            addParamByUserId(userId, params);
 
            params.put("status", status);
            return this.opeUseFlowDao.getOpeUseFlowTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    private void addParamByUserId(String userId, Map<String, Object> params) {
        if (StringUtils.isNotBlank(userId)) {
            SysUser sysUser = sysUserService.getSysUser(userId);
 
            // 不是管理员时,加入userid
            if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey()||sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) {
 
            } else {
                params.put("userId", userId);
            }
 
//            // 负责人,需要把部门的userid传递到SQL
//            if (sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) {
//                List<String> departmentUserIds = new ArrayList<String>();
//                List<SysUser> departmentUsers = sysUserService.getSeeUserList(sysUser.getDepartment());
//                for (SysUser departmentUser : departmentUsers) {
//                    departmentUserIds.add(departmentUser.getId());
//                }
//
//                if (departmentUserIds.size() > 0) {
//                    params.put("departmentUserIds", departmentUserIds);
//                }
//
//            } else if (sysUser.getSeeFlag().getKey() == SeeFlag.NORMAL.getKey()) {
//                params.put("userId", userId);
//            }
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean update(OpeUseFlow opeUseFlow) {
        try {
            int row = this.opeUseFlowDao.update(opeUseFlow);
            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);
        }
    }
 
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void syncReagentFlow(List<LinkedHashMap> reagentFlowList) throws ParseException {
        Assert.notNull(reagentFlowList, "请求参数不能为空!");
        for (Map params : reagentFlowList) {
            //0:个人领用 1:在库 3:报废
            //<!--0:个人领用 1:在仓库sys_warehouse_container 2:在实验室sys_laboratory_container 3:报废-->
            OpeUseFlow opeUseFlow = new OpeUseFlow();
            opeUseFlow.setId(IDUtils.uuid());
            opeUseFlow.setCreateTime(new Timestamp(DateUtils.parseDate(params.get("updateTime").toString(), Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS).getTime()));
            opeUseFlow.setStatus(ArrivalStatus.parse(Integer.valueOf(String.valueOf(params.get("status")))));
            opeUseFlow.setReagentCode((String) params.get("reagentCode"));
            if (params.get("idcard") != null) {
                SysUser sysUser = sysUserService.getSysUserByIdCard((String) params.get("idcard"));
                if (Objects.isNull(sysUser)) {
                    logger.error("The idcard:" + (String) params.get("idcard") + " has not been exists!");
                    continue;
                }
                opeUseFlow.setUserId(sysUser.getId());
                opeUseFlow.setContainerId("0");
                opeUseFlow.setHouseId("0");
            } else {
                logger.error("The idcard is empty!");
                continue;
            }
 //           if (params.get("status").toString().equals("1")) {
                SysWarehouseContainer container = sysWarehouseContainerDao.getSysWarehouseContainerByContainerCode((String) params.get("containerCode"));
                if (Objects.isNull(container)==false) {
                    if (params.get("status").toString().equals("1")) {
                        opeUseFlow.setStatus(ArrivalStatus.WAREHOUSE);
                    } else {
                        opeUseFlow.setStatus(ArrivalStatus.PERSONAL);
                    }
                    opeUseFlow.setContainerId(container.getId());
                    opeUseFlow.setHouseId(container.getWarehouseId());
                    opeUseFlow.setPlace(container.getStructure());
                }else{
                    SysLaboratoryContainer sysLaboratoryContainer = sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode((String) params.get("containerCode"));
                    if (Objects.isNull(sysLaboratoryContainer)) {
                        logger.error("The containerCode["+(String) params.get("containerCode")+"] has not been exists!");
                        continue;
                    }
                    if (params.get("status").toString().equals("1")) {
                        opeUseFlow.setStatus(ArrivalStatus.LABORATORY);
                    } else {
                        opeUseFlow.setStatus(ArrivalStatus.PERSONAL);
                    }
                    opeUseFlow.setContainerId(sysLaboratoryContainer.getId());
                    opeUseFlow.setHouseId(sysLaboratoryContainer.getLaboratoryId());
                    opeUseFlow.setPlace(sysLaboratoryContainer.getStructure());
                }
 //           }
 
            if(params.get("remainder") != null && !params.get("remainder").toString().equals("")) {
 
                opeUseFlow.setRemainder(new BigDecimal(params.get("remainder").toString()).setScale(2, BigDecimal.ROUND_HALF_UP));
            }
 
            if(opeUseFlow.getCreateTime()==null){
                opeUseFlow.setCreateTime(new Timestamp(System.currentTimeMillis()));
            }
 
 
            opeUseFlowDao.insertOpeUseFlow(opeUseFlow);
        }
    }
 
    @Override
    public void deleteByReagentCode(String reagentCode) {
        opeUseFlowDao.deleteByReagentCode(reagentCode);
    }
 
    @Override
    public void updateTimeByCode(String code) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("code", code);
        params.put("status",-1);
        opeUseFlowDao.updateTimeByCode(params);
    }
}