Authored by tanling

优惠券优化

... ... @@ -17,6 +17,7 @@ import com.yoho.ufo.exception.PlatformException;
import com.yoho.ufo.model.coupon.Coupon;
import com.yoho.ufo.model.coupon.CouponProductLimit;
import com.yoho.ufo.model.coupon.UserCoupon;
import com.yoho.ufo.model.coupon.UserCouponNum;
import com.yoho.ufo.model.coupon.resp.CouponQueryResp;
import com.yoho.ufo.model.coupon.resp.UidCouponQueryResp;
import com.yoho.ufo.model.coupon.resp.UserCouponQueryResp;
... ... @@ -46,6 +47,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
... ... @@ -137,45 +139,40 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{
}
List<Coupon> coupons = couponMapper.selectByCondition(req);
// 获取 该券的用户领取|使用记录
Map<Integer, List<UserCoupon>> userCouponMap = null;
// 只获取使用记录
Map<Integer, UserCouponNum> sendNumMap = null;
Map<Integer, UserCouponNum> useNumMap = null;
if (CollectionUtils.isNotEmpty(coupons)){
List<Integer> couponIdList = coupons.stream().map(Coupon::getId).collect(Collectors.toList());
userCouponMap = getUserCouponMap(couponIdList);
List<UserCouponNum> sendNumList = userCouponMapper.selectByCouponIds(couponIdList);
sendNumMap = sendNumList.stream().collect(Collectors.toMap(UserCouponNum::getCouponId, Function.identity(), (k1,k2)->k2));
List<UserCouponNum> useNumList = userCouponMapper.selectByCouponIdsAndStatus(couponIdList);
useNumMap = useNumList.stream().collect(Collectors.toMap(UserCouponNum::getCouponId, Function.identity(), (k1,k2)->k2));
}
List<CouponQueryResp> couponQueryResps = initCouponQueryResps(coupons,req.getCurTime());
// 设置 已经领取数量|已经使用记录
if (MapUtils.isNotEmpty(userCouponMap)){
Map<Integer, List<UserCoupon>> finalUserCouponMap = userCouponMap;
if (MapUtils.isEmpty(sendNumMap) && MapUtils.isEmpty(useNumMap)) {
Map<Integer, UserCouponNum> finalSendNumMap = sendNumMap;
Map<Integer, UserCouponNum> finalUseNumMap = useNumMap;
couponQueryResps = couponQueryResps.stream().map(x->{
couponQueryResps = couponQueryResps.stream().map(x-> {
List<UserCoupon> userCouponList = finalUserCouponMap.get(x.getId());
if (CollectionUtils.isEmpty(userCouponList)) {
return x;
if (MapUtils.isNotEmpty(finalSendNumMap)){
x.setSendNum(finalSendNumMap.get(x.getId()) == null ? 0 :finalSendNumMap.get(x.getId()).getCnt());
}
if (MapUtils.isNotEmpty(finalUseNumMap)){
x.setUseNum(finalUseNumMap.get(x.getId()) == null ? 0 : finalUseNumMap.get(x.getId()).getCnt());
}
x.setSendNum(userCouponList.size());
x.setUseNum(userCouponList.stream().filter(y->y.getStatus()== UserCouponsStatusEnum.USED.getCode()).count());
return x;
}).collect(Collectors.toList());
}
jsonObject.put("coupons",couponQueryResps);
return new ApiResponse.ApiResponseBuilder().data(jsonObject).build();
}
private Map<Integer, List<UserCoupon>> getUserCouponMap(List<Integer> couponIdList) {
List<UserCoupon> userCouponList = userCouponMapper.selectByCouponIds(couponIdList);
if (CollectionUtils.isEmpty(userCouponList)){
return null;
}
Map<Integer, List<UserCoupon>> userCouponMap = userCouponList.stream().collect(Collectors.groupingBy(UserCoupon::getCouponId));
return userCouponMap;
}
@Override
public ApiResponse saveOrUpdateCoupon(CouponSaveUpdateReq req) {
LOGGER.info("enter saveOrUpdateCoupon,param is {}",req);
... ... @@ -522,6 +519,7 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{
com.yoho.ufo.util.DateUtil.int2DateStr(coupon.getEndTime(),"yyyy-MM-dd HH:mm:ss");
CouponQueryResp resp = new CouponQueryResp(coupon.getId(),coupon.getCouponName(),coupon.getCouponNum(),
useTime,coupon.getRemark(), getStatusDesc(coupon,curTime),coupon.getStatus(),coupon.getCouponToken());
resp.setSendNum(coupon.getSendNum());
resps.add(resp);
}
return resps;
... ...
package com.yoho.ufo.dal;
import com.yoho.ufo.model.coupon.UserCoupon;
import com.yoho.ufo.model.coupon.UserCouponNum;
import com.yohobuy.ufo.coupon.req.UserCouponQueryReq;
import org.apache.ibatis.annotations.Param;
... ... @@ -13,7 +14,10 @@ public interface UserCouponMapper {
List<UserCoupon> selectByCondition(@Param("param") UserCouponQueryReq param);
List<UserCoupon> selectByCouponIds(@Param("couponIds") List<Integer> couponIds);
List<UserCouponNum> selectByCouponIds(@Param("couponIds") List<Integer> couponIds);
List<UserCouponNum> selectByCouponIdsAndStatus(@Param("couponIds") List<Integer> couponIds);
int selectTotalByCondition(@Param("param") UserCouponQueryReq param);
}
... ...
package com.yoho.ufo.model.coupon;
import lombok.Data;
import java.io.Serializable;
/**
* Created by shengguo.cai on 2018/11/20.
*/
@Data
public class Coupon implements Serializable {
private Integer id;
... ... @@ -25,6 +28,10 @@ public class Coupon implements Serializable {
private Integer createTime;
private Integer pid;
private String remark;
/**
* 禁止商品类型
*/
private String skupForbidType;
/**商品限制条件-特定商品*/
public static final int PRODUCTLIMITTYPE_SPECIALPRODUCT = 1;
/**商品限制条件-无限制*/
... ... @@ -70,147 +77,5 @@ public class Coupon implements Serializable {
}
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCouponToken() {
return couponToken;
}
public void setCouponToken(String couponToken) {
this.couponToken = couponToken;
}
public String getCouponName() {
return couponName;
}
public void setCouponName(String couponName) {
this.couponName = couponName;
}
public Float getCouponAmount() {
return couponAmount;
}
public void setCouponAmount(Float couponAmount) {
this.couponAmount = couponAmount;
}
public Integer getCouponType() {
return couponType;
}
public void setCouponType(Integer couponType) {
this.couponType = couponType;
}
public Integer getCouponNum() {
return couponNum;
}
public void setCouponNum(Integer couponNum) {
this.couponNum = couponNum;
}
public Integer getUseNum() {
return useNum;
}
public void setUseNum(Integer useNum) {
this.useNum = useNum;
}
public Integer getSendNum() {
return sendNum;
}
public void setSendNum(Integer sendNum) {
this.sendNum = sendNum;
}
public Integer getUseLimitType() {
return useLimitType;
}
public void setUseLimitType(Integer useLimitType) {
this.useLimitType = useLimitType;
}
public Integer getUseLimitValue() {
return useLimitValue;
}
public void setUseLimitValue(Integer useLimitValue) {
this.useLimitValue = useLimitValue;
}
public Integer getProductLimitType() {
return productLimitType;
}
public void setProductLimitType(Integer productLimitType) {
this.productLimitType = productLimitType;
}
public String getProductLimitValue() {
return productLimitValue;
}
public void setProductLimitValue(String productLimitValue) {
this.productLimitValue = productLimitValue;
}
public Integer getStartTime() {
return startTime;
}
public void setStartTime(Integer startTime) {
this.startTime = startTime;
}
public Integer getEndTime() {
return endTime;
}
public void setEndTime(Integer endTime) {
this.endTime = endTime;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
... ...
package com.yoho.ufo.model.coupon;
import lombok.Data;
@Data
public class UserCouponNum {
int cnt;
int couponId;
}
... ...
... ... @@ -20,12 +20,13 @@
<result column="create_time" property="createTime" jdbcType="INTEGER" />
<result column="pid" property="pid" jdbcType="INTEGER" />
<result column="remark" property="remark" jdbcType="VARCHAR" />
<result column="skup_forbid_type" property="skupForbidType" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List">
id,coupon_token,coupon_name,coupon_amount,coupon_type,coupon_num,use_num,send_num,
use_limit_type,use_limit_value,product_limit_type,product_limit_value,start_time,
end_time,status,create_time,pid,remark
end_time,status,create_time,pid,remark,skup_forbid_type
</sql>
<insert id="insertOrUpdate" useGeneratedKeys="true" keyProperty="param.id">
insert into coupon(id,coupon_token,coupon_name,coupon_amount,coupon_type,coupon_num,use_num,send_num,
... ...
... ... @@ -21,15 +21,30 @@
</sql>
<select id="selectByCouponIds" resultMap="BaseResultMap">
<select id="selectByCouponIds" resultType="com.yoho.ufo.model.coupon.UserCouponNum">
select
<include refid="Base_Column_List"/>
count(uid) cnt, coupon_id
from user_coupon
where
coupon_id IN
<foreach collection="couponIds" item="couponId" open="(" separator="," close=")">
#{couponId,jdbcType=INTEGER}
</foreach>
group by coupon_id
</select>
<select id="selectByCouponIdsAndStatus" resultType="com.yoho.ufo.model.coupon.UserCouponNum">
select
count(uid) cnt, coupon_id
from user_coupon
where
status=1 and
coupon_id IN
<foreach collection="couponIds" item="couponId" open="(" separator="," close=")">
#{couponId,jdbcType=INTEGER}
</foreach>
group by coupon_id
</select>
... ...