優惠券介面的總結
阿新 • • 發佈:2018-11-10
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>