Authored by tanling

优惠券

... ... @@ -2,6 +2,9 @@ package com.yohoufo.dal.promotion;
import com.yohoufo.dal.promotion.model.Coupon;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface CouponMapper {
int deleteByPrimaryKey(Integer id);
... ... @@ -12,6 +15,8 @@ public interface CouponMapper {
Coupon selectByPrimaryKey(Integer id);
List<Coupon> selectByIds(@Param("ids") List<Integer> id);
int updateByPrimaryKeySelective(Coupon record);
int updateByPrimaryKey(Coupon record);
... ...
package com.yohoufo.dal.promotion;
import com.yohoufo.dal.promotion.model.Coupon;
import com.yohoufo.dal.promotion.model.CouponType;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface CouponTypeMapper {
int deleteByPrimaryKey(Byte id);
... ... @@ -14,5 +18,7 @@ public interface CouponTypeMapper {
int updateByPrimaryKeySelective(CouponType record);
List<CouponType> selectByIds(@Param("ids") List<Integer> id);
int updateByPrimaryKey(CouponType record);
}
\ No newline at end of file
... ...
... ... @@ -2,6 +2,9 @@ package com.yohoufo.dal.promotion;
import com.yohoufo.dal.promotion.model.UserCoupon;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserCouponMapper {
int deleteByPrimaryKey(Integer id);
... ... @@ -12,6 +15,8 @@ public interface UserCouponMapper {
UserCoupon selectByPrimaryKey(Integer id);
List<UserCoupon> selectUsableCouponByUid(@Param("uid") Integer uid, @Param("now") int now);
int updateByPrimaryKeySelective(UserCoupon record);
int updateByPrimaryKey(UserCoupon record);
... ...
package com.yohoufo.dal.promotion.model;
public class CouponType {
private Byte id;
private Integer id;
private String caption;
public Byte getId() {
public Integer getId() {
return id;
}
public void setId(Byte id) {
public void setId(Integer id) {
this.id = id;
}
... ...
... ... @@ -32,250 +32,19 @@
from coupon
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from coupon
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yohoufo.dal.promotion.model.Coupon">
insert into coupon (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
)
values (#{id,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR}, #{couponName,jdbcType=VARCHAR},
#{couponAmount,jdbcType=DECIMAL}, #{couponType,jdbcType=TINYINT}, #{couponNum,jdbcType=INTEGER},
#{useNum,jdbcType=SMALLINT}, #{sendNum,jdbcType=INTEGER}, #{useLimitType,jdbcType=TINYINT},
#{useLimitValue,jdbcType=SMALLINT}, #{productLimitType,jdbcType=TINYINT}, #{productLimitValue,jdbcType=VARCHAR},
#{startTime,jdbcType=INTEGER}, #{endTime,jdbcType=INTEGER}, #{status,jdbcType=TINYINT},
#{createTime,jdbcType=INTEGER}, #{pid,jdbcType=INTEGER}, #{remark,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.yohoufo.dal.promotion.model.Coupon">
insert into coupon
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="couponToken != null">
coupon_token,
</if>
<if test="couponName != null">
coupon_name,
</if>
<if test="couponAmount != null">
coupon_amount,
</if>
<if test="couponType != null">
coupon_type,
</if>
<if test="couponNum != null">
coupon_num,
</if>
<if test="useNum != null">
use_num,
</if>
<if test="sendNum != null">
send_num,
</if>
<if test="useLimitType != null">
use_limit_type,
</if>
<if test="useLimitValue != null">
use_limit_value,
</if>
<if test="productLimitType != null">
product_limit_type,
</if>
<if test="productLimitValue != null">
product_limit_value,
</if>
<if test="startTime != null">
start_time,
</if>
<if test="endTime != null">
end_time,
</if>
<if test="status != null">
status,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="pid != null">
pid,
</if>
<if test="remark != null">
remark,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="couponToken != null">
#{couponToken,jdbcType=VARCHAR},
</if>
<if test="couponName != null">
#{couponName,jdbcType=VARCHAR},
</if>
<if test="couponAmount != null">
#{couponAmount,jdbcType=DECIMAL},
</if>
<if test="couponType != null">
#{couponType,jdbcType=TINYINT},
</if>
<if test="couponNum != null">
#{couponNum,jdbcType=INTEGER},
</if>
<if test="useNum != null">
#{useNum,jdbcType=SMALLINT},
</if>
<if test="sendNum != null">
#{sendNum,jdbcType=INTEGER},
</if>
<if test="useLimitType != null">
#{useLimitType,jdbcType=TINYINT},
</if>
<if test="useLimitValue != null">
#{useLimitValue,jdbcType=SMALLINT},
</if>
<if test="productLimitType != null">
#{productLimitType,jdbcType=TINYINT},
</if>
<if test="productLimitValue != null">
#{productLimitValue,jdbcType=VARCHAR},
</if>
<if test="startTime != null">
#{startTime,jdbcType=INTEGER},
</if>
<if test="endTime != null">
#{endTime,jdbcType=INTEGER},
</if>
<if test="status != null">
#{status,jdbcType=TINYINT},
</if>
<if test="createTime != null">
#{createTime,jdbcType=INTEGER},
</if>
<if test="pid != null">
#{pid,jdbcType=INTEGER},
</if>
<if test="remark != null">
#{remark,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yohoufo.dal.promotion.model.Coupon">
update coupon
<set>
<if test="couponToken != null">
coupon_token = #{couponToken,jdbcType=VARCHAR},
</if>
<if test="couponName != null">
coupon_name = #{couponName,jdbcType=VARCHAR},
</if>
<if test="couponAmount != null">
coupon_amount = #{couponAmount,jdbcType=DECIMAL},
</if>
<if test="couponType != null">
coupon_type = #{couponType,jdbcType=TINYINT},
</if>
<if test="couponNum != null">
coupon_num = #{couponNum,jdbcType=INTEGER},
</if>
<if test="useNum != null">
use_num = #{useNum,jdbcType=SMALLINT},
</if>
<if test="sendNum != null">
send_num = #{sendNum,jdbcType=INTEGER},
</if>
<if test="useLimitType != null">
use_limit_type = #{useLimitType,jdbcType=TINYINT},
</if>
<if test="useLimitValue != null">
use_limit_value = #{useLimitValue,jdbcType=SMALLINT},
</if>
<if test="productLimitType != null">
product_limit_type = #{productLimitType,jdbcType=TINYINT},
</if>
<if test="productLimitValue != null">
product_limit_value = #{productLimitValue,jdbcType=VARCHAR},
</if>
<if test="startTime != null">
start_time = #{startTime,jdbcType=INTEGER},
</if>
<if test="endTime != null">
end_time = #{endTime,jdbcType=INTEGER},
</if>
<if test="status != null">
status = #{status,jdbcType=TINYINT},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=INTEGER},
</if>
<if test="pid != null">
pid = #{pid,jdbcType=INTEGER},
</if>
<if test="remark != null">
remark = #{remark,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yohoufo.dal.promotion.model.Coupon">
update coupon
set coupon_token = #{couponToken,jdbcType=VARCHAR},
coupon_name = #{couponName,jdbcType=VARCHAR},
coupon_amount = #{couponAmount,jdbcType=DECIMAL},
coupon_type = #{couponType,jdbcType=TINYINT},
coupon_num = #{couponNum,jdbcType=INTEGER},
use_num = #{useNum,jdbcType=SMALLINT},
send_num = #{sendNum,jdbcType=INTEGER},
use_limit_type = #{useLimitType,jdbcType=TINYINT},
use_limit_value = #{useLimitValue,jdbcType=SMALLINT},
product_limit_type = #{productLimitType,jdbcType=TINYINT},
product_limit_value = #{productLimitValue,jdbcType=VARCHAR},
start_time = #{startTime,jdbcType=INTEGER},
end_time = #{endTime,jdbcType=INTEGER},
status = #{status,jdbcType=TINYINT},
create_time = #{createTime,jdbcType=INTEGER},
pid = #{pid,jdbcType=INTEGER},
remark = #{remark,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<resultMap id="BaseResultMap" type="com.yohoufo.dal.promotion.model.Coupon">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="coupon_token" jdbcType="VARCHAR" property="couponToken" />
<result column="coupon_name" jdbcType="VARCHAR" property="couponName" />
<result column="coupon_amount" jdbcType="DECIMAL" property="couponAmount" />
<result column="coupon_type" jdbcType="TINYINT" property="couponType" />
<result column="coupon_num" jdbcType="INTEGER" property="couponNum" />
<result column="use_num" jdbcType="SMALLINT" property="useNum" />
<result column="send_num" jdbcType="INTEGER" property="sendNum" />
<result column="use_limit_type" jdbcType="TINYINT" property="useLimitType" />
<result column="use_limit_value" jdbcType="SMALLINT" property="useLimitValue" />
<result column="product_limit_type" jdbcType="TINYINT" property="productLimitType" />
<result column="product_limit_value" jdbcType="VARCHAR" property="productLimitValue" />
<result column="start_time" jdbcType="INTEGER" property="startTime" />
<result column="end_time" jdbcType="INTEGER" property="endTime" />
<result column="status" jdbcType="TINYINT" property="status" />
<result column="create_time" jdbcType="INTEGER" property="createTime" />
<result column="pid" jdbcType="INTEGER" property="pid" />
<result column="remark" jdbcType="VARCHAR" property="remark" />
</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
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<select id="selectByIds" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from coupon
where id = #{id,jdbcType=INTEGER}
where 1=1
AND id IN
<foreach item="id" index="index" collection="ids"
open="(" separator="," close=")">
#{id}
</foreach>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from coupon
where id = #{id,jdbcType=INTEGER}
... ... @@ -489,4 +258,5 @@
remark = #{remark,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -2,22 +2,38 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yohoufo.dal.promotion.CouponTypeMapper" >
<resultMap id="BaseResultMap" type="com.yohoufo.dal.promotion.model.CouponType" >
<id column="id" property="id" jdbcType="TINYINT" />
<id column="id" property="id" jdbcType="INTEGER" />
<result column="caption" property="caption" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, caption
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Byte" >
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from coupon_type
where id = #{id,jdbcType=TINYINT}
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Byte" >
<select id="selectByIds" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from coupon_type
where 1=1
AND id IN
<foreach item="id" index="index" collection="ids"
open="(" separator="," close=")">
#{id}
</foreach>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from coupon_type
where id = #{id,jdbcType=TINYINT}
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yohoufo.dal.promotion.model.CouponType" >
insert into coupon_type (id, caption)
values (#{id,jdbcType=TINYINT}, #{caption,jdbcType=VARCHAR})
... ...
... ... @@ -18,12 +18,35 @@
id, uid, coupon_id, coupon_type, coupon_code, status, order_code, use_time, start_time,
end_time, create_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user_coupon
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectUsableCouponByUid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user_coupon
where uid = #{uid,jdbcType=INTEGER}
<!-- 在有效时间范围内 -->
and <![CDATA[ start_time > #{now, jdbcType=INTEGER} ]]>
and <![CDATA[ end_time < #{now, jdbcType=INTEGER} ]]>
<!-- 未使用-->
and status = 0
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user_coupon
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user_coupon
where id = #{id,jdbcType=INTEGER}
... ...
package com.yohoufo.promotion.controller;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.promotion.model.response.CouponInfoListBO;
import com.yohoufo.promotion.service.ICouponService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/coupon")
public class CouponController {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
ICouponService couponService;
/**
* 个人中心优惠券列表
* @param uid
* @return
*/
@RequestMapping(params = "method=app.coupons.get")
@ResponseBody
public ApiResponse queryCouponList(@RequestParam(value = "uid") Integer uid) {
logger.info("query user coupons, uid: {}", uid);
CouponInfoListBO couponInfoListBo = couponService.queryCouponList(uid);
logger.info("query user coupons uid: {}, response: {}", uid, couponInfoListBo);
return new ApiResponse.ApiResponseBuilder().code(200).data(couponInfoListBo).build();
}
/**
* 个人中心优惠券数量
* @param uid
* @return
*/
@RequestMapping(params = "method=app.coupons.count")
@ResponseBody
public ApiResponse queryCouponCnt(@RequestParam(value = "uid") Integer uid) {
logger.info("query user coupons, uid: {}", uid);
int couponCnt = couponService.queryCouponCnt(uid);
logger.info("query user coupons uid: {}, response: {}", uid, couponCnt);
return new ApiResponse.ApiResponseBuilder().code(200).data(couponCnt).build();
}
}
... ...
package com.yohoufo.promotion.model.response;
import java.math.BigDecimal;
import java.util.List;
import lombok.Data;
@Data
public class CouponInfo {
private String coupon_token;
private String coupon_name;
private String coupon_code;
private BigDecimal coupon_value;
private String coupon_value_str;
private String use_rule;
private String coupon_validity;
private Integer coupon_type; //大类
private String coupon_type_name;
List<String> notes;
}
... ...
package com.yohoufo.promotion.model.response;
import java.util.List;
import lombok.Data;
import lombok.experimental.Builder;
@Data
@Builder
public class CouponInfoListBO {
private List<CouponInfo> couponList;
}
... ...
package com.yohoufo.promotion.service;
import com.google.common.collect.Lists;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.promotion.CouponMapper;
import com.yohoufo.dal.promotion.CouponTypeMapper;
import com.yohoufo.dal.promotion.UserCouponMapper;
import com.yohoufo.dal.promotion.model.Coupon;
import com.yohoufo.dal.promotion.model.CouponType;
import com.yohoufo.dal.promotion.model.UserCoupon;
import com.yohoufo.promotion.model.response.CouponInfoListBO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class CouponServiceImpl implements ICouponService{
@Autowired
UserCouponMapper userCouponMapper;
@Autowired
CouponMapper couponMapper;
@Autowired
CouponTypeMapper couponTypeMapper;
@Override
public CouponInfoListBO queryCouponList(Integer uid) {
// 获取未使用未过期的优惠券
List<UserCoupon> list = userCouponMapper.selectUsableCouponByUid(uid, DateUtil.getCurrentTimeSecond());
if (CollectionUtils.isEmpty(list)){
return CouponInfoListBO.builder().build();
}
// 获取 coupon
List<Integer> couponIdList = list.stream().map(UserCoupon::getCouponId).collect(Collectors.toList());
List<Coupon> couponList = couponMapper.selectByIds(couponIdList);
Map<Integer, Coupon> couponMap = couponList.stream().collect(Collectors.toMap(Coupon::getId, Function.identity()));
// 获取 couponTyoe
List<Integer> couponTypeIdList = list.stream().map(UserCoupon::getCouponType).map(Byte::intValue).collect(Collectors.toList());
List<CouponType> couponTypeList = couponTypeMapper.selectByIds(couponTypeIdList);
Map<Integer, CouponType> couponTypeMap = couponTypeList.stream().collect(Collectors.toMap(CouponType::getId, Function.identity()));
return null;
}
@Override
public int queryCouponCnt(Integer uid) {
return 0;
}
}
... ...
package com.yohoufo.promotion.service;
import com.yohoufo.promotion.model.response.CouponInfoListBO;
public interface ICouponService {
public CouponInfoListBO queryCouponList(Integer uid);
public int queryCouponCnt(Integer uid);
}
... ...