package com.aps.core.service.impl.ApsPlate; import cn.hutool.core.util.IdUtil; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.aps.common.core.utils.DateUtils; import com.aps.common.core.utils.uuid.IdUtils; import com.aps.common.security.utils.DictUtils; import com.aps.core.domain.ApsPlate.ApsPlatePlan; import com.aps.core.domain.ApsPlate.ApsPlatePlanTemp; import com.aps.core.mapper.ApsPartPlanTempMapper; import com.aps.core.mapper.ApsPlatePlanMapper; import com.aps.core.mapper.ApsPlatePlanTempMapper; import com.aps.core.service.ApsPlate.IApsPlatePlanService; import com.aps.system.api.domain.SysDictData; import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.util.Strings; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * 钣金计划管理Service业务层处理 * * @author ruoyi * @date 2025-04-08 */ @Slf4j @Service public class ApsPlatePlanServiceImpl implements IApsPlatePlanService { @Autowired private ApsPlatePlanMapper apsPlatePlanMapper; @Autowired private ApsPartPlanTempMapper apsPartPlanTempMapper; @Autowired private ApsPlatePlanTempMapper apsPlatePlanTempMapper; @Autowired public RedisTemplate redisTemplate; /** * 查询钣金计划管理 * * @param id 钣金计划管理主键 * @return 钣金计划管理 */ @Override public ApsPlatePlan selectApsPlatePlanById(String id) { return apsPlatePlanMapper.selectApsPlatePlanById(id); } /** * 查询钣金计划管理列表 * * @param apsPlatePlan 钣金计划管理 * @return 钣金计划管理 */ @Override public List selectApsPlatePlanList(ApsPlatePlan apsPlatePlan) { return apsPlatePlanMapper.selectApsPlatePlanList(apsPlatePlan); } /** * 新增钣金计划管理 * * @param apsPlatePlan 钣金计划管理 * @return 结果 */ @Override public int insertApsPlatePlan(ApsPlatePlan apsPlatePlan) { apsPlatePlan.setId(IdUtil.getSnowflakeNextId()); apsPlatePlan.setCreateTime(DateUtils.getNowDate()); return apsPlatePlanMapper.insertApsPlatePlan(apsPlatePlan); } /** * 修改钣金计划管理 * * @param apsPlatePlan 钣金计划管理 * @return 结果 */ @Override public int updateApsPlatePlan(ApsPlatePlan apsPlatePlan) { return apsPlatePlanMapper.updateApsPlatePlan(apsPlatePlan); } /** * 批量删除钣金计划管理 * * @param ids 需要删除的钣金计划管理主键 * @return 结果 */ @Override public int deleteApsPlatePlanByIds(String[] ids) { return apsPlatePlanMapper.deleteApsPlatePlanByIds(ids); } /** * 删除钣金计划管理信息 * * @param id 钣金计划管理主键 * @return 结果 */ @Override public int deleteApsPlatePlanById(String id) { return apsPlatePlanMapper.deleteApsPlatePlanById(id); } @Override public int confirmPlan(ApsPlatePlanTemp tempPlan) { //先删除所有 apsPlatePlanMapper.removeAllPlatePlans(); //查询临时表数据 List apsPartPlanTemps=apsPlatePlanTempMapper.selectApsPlatePlanTempList(tempPlan); int count=0; String[] ids=new String[apsPartPlanTemps.size()]; for (int i = 0; i tempList) throws Exception { String batchNum= IdUtils.fastUUID(); //判断导入数据是否为空 if (tempList.size() > 0) { /* * aps_business_type aps_document_status aps_is_suspended aps_account * */ List suspendedDic = DictUtils.getDictCache("aps_is_suspended"); List businessTypeDic = DictUtils.getDictCache("aps_business_type"); List documentStatusDic = DictUtils.getDictCache("aps_document_status"); List accountDic = DictUtils.getDictCache("aps_account"); for (int i = 0; i < tempList.size(); i++) { ApsPlatePlanTemp planTemp = tempList.get(i); String isSuspendedTxt = planTemp.getIsSuspended().trim(); String businessType = planTemp.getBusinessType().trim(); String documentStatusText = planTemp.getDocumentStatus().trim(); String account = planTemp.getAccount().trim(); Optional documentStatusFirst = documentStatusDic.stream().filter(x -> x.getDictLabel().equals(documentStatusText)).findFirst(); documentStatusFirst.ifPresent(sysDictData -> planTemp.setDocumentStatus(sysDictData.getDictValue())); Optional accountFirst = accountDic.stream().filter(x -> x.getDictLabel().equals(account)).findFirst(); accountFirst.ifPresent(sysDictData -> planTemp.setAccount(sysDictData.getDictValue())); Optional businessTypeFirst = businessTypeDic.stream().filter(x -> x.getDictLabel().equals(businessType)).findFirst(); businessTypeFirst.ifPresent(sysDictData -> planTemp.setBusinessType(sysDictData.getDictValue())); Optional isSuspendedFirst = suspendedDic.stream().filter(x -> x.getDictLabel().equals(isSuspendedTxt)).findFirst(); isSuspendedFirst.ifPresent(sysDictData -> planTemp.setIsSuspended(sysDictData.getDictValue())); planTemp.setId(IdUtils.fastUUID()); //插入版本号 planTemp.setBatchNumber(batchNum); //插入临时表 apsPlatePlanTempMapper.insertApsPlatePlanTemp(planTemp); } return batchNum ; } else { return Strings.EMPTY; } } /** * 钣金冗余工单报表 * @param apsPlatePlan * @return */ @Override public List selectPlateRedundantOrderList(ApsPlatePlan apsPlatePlan) { return apsPlatePlanMapper.selectPlateRedundantOrderList(apsPlatePlan); } @Override public ApsPlatePlan selectUnMatchPlateSubPlan(String plant, String itemNumber, Hashtable> subPlans) { if(subPlans.containsKey(itemNumber)) { List subPlanList = subPlans.get(itemNumber); Optional planOptional = subPlanList.stream().filter(x -> x.getUnmatchedQuantity().compareTo(BigDecimal.ZERO) > 0).findFirst(); return planOptional.orElse(null); }else { List plans = getSubPlansFromRedis(itemNumber); if (!plans.isEmpty()) { subPlans.put(itemNumber, plans); return selectUnMatchPlateSubPlan(plant, itemNumber, subPlans); } else { return null; } } } @Override public boolean setSubPlansToRedis() { try { log.info("开始设置钣金子计划到redis"); Set keys = redisTemplate.keys("PLATE_SUB_PLAN:*"); if (keys != null && !keys.isEmpty()) { redisTemplate.delete(keys); } List subPlans = apsPlatePlanMapper.selectApsSubPlatePlan(); // ApsPlatePlan a = JSONObject.parseObject(String.valueOf(subPlans.get(0)), ApsPlatePlan.class); Map> groupByItemNumber = subPlans.stream().collect(Collectors.groupingBy(obj -> obj.getString("itemNumber"))); Map bulkData = new HashMap<>(); for (Map.Entry> entry : groupByItemNumber.entrySet()) { String key = entry.getKey(); List value = entry.getValue(); bulkData.put("PLATE_SUB_PLAN:"+key, value); } redisTemplate.opsForValue().multiSet(bulkData); log.info("设置钣金子计划到redis完成"); return true; } catch (Exception e) { e.printStackTrace(); log.error("设置钣金子计划到redis失败"+e.getMessage()); return false; } } public List getSubPlansFromRedis(String itemNumber) { List subPlans=new ArrayList<>(); JSONArray jsonArray =(JSONArray) redisTemplate.opsForValue().get("PLATE_SUB_PLAN:" + itemNumber); if (jsonArray != null && !jsonArray.isEmpty()) { subPlans = jsonArray.stream().map(obj -> JSONObject.parseObject(String.valueOf(obj), ApsPlatePlan.class)).toList(); subPlans.forEach(subPlan -> subPlan.setVersion(0)); } return subPlans; } }