1.接口实现的目标:
看到这个接口首先a.我们需要给前端,这三种不同情况下优惠券的数量—所以需要一个方法查询这个memberId优惠券
b.我们需要各自状态下的优惠券信息的方法
c.未使用:即状态为可用,使用结束时间比当前时间大(sql语句做判断)
已使用:状态为不可用,使用结束时间比当前时间大
已过期:状态不可用,使用结束时间比当前时间小
现在我们开始实现他
2.控制层
/**
* 获取用户当前可用,已用,过期的已绑定的优惠券
* @return
*/
@ApiOperation(value = "优惠券各种状态接口", notes = "优惠券状态", produces = "application/json")
@PostMapping("/couponList")
public @ResponseBody BaseResponse<List<CouponUser>> getSellerCoupon(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
HttpServletRequest request,
@RequestBody CouponMemberRequest couponMemberRequest) {
//这段是获取用户的信息
String memberName = couponMemberRequest.getUserName();
Integer state = couponMemberRequest.getState();//前端传来的状态1是可用,2是已使用,3是已过期
ServiceResult<List<Member>> memberResult = memberService.getMemberByName(memberName);
List<Member> members = memberResult.getResult();
if (members.size() == 0) {
return new BaseResponse<>(false, null, "用户名或密码错误!");
} else if (members.size() > 1) {
return new BaseResponse<>(false, null, "用户名重复,请联系系统管理员!!");
}
Member member = members.get(0);
Integer sellerId = couponMemberRequest.getSellerId();
BaseResponse<List<CouponUser>> baseResponse = new BaseResponse<List<CouponUser>>();
//如果状态为1表示可用优惠券,那么获取状态符合的优惠券信息
if (state != null && state == 1) {
ServiceResult<List<CouponUser>> serviceResult = couponService
.getEffectiveByMemberIdAndSellerId(member.getId(), sellerId);
if (!serviceResult.getSuccess()) {
baseResponse = new BaseResponse<List<CouponUser>>(false, null,
serviceResult.getMessage());
}
baseResponse.setData(serviceResult.getResult());
baseResponse.setSuccess(true);
}
//如果状态等于二表示已使用优惠券,支付接口记得把使用优惠券的状态改了
if (state != null && state == 2) {
ServiceResult<List<CouponUser>> serviceResult = couponService
.getDisEffectiveByMemberIdAndSellerId(member.getId(), sellerId);
if (!serviceResult.getSuccess()) {
baseResponse = new BaseResponse<List<CouponUser>>(false, null,
serviceResult.getMessage());
}
baseResponse.setData(serviceResult.getResult());
baseResponse.setSuccess(true);
}
//如果状态等于3表示已过期优惠券,支付接口记得把使用优惠券的状态改了
if (state != null && state == 3) {
ServiceResult<List<CouponUser>> serviceResult = couponService
.getOutEffectiveByMemberIdAndSellerId(member.getId(), sellerId);
if (!serviceResult.getSuccess()) {
baseResponse = new BaseResponse<List<CouponUser>>(false, null,
serviceResult.getMessage());
}
baseResponse.setData(serviceResult.getResult());
baseResponse.setSuccess(true);
}
return baseResponse;
}
2.服务层的接口
public List<CouponUser> getCouponUsers(Map<String, String> queryMap, Integer start,
Integer size) {
List<CouponUser> couponUsers = couponUserReadDao.getCouponUsers(queryMap, start, size);
this.setExtendAttr(couponUsers);
return couponUsers;
}
//设置拓展属性
private void setExtendAttr(List<CouponUser> couponUsers) {
if (couponUsers != null && couponUsers.size() > 0) {
// 保存取到的商家信息,避免多次读取同一条数据,增加效率(下同理)
Map<Integer, Seller> map = new HashMap<Integer, Seller>();
Map<Integer, Coupon> couponMap = new HashMap<Integer, Coupon>();
Map<Integer, Member> memberMap = new HashMap<Integer, Member>();
for (CouponUser couponUser : couponUsers) {
// 店铺名称
if (map.get(couponUser.getSellerId()) != null) {
couponUser.setSellerName(map.get(couponUser.getSellerId()).getSellerName());
} else {
Seller seller = sellerReadDao.get(couponUser.getSellerId());
if (seller != null) {
map.put(seller.getId(), seller);
couponUser.setSellerName(seller.getSellerName());
}
}
// 用户名称
if (couponUser.getMemberId() > 0) {
if (memberMap.get(couponUser.getMemberId()) != null) {
couponUser.setMemberName(memberMap.get(couponUser.getMemberId()).getName());
} else {
Member member = memberReadDao.get(couponUser.getMemberId());
if (member != null) {
memberMap.put(member.getId(), member);
couponUser.setMemberName(member.getName());
}
}
}
// 优惠券名称
if (couponMap.get(couponUser.getCouponId()) != null) {
couponUser
.setCouponName(couponMap.get(couponUser.getCouponId()).getCouponName());
couponUser
.setCouponValue(couponMap.get(couponUser.getCouponId()).getCouponValue());
couponUser.setMinAmount(couponMap.get(couponUser.getCouponId()).getMinAmount());
couponUser.setChannel(couponMap.get(couponUser.getCouponId()).getChannel());
} else {
Coupon coupon = couponReadDao.get(couponUser.getCouponId());
if (coupon != null) {
couponMap.put(coupon.getId(), coupon);
couponUser.setCouponName(coupon.getCouponName());
couponUser.setCouponValue(coupon.getCouponValue());
couponUser.setMinAmount(coupon.getMinAmount());
couponUser.setChannel(coupon.getChannel());
}
}
// 订单序列号
if (couponUser.getOrderId() > 0) {
Orders orders = ordersReadDao.get(couponUser.getOrderId());
if (orders != null) {
couponUser.setOrderSn(orders.getOrderSn());
}
}
}
}
}
3.mapper
lggt:老铁干他,小于大于
<select id="getEffectiveByMemberIdAndSellerId" resultMap="CouponUserResult">
select
*
from `coupon_user`
where `member_id` = #{memberId}
and `seller_id` = #{sellerId}
and `can_use` > 0
and `use_start_time` < now()
and `use_end_time` > now()
</select>