package cn.iocoder.yudao.module.mes.service.cal.team; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.StrUtil; 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.cal.team.vo.MesCalTeamImportExcelVO; import cn.iocoder.yudao.module.mes.controller.admin.cal.team.vo.MesCalTeamImportRespVO; import cn.iocoder.yudao.module.mes.controller.admin.cal.team.vo.MesCalTeamPageReqVO; import cn.iocoder.yudao.module.mes.controller.admin.cal.team.vo.MesCalTeamSaveReqVO; import cn.iocoder.yudao.module.mes.dal.dataobject.cal.team.MesCalTeamDO; import cn.iocoder.yudao.module.mes.dal.mysql.cal.team.MesCalTeamMapper; 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.*; /** * MES 班组 Service 实现类 * * @author 超级管理员 */ @Service @Validated public class MesCalTeamServiceImpl implements MesCalTeamService { @Resource private MesCalTeamMapper teamMapper; @Resource @Lazy private MesCalTeamMemberService teamMemberService; @Resource @Lazy private MesCalTeamShiftService teamShiftService; @Override public Long createTeam(MesCalTeamSaveReqVO createReqVO) { // 1. 校验编码唯一 validateTeamCodeUnique(null, createReqVO.getCode()); // 2. 插入 MesCalTeamDO team = BeanUtils.toBean(createReqVO, MesCalTeamDO.class); teamMapper.insert(team); return team.getId(); } @Override public void updateTeam(MesCalTeamSaveReqVO updateReqVO) { // 1.1 校验存在 validateTeamExists(updateReqVO.getId()); // 1.2 校验编码唯一 validateTeamCodeUnique(updateReqVO.getId(), updateReqVO.getCode()); // 2. 更新 MesCalTeamDO updateObj = BeanUtils.toBean(updateReqVO, MesCalTeamDO.class); teamMapper.updateById(updateObj); } @Override @Transactional(rollbackFor = Exception.class) public void deleteTeam(Long id) { // 1. 校验存在 validateTeamExists(id); // 2.1 级联删除成员 teamMemberService.deleteByTeamId(id); // 2.2 级联删除排班记录 teamShiftService.deleteByTeamId(id); // 2.3 删除班组 teamMapper.deleteById(id); } @Override public MesCalTeamDO getTeam(Long id) { return teamMapper.selectById(id); } @Override public PageResult getTeamPage(MesCalTeamPageReqVO pageReqVO) { return teamMapper.selectPage(pageReqVO); } @Override public List getTeamList() { return teamMapper.selectList(); } @Override public List getTeamList(Collection ids) { if (CollUtil.isEmpty(ids)) { return Collections.emptyList(); } return teamMapper.selectByIds(ids); } @Override public MesCalTeamDO validateTeamExists(Long id) { MesCalTeamDO team = teamMapper.selectById(id); if (team == null) { throw exception(CAL_TEAM_NOT_EXISTS); } return team; } private void validateTeamCodeUnique(Long id, String code) { MesCalTeamDO team = teamMapper.selectByCode(code); if (team == null) { return; } if (ObjUtil.notEqual(id, team.getId())) { throw exception(CAL_TEAM_CODE_DUPLICATE); } } @Override @Transactional(rollbackFor = Exception.class) public MesCalTeamImportRespVO importTeamList(List importList, boolean updateSupport) { // 1. 校验非空 if (CollUtil.isEmpty(importList)) { throw exception(CAL_TEAM_IMPORT_LIST_IS_EMPTY); } // 2. 逐行处理 MesCalTeamImportRespVO respVO = MesCalTeamImportRespVO.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; } if (importItem.getCalendarType() == null) { respVO.getFailureCodes().put(key, "班组类型不能为空"); return; } // 2.1 判断:创建 or 更新 MesCalTeamDO existTeam = teamMapper.selectByCode(importItem.getCode()); if (existTeam == null) { // 2.1.1 新增 MesCalTeamDO team = BeanUtils.toBean(importItem, MesCalTeamDO.class); team.setId(null); teamMapper.insert(team); respVO.getCreateCodes().add(importItem.getCode()); } else if (updateSupport) { // 2.1.2 更新 MesCalTeamDO updateObj = BeanUtils.toBean(importItem, MesCalTeamDO.class); updateObj.setId(existTeam.getId()); teamMapper.updateById(updateObj); respVO.getUpdateCodes().add(importItem.getCode()); } else { respVO.getFailureCodes().put(key, "班组编码已存在"); } }); return respVO; } }