|
@@ -1,10 +1,9 @@
|
|
|
package com.jeesite.modules.mes.service;
|
|
package com.jeesite.modules.mes.service;
|
|
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -15,6 +14,7 @@ import com.jeesite.modules.mes.entity.MesMoldMaintenanceItem;
|
|
|
import com.jeesite.modules.mes.entity.MesMoldMaintenancePlan;
|
|
import com.jeesite.modules.mes.entity.MesMoldMaintenancePlan;
|
|
|
import com.jeesite.modules.mes.dao.MesMoldMaintenanceItemDao;
|
|
import com.jeesite.modules.mes.dao.MesMoldMaintenanceItemDao;
|
|
|
import com.jeesite.modules.mes.dao.MesMoldMaintenancePlanDao;
|
|
import com.jeesite.modules.mes.dao.MesMoldMaintenancePlanDao;
|
|
|
|
|
+import com.jeesite.modules.mes.util.TencentSmsUtil;
|
|
|
import com.jeesite.modules.tianHong.dao.RptProductionSummaryDao;
|
|
import com.jeesite.modules.tianHong.dao.RptProductionSummaryDao;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -25,6 +25,14 @@ import com.jeesite.modules.tianHong.dao.RptProductionSummaryDao;
|
|
|
@Service
|
|
@Service
|
|
|
public class MesMoldMaintenancePlanService extends CrudService<MesMoldMaintenancePlanDao, MesMoldMaintenancePlan> {
|
|
public class MesMoldMaintenancePlanService extends CrudService<MesMoldMaintenancePlanDao, MesMoldMaintenancePlan> {
|
|
|
|
|
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(MesMoldMaintenancePlanService.class);
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: 请在此处填写接收预警短信的手机号(多个用英文逗号分隔)
|
|
|
|
|
+ private static final String SMS_PHONE_NUMBERS = "17203891886";
|
|
|
|
|
+
|
|
|
|
|
+ // 短信发送开关:true允许发送,发送成功后置为false,执行保养后置为true
|
|
|
|
|
+ private static volatile boolean smsAlertEnabled = true;
|
|
|
|
|
+
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private MesMoldMaintenanceItemDao mesMoldMaintenanceItemDao;
|
|
private MesMoldMaintenanceItemDao mesMoldMaintenanceItemDao;
|
|
|
|
|
|
|
@@ -41,63 +49,11 @@ public class MesMoldMaintenancePlanService extends CrudService<MesMoldMaintenanc
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 查询分页数据
|
|
* 查询分页数据
|
|
|
- * 查询后实时计算批次完成数和剩余批次生产数:
|
|
|
|
|
- * 批次完成数 = bzh库FinishQTY - 上次保养生产完成数
|
|
|
|
|
- * 剩余批次生产数 = 数据库中的remainBatchProductionNum - 批次完成数
|
|
|
|
|
|
|
+ * 剩余批次生产数由定时任务实时计算并更新到数据库,此处直接返回数据库值
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
public Page<MesMoldMaintenancePlan> findPage(MesMoldMaintenancePlan mesMoldMaintenancePlan) {
|
|
public Page<MesMoldMaintenancePlan> findPage(MesMoldMaintenancePlan mesMoldMaintenancePlan) {
|
|
|
- Page<MesMoldMaintenancePlan> page = super.findPage(mesMoldMaintenancePlan);
|
|
|
|
|
- List<MesMoldMaintenancePlan> list = page.getList();
|
|
|
|
|
- if (list != null && !list.isEmpty()) {
|
|
|
|
|
- // 收集所有非空moldCode
|
|
|
|
|
- List<String> moldCodes = new ArrayList<>();
|
|
|
|
|
- for (MesMoldMaintenancePlan plan : list) {
|
|
|
|
|
- if (plan.getMoldCode() != null && !plan.getMoldCode().isEmpty()) {
|
|
|
|
|
- moldCodes.add(plan.getMoldCode());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // 批量查询bzh库FinishQTY
|
|
|
|
|
- Map<String, Integer> finishQtyMap = new HashMap<>();
|
|
|
|
|
- if (!moldCodes.isEmpty()) {
|
|
|
|
|
- List<Map<String, Object>> finishQtyList = rptProductionSummaryDao.getFinishQtyByEqpIds(moldCodes);
|
|
|
|
|
- if (finishQtyList != null) {
|
|
|
|
|
- for (Map<String, Object> item : finishQtyList) {
|
|
|
|
|
- String eqpId = (String) item.get("eqpId");
|
|
|
|
|
- Integer finishQty = ((Number) item.get("finishQty")).intValue();
|
|
|
|
|
- finishQtyMap.put(eqpId, finishQty);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // 计算批次完成数和剩余批次生产数
|
|
|
|
|
- for (MesMoldMaintenancePlan plan : list) {
|
|
|
|
|
- Integer finishQty = finishQtyMap.get(plan.getMoldCode());
|
|
|
|
|
- if (finishQty == null) {
|
|
|
|
|
- finishQty = 0;
|
|
|
|
|
- }
|
|
|
|
|
- Integer lastFinishQty = plan.getLastMaintenanceFinishQty();
|
|
|
|
|
- if (lastFinishQty == null) {
|
|
|
|
|
- lastFinishQty = 0;
|
|
|
|
|
- }
|
|
|
|
|
- // 批次完成数 = FinishQTY - 上次保养生产完成数
|
|
|
|
|
- int batchCompleteNum = finishQty - lastFinishQty;
|
|
|
|
|
- if (batchCompleteNum < 0) {
|
|
|
|
|
- batchCompleteNum = 0;
|
|
|
|
|
- }
|
|
|
|
|
- plan.setBatchCompleteNum(batchCompleteNum);
|
|
|
|
|
- // 剩余批次生产数 = 当前值 - 批次完成数
|
|
|
|
|
- Integer remainNum = plan.getRemainBatchProductionNum();
|
|
|
|
|
- if (remainNum == null) {
|
|
|
|
|
- remainNum = 0;
|
|
|
|
|
- }
|
|
|
|
|
- int displayRemainNum = remainNum - batchCompleteNum;
|
|
|
|
|
- if (displayRemainNum < 0) {
|
|
|
|
|
- displayRemainNum = 0;
|
|
|
|
|
- }
|
|
|
|
|
- plan.setRemainBatchProductionNum(displayRemainNum);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return page;
|
|
|
|
|
|
|
+ return super.findPage(mesMoldMaintenancePlan);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -128,7 +84,7 @@ public class MesMoldMaintenancePlanService extends CrudService<MesMoldMaintenanc
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 执行保养:根据选中的保养项更新维保计划
|
|
* 执行保养:根据选中的保养项更新维保计划
|
|
|
- * 保养时记录bzh库当前FinishQTY作为上次保养生产完成数
|
|
|
|
|
|
|
+ * 保养时记录bzh库当前FinishQTY作为上次保养生产完成数,保存初始批次生产数,并重新开启短信预警
|
|
|
*/
|
|
*/
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public void doUpkeep(MesMoldMaintenancePlan mesMoldMaintenancePlan, String itemId) {
|
|
public void doUpkeep(MesMoldMaintenancePlan mesMoldMaintenancePlan, String itemId) {
|
|
@@ -138,6 +94,8 @@ public class MesMoldMaintenancePlanService extends CrudService<MesMoldMaintenanc
|
|
|
if (item != null) {
|
|
if (item != null) {
|
|
|
mesMoldMaintenancePlan.setMaintenanceItemId(item.getId());
|
|
mesMoldMaintenancePlan.setMaintenanceItemId(item.getId());
|
|
|
mesMoldMaintenancePlan.setRemainBatchProductionNum(item.getBatchProductionNum());
|
|
mesMoldMaintenancePlan.setRemainBatchProductionNum(item.getBatchProductionNum());
|
|
|
|
|
+ // 保存初始批次生产数,供定时任务计算使用
|
|
|
|
|
+ mesMoldMaintenancePlan.setInitialBatchProductionNum(item.getBatchProductionNum());
|
|
|
mesMoldMaintenancePlan.setLastMaintenanceLevel(item.getMaintenanceLevel());
|
|
mesMoldMaintenancePlan.setLastMaintenanceLevel(item.getMaintenanceLevel());
|
|
|
mesMoldMaintenancePlan.setLastMaintenanceContent(item.getMaintenanceContent());
|
|
mesMoldMaintenancePlan.setLastMaintenanceContent(item.getMaintenanceContent());
|
|
|
mesMoldMaintenancePlan.setLastMaintenanceDate(new Date());
|
|
mesMoldMaintenancePlan.setLastMaintenanceDate(new Date());
|
|
@@ -149,6 +107,59 @@ public class MesMoldMaintenancePlanService extends CrudService<MesMoldMaintenanc
|
|
|
mesMoldMaintenancePlan.setLastMaintenanceFinishQty(finishQty);
|
|
mesMoldMaintenancePlan.setLastMaintenanceFinishQty(finishQty);
|
|
|
}
|
|
}
|
|
|
super.save(mesMoldMaintenancePlan);
|
|
super.save(mesMoldMaintenancePlan);
|
|
|
|
|
+ // 执行保养后重新开启短信预警
|
|
|
|
|
+ smsAlertEnabled = true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 更新剩余批次生产数和批次完成数(供定时任务调用)
|
|
|
|
|
+ * 使用 REQUIRES_NEW 传播:每次更新在独立新事务中提交,避免加入外层只读事务导致长事务持锁
|
|
|
|
|
+ * @param id 维保计划ID
|
|
|
|
|
+ * @param remainBatchProductionNum 计算后的剩余批次生产数
|
|
|
|
|
+ * @param batchCompleteNum 批次完成数
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional(propagation = org.springframework.transaction.annotation.Propagation.REQUIRES_NEW)
|
|
|
|
|
+ public void updateRemainBatchProductionNum(String id, Integer remainBatchProductionNum, Integer batchCompleteNum) {
|
|
|
|
|
+ dao.updateRemainBatchProductionNum(id, remainBatchProductionNum, batchCompleteNum);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送剩余批次生产数预警短信
|
|
|
|
|
+ * 仅当 smsAlertEnabled 为 true 时才发送,发送成功后置为 false
|
|
|
|
|
+ * 短信发送失败不影响业务,仅记录日志
|
|
|
|
|
+ */
|
|
|
|
|
+ public void sendBatchAlertSms(MesMoldMaintenancePlan plan) {
|
|
|
|
|
+ if (!smsAlertEnabled) {
|
|
|
|
|
+ log.debug("短信预警已关闭,跳过发送。模具号={}, 剩余批次生产数={}",
|
|
|
|
|
+ plan.getMoldCode(), plan.getRemainBatchProductionNum());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (SMS_PHONE_NUMBERS == null || SMS_PHONE_NUMBERS.trim().isEmpty()) {
|
|
|
|
|
+ log.warn("未配置预警短信接收手机号,跳过发送。模具号={}, 剩余批次生产数={}",
|
|
|
|
|
+ plan.getMoldCode(), plan.getRemainBatchProductionNum());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ String[] phones = SMS_PHONE_NUMBERS.split(",");
|
|
|
|
|
+ boolean allSuccess = true;
|
|
|
|
|
+ for (String phone : phones) {
|
|
|
|
|
+ String trimmedPhone = phone.trim();
|
|
|
|
|
+ if (trimmedPhone.isEmpty()) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ TencentSmsUtil.sendSms(trimmedPhone);
|
|
|
|
|
+ log.info("模具维保预警短信发送成功: 模具号={}, 剩余批次生产数={}, 手机号={}",
|
|
|
|
|
+ plan.getMoldCode(), plan.getRemainBatchProductionNum(), trimmedPhone);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ allSuccess = false;
|
|
|
|
|
+ log.error("模具维保预警短信发送失败: 模具号={}, 剩余批次生产数={}, 手机号={}",
|
|
|
|
|
+ plan.getMoldCode(), plan.getRemainBatchProductionNum(), trimmedPhone, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 所有短信发送成功后,关闭短信预警,直到下次保养才重新开启
|
|
|
|
|
+ if (allSuccess) {
|
|
|
|
|
+ smsAlertEnabled = false;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|