|
@@ -0,0 +1,137 @@
|
|
|
+package com.hssx.cloudmodel.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.hssx.cloudmodel.entity.Mould;
|
|
|
+import com.hssx.cloudmodel.entity.Part;
|
|
|
+import com.hssx.cloudmodel.entity.User;
|
|
|
+import com.hssx.cloudmodel.entity.vo.UserVO;
|
|
|
+import com.hssx.cloudmodel.mapper.MouldMapper;
|
|
|
+import com.hssx.cloudmodel.mapper.PartMapper;
|
|
|
+import com.hssx.cloudmodel.mapper.UserMapper;
|
|
|
+import com.hssx.cloudmodel.service.PartService;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.hssx.cloudmodel.util.HttpRespMsg;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author 吴涛涛
|
|
|
+ * @since 2019-08-13
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class PartServiceImpl extends ServiceImpl<PartMapper, Part> implements PartService {
|
|
|
+ @Resource
|
|
|
+ PartMapper partMapper;
|
|
|
+ @Resource
|
|
|
+ UserMapper userMapper;
|
|
|
+ @Resource
|
|
|
+ MouldMapper mouldMapper;
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg importPartExcel(MultipartFile file, UserVO userVO) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
|
|
|
+ if(user != null){
|
|
|
+ try {
|
|
|
+ File f = null;
|
|
|
+ if ("".equals(file) || file.getSize() <= 0) {
|
|
|
+ file = null;
|
|
|
+ } else {
|
|
|
+ //获取输入流
|
|
|
+ InputStream ins = file.getInputStream();
|
|
|
+ //新建一个文件
|
|
|
+ f = new File(file.getOriginalFilename());
|
|
|
+ //输入流转file
|
|
|
+ inputStreamToFile(ins, f);
|
|
|
+ }
|
|
|
+ Mould mould = mouldMapper.selectById(userVO.getMouldId());
|
|
|
+ //根据文件创建工作簿
|
|
|
+ XSSFWorkbook wookbook = new XSSFWorkbook(f);
|
|
|
+ XSSFSheet sheet = wookbook.getSheetAt(0);
|
|
|
+ int s = sheet.getLastRowNum();
|
|
|
+ // 遍历当前sheet中的所有行,第一行是数据对应的字段,不是数据,
|
|
|
+ // 故从第二行开始遍历拿数据(如果有标题的话,则从第三行开始拿数据)
|
|
|
+ for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
|
|
|
+ XSSFRow row = sheet.getRow(j);
|
|
|
+ //新建零件
|
|
|
+ Part part = new Part();
|
|
|
+ // 遍历所有的列,下面的10是excle表格里共有10列即对应了10个字段
|
|
|
+ for (int y = 0; y < 2; y++) {
|
|
|
+ XSSFCell cell = row.getCell(y);
|
|
|
+ cell.setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ //取出当前列的值
|
|
|
+ String value = cell.getStringCellValue();
|
|
|
+ //判断第几列插入数据,后面就是从列中取数据往对象里放,然后插入到数据库里
|
|
|
+ if (value == null && "".equals(value)) {
|
|
|
+ log.error("数据不可为空");
|
|
|
+ msg.setError("数据不可为空");
|
|
|
+ return msg;
|
|
|
+ } else if (y == 0) {
|
|
|
+ //零件编号
|
|
|
+ part.setPartNo(value);
|
|
|
+ } else if (y == 1) {
|
|
|
+ //零件名称
|
|
|
+ part.setPartName(value);
|
|
|
+ } else if (y == 2) {
|
|
|
+ //零件寿命
|
|
|
+ part.setPartLife(Integer.parseInt(value));
|
|
|
+ if(Integer.parseInt(value)<mould.getSettingLife()){
|
|
|
+ part.setIsVulnerable(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ part.setCreatorId(user.getId());
|
|
|
+ part.setCreator(user.getUsername());
|
|
|
+ partMapper.insert(part);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ msg.setError(e.getMessage());
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ msg.setError("用户不存在或者未登录");
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 输入流转file
|
|
|
+ *
|
|
|
+ * @param ins
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public static void inputStreamToFile(InputStream ins, File file) {
|
|
|
+ try {
|
|
|
+ OutputStream os = new FileOutputStream(file);
|
|
|
+ int bytesRead = 0;
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ os.close();
|
|
|
+ ins.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|