gdg
2020-12-09 458dba2e9a28227c3371571b0e7916baf9cce0e3
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
package com.nanometer.smartlab.service;
 
import com.nanometer.smartlab.dao.SysSupplierDao;
import com.nanometer.smartlab.entity.SysSupplier;
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.ExcelUtils;
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.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
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.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
 
/**
 * Created by johnny on 17/11/29.
 */
@Service("sysSupplierService")
public class SysSupplierServiceImpl implements SysSupplierService {
 
    private static Logger logger = Logger.getLogger(SysSupplierService.class);
 
    @Resource(name = "sysSupplierDao")
    SysSupplierDao sysSupplierDao;
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysSupplier> getSysSupplierList(String name, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.sysSupplierDao.getSysSupplierList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
    public List<SysSupplier> getSysSupplierList() {
        return this.getSysSupplierList(null, null, null);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public int getSysSupplierTotalCount(String name) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            return this.sysSupplierDao.getSysSupplierTotalCount(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 SysSupplier getSysSupplier(String id) {
        try {
            return this.sysSupplierDao.getSysSupplier(id);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public String getSysSupplierId(String name, String groupId) {
        try {
            return this.sysSupplierDao.getSysSupplierId(name, groupId);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public String getSysSupplierIdByname(String name) {
        try {
            return this.sysSupplierDao.getSysSupplierIdByname(name);
        } 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 SysSupplier insertSysSupplier(SysSupplier sysSupplier) {
        try {
            if (sysSupplier.getId() == null) {
                sysSupplier.setId(IDUtils.uuid());
            }
            this.sysSupplierDao.insertSysSupplier(sysSupplier);
            return sysSupplier;
        } 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 updateSysSupplier(SysSupplier sysSupplier) {
        try {
            int row = this.sysSupplierDao.updateSysSupplier(sysSupplier);
 
            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 deleteSysSupplier(List<SysSupplier> sysSupplierList) {
        try {
            if (sysSupplierList == null || sysSupplierList.size() == 0) {
                return false;
            }
 
            List<String> ids = new ArrayList<String>();
            for (SysSupplier sysSupplier : sysSupplierList) {
                ids.add(sysSupplier.getId());
            }
 
            int row = this.sysSupplierDao.deleteSysSupplier(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);
        }
    }
 
    @Override
    @Transactional
    public void importSupplier(FileUploadEvent event) throws Exception {
 
        List<SysSupplier> list = sysSupplierDao.getSysSupplierList(new HashMap());
        Map<String, String> supplierMap = new HashMap();
        list.forEach(supplier -> {
            supplierMap.put(supplier.getName(), supplier.getId());
        });
 
        UploadedFile file = event.getFile();
        InputStream is = file.getInputstream();
        boolean isExcel2003 = true;
        if (file.getFileName().matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        List<SysSupplier> sysSuppliers = new ArrayList<>();
        int totalRows = sheet.getPhysicalNumberOfRows();
        Row row = null;
        int totalCells = 0;
        for (int i = 1; i < totalRows; i++) {
            List<String> valuesList = new ArrayList<String>();
            row = sheet.getRow(i);
 
            totalCells = row.getPhysicalNumberOfCells();
            //System.out.println("====="+totalCells);
 
            // 目前导入文件11列,不满足条件的情况跳过
                /*if (totalCells != 12) {
                    throw new Exception("导入文件格式不正确");
                }*/
 
 
            for (int t = 0; t < totalCells; t++) {
                String cellInfo = "";
                if (row.getCell(t) != null) {
                    cellInfo = row.getCell(t).getStringCellValue();
                }
                valuesList.add(cellInfo);
            }
 
            SysSupplier sysSupplier = new SysSupplier();
            //这一行有供应商是数据库里的就跳过
            if (supplierMap.get(valuesList.get(0)) != null) {
                continue;
            }
 
            sysSupplier.setId(IDUtils.uuid());
            sysSupplier.setName(valuesList.get(0));
            sysSupplier.setPersonName(valuesList.get(1));
            sysSupplier.setPhone(valuesList.get(2));
            sysSupplier.setMemo(valuesList.get(3));
            sysSuppliers.add(sysSupplier);
 
        }
 
        if (sysSuppliers.size() > 0) {
            sysSupplierDao.insertBatch(sysSuppliers);
        }
 
    }
 
    @Override
    public List<Map> exportExcelList(String name) {
        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        return sysSupplierDao.exportExcelList(params);
    }
 
    @Override
    public void export2Excel(List<Map> list) throws Exception {
        Map<String,String> map = new LinkedHashMap<>();
        map.put("name", "供应商名");
        map.put("personName", "联系人");
        map.put("phone", "电话");
        map.put("memo", "备注");
        ExcelUtils.export2Excel(list,"供应商信息",map);
    }
 
}