refactor(mybatis): 移除 XML 配置,使用注解替代

- 在 PriceVoucherBatchConfigMapper、PriceVoucherCodeMapper 和 VoucherPrintRecordMapper 中添加了 @Select 和 @Update 注解
- 删除了对应的 XML 配置文件
- 优化了 SQL 查询,直接在 Java 接口中定义
This commit is contained in:
2025-09-17 17:03:12 +08:00
parent ad111cdebb
commit f3fdb44742
6 changed files with 71 additions and 263 deletions

View File

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.pricing.entity.PriceVoucherBatchConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
@@ -19,7 +21,12 @@ public interface PriceVoucherBatchConfigMapper extends BaseMapper<PriceVoucherBa
* @param brokerId 推客ID
* @return 批次列表
*/
List<PriceVoucherBatchConfig> selectActiveBatchesByScenicAndBroker(@Param("scenicId") Long scenicId,
@Select("SELECT id, batch_name, scenic_id, broker_id, discount_type, discount_value, applicable_products, " +
"total_count, used_count, claimed_count, status, create_time, update_time, " +
"create_by, update_by, deleted, deleted_at " +
"FROM price_voucher_batch_config WHERE scenic_id = #{scenicId} AND broker_id = #{brokerId} " +
"AND status = 1 AND deleted = 0 ORDER BY create_time DESC")
List<PriceVoucherBatchConfig> selectActiveBatchesByScenicAndBroker(@Param("scenicId") Long scenicId,
@Param("brokerId") Long brokerId);
/**
@@ -28,6 +35,8 @@ public interface PriceVoucherBatchConfigMapper extends BaseMapper<PriceVoucherBa
* @param increment 增量(可为负数)
* @return 影响行数
*/
@Update("UPDATE price_voucher_batch_config SET claimed_count = claimed_count + #{increment}, " +
"update_time = NOW() WHERE id = #{batchId} AND deleted = 0")
int updateClaimedCount(@Param("batchId") Long batchId, @Param("increment") Integer increment);
/**
@@ -36,6 +45,8 @@ public interface PriceVoucherBatchConfigMapper extends BaseMapper<PriceVoucherBa
* @param increment 增量(可为负数)
* @return 影响行数
*/
@Update("UPDATE price_voucher_batch_config SET used_count = used_count + #{increment}, " +
"update_time = NOW() WHERE id = #{batchId} AND deleted = 0")
int updateUsedCount(@Param("batchId") Long batchId, @Param("increment") Integer increment);
/**
@@ -43,6 +54,10 @@ public interface PriceVoucherBatchConfigMapper extends BaseMapper<PriceVoucherBa
* @param batchId 批次ID
* @return 统计信息
*/
@Select("SELECT id, batch_name, scenic_id, broker_id, discount_type, discount_value, applicable_products, " +
"total_count, used_count, claimed_count, status, create_time, update_time, " +
"create_by, update_by, deleted, deleted_at " +
"FROM price_voucher_batch_config WHERE id = #{batchId} AND deleted = 0")
PriceVoucherBatchConfig selectBatchStats(@Param("batchId") Long batchId);
/**
@@ -50,5 +65,10 @@ public interface PriceVoucherBatchConfigMapper extends BaseMapper<PriceVoucherBa
* @param voucherCode 券码
* @return 券码批次配置
*/
@Select("SELECT b.id, b.batch_name, b.scenic_id, b.broker_id, b.discount_type, b.discount_value, b.applicable_products, " +
"b.total_count, b.used_count, b.claimed_count, b.status, b.create_time, b.update_time, " +
"b.create_by, b.update_by, b.deleted, b.deleted_at " +
"FROM price_voucher_batch_config b INNER JOIN price_voucher_code c ON b.id = c.batch_id " +
"WHERE c.code = #{voucherCode} AND b.deleted = 0 AND c.deleted = 0")
PriceVoucherBatchConfig selectByVoucherCode(@Param("voucherCode") String voucherCode);
}

View File

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.pricing.entity.PriceVoucherCode;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.time.LocalDateTime;
import java.util.List;
@@ -19,6 +21,9 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param code 券码
* @return 券码信息
*/
@Select("SELECT id, batch_id, scenic_id, code, status, face_id, claimed_time, used_time, " +
"current_use_count, last_used_time, remark, create_time, update_time, deleted, deleted_at " +
"FROM price_voucher_code WHERE code = #{code} AND deleted = 0 LIMIT 1")
PriceVoucherCode selectByCode(@Param("code") String code);
/**
@@ -27,6 +32,7 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param scenicId 景区ID
* @return 数量
*/
@Select("SELECT COUNT(1) FROM price_voucher_code WHERE face_id = #{faceId} AND scenic_id = #{scenicId} AND deleted = 0")
Integer countByFaceIdAndScenicId(@Param("faceId") Long faceId, @Param("scenicId") Long scenicId);
/**
@@ -35,7 +41,11 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param scenicId 景区ID
* @return 券码列表
*/
List<PriceVoucherCode> selectAvailableVouchersByFaceIdAndScenicId(@Param("faceId") Long faceId,
@Select("SELECT id, batch_id, scenic_id, code, status, face_id, claimed_time, used_time, " +
"current_use_count, last_used_time, remark, create_time, update_time, deleted, deleted_at " +
"FROM price_voucher_code WHERE face_id = #{faceId} AND scenic_id = #{scenicId} AND status = 1 AND deleted = 0 " +
"ORDER BY claimed_time DESC")
List<PriceVoucherCode> selectAvailableVouchersByFaceIdAndScenicId(@Param("faceId") Long faceId,
@Param("scenicId") Long scenicId);
/**
@@ -44,7 +54,10 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param limit 限制数量
* @return 券码列表
*/
List<PriceVoucherCode> selectUnclaimedVouchersByBatchId(@Param("batchId") Long batchId,
@Select("SELECT id, batch_id, scenic_id, code, status, face_id, claimed_time, used_time, " +
"current_use_count, last_used_time, remark, create_time, update_time, deleted, deleted_at " +
"FROM price_voucher_code WHERE batch_id = #{batchId} AND status = 0 AND deleted = 0 LIMIT #{limit}")
List<PriceVoucherCode> selectUnclaimedVouchersByBatchId(@Param("batchId") Long batchId,
@Param("limit") Integer limit);
/**
@@ -54,8 +67,10 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param claimedTime 领取时间
* @return 影响行数
*/
int claimVoucher(@Param("id") Long id,
@Param("faceId") Long faceId,
@Update("UPDATE price_voucher_code SET status = 1, face_id = #{faceId}, claimed_time = #{claimedTime}, " +
"update_time = NOW() WHERE id = #{id} AND status = 0 AND deleted = 0")
int claimVoucher(@Param("id") Long id,
@Param("faceId") Long faceId,
@Param("claimedTime") LocalDateTime claimedTime);
/**
@@ -63,6 +78,9 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param batchId 批次ID
* @return 券码列表
*/
@Select("SELECT id, batch_id, scenic_id, code, status, face_id, claimed_time, used_time, " +
"current_use_count, last_used_time, remark, create_time, update_time, deleted, deleted_at " +
"FROM price_voucher_code WHERE batch_id = #{batchId} AND deleted = 0 ORDER BY create_time DESC")
List<PriceVoucherCode> selectByBatchId(@Param("batchId") Long batchId);
/**
@@ -71,7 +89,14 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param scenicId 景区ID(可选)
* @return 券码列表
*/
List<PriceVoucherCode> selectUserVouchers(@Param("faceId") Long faceId,
@Select("<script>" +
"SELECT id, batch_id, scenic_id, code, status, face_id, claimed_time, used_time, " +
"current_use_count, last_used_time, remark, create_time, update_time, deleted, deleted_at " +
"FROM price_voucher_code WHERE face_id = #{faceId}" +
"<if test='scenicId != null'> AND scenic_id = #{scenicId}</if>" +
" AND deleted = 0 ORDER BY claimed_time DESC" +
"</script>")
List<PriceVoucherCode> selectUserVouchers(@Param("faceId") Long faceId,
@Param("scenicId") Long scenicId);
/**
@@ -79,6 +104,9 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param batchId 批次ID
* @return 可用券码
*/
@Select("SELECT id, batch_id, scenic_id, code, status, face_id, claimed_time, used_time, " +
"current_use_count, last_used_time, remark, create_time, update_time, deleted, deleted_at " +
"FROM price_voucher_code WHERE batch_id = #{batchId} AND status = 0 AND deleted = 0 LIMIT 1")
PriceVoucherCode findFirstAvailableByBatchId(@Param("batchId") Long batchId);
/**
@@ -86,5 +114,10 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
* @param scenicId 景区ID
* @return 可用券码
*/
@Select("SELECT pvc.id, pvc.batch_id, pvc.scenic_id, pvc.code, pvc.status, pvc.face_id, pvc.claimed_time, pvc.used_time, " +
"pvc.current_use_count, pvc.last_used_time, pvc.remark, pvc.create_time, pvc.update_time, pvc.deleted, pvc.deleted_at " +
"FROM price_voucher_code pvc WHERE pvc.scenic_id = #{scenicId} AND pvc.status = 0 AND pvc.deleted = 0 " +
"AND NOT EXISTS (SELECT 1 FROM voucher_print_record vpr WHERE vpr.voucher_code_id = pvc.id AND vpr.deleted = 0) " +
"ORDER BY RAND() LIMIT 1")
PriceVoucherCode findRandomUnprintedVoucher(@Param("scenicId") Long scenicId);
}

View File

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.pricing.entity.VoucherPrintRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
* 优惠券打印记录Mapper
@@ -17,6 +19,9 @@ public interface VoucherPrintRecordMapper extends BaseMapper<VoucherPrintRecord>
* @param scenicId 景区ID
* @return 打印记录
*/
@Select("SELECT id, code, face_id, broker_id, scenic_id, voucher_code_id, voucher_code, " +
"print_status, error_message, create_time, update_time, deleted, deleted_at " +
"FROM voucher_print_record WHERE face_id = #{faceId} AND scenic_id = #{scenicId} AND deleted = 0 LIMIT 1")
VoucherPrintRecord selectByFaceBrokerScenic(@Param("faceId") Long faceId,
@Param("scenicId") Long scenicId);
@@ -25,6 +30,9 @@ public interface VoucherPrintRecordMapper extends BaseMapper<VoucherPrintRecord>
* @param voucherCodeId 券码ID
* @return 打印记录
*/
@Select("SELECT id, code, face_id, broker_id, scenic_id, voucher_code_id, voucher_code, " +
"print_status, error_message, create_time, update_time, deleted, deleted_at " +
"FROM voucher_print_record WHERE voucher_code_id = #{voucherCodeId} AND deleted = 0 LIMIT 1")
VoucherPrintRecord selectByVoucherCodeId(@Param("voucherCodeId") Long voucherCodeId);
/**
@@ -34,7 +42,9 @@ public interface VoucherPrintRecordMapper extends BaseMapper<VoucherPrintRecord>
* @param errorMessage 错误信息(可为null)
* @return 影响行数
*/
int updatePrintStatus(@Param("id") Long id,
@Param("printStatus") Integer printStatus,
@Update("UPDATE voucher_print_record SET print_status = #{printStatus}, error_message = #{errorMessage}, " +
"update_time = NOW() WHERE id = #{id}")
int updatePrintStatus(@Param("id") Long id,
@Param("printStatus") Integer printStatus,
@Param("errorMessage") String errorMessage);
}