package com.vincent.rsf.server.manager.service.impl; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.vincent.rsf.framework.common.R; import com.vincent.rsf.framework.exception.CoolException; import com.vincent.rsf.server.common.utils.ExcelUtil; import com.vincent.rsf.server.manager.entity.*; import com.vincent.rsf.server.manager.entity.excel.AsnOrderTemplate; import com.vincent.rsf.server.manager.entity.excel.DeliveryTemplate; import com.vincent.rsf.server.manager.enums.*; import com.vincent.rsf.server.manager.mapper.DeliveryItemMapper; import com.vincent.rsf.server.manager.service.CompanysService; import com.vincent.rsf.server.manager.service.DeliveryItemService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.vincent.rsf.server.manager.service.DeliveryService; import com.vincent.rsf.server.manager.service.MatnrService; import com.vincent.rsf.server.system.constant.SerialRuleCode; import com.vincent.rsf.server.system.utils.SerialRuleUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @Service("deliveryItemService") public class DeliveryItemServiceImpl extends ServiceImpl implements DeliveryItemService { @Autowired private DeliveryService deliveryService; @Autowired private MatnrService matnrService; @Autowired private CompanysService companysService; @Autowired private DeliveryItemService deliveryItemService; /** * excel DO单导入 * * @param file * @param hashMap * @param loginUserId * @return */ @Override public R excelImport(MultipartFile file, Map hashMap, Long loginUserId) throws Exception { ExcelImportResult result = ExcelImportUtil.importExcelMore(file.getInputStream(), DeliveryTemplate.class, ExcelUtil.getDefaultImportParams()); if (result.getList().isEmpty()) { throw new CoolException("表格内容不能为空!!"); } List resultList = result.getList(); Map> listMap = resultList.stream().collect(Collectors.groupingBy(DeliveryTemplate::getDoCode)); StringBuffer sbFaild = new StringBuffer(); for (String key : listMap.keySet()) { if (StringUtils.isBlank(key)) { throw new CoolException("单号不能为空!!"); } DeliveryTemplate template = listMap.get(key).stream().findFirst().get(); Delivery order = deliveryService.getOne(new LambdaQueryWrapper().eq(Delivery::getCode, key)); if (!Objects.isNull(order)) { sbFaild.append(template.getDoCode()).append(",已添加!!"); throw new CoolException(sbFaild.toString()); } order = new Delivery(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = dateFormat.parse(template.getArrTime()); if (Objects.isNull(date)) { throw new CoolException("时间格式错误!!"); } if (Objects.isNull((OrderType.getTypeVal(template.getType())))) { sbFaild.append(template.getDoCode()).append(",单据状态不存在!!"); throw new CoolException(sbFaild.toString()); } if (Objects.isNull((OrderWorkType.getWorkType(template.getWkType())))) { sbFaild.append(template.getDoCode()).append(",业务状态不存在!!"); throw new CoolException(sbFaild.toString()); } order.setCode(template.getDoCode()) .setType(OrderType.getTypeVal(template.getType())) .setWkType(OrderWorkType.getWorkType(template.getWkType())) .setMemo(template.getMemo()) .setSource(OrderSourceType.ORDER_SOURCE_TYPE_ERP.desc) .setAnfme(Double.parseDouble(template.getAnfme())) .setUpdateBy(loginUserId) .setCreateBy(loginUserId); if (!deliveryService.save(order)) { throw new CoolException("单据保存失败!!"); } List items = new ArrayList<>(); for (DeliveryTemplate orderTemplate : listMap.get(key)) { DeliveryItem orderItem = new DeliveryItem(); if (StringUtils.isBlank(orderTemplate.getMatnrCode())) { throw new CoolException(orderTemplate.getDoCode() + ":物料编码不能为空!!"); } if (StringUtils.isBlank(orderTemplate.getAnfme())) { throw new CoolException(orderTemplate.getDoCode() + ":数量不能为空!!"); } if (StringUtils.isBlank(orderTemplate.getType())) { throw new CoolException(orderTemplate.getDoCode() + ":单据类型不能为空!!"); } if (StringUtils.isBlank(orderTemplate.getWkType())) { throw new CoolException(orderTemplate.getDoCode() + ":业务类型不能为空!!"); } Matnr matnr = matnrService.getOne(new LambdaQueryWrapper() .eq(Matnr::getCode, orderTemplate.getMatnrCode())); if (Objects.isNull(matnr)) { sbFaild.append(orderTemplate.getMatnrCode()).append("物料不存在"); throw new CoolException(sbFaild.toString()); } orderItem.setDeliveryId(order.getId()) .setDeliveryCode(order.getCode()) .setSplrBatch(orderTemplate.getSplrBatch()) .setPlatItemId(orderTemplate.getPlatItemId()) .setAnfme(Double.parseDouble(orderTemplate.getAnfme())) .setUnit(matnr.getUnit()) .setCreateBy(loginUserId) .setUpdateBy(loginUserId) .setSplrBatch(orderTemplate.getSplrBatch()) .setMaktx(matnr.getName()) .setMatnrCode(matnr.getCode()) .setMatnrId(matnr.getId()); if (!Objects.isNull(orderTemplate.getSplrCode())) { Companys companys = companysService.getOne(new LambdaQueryWrapper() .eq(Companys::getType, CompanysType.COMPANYS_TYPE_SUPPLIER.val) .eq(Companys::getCode, orderTemplate.getSplrCode())); if (!Objects.isNull(companys)) { orderItem.setSplrCode(companys.getCode()).setSplrName(companys.getName()); } else { sbFaild.append("供应商:" + orderTemplate.getSplrCode()).append("不存在"); throw new CoolException(sbFaild.toString()); } } else { continue; } items.add(orderItem); if (!deliveryItemService.save(orderItem)) { throw new CoolException("单据明细保存失败!!"); } } if (!items.isEmpty()) { Double anfme = items.stream().mapToDouble(DeliveryItem::getAnfme).sum(); if (!deliveryService.update(new LambdaUpdateWrapper() .set(Delivery::getAnfme, anfme) .eq(Delivery::getId, order.getId()))) { throw new CoolException("单据数量修改失败!!"); } } } return null; } }