diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisWaybillAjustApplyServiceImpl.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisWaybillAjustApplyServiceImpl.java index 5ec79d07e..393d28396 100644 --- a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisWaybillAjustApplyServiceImpl.java +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisWaybillAjustApplyServiceImpl.java @@ -10,12 +10,14 @@ package com.xdadan.erp.emis.service.impl; +import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.ZoneId; -import java.util.Calendar; +import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; import java.util.List; -import java.util.Objects; +import java.util.Set; import com.alibaba.fastjson.JSONObject; import com.xdadan.erp.common.core.domain.entity.SysUser; @@ -27,12 +29,13 @@ import com.xdadan.erp.emis.domain.exception.EmisBizError; import com.xdadan.erp.emis.domain.vo.WaybillOrder; import com.xdadan.erp.emis.mapper.EmisCommissionDtlMapper; import com.xdadan.erp.emis.service.*; -import com.xdadan.erp.emis.utils.WaybillHelper; +import com.xdadan.erp.system.domain.SysPost; import com.xdadan.erp.system.mapper.SysPostMapper; -import com.xdadan.erp.system.service.ISysFileInfoService; -import com.xdadan.erp.system.service.ISysNoticeService; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.xdadan.erp.emis.mapper.EmisWaybillAjustApplyMapper; @@ -45,6 +48,7 @@ import org.springframework.transaction.annotation.Transactional; * @date 2024-06-25 23:39:28 */ @Service +@Slf4j public class EmisWaybillAjustApplyServiceImpl extends EmisBaseService implements IEmisWaybillAjustApplyService { @Autowired @@ -417,8 +421,12 @@ public class EmisWaybillAjustApplyServiceImpl extends EmisBaseService implements emisWaybillService.reUpdateWayBill(emisWaybillOld, emisWaybillSave, false, false); } - String title="费用调整已审批通过,单号【"+emisWaybillAjustApply.getBillCode()+"】"; - sendNoticeToEmpUser(emisWaybillAjustApply.getApplyManCode(), title, "", "/d24/expenseRecognitionManagemen/WaybillAjustApplyList"); + String title = "费用调整已审批通过,单号【" + emisWaybillAjustApply.getBillCode() + "】"; + sendNoticeToEmpUser(emisWaybillAjustApply.getApplyManCode(), title, "", + "/d24/expenseRecognitionManagemen/WaybillAjustApplyList"); + + // 检查申请人是否为单证员且费用发生变化时,需要通知相关人员 + checkAndNotifyFeeChange(emisWaybillAjustApply, emisWaybillOld, emisWaybillSave); if ("6".equals(emisWaybillOld.getPaymentType()) && !"6".equals(emisWaybillSave.getPaymentType())) { // 重新计算提成 @@ -519,4 +527,165 @@ public class EmisWaybillAjustApplyServiceImpl extends EmisBaseService implements return emisWaybillAjustApplyMapper.auditCenterNoPass(emisWaybillAjustApply); } + /** + * 检查申请人是否为单证员且费用发生变化时,通知相关人员 + * + * @param emisWaybillAjustApply 运单调整申请 + * @param emisWaybillOld 原运单信息 + * @param emisWaybillSave 新运单信息 + */ + private void checkAndNotifyFeeChange(EmisWaybillAjustApply emisWaybillAjustApply, + EmisWaybill emisWaybillOld, + EmisWaybill emisWaybillSave) { + try { + String title = "费用调整已审批通过,单号【" + emisWaybillAjustApply.getBillCode() + "】"; + String message = "单证员已对运单费用进行调整,请关注相关变化"; + String clickPath = "/d24/expenseRecognitionManagemen/WaybillAjustApplyList"; + + // 1. 根据applyManCode查询用户岗位,判断是否包含CP(单证员) + boolean isCP = checkIsCP(emisWaybillAjustApply.getApplyManCode()); + + // 2. 检测网点费用申请是否涉及费用发生变化 + boolean feeChanged = checkFeeChanged(emisWaybillOld, emisWaybillSave); + + if (!isCP || !feeChanged) { + return; // 不是单证员/费用没有变化,无需特殊处理 + } + + // 3. 获取运单对应的payee、salesmen信息 + String payee = emisWaybillSave.getPayee(); + String salesmen = emisWaybillSave.getSalesmen(); + + // 4. 将payee、salesmen放到Set集合中去重 + Set empNames = new HashSet<>(); + if (!StringUtils.isEmpty(payee)) { + empNames.add(payee); + } + if (!StringUtils.isEmpty(salesmen)) { + empNames.add(salesmen); + } + + // 5. 查询去重后的emp_name对应的emp_code + Set notifyEmpCodes = new HashSet<>(); + for (String empName : empNames) { + List empCodes = getEmpCodesByEmpName(empName); + notifyEmpCodes.addAll(empCodes); + } + + // 6. 分别调用sendNoticeToEmpUser发送通知 + if (!notifyEmpCodes.isEmpty()) { + for (String empCode : notifyEmpCodes) { + if (!StringUtils.isEmpty(empCode)) { + sendNoticeToEmpUser(empCode, title, message, + clickPath); + } + } + } + + } catch (Exception e) { + // 记录异常但不影响主流程 + log.error("检查费用变化通知时发生异常", e); + } + } + + /** + * 检查申请人是否为单证员(岗位代码包含CP) + * + * @param empCode 员工代码 + * @return 是否为单证员 + */ + private boolean checkIsCP(String empCode) { + if (StringUtils.isEmpty(empCode)) { + return false; + } + + try { + SysUser opUser = getEmisStaffByCode(empCode); + if (opUser == null) { + return false; + } + + // 根据user_id关联sys_user_post查询post_id + List postIds = postMapper.selectPostListByUserId(opUser.getUserId()); + if (CollectionUtils.isEmpty(postIds)) { + return false; + } + + // 根据post_id关联sys_post获取post_code,判断是否包含CP + for (Long postId : postIds) { + SysPost sysPost = new SysPost(); + sysPost.setPostId(postId); + List postList = postMapper.selectPostList(sysPost); + if (!CollectionUtils.isEmpty(postList)) { + SysPost post = postList.get(0); + if (post.getPostCode() != null && post.getPostCode().contains("CP")) { + return true; + } + } + } + + } catch (Exception e) { + log.error("检查单证员岗位时发生异常", e); + } + + return false; + } + + /** + * 检查费用是否发生变化 + * + * @param oldWaybill 原运单 + * @param newWaybill 新运单 + * @return 费用是否发生变化 + */ + private boolean checkFeeChanged(EmisWaybill oldWaybill, EmisWaybill newWaybill) { + if (oldWaybill == null || newWaybill == null) { + return false; + } + + // 比较运费 + BigDecimal oldFreight = oldWaybill.getFreight(); + BigDecimal newFreight = newWaybill.getFreight(); + + if (oldFreight == null && newFreight == null) { + return false; + } + + if (oldFreight == null || newFreight == null) { + return true; + } + + return oldFreight.compareTo(newFreight) != 0; + } + + /** + * 根据员工姓名获取员工代码列表 + * 一个emp_name可能对应多个emp_code + * + * @param empName 员工姓名 + * @return 员工代码列表 + */ + private List getEmpCodesByEmpName(String empName) { + List empCodes = new ArrayList<>(); + if (StringUtils.isEmpty(empName)) { + return empCodes; + } + + try { + // 使用现有的方法获取单个用户 + SysUser user = getEmisStaffByEmpName(empName); + if (user != null && !StringUtils.isEmpty(user.getEmpCode())) { + empCodes.add(user.getEmpCode()); + } + + // 如果还需要查询多个同名用户,可以在这里添加额外的查询逻辑 + // 目前先使用单个查询,如果需要支持多个同名用户,需要添加新的查询方法 + + } catch (Exception e) { + log.error("根据员工姓名查询员工代码时发生异常,empName: {}", empName, e); + } + + return empCodes; + } + }