Commit 8de7ef1d by zhouhaibao

feat(merchant): 添加门店交付数目标配置相关组件

- 创建 MerchantDeliverTargetConfigDTO 数据传输对象
- 实现 MerchantDeliverTargetConfigMapper 数据访问接口
- 配置 MyBatis XML 映射文件实现数据库操作
- 提供按商户和月份查询目标配置的功能
- 实现查询门店最大归属周期配置的方法
- 添加查询门店所有交付目标配置的接口
- 实现计算门店指定月份目标交付数总和的功能
1 parent dd32f08e
package cn.com.glsx.dj.peanalysis.dto;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
/**
* 门店交付数目标配置DTO
*
* 对应表: dj_car_life_merchant_deliver_target_config
*/
public class MerchantDeliverTargetConfigDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
private Integer id;
/**
* 商户号
*/
private Integer merchantId;
/**
* 商户名称
*/
private String merchantName;
/**
* 目标交付数
*/
private BigDecimal targetValue;
/**
* 目标月份(yyyy-MM)
*/
private String targetMonth;
/**
* 品牌id
*/
private Integer brandId;
/**
* 主品牌名称
*/
private String brandName;
/**
* 子品牌id
*/
private Integer childId;
/**
* 子品牌名称
*/
private String childName;
/**
* 归属周期-开始日期(yyyy-MM-dd)
*/
private String periodStartDate;
/**
* 归属周期-结束日期(yyyy-MM-dd)
*/
private String periodEndDate;
/**
* 创建人
*/
private String createdBy;
/**
* 创建时间
*/
private Date createdDate;
/**
* 修改人
*/
private String updatedBy;
/**
* 修改时间
*/
private Date updatedDate;
/**
* 删除标识(D:已删除;N:未删除)
*/
private String deletedFlag;
// Getters and Setters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getMerchantId() {
return merchantId;
}
public void setMerchantId(Integer merchantId) {
this.merchantId = merchantId;
}
public String getMerchantName() {
return merchantName;
}
public void setMerchantName(String merchantName) {
this.merchantName = merchantName;
}
public BigDecimal getTargetValue() {
return targetValue;
}
public void setTargetValue(BigDecimal targetValue) {
this.targetValue = targetValue;
}
public String getTargetMonth() {
return targetMonth;
}
public void setTargetMonth(String targetMonth) {
this.targetMonth = targetMonth;
}
public Integer getBrandId() {
return brandId;
}
public void setBrandId(Integer brandId) {
this.brandId = brandId;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public Integer getChildId() {
return childId;
}
public void setChildId(Integer childId) {
this.childId = childId;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
public String getPeriodStartDate() {
return periodStartDate;
}
public void setPeriodStartDate(String periodStartDate) {
this.periodStartDate = periodStartDate;
}
public String getPeriodEndDate() {
return periodEndDate;
}
public void setPeriodEndDate(String periodEndDate) {
this.periodEndDate = periodEndDate;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
public Date getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(Date updatedDate) {
this.updatedDate = updatedDate;
}
public String getDeletedFlag() {
return deletedFlag;
}
public void setDeletedFlag(String deletedFlag) {
this.deletedFlag = deletedFlag;
}
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.peanalysis.mapper;
import cn.com.glsx.dj.peanalysis.dto.MerchantDeliverTargetConfigDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 门店交付数目标配置Mapper
*/
@Mapper
public interface MerchantDeliverTargetConfigMapper {
/**
* 查询门店交付数目标配置
*
* @param merchantId 商户号
* @param targetMonth 目标月份(yyyy-MM)
* @return 配置列表
*/
List<MerchantDeliverTargetConfigDTO> selectByMerchantAndMonth(@Param("merchantId") Integer merchantId,
@Param("targetMonth") String targetMonth);
/**
* 查询门店最大归属周期
*
* @param merchantId 商户号
* @param targetMonth 目标月份(yyyy-MM)
* @return 最大周期配置
*/
MerchantDeliverTargetConfigDTO selectMaxPeriodByMerchantAndMonth(@Param("merchantId") Integer merchantId,
@Param("targetMonth") String targetMonth);
/**
* 查询门店的所有交付目标配置(按周期结束日期降序)
*
* @param merchantId 商户号
* @return 配置列表
*/
List<MerchantDeliverTargetConfigDTO> selectByMerchantId(@Param("merchantId") Integer merchantId);
/**
* 查询门店在指定月份的目标交付数总和
*
* @param merchantId 商户号
* @param targetMonth 目标月份(yyyy-MM)
* @return 目标交付数
*/
java.math.BigDecimal selectTargetValueSumByMerchantAndMonth(@Param("merchantId") Integer merchantId,
@Param("targetMonth") String targetMonth);
}
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.dj.peanalysis.mapper.MerchantDeliverTargetConfigMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.dj.peanalysis.dto.MerchantDeliverTargetConfigDTO">
<id column="id" property="id" />
<result column="merchant_id" property="merchantId" />
<result column="merchant_name" property="merchantName" />
<result column="target_value" property="targetValue" />
<result column="target_month" property="targetMonth" />
<result column="brand_id" property="brandId" />
<result column="brand_name" property="brandName" />
<result column="child_id" property="childId" />
<result column="child_name" property="childName" />
<result column="period_start_date" property="periodStartDate" />
<result column="period_end_date" property="periodEndDate" />
<result column="created_by" property="createdBy" />
<result column="created_date" property="createdDate" />
<result column="updated_by" property="updatedBy" />
<result column="updated_date" property="updatedDate" />
<result column="deleted_flag" property="deletedFlag" />
</resultMap>
<select id="selectByMerchantAndMonth" resultMap="BaseResultMap">
SELECT
id,
merchant_id,
merchant_name,
target_value,
target_month,
brand_id,
brand_name,
child_id,
child_name,
period_start_date,
period_end_date,
created_by,
created_date,
updated_by,
updated_date,
deleted_flag
FROM dj_car_life_merchant_deliver_target_config
WHERE merchant_id = #{merchantId}
AND target_month = #{targetMonth}
AND deleted_flag = 'N'
ORDER BY period_end_date DESC
</select>
<select id="selectMaxPeriodByMerchantAndMonth" resultMap="BaseResultMap">
SELECT
id,
merchant_id,
merchant_name,
target_value,
target_month,
brand_id,
brand_name,
child_id,
child_name,
period_start_date,
period_end_date,
created_by,
created_date,
updated_by,
updated_date,
deleted_flag
FROM dj_car_life_merchant_deliver_target_config
WHERE merchant_id = #{merchantId}
AND target_month = #{targetMonth}
AND deleted_flag = 'N'
ORDER BY period_end_date DESC
LIMIT 1
</select>
<select id="selectByMerchantId" resultMap="BaseResultMap">
SELECT
id,
merchant_id,
merchant_name,
target_value,
target_month,
brand_id,
brand_name,
child_id,
child_name,
period_start_date,
period_end_date,
created_by,
created_date,
updated_by,
updated_date,
deleted_flag
FROM dj_car_life_merchant_deliver_target_config
WHERE merchant_id = #{merchantId}
AND deleted_flag = 'N'
ORDER BY period_end_date DESC
</select>
<select id="selectTargetValueSumByMerchantAndMonth" resultType="java.math.BigDecimal">
SELECT COALESCE(SUM(target_value), 0)
FROM dj_car_life_merchant_deliver_target_config
WHERE merchant_id = #{merchantId}
AND target_month = #{targetMonth}
AND deleted_flag = 'N'
</select>
</mapper>
\ No newline at end of file \ No newline at end of file
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. MyBatis XML 存在大量重复的列名定义(最佳实践与可维护性)

    • 问题描述:在 MerchantDeliverTargetConfigMapper.xml 中,selectByMerchantAndMonth、selectMaxPeriodByMerchantAndMonth 和 selectByMerchantId 三个查询语句完全重复编写了所有返回字段的列名。
    • 影响分析:违反了 DRY (Don't Repeat Yourself) 原则。未来如果数据库表增加或删除字段,需要在多处修改 SQL,极易出现遗漏,导致线上隐患。
    • 优化建议:使用 MyBatis 的 <sql> 标签提取公共的查询字段,并通过 <include> 引入。
    • 代码示例: ```xml <!-- 定义公共字段 --> id, merchant_id, merchant_name, target_value, target_month, brand_id, brand_name, child_id, child_name, period_start_date, period_end_date, created_by, created_date, updated_by, updated_date, deleted_flag

    <!-- 在查询中使用 --> SELECT FROM dj_car_life_merchant_deliver_target_config WHERE merchant_id = #{merchantId} AND target_month = #{targetMonth} AND deleted_flag = 'N' ORDER BY period_end_date DESC

    
    **2. 日期与月份字段采用 String 类型(功能正确性与健壮性)**
    - **问题描述**:`MerchantDeliverTargetConfigDTO` 中的 `targetMonth`、`periodStartDate`、`periodEndDate` 均被定义为 `String` 类型。
    - **影响分析**:使用字符串表示日期会丧失强类型校验,极易引发格式不一致的问题(例如 `"2023-01"` 与 `"2023-1"`),并且在后续的业务逻辑中如果需要进行日期比较、加减计算时会非常繁琐且容易出错。
    - **优化建议**:建议将 `periodStartDate` 和 `periodEndDate` 改为 `java.time.LocalDate` 或 `java.util.Date` 类型;`targetMonth` 若框架支持可考虑 `java.time.YearMonth`,或者在 DTO 层面及入参处增加严格的正则格式校验(如 `@Pattern(regexp = "^\\d{4}-\\d{2}$")`)。
    
    **3. 潜在的查询性能瓶颈(性能与资源利用)**
    - **问题描述**:Mapper 接口中高频使用了 `merchant_id` 和 `target_month` 作为联合查询条件,并且涉及 `period_end_date` 的排序操作。
    - **影响分析**:由于代码审查无法看到数据库 DDL,若表 `dj_car_life_merchant_deliver_target_config` 数据量较大,且未建立合适的索引,会导致全表扫描和文件排序(filesort),严重影响接口性能。
    - **优化建议**:请确保在数据库层面已经建立了复合索引,建议的索引结构为:`idx_merchant_month_flag (merchant_id, target_month, deleted_flag)`。如果排序操作也是高频刚需,可考虑将 `period_end_date` 加入索引以避免 filesort。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(38/40分)**:逻辑基本正确,特别是 `selectTargetValueSum` 中使用了 `COALESCE` 处理 NULL 值避免了拆箱引发的 NPE,表现优秀。未发现 MyBatis 子查询,符合规范。扣除2分是因为日期字段使用 String 类型可能带来潜在的格式化健壮性问题。
    - **安全性与潜在风险(30/30分)**:Mapper XML 均正确使用了 `#{}` 参数占位符,不存在 SQL 注入风险。未使用 Dubbo Remote 接口,无参数变更风险。
    - **最佳实践与可维护性(15/20分)**:代码结构清晰,注释完备。扣除5分主要因为 XML 中存在大段重复的 SQL 列名,且 DTO 实体类未使用 Lombok(如 `@Data`)导致存在大量冗余的 Getter/Setter 代码。
    - **性能与资源利用(4/5分)**:SQL 语句编写规范,求最大周期使用了 `LIMIT 1` 避免了数据全量加载。扣除1分作为防御性提醒,需确保相关高频查询字段已建立合理的复合索引。
    - **提交信息质量(5/5分)**:Commit Message 采用了标准规范(feat),清晰地列出了本次提交包含的 7 项具体改动内容,可追溯性极强。
    
    总分:92分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!