| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.workstation.vo.workshop.MesMdWorkshopImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.workstation.vo.workshop.MesMdWorkshopImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.workstation.vo.workshop.MesMdWorkshopPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.workstation.vo.workshop.MesMdWorkshopSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkshopDO; |
| | |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | |
| | | return workshopMapper.selectListByStatus(status); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesMdWorkshopImportRespVO importWorkshopList(List<MesMdWorkshopImportExcelVO> importList, |
| | | boolean updateSupport) { |
| | | // 1. 校验非空 |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(MD_WORKSHOP_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐行处理 |
| | | MesMdWorkshopImportRespVO respVO = MesMdWorkshopImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String key = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(importItem.getCode())) { |
| | | respVO.getFailureCodes().put(key, "车间编码不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getName())) { |
| | | respVO.getFailureCodes().put(key, "车间名称不能为空"); |
| | | return; |
| | | } |
| | | // 2.1 判断:创建 or 更新 |
| | | MesMdWorkshopDO existWorkshop = workshopMapper.selectByCode(importItem.getCode()); |
| | | if (existWorkshop == null) { |
| | | // 2.1.1 新增 |
| | | MesMdWorkshopDO workshop = BeanUtils.toBean(importItem, MesMdWorkshopDO.class); |
| | | workshop.setId(null); |
| | | workshop.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), CommonStatusEnum.ENABLE.getStatus())); |
| | | workshopMapper.insert(workshop); |
| | | // 自动生成条码 |
| | | barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.WORKSHOP.getValue(), |
| | | workshop.getId(), workshop.getCode(), workshop.getName()); |
| | | respVO.getCreateCodes().add(importItem.getCode()); |
| | | } else if (updateSupport) { |
| | | // 2.1.2 更新 |
| | | MesMdWorkshopDO updateObj = BeanUtils.toBean(importItem, MesMdWorkshopDO.class); |
| | | updateObj.setId(existWorkshop.getId()); |
| | | workshopMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(importItem.getCode()); |
| | | } else { |
| | | respVO.getFailureCodes().put(key, "车间编码已存在"); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |