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
package com.nanometer.smartlab.service;
 
import com.nanometer.smartlab.dao.SysWarehouseDao;
import com.nanometer.smartlab.entity.SysWarehouse;
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.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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Created by cmower on 17/11/20.
 */
@Service("sysWarehouseService")
public class SysWarehouseServiceImpl implements SysWarehouseService {
 
    private static Logger logger = Logger.getLogger(SysWarehouseService.class);
 
    @Resource(name = "sysWarehouseDao")
    SysWarehouseDao sysWarehouseDao;
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysWarehouse> getSysWarehouseList(String type, String name, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(type)) {
                params.put("type", type);
            }
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.sysWarehouseDao.getSysWarehouseList(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 getSysWarehouseTotalCount(String type, String name) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(type)) {
                params.put("type", type);
            }
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            return this.sysWarehouseDao.getSysWarehouseTotalCount(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 SysWarehouse getSysWarehouse(String id) {
        try {
            return this.sysWarehouseDao.getSysWarehouse(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 SysWarehouse insertSysWarehouse(SysWarehouse sysWarehouse) {
        try {
            if (sysWarehouse.getId() == null) {
                sysWarehouse.setId(IDUtils.uuid());
            }
            this.sysWarehouseDao.insertSysWarehouse(sysWarehouse);
            return sysWarehouse;
        } 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 updateSysWarehouse(SysWarehouse sysWarehouse) {
        try {
            int row = this.sysWarehouseDao.updateSysWarehouse(sysWarehouse);
 
            if (row == 0) {
                return false;
            }
            return true;
        } 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 deleteSysWarehouse(List<SysWarehouse> sysWarehouseList) {
        try {
            if (sysWarehouseList == null || sysWarehouseList.size() == 0) {
                return false;
            }
 
            List<String> ids = new ArrayList<String>();
            for (SysWarehouse sysWarehouse : sysWarehouseList) {
                ids.add(sysWarehouse.getId());
            }
 
            int row = this.sysWarehouseDao.deleteSysWarehouses(ids);
            if (row == 0) {
                return false;
            }
 
            return true;
        } 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);
        }
    }
    public List<SysWarehouse> getAllSysWarehouseList() {
        return this.sysWarehouseDao.getAllSysWarehouseList();
    }
    
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean isSysWarehouseExist(String barCode, String editId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("barCode", barCode);
            params.put("editId", editId);
 
            int count = this.sysWarehouseDao.getSysWarehouseTotalCount(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);
        }
    }
}