LockCouponRecordRequest
@ApiModel(value = "优惠券锁定对象",description = "优惠券锁定对象")
@Data
public class LockCouponRecordRequest {
/**
* 优惠券记录id列表
*/
@ApiModelProperty(value = "优惠券记录id列表",example = "[1,2,3]")
private List<Long> lockCouponRecordIds;
/**
* 订单号
*/
@ApiModelProperty(value = "订单号",example = "3234fw234rfd232")
private String orderOutTradeNo;
}
Controller
@ApiOperation("rpc-锁定,优惠券记录")
@PostMapping("lock_records")
public JsonData lockCouponRecords(@ApiParam("锁定优惠券请求对象") @RequestBody LockCouponRecordRequest recordRequest){
JsonData jsonData = couponRecordService.lockCouponRecords(recordRequest);
return jsonData;
} Service
/**
* 锁定优惠券
* @param recordRequest
* @return
*/
JsonData lockCouponRecords(LockCouponRecordRequest recordRequest); CouponRecordServiceImpl
/**
* 锁定优惠券
*
* 1)锁定优惠券记录
* 2)task表插入记录
* 3)发送延迟消息
*
* @param recordRequest
* @return
*/
@Override
public JsonData lockCouponRecords(LockCouponRecordRequest recordRequest) {
LoginUser loginUser = LoginInterceptor.threadLocal.get();
//订单号
String orderOutTradeNo = recordRequest.getOrderOutTradeNo();
//锁定的优惠券ID 可能不止一张
List<Long> lockCouponRecordIds = recordRequest.getLockCouponRecordIds();
//锁定优惠券 (用户id,更新成什么,更新的id)
int updateRows = couponRecordMapper.lockUseStateBatch(loginUser.getId(), CouponStateEnum.USED.name(),lockCouponRecordIds);
List<CouponTaskDO> couponTaskDOList = lockCouponRecordIds.stream().map(obj->{
CouponTaskDO couponTaskDO = new CouponTaskDO();
couponTaskDO.setCreateTime(new Date());
couponTaskDO.setOutTradeNo(orderOutTradeNo);
couponTaskDO.setCouponRecordId(obj);
couponTaskDO.setLockState(StockTaskStateEnum.LOCK.name());
return couponTaskDO;
}).collect(Collectors.toList());
int insertRows = couponTaskMapper.insertBatch(couponTaskDOList);
log.info("优惠券记录锁定updateRows={}",updateRows);
log.info("新增优惠券记录task insertRows={}",insertRows);
if(lockCouponRecordIds.size() == insertRows && insertRows==updateRows){
//发送延迟消息 TODO
return JsonData.buildSuccess();
}else {
throw new BizException(BizCodeEnum.COUPON_RECORD_LOCK_FAIL);
}
} 需要用到的类方法
/**
* 批量插入
* @param couponTaskDOList
* @return
*/
int insertBatch(@Param("couponTaskList") List<CouponTaskDO> couponTaskDOList);
/**
* 批量更新优惠券使用记录
* @param userId
* @param useState
* @param lockCouponRecordIds
* @return
*/
int lockUseStateBatch(@Param("userId") Long userId, @Param("useState") String useState, @Param("lockCouponRecordIds") List<Long> lockCouponRecordIds);
两个自定义xml
CouponTaskMapper
<!-- 通用查询结果列 -->
<sql id="Base_Column_List_No_Id">
coupon_record_id, create_time, out_trade_no, lock_state
</sql>
<!--批量插入-->
<insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
insert into coupon_task
( <include refid="Base_Column_List_No_Id"/> )
values
<foreach collection="couponTaskList" item="item" index="index" separator=",">
(
#{item.couponRecordId},
#{item.createTime},
#{item.outTradeNo},
#{item.lockState}
)
</foreach>
</insert> CouponRecordMapper
<!--批量锁定优惠券记录-->
<update id="lockUseStateBatch">
update coupon_record set use_state=#{useState} where user_id=#{userId} and use_state='NEW'
and id in
<!--open 拼接最开始 close 拼接结束 目的是构件成这样 (1,2,3)-->
<foreach collection="lockCouponRecordIds" index="index" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</update> 新枚举类StockTaskStateEnum
public enum StockTaskStateEnum {
/**
* 锁定
*/
LOCK,
/**
* 完成
*/
FINISH,
/**
* 取消,释放库存
*/
CANCEL;
}
本文作者为DBC,转载请注明。