PartServiceImpl.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.hssx.cloudmodel.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.hssx.cloudmodel.entity.Mould;
  4. import com.hssx.cloudmodel.entity.Part;
  5. import com.hssx.cloudmodel.entity.User;
  6. import com.hssx.cloudmodel.entity.vo.UserVO;
  7. import com.hssx.cloudmodel.mapper.MouldMapper;
  8. import com.hssx.cloudmodel.mapper.PartMapper;
  9. import com.hssx.cloudmodel.mapper.UserMapper;
  10. import com.hssx.cloudmodel.service.PartService;
  11. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  12. import com.hssx.cloudmodel.util.HttpRespMsg;
  13. import org.apache.poi.ss.usermodel.Cell;
  14. import org.apache.poi.xssf.usermodel.XSSFCell;
  15. import org.apache.poi.xssf.usermodel.XSSFRow;
  16. import org.apache.poi.xssf.usermodel.XSSFSheet;
  17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import javax.annotation.Resource;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.io.File;
  24. import java.io.FileOutputStream;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.text.SimpleDateFormat;
  28. import java.util.ArrayList;
  29. import java.util.Date;
  30. import java.util.List;
  31. /**
  32. * <p>
  33. * 服务实现类
  34. * </p>
  35. *
  36. * @author 吴涛涛
  37. * @since 2019-08-13
  38. */
  39. @Service
  40. public class PartServiceImpl extends ServiceImpl<PartMapper, Part> implements PartService {
  41. @Resource
  42. PartMapper partMapper;
  43. @Resource
  44. UserMapper userMapper;
  45. @Resource
  46. MouldMapper mouldMapper;
  47. @Override
  48. public HttpRespMsg importPartExcel(MultipartFile file, UserVO userVO) {
  49. HttpRespMsg msg = new HttpRespMsg();
  50. User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
  51. if(user != null){
  52. try {
  53. File f = null;
  54. if ("".equals(file) || file.getSize() <= 0) {
  55. file = null;
  56. } else {
  57. //获取输入流
  58. InputStream ins = file.getInputStream();
  59. //新建一个文件
  60. f = new File(file.getOriginalFilename());
  61. //输入流转file
  62. inputStreamToFile(ins, f);
  63. }
  64. Mould mould = mouldMapper.selectById(userVO.getMouldId());
  65. //根据文件创建工作簿
  66. XSSFWorkbook wookbook = new XSSFWorkbook(f);
  67. XSSFSheet sheet = wookbook.getSheetAt(0);
  68. int s = sheet.getLastRowNum();
  69. // 遍历当前sheet中的所有行,第一行是数据对应的字段,不是数据,
  70. // 故从第二行开始遍历拿数据(如果有标题的话,则从第三行开始拿数据)
  71. for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
  72. XSSFRow row = sheet.getRow(j);
  73. //新建零件
  74. Part part = new Part();
  75. // 遍历所有的列,下面的10是excle表格里共有10列即对应了10个字段
  76. for (int y = 0; y < 2; y++) {
  77. XSSFCell cell = row.getCell(y);
  78. cell.setCellType(Cell.CELL_TYPE_STRING);
  79. //取出当前列的值
  80. String value = cell.getStringCellValue();
  81. //判断第几列插入数据,后面就是从列中取数据往对象里放,然后插入到数据库里
  82. if (value == null && "".equals(value)) {
  83. log.error("数据不可为空");
  84. msg.setError("数据不可为空");
  85. return msg;
  86. } else if (y == 0) {
  87. //零件编号
  88. part.setPartNo(value);
  89. } else if (y == 1) {
  90. //零件名称
  91. part.setPartName(value);
  92. } else if (y == 2) {
  93. //零件寿命
  94. part.setPartLife(Integer.parseInt(value));
  95. if(Integer.parseInt(value)<mould.getSettingLife()){
  96. part.setIsVulnerable(1);
  97. }
  98. }
  99. }
  100. part.setCreatorId(user.getId());
  101. part.setCreator(user.getUsername());
  102. partMapper.insert(part);
  103. }
  104. } catch (Exception e) {
  105. log.error(e.getMessage(), e);
  106. msg.setError(e.getMessage());
  107. return msg;
  108. }
  109. }else{
  110. msg.setError("用户不存在或者未登录");
  111. }
  112. return msg;
  113. }
  114. /**
  115. * 输入流转file
  116. *
  117. * @param ins
  118. * @param file
  119. */
  120. public static void inputStreamToFile(InputStream ins, File file) {
  121. try {
  122. OutputStream os = new FileOutputStream(file);
  123. int bytesRead = 0;
  124. byte[] buffer = new byte[8192];
  125. while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
  126. os.write(buffer, 0, bytesRead);
  127. }
  128. os.close();
  129. ins.close();
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. }
  133. }
  134. }