18516761980
2021-06-07 f5da19565cebc364d23a65ebe931b3c38cecfa09
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
package com.slcf.service.impl;
 
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletResponse;
 
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;
 
import com.slcf.bean.MatCodeCondition;
import com.slcf.dao.MatCodeDao;
import com.slcf.erp.OperateOracle;
import com.slcf.pojo.LocDetailBean;
import com.slcf.pojo.MatCodeBean;
import com.slcf.pojo.WorkDetailBean;
import com.slcf.service.MatCodeService;
import com.slcf.util.DateTimeUtil;
 
/**
 * 物料编码接口实现
 * @author admin
 * @date 2018年11月10日
 */
@Service
public class MatCodeServiceImpl implements MatCodeService {
 
    @Autowired
    MatCodeDao matCodeDao;
    
    /**
     * 添加
     */
    @Transactional
//    public int insertMatCode(MatCodeBean matCode) throws Exception{
//        int result=0;
//        try {
//            result=matCodeDao.insertMatCode(matCode);
//        }catch(Exception e) {
//            throw new RuntimeException(e.getMessage());
//        }
//        return result;
//    }
    public int insertMatCode(MatCodeBean matCode) throws Exception{
        int result=0;
        try {
            result=matCodeDao.insertMatCode(matCode);
//            if(result>0) {
////                matCode.setMat_no("555");
////                matCode.setMat_name("abcdefghijk");
//                result=matCodeDao.insertMatCode(matCode);
//                if(result>=0){
//                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                    throw new RuntimeException("1111");
//                }
//            }
        }catch(Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            System.out.println(e.getMessage());
            throw new RuntimeException("1111");
        }
        return result;
    }
 
//    /**
//     * 统计数量
//     */
//    public int queryMatCodeCount(){
//        int result = 0;
//        try {
//            result = matCodeDao.getMatCodeCount();
//        }catch(Exception e) {
//            System.out.println(e.getMessage());
//        }
//        return result;
//    }
 
    /**
     * 分页查询所有
     */
    public Map<String,Object> queryMatCodeList(MatCodeCondition matCodeCon) {
        try {
            Map<String,Object> map=new HashMap<String, Object>();
            List<MatCodeBean> list=matCodeDao.queryMatCodeList(matCodeCon);
            int count =matCodeDao.getMatCodeCount(matCodeCon);
            map.put("rows", list);
            map.put("total", count);
            return map;
        }catch(Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
    public Map<String,Object> queryMatCodeByNo(MatCodeCondition matCodeCon) {
        try {
            Map<String,Object> map=new HashMap<String, Object>();
            int count =matCodeDao.getMatCountByNo(matCodeCon);
            if(count<=0) {
                //ERP中提取数据
                OperateOracle oerp=new OperateOracle();
                MatCodeBean matCode = oerp.SelectMatCode(matCodeCon.getMat_no());
                if(matCode!=null) {
                    matCodeDao.insertMatCode(matCode);
                }
            }
            count =matCodeDao.getMatCountByNo(matCodeCon);
            List<MatCodeBean> list=matCodeDao.queryMatCodeByNo(matCodeCon);
            
            map.put("rows", list);
            map.put("total", count);
            return map;
        }catch(Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
//    public List<MatCodeBean> queryMatCodeList(int spage, int epage) {
//        try {
//            return matCodeDao.queryMatCodeList(spage, epage);
//        }catch(Exception e) {
//            System.out.println(e.getMessage());
//            return null;
//        }
//    }
 
    /**
     * 根据id查找
     */
    public MatCodeBean queryMatCodeById(String id) {
        try {
            return matCodeDao.getMatCodeById(id);
        }catch(Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
    /**
     * 根据id查找
     */
    public MatCodeBean queryMatCodeByBarcode(String id) {
        try {
            return matCodeDao.getMatCodeByBarcode(id);
        }catch(Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
    //更新
    public int upMatCode(MatCodeBean matCode) {
        int result=0;
        try {
            result=matCodeDao.upMatCode(matCode);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
 
    //更新
    public int upMatBarcode(MatCodeBean matCode) {
        int result=0;
        try {
            result=matCodeDao.upMatBarcode(matCode);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
        
    /**
     * 根据id删除
     */
    public int delMatCode(String did) {
        int result=0;
        try {
            List<LocDetailBean> listLoc = matCodeDao.getLocMatNoList(did);
            List<WorkDetailBean> listWrk = matCodeDao.getWrkMatNoList(did);
            if ((listLoc!=null && listLoc.size()>0)
                    ||listWrk!=null && listWrk.size()>0) {
                return result;
            }
            result=matCodeDao.delMatCodeById(did);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
    
    /**
     * 批量删除
     */
    public int delMatCodes(String did) {
        int result=0;
        try {
            result=matCodeDao.delMatCodesById(did);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
 
    /**
     * 查询所有
     * @return
     */
    public List<MatCodeBean> getMatCodeList() {
        try {
            return matCodeDao.getMatCodeList();
        }catch(Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
    /**
     * 导出excel 
     */
    public void ExportMatCodeList(MatCodeCondition matCodeCon, HttpServletResponse response) {
        // 输出Excel文件
        try {
            // HSSFWorkbook对象(excel的文档对象)
            HSSFWorkbook workBook = new HSSFWorkbook();
 
            // sheet对象(excel的表单)
            HSSFSheet sheet = workBook.createSheet("物料编码信息");
 
            // 行数,参数为行索引(excel的行)
            // HSSFRow rowHead=sheet.createRow(0);//第一行 可以是0~65535之间的任何一个
 
            // //创建excel的单元格,参数为列索引,可以是0~255之间的任何一个
            // HSSFCell cellOne=rowHead.createCell(0);
            // cellOne.setCellValue("用户信息");//表头
 
            // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
            // sheet.addMergedRegion(new CellRangeAddress(0,0,0,5));
 
            sheet.setColumnWidth(0, (int) ((15 + 0.72) * 256));
            sheet.setColumnWidth(1, (int) ((30 + 0.72) * 256));
            sheet.setColumnWidth(2, (int) ((10 + 0.72) * 256));
            sheet.setColumnWidth(3, (int) ((20 + 0.72) * 256));
            sheet.setColumnWidth(4, (int) ((30 + 0.72) * 256));
            sheet.setColumnWidth(5, (int) ((30 + 0.72) * 256));
            sheet.setColumnWidth(6, (int) ((30 + 0.72) * 256));
 
            HSSFRow rows = sheet.createRow(0);// 第二行
            // 创建单元格并设置单元格内容
            rows.createCell(0).setCellValue("箱号/批号");
            rows.createCell(1).setCellValue("托盘号");
            rows.createCell(2).setCellValue("物料编号");
            rows.createCell(3).setCellValue("物料描述");
            rows.createCell(4).setCellValue("客户名称");
            rows.createCell(5).setCellValue("毛重");
            rows.createCell(6).setCellValue("净重");
 
            List<MatCodeBean> matCodeList = matCodeDao.getMatCodeAll(matCodeCon);
            for (int i = 0; i < matCodeList.size(); i++) {
                HSSFRow row = sheet.createRow(i + 1);// 从第三行开始
                // 创建单元格并设置单元格内容
                row.createCell(0).setCellValue(matCodeList.get(i).getMat_no());
                row.createCell(1).setCellValue(matCodeList.get(i).getBarcode());
                row.createCell(2).setCellValue(matCodeList.get(i).getStr3());
                row.createCell(3).setCellValue(matCodeList.get(i).getMat_name());
                row.createCell(4).setCellValue(matCodeList.get(i).getStr2());
                row.createCell(5).setCellValue(matCodeList.get(i).getNum4());
                row.createCell(6).setCellValue(matCodeList.get(i).getNum6());
            }
            // //输出Excel文件
            // try {
            OutputStream output = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition",
                    "attachment; filename=MatCode" + DateTimeUtil.getStringDateTime(14) + ".xls");
            response.setContentType("application/msexcel");
            workBook.write(output);
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        
    /**
     * 导入excel
     * @param file
     * @return
     */
    public Map<String, Object> importExcel(MultipartFile file) {
        List<MatCodeBean> matList = new ArrayList<MatCodeBean>();
        Map<String, Object> map = new HashMap<String, Object>();
        int x = 0, y = 0, sum = 0;
        boolean flag = false;
        if (file != null) {
            // 根据指定的文件输入流导入Excel从而产生Workbook对象
            try {
                Workbook workBook = new HSSFWorkbook(file.getInputStream());
                // 获取Excel文档中的第一个表单
                Sheet sheet = workBook.getSheetAt(0);
                // 对Sheet中的每一行进行迭代
                for (Row r : sheet) {
                    // 如果当前行的行号(从0开始)未达到1(第二行)则从新循环
                    if (r.getRowNum() < 1) {
                        continue;
                    }
 
                    MatCodeBean mat = new MatCodeBean();
                    if(r.getCell(0)==null || r.getCell(0).getStringCellValue()=="") {
                        continue;
                    }
                    mat.setMat_no(r.getCell(0)!=null ? r.getCell(0).getStringCellValue() : "");
                    mat.setBarcode(r.getCell(1)!=null ? r.getCell(1).getStringCellValue() : "");
                    mat.setStr3(r.getCell(2)!=null ? r.getCell(2).getStringCellValue() : "");
                    mat.setMat_name(r.getCell(3)!=null ? r.getCell(3).getStringCellValue() : "");
                    mat.setStr2(r.getCell(4)!=null ? r.getCell(4).getStringCellValue() : "");
                    mat.setNum4(r.getCell(5)!=null ? Double.parseDouble(r.getCell(5).getStringCellValue()) : 0);
                    mat.setNum6(r.getCell(6)!=null ? Double.parseDouble(r.getCell(6).getStringCellValue()) : 0);
                    matList.add(mat);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
 
            y = matList.size();
            for (MatCodeBean m : matList) {
                MatCodeBean matCode = matCodeDao.getMatCodeById(m.getMat_no());
                if (matCode != null && !matCode.getMat_no().equals("")) {
                    x++;
                } else {
                    int i = matCodeDao.insertMatCode(m);
                    sum += i;
                }
            }
            if (sum == (y - x)) {
                flag = true;
            }
        }
        map.put("msg", "共有" + y + "条数据,成功导入" + sum + "条数据,物料编码重名的数据有" + x + "条");
        map.put("flag", flag);
        return map;
    }
    
    /*
     * 查询库存、工作档中已存在物料数据数量
     */
    public int getMatCount(String mat_no) {
        int result=0;
        try {
            List<LocDetailBean> listLoc = matCodeDao.getLocMatNoList(mat_no);
            List<WorkDetailBean> listWrk = matCodeDao.getWrkMatNoList(mat_no);
            if (listWrk!=null && listWrk.size()>0) {
                result = listWrk.size();
            }
            if (listLoc!=null && listLoc.size()>0) {
                result = listLoc.size();
            }
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
        
}