Authored by chenchao

add display of coupon

package com.yohoufo.dal.promotion;
import com.yohoufo.dal.promotion.model.CouponDisplay;
import java.util.List;
public interface CouponDisplayMapper {
int insert(CouponDisplay record);
List<CouponDisplay> selectByCouponId(CouponDisplay record);
CouponDisplay selectByCouponIdPositionType(CouponDisplay record);
int updateByPrimaryKey(CouponDisplay record);
}
\ No newline at end of file
... ...
package com.yohoufo.dal.promotion;
import com.yohoufo.dal.promotion.model.Coupon;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface CouponViewMapper {
List<Coupon> selectByAssociatedPrd(@Param("productId") Integer productId,
@Param("positionType") int positionType,
@Param("currentTime") int currentTime,
@Param("offset")int offset,
@Param("limit")int limit);
int selectCntByAssociatedPrd(@Param("productId") Integer productId,
@Param("positionType") int positionType,
@Param("currentTime") int currentTime);
}
\ No newline at end of file
... ...
package com.yohoufo.dal.promotion.model;
import lombok.Data;
@Data
public class CouponDisplay {
private Integer id;
private Integer couponId;
private Integer positionType;
private Byte visibility;
}
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yohoufo.dal.promotion.CouponDisplayMapper">
<resultMap id="BaseResultMap" type="com.yohoufo.dal.promotion.model.CouponDisplay">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="coupon_id" jdbcType="INTEGER" property="couponId" />
<result column="position_type" jdbcType="INTEGER" property="positionType" />
<result column="visibility" jdbcType="TINYINT" property="visibility" />
</resultMap>
<sql id="Base_Column_List">
id, coupon_id, position_type, visibility
</sql>
<select id="selectByCouponId" parameterType="com.yohoufo.dal.promotion.model.CouponDisplay" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from coupon_display
where coupon_id = #{couponId,jdbcType=INTEGER}
</select>
<select id="selectByCouponIdPositionType" parameterType="com.yohoufo.dal.promotion.model.CouponDisplay" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from coupon_display
where coupon_id = #{couponId,jdbcType=INTEGER} and position_type= #{positionType}
</select>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yohoufo.dal.promotion.model.CouponDisplay" useGeneratedKeys="true">
insert into coupon_display (coupon_id, position_type, visibility
)
values (#{couponId,jdbcType=VARCHAR}, #{positionType,jdbcType=INTEGER}, #{visibility,jdbcType=TINYINT}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.yohoufo.dal.promotion.model.CouponDisplay">
update coupon_display
<set>
visibility = #{visibility,jdbcType=TINYINT},
</set>
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yohoufo.dal.promotion.CouponViewMapper">
<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="INTEGER" 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" />
<result column="skup_forbid_type" jdbcType="VARCHAR" property="skupForbidType" />
<result column="skup_allow_type" jdbcType="VARCHAR" property="skupAllowType" />
<result column="business_client" jdbcType="VARCHAR" property="businessClient" />
<result column="receive_start_time" property="receiveStartTime" jdbcType="INTEGER" />
<result column="receive_end_time" property="receiveEndTime" jdbcType="INTEGER" />
<result column="check_required" property="checkRequired" jdbcType="INTEGER" />
</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,skup_forbid_type,skup_allow_type,business_client,
receive_start_time,receive_end_time,check_required
</sql>
<sql id="Base_Column_List_With_Alias">
cpn.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,skup_forbid_type,skup_allow_type,business_client,
receive_start_time,receive_end_time,check_required
</sql>
<select id="selectByAssociatedPrd" resultMap="BaseResultMap">
select
<include refid="Base_Column_List_With_Alias" />
from coupon cpn
LEFT JOIN `coupon_display` cdp ON cpn.`id` = cdp.coupon_id
WHERE cpn.status = 1
AND cpn.id IN
(<include refid="query_prd_limit"/> )
AND cpn.`coupon_num` > cpn.`send_num`
AND cdp.position_type= #{positionType} AND cdp.`visibility`=1
AND (#{currentTime} <![CDATA[ < ]]> `end_time` OR #{currentTime} <![CDATA[ < ]]> `receive_end_time`)
ORDER BY coupon_type,use_limit_type,`use_limit_value` DESC
limit #{offset},#{limit}
</select>
<select id="selectCntByAssociatedPrd" resultType="int">
SELECT COUNT(*) cnt FROM coupon cpn
LEFT JOIN `coupon_display` cdp ON cpn.`id` = cdp.coupon_id
WHERE cpn.status = 1
AND cpn.id IN
(<include refid="query_prd_limit"/> )
AND cpn.`coupon_num` > cpn.`send_num`
AND cdp.position_type= #{positionType} AND cdp.`visibility`=1
AND (#{currentTime} <![CDATA[ < ]]> `end_time` OR #{currentTime} <![CDATA[ < ]]> `receive_end_time`)
</select>
<sql id="query_prd_limit">
SELECT DISTINCT cpn.id FROM coupon cpn
LEFT JOIN `coupon_product_limit` cpl ON cpl.`coupon_id` = cpn.id
WHERE ((cpn.`product_limit_type` = 1 AND cpl.`product_id` = #{productId}) OR (cpn.`product_limit_type` = 3 AND cpl.`product_id` != #{productId}) OR cpn.`product_limit_type` =2)
</sql>
</mapper>
\ No newline at end of file
... ...
package com.yohoufo.promotion.controller;
import com.yohobuy.ufo.model.promotion.CouponSimpleBo;
import com.yohobuy.ufo.model.promotion.request.CouponListReq;
import com.yohobuy.ufo.model.promotion.response.CouponInfo;
import com.yohobuy.ufo.model.promotion.response.CouponInfoListBo;
import com.yohobuy.ufo.model.promotion.response.CouponListBo;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.promotion.service.impl.CollectiveCouponService;
import org.slf4j.Logger;
... ... @@ -13,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by chenchao on 2020/2/10.
*/
... ... @@ -40,14 +44,14 @@ public class CouponCenterController {
.limit(Math.max(20, limit))
.build();
logger.info("in ufo.couponsOfPrdDetail.list, req:{}", req);
CouponInfoListBo listBo = collectiveCouponService.queryCouponListOfPrdDetail(req);
CouponListBo listBo = collectiveCouponService.queryCouponListOfPrdDetail(req);
logger.info("exit ufo.couponsOfPrdDetail.list, req {}, response {}", req, listBo);
return new ApiResponse.ApiResponseBuilder().code(200).data(listBo).build();
}
@RequestMapping("/couponTopListOfPrdDetail")
@ResponseBody
public CouponInfo[] couponTopListOfPrdDetail(
public List<CouponSimpleBo> couponTopListOfPrdDetail(
@RequestParam(value = "productId") Integer productId,
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
@RequestParam(value = "limit", required = false, defaultValue = "2") int limit,
... ... @@ -57,10 +61,10 @@ public class CouponCenterController {
.productId(productId)
.businessClient(businessClient)
.page(Math.max(1, page))
.limit(Math.max(20, limit))
.limit(Math.max(2, limit))
.build();
logger.info("in couponTopListOfPrdDetail, req:{}", req);
CouponInfo[] resp = collectiveCouponService.getCouponTopListOfPrdDetail(req);
List<CouponSimpleBo> resp = collectiveCouponService.getCouponTopListOfPrdDetail(req);
logger.info("exit couponTopListOfPrdDetail, req {}, response {}", req, resp);
return resp;
}
... ...
package com.yohoufo.promotion.controller;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.promotion.CouponDisplayBo;
import com.yohobuy.ufo.model.promotion.CouponSimpleBo;
import com.yohobuy.ufo.model.promotion.response.CouponSendBo;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.common.annotation.IgnoreSession;
import com.yohoufo.dal.promotion.model.Coupon;
import com.yohoufo.promotion.service.ICouponCacheService;
import com.yohoufo.promotion.service.ICouponService;
import com.yohoufo.promotion.service.impl.CouponDisplayService;
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;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/erp")
... ... @@ -27,6 +24,9 @@ public class ErpGWCouponController {
@Autowired
ICouponService couponService;
@Autowired
private CouponDisplayService couponDisplayService;
@Autowired
ICouponCacheService couponCacheService;
... ... @@ -87,4 +87,10 @@ public class ErpGWCouponController {
return couponSendBoList;
}
@RequestMapping("/coupon/addOrUpdateDisplay")
@IgnoreSession
public int addOrUpdateDisplay(@RequestBody CouponDisplayBo req){
return couponDisplayService.addOrUpdate(req);
}
}
... ...
package com.yohoufo.promotion.convert;
import com.google.common.collect.Lists;
import com.yohobuy.ufo.model.promotion.CouponSimpleBo;
import com.yohobuy.ufo.model.promotion.UserCouponsBo;
import com.yohobuy.ufo.model.promotion.constant.*;
import com.yohobuy.ufo.model.promotion.response.CouponInfo;
... ... @@ -162,4 +163,17 @@ public class CouponConvert {
return userCoupon.getStatus() == CouponUseStatusEnum.NOT_USED.getCode()
&& DateUtil.between(userCoupon.getEndTime() - DateUtil.getCurrentTimeSecond(), 0, THREE_DAY_SECOND);
}
public static CouponSimpleBo do2CouponSimpleBo(Coupon coupon){
CouponSimpleBo couponSimpleBo = new CouponSimpleBo();
couponSimpleBo.setCouponName(coupon.getCouponName());
couponSimpleBo.setCouponAmount(coupon.getCouponAmount());
couponSimpleBo.setUseLimitType(coupon.getUseLimitType().intValue());
couponSimpleBo.setUseLimitValue(String.valueOf(coupon.getUseLimitValue()));
couponSimpleBo.setCheckRequired(coupon.getCheckRequired());
couponSimpleBo.setCouponToken(coupon.getCouponToken());
return couponSimpleBo;
}
}
... ...
package com.yohoufo.promotion.convert;
import com.yohobuy.ufo.model.promotion.CouponDisplayBo;
import com.yohoufo.dal.promotion.model.CouponDisplay;
public final class CouponDisplayConvertor {
public static CouponDisplay bo2Do(CouponDisplayBo bo){
CouponDisplay couponDisplay = new CouponDisplay();
couponDisplay.setCouponId(bo.getCouponId());
couponDisplay.setPositionType(bo.getPositionType());
couponDisplay.setVisibility(bo.getVisibility());
return couponDisplay;
}
}
... ...
package com.yohoufo.promotion.service.impl;
import com.yohobuy.ufo.model.promotion.CouponSimpleBo;
import com.yohobuy.ufo.model.promotion.constant.PositionType;
import com.yohobuy.ufo.model.promotion.request.CouponListReq;
import com.yohobuy.ufo.model.promotion.response.CouponInfo;
import com.yohobuy.ufo.model.promotion.response.CouponInfoListBo;
import com.yohobuy.ufo.model.promotion.response.CouponListBo;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.common.utils.PageHelper;
import com.yohoufo.dal.promotion.CouponViewMapper;
import com.yohoufo.dal.promotion.model.Coupon;
import com.yohoufo.promotion.convert.CouponConvert;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Created by chenchao on 2020/2/10.
*/
@Service
public class CollectiveCouponService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private CouponViewMapper couponViewMapper;
public CouponInfoListBo queryCouponListOfPrdDetail(CouponListReq req){
public CouponListBo queryCouponListOfPrdDetail(CouponListReq req){
int pageNum = req.getPage();
int limit = req.getLimit();
CouponInfoListBo couponInfoListBo = CouponInfoListBo.builder()
CouponListBo.CouponListBoBuilder couponInfoListBuilder = CouponListBo.builder()
.page(pageNum)
.limit(limit)
.build();
.limit(limit);
Integer prdId;
if (Objects.isNull(prdId=req.getProductId())|| prdId<=0){
logger.warn("queryCouponListOfPrdDetail prdId illegal, {}",req);
return couponInfoListBuilder.build();
}
final int currentDT = DateUtil.getCurrentTimeSecond();
final int positionType = PositionType.PRD_DETAIL.getCode();
return couponInfoListBo;
int total = couponViewMapper.selectCntByAssociatedPrd(prdId,positionType,currentDT);
if(total == 0){
logger.warn("queryCouponListOfPrdDetail find total number zero, {}", req);
return couponInfoListBuilder.build();
}
int totalPage = PageHelper.getPageTotal(total, limit);
final int offset = PageHelper.getOffsetOfMysql(pageNum, limit);
List<Coupon> datas= couponViewMapper.selectByAssociatedPrd(prdId, positionType,currentDT,offset,limit);
List<CouponSimpleBo> couponInfoListBo = datas.stream().map(coupon -> CouponConvert.do2CouponSimpleBo(coupon))
.collect(Collectors.toList());
return couponInfoListBuilder.total(total)
.totalPage(totalPage).coupons(couponInfoListBo)
.build();
}
public CouponInfo[] getCouponTopListOfPrdDetail(CouponListReq req){
return null;
public List<CouponSimpleBo> getCouponTopListOfPrdDetail(CouponListReq req){
logger.info("getCouponTopListOfPrdDetail req {}",req);
CouponListBo couponListBo = queryCouponListOfPrdDetail(req);
List<CouponSimpleBo> list;
if(CollectionUtils.isEmpty(list = couponListBo.getCoupons())){
logger.warn("getCouponTopListOfPrdDetail get empty, {}",req);
}
return list;
}
}
... ...
package com.yohoufo.promotion.service.impl;
import com.yohobuy.ufo.model.promotion.CouponDisplayBo;
import com.yohoufo.dal.promotion.CouponDisplayMapper;
import com.yohoufo.dal.promotion.model.CouponDisplay;
import com.yohoufo.promotion.convert.CouponDisplayConvertor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class CouponDisplayService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private CouponDisplayMapper couponDisplayMapper;
public int addOrUpdate(CouponDisplayBo bo){
if (Objects.isNull(bo.getCouponId()) || Objects.isNull(bo.getPositionType()) || Objects.isNull(bo.getVisibility())){
logger.warn("addOrUpdate CouponDisplay req illegal, {}", bo);
return -1;
}
CouponDisplay condition = CouponDisplayConvertor.bo2Do(bo);
CouponDisplay pcd = couponDisplayMapper.selectByCouponIdPositionType(condition);
int rows = 0;
if (Objects.isNull(pcd)){
CouponDisplay icd = condition;
rows = couponDisplayMapper.insert(icd);
}else{
if (bo.getVisibility().equals(pcd.getVisibility())){
logger.warn("addOrUpdate CouponDisplay visibility no change, req {}", bo);
return 0;
}
CouponDisplay ucd = new CouponDisplay();
ucd.setId(pcd.getId());
ucd.setVisibility(bo.getVisibility());
rows = couponDisplayMapper.updateByPrimaryKey(ucd);
}
//TODO clean cache of coupon
return rows;
}
}
... ...
... ... @@ -163,3 +163,7 @@ unionpay.env=00
unionpay.notifyurl=http://testapi.yohops.com/payment/ufo_unionpay_notify
ufo.alipay.sendPaymentInfo.switch=false
dataSource.URL = jdbc:mysql://192.168.102.219:3306/yh_jobmanager?useUnicode=true&amp;characterEncoding=UTF-8&testConnectionOnCheckout=false&testConnectionOnCheckin=true&idleConnectionTestPeriod=60
dataSource.user = yh_test
dataSource.password = 9nm0icOwt6bMHjMusIfMLw==
\ No newline at end of file
... ...
... ... @@ -142,6 +142,7 @@ datasources:
- com.yohoufo.dal.promotion.IActivityMapper
- com.yohoufo.dal.promotion.IActivityProductScopeMapper
- com.yohoufo.dal.promotion.IActivityAmountConditionMapper
- com.yohoufo.dal.promotion.CouponDisplayMapper
ufo_resource:
servers:
... ...
... ... @@ -145,6 +145,7 @@ datasources:
- com.yohoufo.dal.promotion.IActivityMapper
- com.yohoufo.dal.promotion.IActivityAmountConditionMapper
- com.yohoufo.dal.promotion.IActivityProductScopeMapper
- com.yohoufo.dal.promotion.CouponDisplayMapper
ufo_resource:
... ...