| | |
| | | package org.jeecg.modules.eam.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import org.jeecg.modules.eam.entity.EamEquipment; |
| | | import org.jeecg.modules.eam.entity.EamFactorySecondMaintPlan; |
| | | import org.jeecg.modules.eam.mapper.EamFactorySecondMaintPlanMapper; |
| | | import org.jeecg.modules.eam.service.IEamEquipmentService; |
| | | import org.jeecg.modules.eam.service.IEamFactorySecondMaintPlanService; |
| | | import org.jeecg.modules.system.entity.BaseFactory; |
| | | import org.jeecg.modules.system.service.IBaseFactoryService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.dao.DuplicateKeyException; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @Description: “首页车间二保计划 |
| | | * @Author: jeecg-boot |
| | | * @Author: jeecg-boot |
| | | * @Date: 2025-08-22 |
| | | * @Version: V1.0 |
| | | */ |
| | | @Service |
| | | public class EamFactorySecondMaintPlanServiceImpl extends ServiceImpl<EamFactorySecondMaintPlanMapper, EamFactorySecondMaintPlan> implements IEamFactorySecondMaintPlanService { |
| | | @Autowired |
| | | private IBaseFactoryService baseFactoryService; |
| | | |
| | | @Autowired |
| | | private IEamEquipmentService eamEquipmentService; |
| | | |
| | | /** |
| | | * 设备管理首页-车间二保计划列表 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<EamFactorySecondMaintPlan> queryAllList(){ |
| | | return this.baseMapper.queryAllList(); |
| | | } |
| | | |
| | | /** |
| | | * 新增首页车间二保计划 |
| | | * @param equipmentId |
| | | * @param monthPlan |
| | | * @return |
| | | */ |
| | | @Override |
| | | public EamFactorySecondMaintPlan add(String equipmentId, String monthPlan) { |
| | | EamEquipment equipment=eamEquipmentService.getById(equipmentId); |
| | | if (equipment==null) { |
| | | return null; |
| | | } |
| | | String factoryId = baseFactoryService.queryByCode(equipment.getFactoryOrgCode().substring(0,6)).getId(); |
| | | // 尝试直接更新(原子操作) |
| | | LambdaUpdateWrapper<EamFactorySecondMaintPlan> updateWrapper = new LambdaUpdateWrapper<>(); |
| | | updateWrapper.eq(EamFactorySecondMaintPlan::getFactoryId, factoryId) |
| | | .eq(EamFactorySecondMaintPlan::getMonthPlan, monthPlan) |
| | | .setSql("quantity = quantity + 1"); // 原子增加 |
| | | |
| | | int updated = this.baseMapper.update(null, updateWrapper); |
| | | |
| | | if (updated > 0) { |
| | | // 更新成功,返回更新后的记录 |
| | | return this.getOne(Wrappers.<EamFactorySecondMaintPlan>lambdaQuery() |
| | | .eq(EamFactorySecondMaintPlan::getFactoryId, factoryId) |
| | | .eq(EamFactorySecondMaintPlan::getMonthPlan, monthPlan)); |
| | | } else { |
| | | // 无记录存在,创建新记录 |
| | | return createNewPlan(factoryId, monthPlan); |
| | | } |
| | | } |
| | | |
| | | private EamFactorySecondMaintPlan createNewPlan(String factoryId, String monthPlan) { |
| | | EamFactorySecondMaintPlan newPlan = new EamFactorySecondMaintPlan(); |
| | | newPlan.setFactoryId(factoryId); |
| | | newPlan.setMonthPlan(monthPlan); |
| | | newPlan.setQuantity(1); |
| | | |
| | | // 设置工厂名称(优化处理) |
| | | BaseFactory baseFactory = baseFactoryService.getById(factoryId); |
| | | if (baseFactory != null) { |
| | | String orgCode = baseFactory.getOrgCode(); |
| | | // 使用整个orgCode(如果长度不足6) |
| | | String nameOrgCode = orgCode.length() >= 6 ? orgCode.substring(0, 6) : orgCode; |
| | | newPlan.setFactoryName(baseFactoryService.factoryDataNameByOrgCode(nameOrgCode)); |
| | | } |
| | | |
| | | // 处理并发创建冲突 |
| | | try { |
| | | this.save(newPlan); |
| | | return newPlan; |
| | | } catch (DuplicateKeyException e) { |
| | | // 其他线程已创建,转为更新 |
| | | return add(factoryId, monthPlan); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 日期变化 |
| | | * @param equipmentId 设备ID |
| | | * @param oldMonthPlan 旧月份计划(格式如yyyy-MM) |
| | | * @param newMonthPlan 新月份计划(格式如yyyy-MM) |
| | | * @return 是否成功变更 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean dateChange(String equipmentId, String oldMonthPlan, String newMonthPlan) { |
| | | // 1. 获取设备信息 |
| | | EamEquipment equipment = eamEquipmentService.getById(equipmentId); |
| | | if (equipment == null) { |
| | | return false; |
| | | } |
| | | |
| | | // 2. 获取工厂ID |
| | | String factoryOrgCodePrefix = equipment.getFactoryOrgCode().substring(0, 6); |
| | | BaseFactory factory = baseFactoryService.queryByCode(factoryOrgCodePrefix); |
| | | if (factory == null) { |
| | | return false; |
| | | } |
| | | String factoryId = factory.getId(); |
| | | |
| | | // 3. 如果月份未变化直接返回成功 |
| | | if (oldMonthPlan.equals(newMonthPlan)) { |
| | | return true; |
| | | } |
| | | |
| | | try { |
| | | // 4. 减少旧月份计划数量(原子操作) |
| | | boolean oldDecreased = decreasePlanQuantity(factoryId, oldMonthPlan); |
| | | if (!oldDecreased) { |
| | | throw new RuntimeException("减少旧月份计划数量失败,可能记录不存在或数量为0"); |
| | | } |
| | | |
| | | // 5. 增加新月份计划数量 |
| | | increasePlanQuantity(factoryId, newMonthPlan); |
| | | |
| | | return true; |
| | | } catch (Exception e) { |
| | | // 事务回滚 |
| | | throw new RuntimeException("日期变更失败: " + e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 减少计划数量,如果减少后数量为0则删除记录 |
| | | */ |
| | | private boolean decreasePlanQuantity(String factoryId, String monthPlan) { |
| | | // 先减少数量 |
| | | LambdaUpdateWrapper<EamFactorySecondMaintPlan> updateWrapper = new LambdaUpdateWrapper<>(); |
| | | updateWrapper.eq(EamFactorySecondMaintPlan::getFactoryId, factoryId) |
| | | .eq(EamFactorySecondMaintPlan::getMonthPlan, monthPlan) |
| | | .gt(EamFactorySecondMaintPlan::getQuantity, 0) // 确保数量大于0 |
| | | .setSql("quantity = quantity - 1"); |
| | | |
| | | int updated = baseMapper.update(null, updateWrapper); |
| | | |
| | | if (updated > 0) { |
| | | // 检查更新后的数量是否为0,如果是则删除记录 |
| | | EamFactorySecondMaintPlan updatedPlan = baseMapper.selectOne( |
| | | Wrappers.<EamFactorySecondMaintPlan>lambdaQuery() |
| | | .eq(EamFactorySecondMaintPlan::getFactoryId, factoryId) |
| | | .eq(EamFactorySecondMaintPlan::getMonthPlan, monthPlan) |
| | | ); |
| | | |
| | | if (updatedPlan != null && updatedPlan.getQuantity() == 0) { |
| | | baseMapper.deleteById(updatedPlan.getId()); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 增加计划数量 |
| | | */ |
| | | private void increasePlanQuantity(String factoryId, String monthPlan) { |
| | | // 直接更新 |
| | | LambdaUpdateWrapper<EamFactorySecondMaintPlan> updateWrapper = new LambdaUpdateWrapper<>(); |
| | | updateWrapper.eq(EamFactorySecondMaintPlan::getFactoryId, factoryId) |
| | | .eq(EamFactorySecondMaintPlan::getMonthPlan, monthPlan) |
| | | .setSql("quantity = quantity + 1"); |
| | | |
| | | if (baseMapper.update(null, updateWrapper) == 0) { |
| | | // 更新失败说明记录不存在,创建新记录 |
| | | createNewPlan(factoryId, monthPlan); |
| | | } |
| | | } |
| | | } |