Authored by mingdan.ge

cps订单参与加倍返活动

... ... @@ -50,6 +50,9 @@ public enum ShareOrdersStatusEnum {
return null;
}
public static boolean isFailOrder(String status) {
return (!status.equals(ShareOrdersStatusEnum.CAN_SETTLE)) && (ShareOrdersStatusEnum.getLevelByCode(status)==2);
}
public int getLevel() {
return level;
}
... ...
package com.yoho.unions.dal;
import com.yoho.unions.dal.model.UnionShareOrdersActivityLogs;
import org.apache.ibatis.annotations.Param;
import java.util.List;
... ... @@ -12,9 +13,12 @@ public interface UnionShareOrdersActivityLogsMapper {
int insertSelective(UnionShareOrdersActivityLogs record);
UnionShareOrdersActivityLogs selectByPrimaryKey(Integer id);
List<UnionShareOrdersActivityLogs> selectByOrderIds(List<Integer> ids);
List<UnionShareOrdersActivityLogs> selectByOrderIds(@Param("orderIds") List<Integer> orderIds);
List<UnionShareOrdersActivityLogs> selectByOrderId(@Param("orderId") Integer orderId);
List<UnionShareOrdersActivityLogs> selectByOrderCode(@Param("orderCode") String orderCode);
int updateByPrimaryKeySelective(UnionShareOrdersActivityLogs record);
int updateStatusByOrderId(UnionShareOrdersActivityLogs record);
int updateByPrimaryKey(UnionShareOrdersActivityLogs record);
}
\ No newline at end of file
... ...
... ... @@ -35,6 +35,7 @@ public interface UnionShareOrdersMapper {
List<UnionShareOrders> selectListByCondition(@Param("promoteUid")int promoteUid, @Param("status")String status,@Param("updateTime")int updateTime, @Param("offset") int offset, @Param("rows") int rows);
List<String> selectOrderCodesByCondition(@Param("settlementCode")String settlementCode, @Param("offset") int offset, @Param("rows") int rows);
List<UnionShareOrders> selectOrderByCondition(@Param("settlementCode")String settlementCode, @Param("offset") int offset, @Param("rows") int rows);
List<Integer> selectIdForActivity(@Param("settlementCode")String settlementCode);
int updateByPrimaryKeySelective(UnionShareOrders record);
... ...
... ... @@ -6,6 +6,7 @@ public class UnionShareOrdersActivityLogs {
private Integer id;
private Integer activityId;
private Integer activityType;
private String activityName;
... ... @@ -112,4 +113,12 @@ public class UnionShareOrdersActivityLogs {
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
}
public Integer getActivityType() {
return activityType;
}
public void setActivityType(Integer activityType) {
this.activityType = activityType;
}
}
\ No newline at end of file
... ...
... ... @@ -4,6 +4,7 @@
<resultMap id="BaseResultMap" type="com.yoho.unions.dal.model.UnionShareOrdersActivityLogs" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="activity_id" property="activityId" jdbcType="INTEGER" />
<result column="activity_type" property="activityType" jdbcType="INTEGER" />
<result column="activity_name" property="activityName" jdbcType="INTEGER" />
<result column="activity_item_id" property="activityItemId" jdbcType="INTEGER" />
<result column="promote_uid" property="promoteUid" jdbcType="INTEGER" />
... ... @@ -15,7 +16,7 @@
<result column="update_time" property="updateTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, activity_id, activity_name, activity_item_id, promote_uid, order_id, amount, status, orders, create_time,
id, activity_id, activity_type, activity_name, activity_item_id, promote_uid, order_id, amount, status, orders, create_time,
update_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
... ... @@ -29,20 +30,32 @@
<include refid="Base_Column_List" />
from union_share_orders_activity_logs
where order_id IN
<foreach collection="ids" item="item" separator="," index="index" open="(" close=")">
<foreach collection="orderIds" item="item" separator="," index="index" open="(" close=")">
#{item}
</foreach>
</select>
<select id="selectByOrderId" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from union_share_orders_activity_logs
where order_id = #{orderId,jdbcType=INTEGER}
</select>
<select id="selectByOrderCode" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from union_share_orders_activity_logs
where orders like CONCAT('%', #{orderCode}, '%')
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from union_share_orders_activity_logs
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yoho.unions.dal.model.UnionShareOrdersActivityLogs" >
insert into union_share_orders_activity_logs (id, activity_id, activity_name, activity_item_id,
insert into union_share_orders_activity_logs (id, activity_id,activity_type, activity_name, activity_item_id,
promote_uid, order_id, amount,
status, orders, create_time,
update_time)
values (#{id,jdbcType=INTEGER}, #{activityId,jdbcType=INTEGER}, #{activityName,jdbcType=INTEGER}, #{activityItemId,jdbcType=INTEGER},
values (#{id,jdbcType=INTEGER}, #{activityId,jdbcType=INTEGER},#{activityType,jdbcType=INTEGER}, #{activityName,jdbcType=INTEGER}, #{activityItemId,jdbcType=INTEGER},
#{promoteUid,jdbcType=INTEGER}, #{orderId,jdbcType=INTEGER}, #{amount,jdbcType=DECIMAL},
#{status,jdbcType=INTEGER}, #{orders,jdbcType=VARCHAR}, #{createTime,jdbcType=INTEGER},
#{updateTime,jdbcType=INTEGER})
... ... @@ -56,6 +69,9 @@
<if test="activityId != null" >
activity_id,
</if>
<if test="activityType != null" >
activity_type,
</if>
<if test="activityName != null" >
activity_name,
</if>
... ... @@ -91,6 +107,9 @@
<if test="activityId != null" >
#{activityId,jdbcType=INTEGER},
</if>
<if test="activityType != null" >
#{activityType,jdbcType=INTEGER},
</if>
<if test="activityName != null" >
#{activityName,jdbcType=INTEGER},
</if>
... ... @@ -126,6 +145,9 @@
<if test="activityId != null" >
activity_id = #{activityId,jdbcType=INTEGER},
</if>
<if test="activityType != null" >
activity_type = #{activityType,jdbcType=INTEGER},
</if>
<if test="activityName != null" >
activity_name = #{activityName,jdbcType=INTEGER},
</if>
... ... @@ -156,9 +178,16 @@
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateStatusByOrderId" parameterType="com.yoho.unions.dal.model.UnionShareOrdersActivityLogs" >
update union_share_orders_activity_logs
set status = #{status,jdbcType=INTEGER},
update_time = #{updateTime,jdbcType=INTEGER}
where order_id = #{orderId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yoho.unions.dal.model.UnionShareOrdersActivityLogs" >
update union_share_orders_activity_logs
set activity_id = #{activityId,jdbcType=INTEGER},
activity_type = #{activityType,jdbcType=INTEGER},
activity_name = #{activityName,jdbcType=INTEGER},
activity_item_id = #{activityItemId,jdbcType=INTEGER},
promote_uid = #{promoteUid,jdbcType=INTEGER},
... ...
... ... @@ -26,6 +26,13 @@
from union_share_orders_activity
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByDate" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from union_share_orders_activity
where status=1 and start_time &lt; #{time,jdbcType=INTEGER} and end_time &gt; #{time,jdbcType=INTEGER}
order by priority desc
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from union_share_orders_activity
where id = #{id,jdbcType=INTEGER}
... ...
<?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.yoho.unions.dal.UnionShareOrdersMapper" >
<resultMap id="BaseResultMap" type="com.yoho.unions.dal.com.yoho.unions.dal.model.UnionShareOrders" >
<resultMap id="BaseResultMap" type="com.yoho.unions.dal.model.UnionShareOrders" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="order_code" property="orderCode" jdbcType="VARCHAR" />
<result column="settlement_code" property="settlementCode" jdbcType="VARCHAR" />
... ... @@ -72,6 +72,13 @@
where settlement_code = #{settlementCode,jdbcType=VARCHAR} and order_code is not null
limit #{offset}, #{rows}
</select>
<select id="selectOrderByCondition" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from union_share_orders
where settlement_code = #{settlementCode,jdbcType=VARCHAR} and order_code is not null
limit #{offset}, #{rows}
</select>
<select id="selectIdForActivity" resultType="java.lang.Integer">
select id from union_share_orders
where settlement_code = #{settlementCode,jdbcType=VARCHAR} and order_code is null
... ... @@ -80,7 +87,7 @@
delete from union_share_orders
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yoho.unions.dal.model.UnionShareOrders" >
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.yoho.unions.dal.model.UnionShareOrders" >
insert into union_share_orders (id, order_code, promote_uid,
order_uid, settlement_code, status,
last_order_amount, order_time, is_new,
... ... @@ -94,7 +101,7 @@
#{activityType,jdbcType=INTEGER}, #{activityId,jdbcType=INTEGER}, #{extraAmount,jdbcType=DECIMAL},
#{orderAmount,jdbcType=DECIMAL})
</insert>
<insert id="insertSelective" parameterType="com.yoho.unions.dal.model.UnionShareOrders" >
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="id" parameterType="com.yoho.unions.dal.model.UnionShareOrders" >
insert into union_share_orders
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
... ...
... ... @@ -997,32 +997,104 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
if (result == 0) {
logger.info("saveOrUpdateOrder enter,update nothing,req is {}",bo);
//没有更新到数据,应该有并发更新事件,查询新的再试一下
UnionShareOrders unionShareOrdersNew = unionShareOrdersMapper.selectByCode(bo.getOrderCode());
if (unionShareOrdersNew == null) {
unionShareOrders = unionShareOrdersMapper.selectByCode(bo.getOrderCode());
if (unionShareOrders == null) {
logger.info("saveOrUpdateOrder end,can not find the orderCode,req is {}",bo);
//查不到数据,不该出现的情况,目前没有硬删数据的途径
return;
}
if (!checkOrderStatusChangePass(unionShareOrdersNew.getStatus(), bo.getStatus())) {
if (!checkOrderStatusChangePass(unionShareOrders.getStatus(), bo.getStatus())) {
//订单状态不可变更
logger.info("saveOrUpdateOrder end,Second try,status can not be change,oldStatus is {} ,req is {}",unionShareOrdersNew.getStatus(),bo);
logger.info("saveOrUpdateOrder end,Second try,status can not be change,oldStatus is {} ,req is {}",unionShareOrders.getStatus(),bo);
return;
}
unionShareOrdersMapper.updateStatusById(unionShareOrdersNew.getId(), unionShareOrdersNew.getStatus(), bo.getStatus(), DateUtil.getCurrentTimeSecond());
result=unionShareOrdersMapper.updateStatusById(unionShareOrders.getId(), unionShareOrders.getStatus(), bo.getStatus(), DateUtil.getCurrentTimeSecond());
}
if (result > 0 &&ShareOrdersStatusEnum.isFailOrder(unionShareOrders.getStatus())) {
//要作废活动参与记录
UnionShareOrdersActivityLogs updateLogs = new UnionShareOrdersActivityLogs();
updateLogs.setOrderId(unionShareOrders.getId());
updateLogs.setUpdateTime(DateUtil.getCurrentTimeSecond());
updateLogs.setStatus(0);//类型:1-有效,0-作废
unionShareOrdersActivityLogsMapper.updateStatusByOrderId(updateLogs);
//查询是否参与满单返
List<UnionShareOrdersActivityLogs> activityLogs=unionShareOrdersActivityLogsMapper.selectByOrderCode(unionShareOrders.getOrderCode());
if (CollectionUtils.isNotEmpty(activityLogs)) {
//todo 判断满返是否作废
}
}
//清缓存
clearShareOrderRedis(bo.getPromoteUid());
}
//todo 查询进行中的活动
private void queryActivity() {
private List<UnionShareOrdersActivity> queryActivity() {
List<UnionShareOrdersActivity> activities=unionShareOrdersActivityMapper.selectByDate(DateUtil.getCurrentTimeSecond());
return activities;
}
//todo 查询已参加的活动
private void queryUidActivity() {
List<UnionShareOrdersActivity> activities=unionShareOrdersActivityMapper.selectByDate(DateUtil.getCurrentTimeSecond());
//todo 查询分享人已参加的活动
private void queryUidActivity(int uid) {
// List<UnionShareOrdersActivity> activities=unionShareOrdersActivityLogsMapper.selectByOrderIds()
}
//参加活动
private boolean participateActivity(UnionShareOrders order,UnionShareOrdersActivity activity) {
//1、判断是否符合活动条件
//1.1 活动类型:1-订单返利比例升级,2-订单返利翻x倍,3-额外返
if (activity.getType() == 3) {
//额外返在活动结束后走定时任务
return false;
}
//1.2 是否新人专享 1-是
if (activity.getIsNew().intValue() == 1 && !activity.getIsNew().equals(order.getIsNew())) {
//活动为新人专享,但是订单不是首单
return false;
}
//2、参加活动
//2.1 返利金额计算
BigDecimal extraAmount = activity.getAmount();
/* if (activity.getType() == 1) {
//活动类型:1-订单返利比例升级
extraAmount = order.getLastOrderAmount().multiply(new BigDecimal(activity.getPercent()).divide(new BigDecimal(10000))).setScale(2,BigDecimal.ROUND_DOWN);
}else */
if (activity.getType() == 2) {
//活动类型:2-订单返利翻x倍
extraAmount = order.getAmount().multiply(new BigDecimal(activity.getPercent()));
}
if (activity.getAmount().intValue()>0&&extraAmount.compareTo(activity.getAmount())>1) {
//订单返利金额高于封顶金额
extraAmount = activity.getAmount();
}
order.setExtraAmount(extraAmount);
order.setAmount(order.getAmount().add(extraAmount));
//2.2 保存活动日志
UnionShareOrdersActivityLogs log = new UnionShareOrdersActivityLogs();
BeanUtils.copyProperties(order, log);
log.setActivityId(activity.getId());
log.setActivityName(activity.getActivityName());
log.setCreateTime(DateUtil.getCurrentTimeSecond());
log.setUpdateTime(log.getCreateTime());
log.setOrderId(order.getId());
log.setStatus(1);
log.setOrders(order.getOrderCode());
log.setId(null);
unionShareOrdersActivityLogsMapper.insertSelective(log);
//3 更新订单返利金额
UnionShareOrders updateOrder = new UnionShareOrders();
updateOrder.setId(order.getId());
updateOrder.setAmount(order.getAmount());
updateOrder.setExtraAmount(order.getExtraAmount());
updateOrder.setActivityId(activity.getId());
updateOrder.setActivityType(activity.getType());
unionShareOrdersMapper.updateByPrimaryKeySelective(updateOrder);
//4、返回
logger.info("participateActivity success,order is {},activity is {}",order,activity);
return true;
}
//todo 查询可参加的活动
private void getActivity() {
... ... @@ -1069,9 +1141,22 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
}
//返利金额计算小数位两位后舍弃
insertReq.setAmount((bo.getLastOrderAmount().multiply(new BigDecimal(rebatesRatio).divide(new BigDecimal(100)))).setScale(2,BigDecimal.ROUND_DOWN));
insertReq.setOrderAmount(insertReq.getAmount());
logger.info("insertOrder,orderCode is {},orderAmount is {},rebatesRatio is {},rebates is {}",bo.getOrderCode(),bo.getLastOrderAmount(),rebatesRatio,insertReq.getAmount());
bo.setAmount(insertReq.getAmount());
unionShareOrdersMapper.insertSelective(insertReq);
//活动返利
try {
List<UnionShareOrdersActivity> activityList = queryActivity();
for (UnionShareOrdersActivity a :activityList ) {
if(participateActivity(insertReq,a)) {
//参加了活动
break;
}
}
} catch (Exception e) {
logger.warn("insertOrder warn,fail to handle some activities.bo is {}",bo);
}
//新增订单商品信息
bo.getProductList().forEach(p->{
try {
... ... @@ -1281,23 +1366,27 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
settlementLog.warn("relateSettlementAndOrder end,uid is {},settlementCode is {},totalAmount is {},insert nothing.",uid,settlementCode,totalAmount);
}
Set<String> orderCodes = new HashSet<>();
// Set<String> orderCodes = new HashSet<>();
List<UnionShareOrdersBo> orderList = new ArrayList<>();//该提现对应的订单
int size = 1000;
int page = count % size > 0 ? (count / size) + 1 : count / size;
for (int i = 0; i < page; i++) {
int offset = i * size ;
List<String> periodOrders = unionShareOrdersMapper.selectOrderCodesByCondition(settlementCode, offset, size);
List<UnionShareOrders> periodOrders = unionShareOrdersMapper.selectOrderByCondition(settlementCode, offset, size);
if (CollectionUtils.isEmpty(periodOrders) || periodOrders.get(0) == null) {
break;
}
orderCodes.addAll(periodOrders);
// orderCodes.addAll(periodOrders);
periodOrders.forEach(o->{
UnionShareOrdersBo b = new UnionShareOrdersBo();
BeanUtils.copyProperties(o,b);
orderList.add(b);
});
}
//查询活动额外返的返利单
List<Integer> ids = unionShareOrdersMapper.selectIdForActivity(settlementCode);
if (CollectionUtils.isEmpty(orderCodes)&&CollectionUtils.isEmpty(ids)) {
//不该查不到订单
settlementLog.warn("relateSettlementAndOrder end,uid is {},settlementCode is {},totalAmount is {},can not find orders and activity.",uid,settlementCode,totalAmount);
throw new ServiceException(ServiceError.UNION_SETTLEMENT_CANNOT_FIND_ORDER_ERROR);
}
List<UnionShareOrdersActivityBo> activityBos = new ArrayList<>();
if(CollectionUtils.isNotEmpty(ids)){
List<UnionShareOrdersActivityLogs> activities = unionShareOrdersActivityLogsMapper.selectByOrderIds(ids);
... ... @@ -1308,15 +1397,21 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
activityBos.add(bo);
});
}
if (CollectionUtils.isEmpty(orderList)&&CollectionUtils.isEmpty(activityBos)) {
//不该查不到订单
settlementLog.warn("relateSettlementAndOrder end,uid is {},settlementCode is {},totalAmount is {},can not find orders and activity.",uid,settlementCode,totalAmount);
throw new ServiceException(ServiceError.UNION_SETTLEMENT_CANNOT_FIND_ORDER_ERROR);
}
//清缓存
clearShareOrderRedis(uid);
// 发送取现mq给erp SETTLEMENT_TOPIC
ShareSettlementBo bo = new ShareSettlementBo();
BeanUtils.copyProperties(insertReq,bo);
bo.setOrderCodes(orderCodes);
// bo.setOrderCodes(orderCodes);
bo.setOrderList(orderList);
bo.setActivities(activityBos);
System.out.println(JsonUtil.objectToJSON(bo));
mqLog.info("relateSettlementAndOrder,send mq {} to erp,uid is {},settlementCode is {},bo is {}", SETTLEMENT_TOPIC, uid, settlementCode, bo);
yhProducer.send(SETTLEMENT_TOPIC, bo);
settlementLog.info("relateSettlementAndOrder,send mq {} to erp success,uid is {},settlementCode is {},bo is {}", SETTLEMENT_TOPIC, uid, settlementCode, bo);
... ...