whyclxw
6 天以前 138dead9b8a35d843a74a799e2cfbe1b12b51189
src/main/java/com/whyc/service/DeviceSpareService.java
@@ -1,6 +1,7 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@@ -11,6 +12,7 @@
import com.whyc.util.CommonUtil;
import com.whyc.util.ThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -19,6 +21,7 @@
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -159,7 +162,80 @@
        return new Response<List<DeviceSpare>>().set(1,deviceSpares);
    }
    public void add(List<DeviceSpare> spareList) {
    public void addBatch(List<DeviceSpare> spareList) {
        mapper.insertBatchSomeColumn(spareList);
    }
    public void addOrUpdate(List<DeviceSpare> spareList) {
        //查询库中是否存在该设备,存在则增加数量.
        for (DeviceSpare spare : spareList) {
            QueryWrapper<DeviceSpare> query = Wrappers.query();
            query.eq("name",spare.getName());
            query.eq("model",spare.getModel());
            query.eq("version",spare.getVersion());
            query.eq("brand",spare.getBrand());
            query.eq("type",spare.getType());
            query.eq("supplier",spare.getSupplier());
            query.last(" limit 1");
            DeviceSpare spareInDB = mapper.selectOne(query);
            if(spareInDB != null){
                spareInDB.setQuantity(spareInDB.getQuantity()+spare.getQuantity());
                mapper.updateById(spareInDB);
            }else{
                //不存在则新增
                mapper.insert(spare);
            }
        }
    }
    public void outBound(List<DeviceSpare> spareList) {
        mapper.outBound(spareList);
    }
    public Response addByExcel(MultipartFile file) throws IOException {
        //将文件流转化为workbook
        Workbook book = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = book.getSheetAt(0);
        //获取最后一行
        int lastRowIndex = sheet.getLastRowNum();
        List<DeviceSpare> spareList = new ArrayList<>();
        for (int i = 1; i <= lastRowIndex; i++) {
            DeviceSpare spare = new DeviceSpare();
            Row row = sheet.getRow(i);
            //对每行的前7列进行非空校验
            for (int j = 0; j < 7; j++) {
                Cell cell = row.getCell(j);
                //非图片列进行非空校验.不合规无法上传
                if (cell == null || cell.getCellType() == CellType.BLANK) {
                    return new Response().set(1, false, "第" + (i + 1) + "行第" + (j + 1) + "列数据为空");
                }
            }
            //获取列的值
            String name = row.getCell(0).getStringCellValue();
            String model = row.getCell(1).getStringCellValue();
            String version = row.getCell(2).getStringCellValue();
            int quantity = (int) row.getCell(3).getNumericCellValue();
            String brand = row.getCell(4).getStringCellValue();
            String type = row.getCell(5).getStringCellValue();
            String supplier = row.getCell(6).getStringCellValue();
            spare.setName(name);
            spare.setModel(model);
            spare.setVersion(version);
            spare.setQuantity(quantity);
            spare.setBrand(brand);
            spare.setType(type);
            spare.setSupplier(supplier);
            spareList.add(spare);
            //第七列为图片 TODO 待处理
            /*Cell cell = row.getCell(7);
            CellType cellType = cell.getCellType();
            System.out.println("-");*/
        }
        addBatch(spareList);
        return null;
    }
}