| | |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.aps.common.core.utils.DateUtils; |
| | | import com.aps.common.core.utils.poi.ExcelUtil; |
| | | import com.aps.common.security.utils.DictUtils; |
| | | import com.aps.common.security.utils.SecurityUtils; |
| | | import com.aps.core.domain.ApsGasPipelineMo; |
| | | import com.aps.core.mapper.ApsGasPipelineMoMapper; |
| | | import com.aps.core.service.IApsGasPipelineMoService; |
| | | import io.micrometer.common.util.StringUtils; |
| | | import lombok.SneakyThrows; |
| | | import org.apache.poi.ss.usermodel.Row; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.ss.usermodel.WorkbookFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.sql.Timestamp; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public int batchInsertGasPipelineMo(MultipartFile file) { |
| | | Workbook workbook = WorkbookFactory.create(file.getInputStream()); |
| | | |
| | | Sheet sheet = workbook.getSheetAt(0); |
| | | int rows = sheet.getLastRowNum(); |
| | | if (rows > 0) { |
| | | List<ApsGasPipelineMo> list = new ArrayList<>(); |
| | | ExcelUtil<ApsGasPipelineMo> util = new ExcelUtil<>(ApsGasPipelineMo.class); |
| | | List<ApsGasPipelineMo> tempList = util.importExcel(file.getInputStream()); |
| | | DictUtils.CacheValue cacheValue = DictUtils.getCacheValue("aps_factory"); |
| | | tempList.forEach(apsGasPipelineMo -> { |
| | | apsGasPipelineMo.setId(IdUtil.getSnowflakeNextId()); |
| | | apsGasPipelineMo.setFactory(cacheValue.get(apsGasPipelineMo.getFactory())); |
| | | apsGasPipelineMo.setCreateBy(SecurityUtils.getUsername()); |
| | | apsGasPipelineMo.setCreateTime(DateUtils.getNowDate()); |
| | | }); |
| | | |
| | | /*数据列从1开始*/ |
| | | for (int i = 1; i <= rows; i++) { |
| | | Row row = sheet.getRow(i); |
| | | String mo = row.getCell(0).getStringCellValue(); |
| | | String factory = row.getCell(1).getStringCellValue(); |
| | | String materialNum = row.getCell(2).getStringCellValue(); |
| | | double quantity = row.getCell(3).getNumericCellValue(); |
| | | Date planEnd = row.getCell(4).getDateCellValue(); |
| | | if (StringUtils.isNotBlank(mo) && |
| | | StringUtils.isNotBlank(factory) && |
| | | StringUtils.isNotBlank(materialNum) && |
| | | planEnd != null) { |
| | | ApsGasPipelineMo apsGasPipelineMo = new ApsGasPipelineMo(); |
| | | apsGasPipelineMo.setMo(mo); |
| | | apsGasPipelineMo.setId(IdUtil.getSnowflakeNextId()); |
| | | apsGasPipelineMo.setFactory(factory); |
| | | apsGasPipelineMo.setCreateBy(SecurityUtils.getUsername()); |
| | | apsGasPipelineMo.setCreateTime(DateUtils.getNowDate()); |
| | | apsGasPipelineMo.setQuantity(new BigDecimal(quantity)); |
| | | apsGasPipelineMo.setPlanEnd(new Timestamp(planEnd.getTime())); |
| | | list.add(apsGasPipelineMo); |
| | | } |
| | | } |
| | | if (!list.isEmpty()) { |
| | | apsGasPipelineMoMapper.deleteAll(); |
| | | apsGasPipelineMoMapper.insert(list); |
| | | } |
| | | return list.size(); |
| | | if (!tempList.isEmpty()) { |
| | | apsGasPipelineMoMapper.deleteAll(); |
| | | apsGasPipelineMoMapper.insert(tempList); |
| | | } |
| | | return 0; |
| | | |
| | | return tempList.size(); |
| | | } |
| | | } |