Commit db879f0d by 叶立庆

日报扣款数改为直接查询手机号填写的日报天数和自然月天数进行差值

1 parent 5b8391c1
...@@ -2,8 +2,18 @@ package cn.com.glsx.dj.staff.mapper; ...@@ -2,8 +2,18 @@ package cn.com.glsx.dj.staff.mapper;
import cn.com.glsx.dj.staff.model.StaffMonthBdmCount; import cn.com.glsx.dj.staff.model.StaffMonthBdmCount;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.oreframework.datasource.mybatis.mapper.OreMapper; import org.oreframework.datasource.mybatis.mapper.OreMapper;
@Mapper @Mapper
public interface StaffMonthBdmCountMapper extends OreMapper<StaffMonthBdmCount> { public interface StaffMonthBdmCountMapper extends OreMapper<StaffMonthBdmCount> {
/**
* 统计填写了日报的天数
* @param firstDay
* @param lastDay
* @param staffPhone
* @return
*/
Integer dailyCount(@Param("firstDay") String firstDay,@Param("lastDay") String lastDay,@Param("staffPhone") String staffPhone);
} }
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.staff.service.salary; package cn.com.glsx.dj.staff.service.salary;
import cn.com.glsx.dj.smartcarlife.dto.merchantPerformanceConfig.StaffBehaviorDeductMoneyRecordDTO;
import cn.com.glsx.dj.smartcarlife.enums.MerchantPerformanceConfigEnum.BehaviorDeductType; import cn.com.glsx.dj.smartcarlife.enums.MerchantPerformanceConfigEnum.BehaviorDeductType;
import cn.com.glsx.dj.smartcarlife.model.StaffBaseInfo; import cn.com.glsx.dj.smartcarlife.model.StaffBaseInfo;
import cn.com.glsx.dj.smartcarlife.model.StaffConfig; import cn.com.glsx.dj.smartcarlife.model.StaffConfig;
...@@ -37,10 +36,7 @@ import tk.mybatis.mapper.entity.Example; ...@@ -37,10 +36,7 @@ import tk.mybatis.mapper.entity.Example;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Slf4j @Slf4j
...@@ -196,12 +192,11 @@ public class StaffMonthSalarySlipService { ...@@ -196,12 +192,11 @@ public class StaffMonthSalarySlipService {
RpcAssert.assertNotEmpty(info.getStaffPhone(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.staffPhone not be null"); RpcAssert.assertNotEmpty(info.getStaffPhone(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.staffPhone not be null");
RpcAssert.assertNotEmpty(info.getStaffName(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.staffName not be null"); RpcAssert.assertNotEmpty(info.getStaffName(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.staffName not be null");
RpcAssert.assertNotEmpty(info.getSalaryMonth(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.salaryMonth not be null"); RpcAssert.assertNotEmpty(info.getSalaryMonth(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.salaryMonth not be null");
// RpcAssert.assertNotEmpty(info.getFinalDataFlag(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.finalDataFlag not be null");
RpcAssert.assertNotEmpty(info.getUpdatedBy(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.updatedBy not be null"); RpcAssert.assertNotEmpty(info.getUpdatedBy(), DefaultErrorEnum.DATA_NULL, "StaffMonthSalarySlip.updatedBy not be null");
try { try {
//日报扣款计算
dailyDeduct(info);
StaffConfig sc = staffBaseInfoRemote.getStaffConfig(info.getStaffPhone()).getResult(); StaffConfig sc = staffBaseInfoRemote.getStaffConfig(info.getStaffPhone()).getResult();
//日报扣款天数计算
Integer dailyDeductCount = dailyDeduct(info, sc);
//pad扣款计算 //pad扣款计算
StaffMonthBdmCount bdmCount = padDeduct(info, sc); StaffMonthBdmCount bdmCount = padDeduct(info, sc);
StaffMonthSalarySlip oldRecord = thiz.get(info.getStaffPhone(), info.getSalaryMonth()); StaffMonthSalarySlip oldRecord = thiz.get(info.getStaffPhone(), info.getSalaryMonth());
...@@ -220,30 +215,14 @@ public class StaffMonthSalarySlipService { ...@@ -220,30 +215,14 @@ public class StaffMonthSalarySlipService {
} }
} }
// 平板扣款次数 // 平板扣款次数
int dailyDeductCount = 0;
int padDeductCount = 0; int padDeductCount = 0;
// 先查询平板扣款次数 // 先查询平板扣款次数
if (bdmCount != null) { if (bdmCount != null) {
padDeductCount = bdmCount.getBehaviorCount(); padDeductCount = bdmCount.getBehaviorCount();
} }
// 根据员工手机号和薪资月份查询日报行为扣款记录列表
RpcResponse<List<StaffBehaviorDeductMoneyRecordDTO>> resp = staffBehaviorDeductMoneyRecordRemote.list(StaffBehaviorDeductMoneyRecordDTO.builder()
.staffPhone(info.getStaffPhone()).behaviorDeductType(BehaviorDeductType.DAILY.getValue()).deductMonth(info.getSalaryMonth())
.build());
if (resp.isSuccess() && resp.hasResult()) {
List<StaffBehaviorDeductMoneyRecordDTO> staffBehaviorDeductList = resp.getResult();
// 如果存在行为扣款记录,则遍历处理
if (CollectionUtils.isNotEmpty(staffBehaviorDeductList)) {
for (StaffBehaviorDeductMoneyRecordDTO deduct : staffBehaviorDeductList) {
// 如果未更新日常扣除标志且当前记录为日常类型,则累加日常扣除次数
dailyDeductCount += deduct.getBehaviorCount();
}
}
} else {
log.error("获取员工日常扣除记录失败:{}", JSON.toJSONString(resp));
}
// 获取工资绩效数据 // 获取工资绩效数据
StaffMonthSalaryDTO salary1 = performanceEvaluationMapper.queryStaffMonthSalary(info.getSalaryMonth(), info.getStaffPhone()); StaffMonthSalaryDTO salary1 = performanceEvaluationMapper.queryStaffMonthSalary(info.getSalaryMonth(), info.getStaffPhone());
log.info("员工工资绩效:{}", JSON.toJSONString(salary1));
if (salary1 == null) { if (salary1 == null) {
salary1 = new StaffMonthSalaryDTO(); salary1 = new StaffMonthSalaryDTO();
} }
...@@ -422,13 +401,80 @@ public class StaffMonthSalarySlipService { ...@@ -422,13 +401,80 @@ public class StaffMonthSalarySlipService {
return null; return null;
} }
private void dailyDeduct(StaffMonthSalarySlip info) { private Integer dailyDeduct(StaffMonthSalarySlip info, StaffConfig sc) {
// 跑指定月数据 try {
List<Date> dateList = cn.com.glsx.dj.staff.util.DateUtils.getMonthDates(info.getSalaryMonth()); // 1. 构造查询条件
RpcResponse<Integer> rpcResponse = staffBehaviorDeductMoneyRecordRemote.dailyDeductMoneyHandlerRemote(dateList, info.getStaffPhone()); StaffMonthBdmCount query = new StaffMonthBdmCount();
if (!rpcResponse.isSuccess()) { query.setStaffPhone(info.getStaffPhone());
log.error("计算日报扣款数据异常:{}", rpcResponse); query.setDeductMonth(info.getSalaryMonth());
query.setBehaviorDeductType(BehaviorDeductType.DAILY.getValue());
query.setDeletedFlag(DeletedFlagEnum.N.name());
// 2. 查询已有记录
StaffMonthBdmCount oldRecord = null;
List<StaffMonthBdmCount> list = staffMonthBdmCountMapper.select(query);
if (CollectionUtils.isNotEmpty(list)) {
oldRecord = list.get(0);
}
// 3. 准备数据对象
StaffMonthBdmCount bdmCount = new StaffMonthBdmCount();
if (oldRecord != null) {
bdmCount.setId(oldRecord.getId());
}
// 4. 设置配置项 (这里复用 PadDeduction 字段存储金额配置)
if (sc != null && Objects.equals(sc.getDailyOpenFlag(), StatusEnum.YES.getValue())) {
bdmCount.setPadDeduction(sc.getDailyDeduction());
} else {
log.info("员工日报扣款配置信息为空或未开启:{}", info.getStaffPhone());
bdmCount.setPadDeduction(BigDecimal.ZERO);
}
// 5. 计算行为分 (日报缺失天数)
Integer count = 0;
Integer daysInMonth = 0;
if (oldRecord != null && Objects.equals(oldRecord.getModifyFlag(), 1)) {
// 如果已修正(申诉过),以旧记录为准
count = oldRecord.getBehaviorCount();
} else {
// 否则重新计算
daysInMonth = cn.com.glsx.dj.staff.util.DateUtils.getDaysInMonth(info.getSalaryMonth());
if (daysInMonth == 0) {
log.error("月份天数统计错误:{}", info);
} else {
Map<String, String> map = cn.com.glsx.dj.staff.util.DateUtils.getMonthBoundaries(info.getSalaryMonth());
count = staffMonthBdmCountMapper.dailyCount(map.get("firstDay"), map.get("lastDay"), info.getStaffPhone());
count = (count == null) ? 0 : count;
}
}
bdmCount.setBehaviorCount(Math.abs(count - daysInMonth));
// 6. 设置通用字段
bdmCount.setUpdatedBy(info.getUpdatedBy());
bdmCount.setUpdatedDate(new Date());
// 7. 执行更新或新增
if (oldRecord != null) {
staffMonthBdmCountMapper.updateByPrimaryKeySelective(bdmCount);
} else {
bdmCount.setStaffName(info.getStaffName());
bdmCount.setStaffPhone(info.getStaffPhone());
bdmCount.setDeductMonth(info.getSalaryMonth());
bdmCount.setBehaviorDeductType(BehaviorDeductType.DAILY.getValue());
bdmCount.setModifyFlag(0);
bdmCount.setCreateBy(info.getUpdatedBy());
bdmCount.setCreatedDate(new Date());
bdmCount.setDeletedFlag(DeletedFlagEnum.N.name());
staffMonthBdmCountMapper.insertUseGeneratedKeys(bdmCount);
}
return count;
} catch (Exception e) {
log.error("计算日报扣款数据异常: staffPhone={}, month={}", info.getStaffPhone(), info.getSalaryMonth(), e);
} }
return 0;
} }
public Page<StaffMonthSalarySlipDTO> pageStaffMonthSalary(RpcPagination<StaffMonthSalarySlipDTO> pagination) { public Page<StaffMonthSalarySlipDTO> pageStaffMonthSalary(RpcPagination<StaffMonthSalarySlipDTO> pagination) {
......
...@@ -1616,5 +1616,30 @@ public class DateUtils extends cn.com.glsx.framework.core.util.DateUtils { ...@@ -1616,5 +1616,30 @@ public class DateUtils extends cn.com.glsx.framework.core.util.DateUtils {
} }
} }
/**
* 获取指定月份的天数
* @param monthStr 格式为 yyyy-MM 的字符串
* @return 当月的天数,如果输入格式错误返回0
*/
public static int getDaysInMonth(String monthStr) {
try {
// 验证格式
if (monthStr == null || !monthStr.matches("\\d{4}-\\d{2}")) {
return 0;
}
// 解析为 YearMonth
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
YearMonth yearMonth = YearMonth.parse(monthStr, formatter);
// 返回当月天数
return yearMonth.lengthOfMonth();
} catch (DateTimeParseException e) {
System.err.println("日期格式错误,请使用 yyyy-MM 格式: " + e.getMessage());
return 0;
}
}
} }
...@@ -28,4 +28,18 @@ ...@@ -28,4 +28,18 @@
id, staff_name, staff_phone, deduct_month, behavior_deduct_type, behavior_count, shop_count, recording_count, pad_deduction, id, staff_name, staff_phone, deduct_month, behavior_deduct_type, behavior_count, shop_count, recording_count, pad_deduction,
modify_flag, created_date, create_by, updated_date, updated_by, deleted_flag modify_flag, created_date, create_by, updated_date, updated_by, deleted_flag
</sql> </sql>
<select id="dailyCount" resultType="java.lang.Integer">
SELECT COUNT(DISTINCT dr.report_date) AS days_with_report
FROM dj_car_life_staff_daily_report dr
WHERE EXISTS (
SELECT 1
FROM dj_car_life_staff_info si
WHERE si.staff_uuid = dr.staff_uuid
AND si.phone_number = #{staffPhone}
AND si.deleted_flag = 'N'
)
AND dr.deleted_flag = 'N'
AND dr.report_date <![CDATA[>=]]> #{firstDay}
AND dr.report_date <![CDATA[<=]]> #{lastDay}
</select>
</mapper> </mapper>
\ No newline at end of file \ No newline at end of file
  • @wangxw1
    commented
    Master

    你好,我是资深的软件开发工程师。以下是对你提交的代码变更进行的审查意见:

    一、关键问题与优化建议(Top 3)

    1. 逻辑实现错误:返回值含义冲突与边界值计算风险

      • 问题描述:
        • dailyDeduct 方法内部计算了扣款次数并存入数据库(daysInMonth - count),但其返回值却是 count(即已提交日报的天数)。
        • 在调用方 StaffMonthSalarySlipService 中,变量名被定义为 dailyDeductCount(意为扣款次数),这会导致后续业务逻辑若使用该变量,数值将完全错误(实为报表提交次数)。
        • 此外,当 getDaysInMonth 返回 0(异常情况)时,Math.abs(count - 0) 会导致扣款次数等于已提交天数,造成严重的逻辑反转。
      • 影响分析:薪资计算结果将出现重大偏差,可能导致员工被错误扣款或漏扣,产生业务资损或投诉。
      • 优化建议: java // 修正返回值,确保与变量名含义一致 Integer missingDays = Math.max(0, daysInMonth - count); bdmCount.setBehaviorCount(missingDays); // ... 省略其他逻辑 return missingDays;
    2. 异常处理机制欠佳:过度宽泛的异常捕获与错误屏蔽

      • 问题描述:dailyDeduct 方法使用了 catch (Exception e) 捕获了所有异常,并简单返回 0。
      • 影响分析:如果数据库连接失败、SQL 语法错误或空指针异常发生,系统会默认该员工“扣款天数为 0”,从而掩盖了系统故障,导致数据不一致且难以排查。
      • 优化建议:不要直接返回 0。应根据业务需求决定是向上抛出异常(中断计算),还是返回一个明确的错误标识或 Optional。至少应在日志中记录完整的堆栈信息,并考虑是否需要触发告警。
    3. 工程质量问题:禁止使用 System.err 输出日志

      • 问题描述:DateUtils.getDaysInMonth 方法在捕获 DateTimeParseException 后,使用了 System.err.println 打印错误。
      • 影响分析:在分布式 RPC 服务中,System.err 的输出通常无法被日志系统(如 ELK)正常采集和追踪,且在高并发下可能存在性能瓶颈(同步锁)。
      • 优化建议:使用类中定义的 @Slf4j 日志对象进行输出。 java log.error("日期格式错误,请使用 yyyy-MM 格式: {}, 详情: {}", monthStr, e.getMessage());

    二、评分明细

    评分维度 分数 理由
    功能实现正确性与健壮性 26/40 存在明显的逻辑含义冲突(已交天数 vs 扣款天数),且边界值(0天)处理存在风险。
    安全性与潜在风险 30/30 未发现 SQL 注入等安全漏洞,MyBatis 使用了参数绑定。
    最佳实践与可维护性 14/20 命名存在误导性;使用了 System.err;Math.abs 的使用掩盖了可能的逻辑错误。
    性能与资源利用 5/5 SQL 查询逻辑尚可,使用了 DISTINCT 和 EXISTS 处理关联查询。
    提交信息质量 5/5 Commit 信息清晰地描述了变更目的。

    三、总分

    总分:80分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!