PartServiceImpl.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.hssx.cloudmodel.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.hssx.cloudmodel.entity.Company;
  4. import com.hssx.cloudmodel.entity.Mould;
  5. import com.hssx.cloudmodel.entity.Part;
  6. import com.hssx.cloudmodel.entity.User;
  7. import com.hssx.cloudmodel.entity.vo.PartVO;
  8. import com.hssx.cloudmodel.entity.vo.UserVO;
  9. import com.hssx.cloudmodel.mapper.MouldMapper;
  10. import com.hssx.cloudmodel.mapper.PartMapper;
  11. import com.hssx.cloudmodel.mapper.UserMapper;
  12. import com.hssx.cloudmodel.service.PartService;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import com.hssx.cloudmodel.util.HttpRespMsg;
  15. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  16. import org.apache.poi.ss.usermodel.Cell;
  17. import org.apache.poi.xssf.usermodel.XSSFCell;
  18. import org.apache.poi.xssf.usermodel.XSSFRow;
  19. import org.apache.poi.xssf.usermodel.XSSFSheet;
  20. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import javax.annotation.Resource;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.io.*;
  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) throws IOException, InvalidFormatException {
  49. HttpRespMsg msg = new HttpRespMsg();
  50. User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
  51. if(user != null){
  52. List<Part> parts = partMapper.selectList(new QueryWrapper<Part>().eq("mould_id",userVO.getMouldId()));
  53. // try {
  54. File f = null;
  55. if ("".equals(file) || file.getSize() <= 0) {
  56. file = null;
  57. } else {
  58. //获取输入流
  59. InputStream ins = file.getInputStream();
  60. //新建一个文件
  61. f = new File(file.getOriginalFilename());
  62. //输入流转file
  63. inputStreamToFile(ins, f);
  64. }
  65. Mould mould = mouldMapper.selectById(userVO.getMouldId());
  66. //根据文件创建工作簿
  67. XSSFWorkbook wookbook = new XSSFWorkbook(f);
  68. XSSFSheet sheet = wookbook.getSheetAt(0);
  69. int s = sheet.getLastRowNum();
  70. System.out.println("s==========>"+s);
  71. // 遍历当前sheet中的所有行,第一行是数据对应的字段,不是数据,
  72. // 故从第二行开始遍历拿数据(如果有标题的话,则从第三行开始拿数据)
  73. for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
  74. XSSFRow row = sheet.getRow(j);
  75. //新建零件
  76. Part part = new Part();
  77. part.setMouldId(mould.getId());
  78. // 遍历所有的列,下面的10是excle表格里共有10列即对应了10个字段
  79. for (int y = 0; y < 2; y++) {
  80. XSSFCell cell = row.getCell(y);
  81. cell.setCellType(Cell.CELL_TYPE_STRING);
  82. //取出当前列的值
  83. String value = cell.getStringCellValue();
  84. //判断第几列插入数据,后面就是从列中取数据往对象里放,然后插入到数据库里
  85. if (value == null && "".equals(value)) {
  86. log.error("数据不可为空");
  87. msg.setError("数据不可为空");
  88. return msg;
  89. } else if (y == 0) {
  90. //零件编号
  91. for (Part p : parts) {
  92. if(value.equals(p.getPartNo())){
  93. msg.setError("第"+j+"行的零件编号:"+value+"已被占用,请修改后重新上传");
  94. return msg;
  95. }
  96. }
  97. part.setPartNo(value);
  98. } else if (y == 1) {
  99. //零件名称
  100. part.setPartName(value);
  101. } else if (y == 2) {
  102. //零件寿命
  103. part.setPartLife(Integer.parseInt(value));
  104. if(Integer.parseInt(value)<mould.getSettingLife()){
  105. part.setIsVulnerable(1);
  106. }
  107. part.setPartLife(Integer.parseInt(value));
  108. }
  109. }
  110. part.setCreatorId(user.getId());
  111. part.setCreator(user.getUsername());
  112. partMapper.insert(part);
  113. }
  114. // } catch (Exception e) {
  115. // log.error(e.getMessage(), e);
  116. // msg.setError(e.getMessage());
  117. // return msg;
  118. // }
  119. }else{
  120. msg.setError("用户不存在或者未登录");
  121. }
  122. return msg;
  123. }
  124. @Override
  125. public HttpRespMsg add(Part part, UserVO userVO) {
  126. HttpRespMsg msg = new HttpRespMsg();
  127. if(part.getId() != null){
  128. //修改
  129. Part m = partMapper.selectOne(new QueryWrapper<Part>().eq("part_no", part.getPartNo()));
  130. if ((m != null && m.getId() == part.getId()) || m == null) {
  131. partMapper.updateById(part);
  132. } else {
  133. msg.setError("当前模具编号已存在,请重新输入其他模具编号");
  134. }
  135. }else{
  136. //添加
  137. Mould mould = mouldMapper.selectById(userVO.getMouldId());
  138. User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
  139. if(part.getPartLife()<mould.getSettingLife()){
  140. part.setIsVulnerable(1);
  141. }
  142. part.setCreatorId(user.getId());
  143. part.setCreator(user.getUsername());
  144. partMapper.insert(part);
  145. }
  146. return msg;
  147. }
  148. @Override
  149. public HttpRespMsg getList(UserVO userVO) {
  150. HttpRespMsg msg = new HttpRespMsg();
  151. User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
  152. if(user != null){
  153. List<PartVO> list = new ArrayList<>();
  154. list = partMapper.selectPartFileByMouldId(userVO);
  155. msg.data = list;
  156. }else{
  157. msg.setError("用户不存在或者未登录");
  158. }
  159. return msg;
  160. }
  161. /**
  162. * 输入流转file
  163. *
  164. * @param ins
  165. * @param file
  166. */
  167. public static void inputStreamToFile(InputStream ins, File file) {
  168. try {
  169. OutputStream os = new FileOutputStream(file);
  170. int bytesRead = 0;
  171. byte[] buffer = new byte[8192];
  172. while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
  173. os.write(buffer, 0, bytesRead);
  174. }
  175. os.close();
  176. ins.close();
  177. } catch (Exception e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. }