Authored by tanling

优惠券优化

@@ -17,6 +17,7 @@ import com.yoho.ufo.exception.PlatformException; @@ -17,6 +17,7 @@ import com.yoho.ufo.exception.PlatformException;
17 import com.yoho.ufo.model.coupon.Coupon; 17 import com.yoho.ufo.model.coupon.Coupon;
18 import com.yoho.ufo.model.coupon.CouponProductLimit; 18 import com.yoho.ufo.model.coupon.CouponProductLimit;
19 import com.yoho.ufo.model.coupon.UserCoupon; 19 import com.yoho.ufo.model.coupon.UserCoupon;
  20 +import com.yoho.ufo.model.coupon.UserCouponNum;
20 import com.yoho.ufo.model.coupon.resp.CouponQueryResp; 21 import com.yoho.ufo.model.coupon.resp.CouponQueryResp;
21 import com.yoho.ufo.model.coupon.resp.UidCouponQueryResp; 22 import com.yoho.ufo.model.coupon.resp.UidCouponQueryResp;
22 import com.yoho.ufo.model.coupon.resp.UserCouponQueryResp; 23 import com.yoho.ufo.model.coupon.resp.UserCouponQueryResp;
@@ -46,6 +47,7 @@ import org.springframework.web.multipart.MultipartFile; @@ -46,6 +47,7 @@ import org.springframework.web.multipart.MultipartFile;
46 import javax.annotation.Resource; 47 import javax.annotation.Resource;
47 import javax.servlet.http.HttpServletRequest; 48 import javax.servlet.http.HttpServletRequest;
48 import java.util.*; 49 import java.util.*;
  50 +import java.util.function.Function;
49 import java.util.stream.Collectors; 51 import java.util.stream.Collectors;
50 52
51 /** 53 /**
@@ -137,45 +139,40 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ @@ -137,45 +139,40 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{
137 } 139 }
138 List<Coupon> coupons = couponMapper.selectByCondition(req); 140 List<Coupon> coupons = couponMapper.selectByCondition(req);
139 141
140 - // 获取 该券的用户领取|使用记录  
141 - Map<Integer, List<UserCoupon>> userCouponMap = null; 142 + // 只获取使用记录
  143 + Map<Integer, UserCouponNum> sendNumMap = null;
  144 + Map<Integer, UserCouponNum> useNumMap = null;
142 if (CollectionUtils.isNotEmpty(coupons)){ 145 if (CollectionUtils.isNotEmpty(coupons)){
143 List<Integer> couponIdList = coupons.stream().map(Coupon::getId).collect(Collectors.toList()); 146 List<Integer> couponIdList = coupons.stream().map(Coupon::getId).collect(Collectors.toList());
144 - userCouponMap = getUserCouponMap(couponIdList); 147 + List<UserCouponNum> sendNumList = userCouponMapper.selectByCouponIds(couponIdList);
  148 + sendNumMap = sendNumList.stream().collect(Collectors.toMap(UserCouponNum::getCouponId, Function.identity(), (k1,k2)->k2));
  149 + List<UserCouponNum> useNumList = userCouponMapper.selectByCouponIdsAndStatus(couponIdList);
  150 + useNumMap = useNumList.stream().collect(Collectors.toMap(UserCouponNum::getCouponId, Function.identity(), (k1,k2)->k2));
145 } 151 }
146 152
147 List<CouponQueryResp> couponQueryResps = initCouponQueryResps(coupons,req.getCurTime()); 153 List<CouponQueryResp> couponQueryResps = initCouponQueryResps(coupons,req.getCurTime());
148 154
149 - // 设置 已经领取数量|已经使用记录  
150 - if (MapUtils.isNotEmpty(userCouponMap)){  
151 - Map<Integer, List<UserCoupon>> finalUserCouponMap = userCouponMap; 155 + if (MapUtils.isEmpty(sendNumMap) && MapUtils.isEmpty(useNumMap)) {
  156 + Map<Integer, UserCouponNum> finalSendNumMap = sendNumMap;
  157 + Map<Integer, UserCouponNum> finalUseNumMap = useNumMap;
  158 + couponQueryResps = couponQueryResps.stream().map(x->{
152 159
153 - couponQueryResps = couponQueryResps.stream().map(x-> {  
154 - List<UserCoupon> userCouponList = finalUserCouponMap.get(x.getId());  
155 - if (CollectionUtils.isEmpty(userCouponList)) {  
156 - return x; 160 + if (MapUtils.isNotEmpty(finalSendNumMap)){
  161 + x.setSendNum(finalSendNumMap.get(x.getId()) == null ? 0 :finalSendNumMap.get(x.getId()).getCnt());
  162 + }
  163 +
  164 + if (MapUtils.isNotEmpty(finalUseNumMap)){
  165 + x.setUseNum(finalUseNumMap.get(x.getId()) == null ? 0 : finalUseNumMap.get(x.getId()).getCnt());
157 } 166 }
158 - x.setSendNum(userCouponList.size());  
159 - x.setUseNum(userCouponList.stream().filter(y->y.getStatus()== UserCouponsStatusEnum.USED.getCode()).count()); 167 +
160 return x; 168 return x;
161 }).collect(Collectors.toList()); 169 }).collect(Collectors.toList());
162 } 170 }
163 171
164 -  
165 -  
166 jsonObject.put("coupons",couponQueryResps); 172 jsonObject.put("coupons",couponQueryResps);
167 return new ApiResponse.ApiResponseBuilder().data(jsonObject).build(); 173 return new ApiResponse.ApiResponseBuilder().data(jsonObject).build();
168 } 174 }
169 175
170 - private Map<Integer, List<UserCoupon>> getUserCouponMap(List<Integer> couponIdList) {  
171 - List<UserCoupon> userCouponList = userCouponMapper.selectByCouponIds(couponIdList);  
172 - if (CollectionUtils.isEmpty(userCouponList)){  
173 - return null;  
174 - }  
175 - Map<Integer, List<UserCoupon>> userCouponMap = userCouponList.stream().collect(Collectors.groupingBy(UserCoupon::getCouponId));  
176 - return userCouponMap;  
177 - }  
178 -  
179 @Override 176 @Override
180 public ApiResponse saveOrUpdateCoupon(CouponSaveUpdateReq req) { 177 public ApiResponse saveOrUpdateCoupon(CouponSaveUpdateReq req) {
181 LOGGER.info("enter saveOrUpdateCoupon,param is {}",req); 178 LOGGER.info("enter saveOrUpdateCoupon,param is {}",req);
@@ -522,6 +519,7 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ @@ -522,6 +519,7 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{
522 com.yoho.ufo.util.DateUtil.int2DateStr(coupon.getEndTime(),"yyyy-MM-dd HH:mm:ss"); 519 com.yoho.ufo.util.DateUtil.int2DateStr(coupon.getEndTime(),"yyyy-MM-dd HH:mm:ss");
523 CouponQueryResp resp = new CouponQueryResp(coupon.getId(),coupon.getCouponName(),coupon.getCouponNum(), 520 CouponQueryResp resp = new CouponQueryResp(coupon.getId(),coupon.getCouponName(),coupon.getCouponNum(),
524 useTime,coupon.getRemark(), getStatusDesc(coupon,curTime),coupon.getStatus(),coupon.getCouponToken()); 521 useTime,coupon.getRemark(), getStatusDesc(coupon,curTime),coupon.getStatus(),coupon.getCouponToken());
  522 + resp.setSendNum(coupon.getSendNum());
525 resps.add(resp); 523 resps.add(resp);
526 } 524 }
527 return resps; 525 return resps;
1 package com.yoho.ufo.dal; 1 package com.yoho.ufo.dal;
2 2
3 import com.yoho.ufo.model.coupon.UserCoupon; 3 import com.yoho.ufo.model.coupon.UserCoupon;
  4 +import com.yoho.ufo.model.coupon.UserCouponNum;
4 import com.yohobuy.ufo.coupon.req.UserCouponQueryReq; 5 import com.yohobuy.ufo.coupon.req.UserCouponQueryReq;
5 import org.apache.ibatis.annotations.Param; 6 import org.apache.ibatis.annotations.Param;
6 7
@@ -13,7 +14,10 @@ public interface UserCouponMapper { @@ -13,7 +14,10 @@ public interface UserCouponMapper {
13 List<UserCoupon> selectByCondition(@Param("param") UserCouponQueryReq param); 14 List<UserCoupon> selectByCondition(@Param("param") UserCouponQueryReq param);
14 15
15 16
16 - List<UserCoupon> selectByCouponIds(@Param("couponIds") List<Integer> couponIds); 17 + List<UserCouponNum> selectByCouponIds(@Param("couponIds") List<Integer> couponIds);
  18 +
  19 +
  20 + List<UserCouponNum> selectByCouponIdsAndStatus(@Param("couponIds") List<Integer> couponIds);
17 21
18 int selectTotalByCondition(@Param("param") UserCouponQueryReq param); 22 int selectTotalByCondition(@Param("param") UserCouponQueryReq param);
19 } 23 }
1 package com.yoho.ufo.model.coupon; 1 package com.yoho.ufo.model.coupon;
2 2
  3 +import lombok.Data;
  4 +
3 import java.io.Serializable; 5 import java.io.Serializable;
4 6
5 /** 7 /**
6 * Created by shengguo.cai on 2018/11/20. 8 * Created by shengguo.cai on 2018/11/20.
7 */ 9 */
  10 +@Data
8 public class Coupon implements Serializable { 11 public class Coupon implements Serializable {
9 12
10 private Integer id; 13 private Integer id;
@@ -25,6 +28,10 @@ public class Coupon implements Serializable { @@ -25,6 +28,10 @@ public class Coupon implements Serializable {
25 private Integer createTime; 28 private Integer createTime;
26 private Integer pid; 29 private Integer pid;
27 private String remark; 30 private String remark;
  31 + /**
  32 + * 禁止商品类型
  33 + */
  34 + private String skupForbidType;
28 /**商品限制条件-特定商品*/ 35 /**商品限制条件-特定商品*/
29 public static final int PRODUCTLIMITTYPE_SPECIALPRODUCT = 1; 36 public static final int PRODUCTLIMITTYPE_SPECIALPRODUCT = 1;
30 /**商品限制条件-无限制*/ 37 /**商品限制条件-无限制*/
@@ -70,147 +77,5 @@ public class Coupon implements Serializable { @@ -70,147 +77,5 @@ public class Coupon implements Serializable {
70 } 77 }
71 } 78 }
72 79
73 - public Integer getId() {  
74 - return id;  
75 - }  
76 -  
77 - public void setId(Integer id) {  
78 - this.id = id;  
79 - }  
80 -  
81 - public String getCouponToken() {  
82 - return couponToken;  
83 - }  
84 -  
85 - public void setCouponToken(String couponToken) {  
86 - this.couponToken = couponToken;  
87 - }  
88 80
89 - public String getCouponName() {  
90 - return couponName;  
91 - }  
92 -  
93 - public void setCouponName(String couponName) {  
94 - this.couponName = couponName;  
95 - }  
96 -  
97 - public Float getCouponAmount() {  
98 - return couponAmount;  
99 - }  
100 -  
101 - public void setCouponAmount(Float couponAmount) {  
102 - this.couponAmount = couponAmount;  
103 - }  
104 -  
105 - public Integer getCouponType() {  
106 - return couponType;  
107 - }  
108 -  
109 - public void setCouponType(Integer couponType) {  
110 - this.couponType = couponType;  
111 - }  
112 -  
113 - public Integer getCouponNum() {  
114 - return couponNum;  
115 - }  
116 -  
117 - public void setCouponNum(Integer couponNum) {  
118 - this.couponNum = couponNum;  
119 - }  
120 -  
121 - public Integer getUseNum() {  
122 - return useNum;  
123 - }  
124 -  
125 - public void setUseNum(Integer useNum) {  
126 - this.useNum = useNum;  
127 - }  
128 -  
129 - public Integer getSendNum() {  
130 - return sendNum;  
131 - }  
132 -  
133 - public void setSendNum(Integer sendNum) {  
134 - this.sendNum = sendNum;  
135 - }  
136 -  
137 - public Integer getUseLimitType() {  
138 - return useLimitType;  
139 - }  
140 -  
141 - public void setUseLimitType(Integer useLimitType) {  
142 - this.useLimitType = useLimitType;  
143 - }  
144 -  
145 - public Integer getUseLimitValue() {  
146 - return useLimitValue;  
147 - }  
148 -  
149 - public void setUseLimitValue(Integer useLimitValue) {  
150 - this.useLimitValue = useLimitValue;  
151 - }  
152 -  
153 - public Integer getProductLimitType() {  
154 - return productLimitType;  
155 - }  
156 -  
157 - public void setProductLimitType(Integer productLimitType) {  
158 - this.productLimitType = productLimitType;  
159 - }  
160 -  
161 - public String getProductLimitValue() {  
162 - return productLimitValue;  
163 - }  
164 -  
165 - public void setProductLimitValue(String productLimitValue) {  
166 - this.productLimitValue = productLimitValue;  
167 - }  
168 -  
169 - public Integer getStartTime() {  
170 - return startTime;  
171 - }  
172 -  
173 - public void setStartTime(Integer startTime) {  
174 - this.startTime = startTime;  
175 - }  
176 -  
177 - public Integer getEndTime() {  
178 - return endTime;  
179 - }  
180 -  
181 - public void setEndTime(Integer endTime) {  
182 - this.endTime = endTime;  
183 - }  
184 -  
185 - public Integer getStatus() {  
186 - return status;  
187 - }  
188 -  
189 - public void setStatus(Integer status) {  
190 - this.status = status;  
191 - }  
192 -  
193 - public Integer getCreateTime() {  
194 - return createTime;  
195 - }  
196 -  
197 - public void setCreateTime(Integer createTime) {  
198 - this.createTime = createTime;  
199 - }  
200 -  
201 - public Integer getPid() {  
202 - return pid;  
203 - }  
204 -  
205 - public void setPid(Integer pid) {  
206 - this.pid = pid;  
207 - }  
208 -  
209 - public String getRemark() {  
210 - return remark;  
211 - }  
212 -  
213 - public void setRemark(String remark) {  
214 - this.remark = remark;  
215 - }  
216 } 81 }
  1 +package com.yoho.ufo.model.coupon;
  2 +
  3 +import lombok.Data;
  4 +
  5 +@Data
  6 +public class UserCouponNum {
  7 +
  8 + int cnt;
  9 + int couponId;
  10 +}
@@ -20,12 +20,13 @@ @@ -20,12 +20,13 @@
20 <result column="create_time" property="createTime" jdbcType="INTEGER" /> 20 <result column="create_time" property="createTime" jdbcType="INTEGER" />
21 <result column="pid" property="pid" jdbcType="INTEGER" /> 21 <result column="pid" property="pid" jdbcType="INTEGER" />
22 <result column="remark" property="remark" jdbcType="VARCHAR" /> 22 <result column="remark" property="remark" jdbcType="VARCHAR" />
  23 + <result column="skup_forbid_type" property="skupForbidType" jdbcType="VARCHAR" />
23 </resultMap> 24 </resultMap>
24 25
25 <sql id="Base_Column_List"> 26 <sql id="Base_Column_List">
26 id,coupon_token,coupon_name,coupon_amount,coupon_type,coupon_num,use_num,send_num, 27 id,coupon_token,coupon_name,coupon_amount,coupon_type,coupon_num,use_num,send_num,
27 use_limit_type,use_limit_value,product_limit_type,product_limit_value,start_time, 28 use_limit_type,use_limit_value,product_limit_type,product_limit_value,start_time,
28 - end_time,status,create_time,pid,remark 29 + end_time,status,create_time,pid,remark,skup_forbid_type
29 </sql> 30 </sql>
30 <insert id="insertOrUpdate" useGeneratedKeys="true" keyProperty="param.id"> 31 <insert id="insertOrUpdate" useGeneratedKeys="true" keyProperty="param.id">
31 insert into coupon(id,coupon_token,coupon_name,coupon_amount,coupon_type,coupon_num,use_num,send_num, 32 insert into coupon(id,coupon_token,coupon_name,coupon_amount,coupon_type,coupon_num,use_num,send_num,
@@ -21,15 +21,30 @@ @@ -21,15 +21,30 @@
21 </sql> 21 </sql>
22 22
23 23
24 - <select id="selectByCouponIds" resultMap="BaseResultMap"> 24 + <select id="selectByCouponIds" resultType="com.yoho.ufo.model.coupon.UserCouponNum">
25 select 25 select
26 - <include refid="Base_Column_List"/> 26 + count(uid) cnt, coupon_id
27 from user_coupon 27 from user_coupon
28 where 28 where
29 coupon_id IN 29 coupon_id IN
30 <foreach collection="couponIds" item="couponId" open="(" separator="," close=")"> 30 <foreach collection="couponIds" item="couponId" open="(" separator="," close=")">
31 #{couponId,jdbcType=INTEGER} 31 #{couponId,jdbcType=INTEGER}
32 </foreach> 32 </foreach>
  33 + group by coupon_id
  34 + </select>
  35 +
  36 +
  37 + <select id="selectByCouponIdsAndStatus" resultType="com.yoho.ufo.model.coupon.UserCouponNum">
  38 + select
  39 + count(uid) cnt, coupon_id
  40 + from user_coupon
  41 + where
  42 + status=1 and
  43 + coupon_id IN
  44 + <foreach collection="couponIds" item="couponId" open="(" separator="," close=")">
  45 + #{couponId,jdbcType=INTEGER}
  46 + </foreach>
  47 + group by coupon_id
33 </select> 48 </select>
34 49
35 50