123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package com.hssx.cloudmodel.service.impl;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.hssx.cloudmodel.entity.Company;
- import com.hssx.cloudmodel.entity.Mould;
- import com.hssx.cloudmodel.entity.Part;
- import com.hssx.cloudmodel.entity.User;
- import com.hssx.cloudmodel.entity.vo.PartVO;
- 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.openxml4j.exceptions.InvalidFormatException;
- 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.*;
- 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) throws IOException, InvalidFormatException {
- HttpRespMsg msg = new HttpRespMsg();
- User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
- if(user != null){
- List<Part> parts = partMapper.selectList(new QueryWrapper<Part>().eq("mould_id",userVO.getMouldId()));
- // 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();
- System.out.println("s==========>"+s);
- // 遍历当前sheet中的所有行,第一行是数据对应的字段,不是数据,
- // 故从第二行开始遍历拿数据(如果有标题的话,则从第三行开始拿数据)
- for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
- XSSFRow row = sheet.getRow(j);
- //新建零件
- Part part = new Part();
- part.setMouldId(mould.getId());
- // 遍历所有的列,下面的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) {
- //零件编号
- for (Part p : parts) {
- if(value.equals(p.getPartNo())){
- msg.setError("第"+j+"行的零件编号:"+value+"已被占用,请修改后重新上传");
- return msg;
- }
- }
- 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.setPartLife(Integer.parseInt(value));
- }
- }
- 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;
- }
- @Override
- public HttpRespMsg add(Part part, UserVO userVO) {
- HttpRespMsg msg = new HttpRespMsg();
- if(part.getId() != null){
- //修改
- Part m = partMapper.selectOne(new QueryWrapper<Part>().eq("part_no", part.getPartNo()));
- if ((m != null && m.getId() == part.getId()) || m == null) {
- partMapper.updateById(part);
- } else {
- msg.setError("当前模具编号已存在,请重新输入其他模具编号");
- }
- }else{
- //添加
- Mould mould = mouldMapper.selectById(userVO.getMouldId());
- User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
- if(part.getPartLife()<mould.getSettingLife()){
- part.setIsVulnerable(1);
- }
- part.setCreatorId(user.getId());
- part.setCreator(user.getUsername());
- partMapper.insert(part);
- }
- return msg;
- }
- @Override
- public HttpRespMsg getList(UserVO userVO) {
- HttpRespMsg msg = new HttpRespMsg();
- User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
- if(user != null){
- List<PartVO> list = new ArrayList<>();
- list = partMapper.selectPartFileByMouldId(userVO);
- msg.data = list;
- }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();
- }
- }
- }
|