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..17357f3fc 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,10 @@ 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.Date; -import java.util.List; -import java.util.Objects; +import java.util.*; import com.alibaba.fastjson.JSONObject; import com.xdadan.erp.common.core.domain.entity.SysUser; @@ -26,11 +24,12 @@ import com.xdadan.erp.emis.domain.enumtype.WaybillOperLogType; 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.mapper.EmisSalesmenRelMapper; 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 com.xdadan.erp.system.mapper.SysUserMapper; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -45,6 +44,7 @@ import org.springframework.transaction.annotation.Transactional; * @date 2024-06-25 23:39:28 */ @Service +@Slf4j public class EmisWaybillAjustApplyServiceImpl extends EmisBaseService implements IEmisWaybillAjustApplyService { @Autowired @@ -62,7 +62,6 @@ public class EmisWaybillAjustApplyServiceImpl extends EmisBaseService implements @Autowired private SysPostMapper postMapper; - @Autowired private IEmisSettleSubBillService emisSettleSubBillService; @@ -72,6 +71,12 @@ public class EmisWaybillAjustApplyServiceImpl extends EmisBaseService implements @Autowired private IEmisSettleBillService emisSettleBillService; + @Autowired + private SysUserMapper sysUserMapper; + + @Autowired + private EmisSalesmenRelMapper emisSalesmenRelMapper; + /** * 查询运单调整申请 @@ -417,8 +422,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 +528,204 @@ 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 message = "单证员已对运单费用进行调整,请关注相关变化"; + String clickPath = "/d24/expenseRecognitionManagemen/WaybillAjustApplyList"; + + // 1. 根据applyManCode查询用户岗位,判断是否包含CP(单证员) + // boolean isCP = checkIsCP(emisWaybillAjustApply.getApplyManCode()); + + // 2. 检测网点费用申请是否涉及费用发生变化 + boolean feeChanged = checkFeeChanged(emisWaybillOld, emisWaybillSave); + + if (!feeChanged) { + return; // 费用没有变化,无需特殊处理 + } + + boolean isDZZS = checkIsDZZS(emisWaybillAjustApply.getApplyManCode()); + + if (!isDZZS) { + 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); + // 从销售关联表查询销售助理并加入通知名单 + EmisSalesmenRel relCond = new EmisSalesmenRel(); + relCond.setSalesmen(salesmen); + relCond.setBlOpen("1"); + List relList = emisSalesmenRelMapper.selectEmisSalesmenRelList(relCond); + if (!CollectionUtils.isEmpty(relList)) { + for (EmisSalesmenRel rel : relList) { + if (rel != null && !StringUtils.isEmpty(rel.getSalesAss())) { + empNames.add(rel.getSalesAss()); + } + } + } + } + + // 5. 查询去重后的emp_name对应的emp_code,并构建empCode->empName映射,避免后续重复查询 + Set notifyEmpCodes = new HashSet<>(); + java.util.Map empCodeToName = new java.util.HashMap<>(); + for (String name : empNames) { + SysUser cond = new SysUser(); + cond.setEmpName(name); + List users = sysUserMapper.selectUserList(cond); + if (!CollectionUtils.isEmpty(users)) { + for (SysUser u : users) { + if (u != null && !StringUtils.isEmpty(u.getEmpCode())) { + notifyEmpCodes.add(u.getEmpCode()); + if (!StringUtils.isEmpty(u.getEmpName())) { + empCodeToName.put(u.getEmpCode(), u.getEmpName()); + } + } + } + } + } + + // 6. 循环外预先计算申请人姓名与费用变更字符串,减少重复计算 + SysUser applyUser = getEmisStaffByCode(emisWaybillAjustApply.getApplyManCode()); + String applyManName = (applyUser != null && !StringUtils.isEmpty(applyUser.getEmpName())) + ? applyUser.getEmpName() + : emisWaybillAjustApply.getApplyManCode(); + BigDecimal oldFreight = emisWaybillOld != null ? emisWaybillOld.getFreight() : null; + BigDecimal newFreight = emisWaybillSave != null ? emisWaybillSave.getFreight() : null; + String oldFreightStr = oldFreight == null ? "-" : oldFreight.stripTrailingZeros().toPlainString(); + String newFreightStr = newFreight == null ? "-" : newFreight.stripTrailingZeros().toPlainString(); + + // 7. 分别调用sendNoticeToEmpUser发送通知 + if (!notifyEmpCodes.isEmpty()) { + for (String empCode : notifyEmpCodes) { + if (!StringUtils.isEmpty(empCode)) { + String empName = !StringUtils.isEmpty(empCodeToName.get(empCode)) ? empCodeToName.get(empCode) + : empCode; + String title = applyManName + "(" + emisWaybillAjustApply.getApplyManCode() + ")" + + "已对运单费用进行调整," + "请" + empName + "(" + empCode + ")" + "仔细核对," + + "运单号" + emisWaybillAjustApply.getBillCode() + ",运费由" + oldFreightStr + "调整为" + + newFreightStr; + sendNoticeToEmpUser(empCode, title, "", + 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 = postMapper.selectPostById(postId); + if (Objects.nonNull(sysPost)) { + if (sysPost.getPostCode() != null && sysPost.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 = new SysUser(); + user.setEmpName(empName); + List sysUsers = sysUserMapper.selectUserList(user); + sysUsers.stream().map(SysUser::getEmpCode).forEach(empCodes::add); + + } catch (Exception e) { + log.error("根据员工姓名查询员工代码时发生异常,empName: {}", empName, e); + } + + return empCodes; + } + }