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 queryMatCodeList(MatCodeCondition matCodeCon) { try { Map map=new HashMap(); List 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 queryMatCodeByNo(MatCodeCondition matCodeCon) { try { Map map=new HashMap(); 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 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 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 listLoc = matCodeDao.getLocMatNoList(did); List 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 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 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 importExcel(MultipartFile file) { List matList = new ArrayList(); Map map = new HashMap(); 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 listLoc = matCodeDao.getLocMatNoList(mat_no); List 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; } }