Authored by chenchao

add attributes

add cal buyer cancel penalty
package com.yohoufo.dal.order.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.math.BigDecimal;
@ToString
public class BuyerOrder {
private Integer id;
... ... @@ -35,6 +40,9 @@ public class BuyerOrder {
private Integer sellerOrderStatus;
@Getter@Setter
private Integer attributes;
public Integer getId() {
return id;
}
... ...
... ... @@ -18,10 +18,12 @@
<result column="buyer_order_status" jdbcType="INTEGER" property="buyerOrderStatus" />
<result column="seller_order_status" jdbcType="INTEGER" property="sellerOrderStatus" />
<result column="channel_no" jdbcType="VARCHAR" property="channelNo" />
<result column="attributes" jdbcType="INTEGER" property="attributes" />
</resultMap>
<sql id="Base_Column_List">
id, uid, order_code, seller_uid, client_type, payment, payment_type, is_cancel, amount,
ship_fee, status, create_time, update_time, buyer_order_status, seller_order_status, channel_no
ship_fee, status, create_time, update_time, buyer_order_status, seller_order_status, channel_no, attributes
</sql>
... ... @@ -137,12 +139,13 @@
client_type, payment, payment_type,
is_cancel, amount, ship_fee,
status, create_time, update_time,
buyer_order_status, seller_order_status, channel_no)
buyer_order_status, seller_order_status, channel_no, attributes)
values (#{uid,jdbcType=INTEGER}, #{orderCode,jdbcType=BIGINT}, #{sellerUid,jdbcType=INTEGER},
#{clientType,jdbcType=INTEGER}, #{payment,jdbcType=INTEGER}, #{paymentType,jdbcType=TINYINT},
#{isCancel,jdbcType=TINYINT}, #{amount,jdbcType=DECIMAL}, #{shipFee,jdbcType=DECIMAL},
#{status,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{updateTime,jdbcType=INTEGER},
#{buyerOrderStatus,jdbcType=INTEGER}, #{sellerOrderStatus,jdbcType=INTEGER}, #{channelNo,jdbcType=VARCHAR})
#{buyerOrderStatus,jdbcType=INTEGER}, #{sellerOrderStatus,jdbcType=INTEGER}, #{channelNo,jdbcType=VARCHAR},
#{attributes,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yohoufo.dal.order.model.BuyerOrder" useGeneratedKeys="true">
insert into buyer_order
... ... @@ -192,6 +195,10 @@
<if test="channelNo != null">
channel_no,
</if>
<if test="attributes != null">
attributes,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="uid != null">
... ... @@ -239,6 +246,9 @@
<if test="channelNo != null">
#{channelNo,jdbcType=VARCHAR},
</if>
<if test="attributes != null">
#{attributes,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yohoufo.dal.order.model.BuyerOrder">
... ...
... ... @@ -23,4 +23,8 @@ public interface MetaKey {
* 预支付成功记录的appPayRequest
*/
String APP_PAY_REQUEST = "app_pay_request";
/**
* 买家取消罚款
*/
String BUYER_CANCEL_PENALTY = "cancel_penalty";
}
... ...
package com.yohoufo.order.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Builder;
import java.math.BigDecimal;
/**
* Created by chao.chen on 2019/1/4.
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BuyerPenaltyCalResult {
private Integer uid;
private Long orderCode;
private BigDecimal orderActualAmount;
private BigDecimal penaltyAmount;
private BigDecimal leftAmount;
private BigDecimal penaltyRate;
}
... ...
package com.yohoufo.order.model.request;
import lombok.Data;
import java.math.BigDecimal;
/**
* 单笔转账 最核心的数据
* Created by chao.chen on 2019/1/4.
*/
@Data
public class TranseferCellNode {
/**
* 转账金额
*/
BigDecimal amount;
/**
* yoho uid
*/
Integer uid;
}
... ...
... ... @@ -14,6 +14,8 @@ import lombok.experimental.Builder;
@NoArgsConstructor
@ToString
public class TransferMoneyRequest {
private TranseferCellNode transeferCellNode;
private long buyerOrderCode;
... ...
package com.yohoufo.order.service.impl;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.order.bo.MerchantOrderAttachInfo;
import com.yohobuy.ufo.model.order.common.OrderStatus;
import com.yohobuy.ufo.model.order.common.SellerOrderStatus;
import com.yohobuy.ufo.model.order.common.SkupStatus;
import com.yohobuy.ufo.model.order.common.TabType;
import com.yohobuy.ufo.model.order.common.*;
import com.yohoufo.common.alarm.EventBusPublisher;
import com.yohoufo.common.alarm.SmsAlarmEvent;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.common.utils.BigDecimalHelper;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.order.BuyerOrderMapper;
import com.yohoufo.dal.order.OrderCouponMapper;
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
import com.yohoufo.dal.order.SellerOrderMapper;
import com.yohoufo.dal.order.*;
import com.yohoufo.dal.order.model.*;
import com.yohoufo.order.common.BillTradeStatus;
import com.yohoufo.order.common.RefundCase;
... ... @@ -22,7 +20,10 @@ import com.yohoufo.order.event.BillLogEvent;
import com.yohoufo.order.event.ErpBuyerOrderEvent;
import com.yohoufo.order.model.PayRefundBo;
import com.yohoufo.order.model.bo.CouponBo;
import com.yohoufo.order.model.dto.BuyerPenalty;
import com.yohoufo.order.model.dto.BuyerPenaltyCalResult;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.request.TranseferCellNode;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.service.IPaymentService;
import com.yohoufo.order.service.cache.CacheCleaner;
... ... @@ -40,6 +41,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
... ... @@ -83,6 +86,111 @@ public class BuyerOrderCancelService {
@Autowired
private SellerService sellerService;
@Autowired
private MetaConfigService metaConfigService;
@Autowired
private OrdersPayMapper ordersPayMapper;
/**
*
* @param buyerUid
* @param orderActualAmount 订单实付金额
* @return
*/
public BuyerPenaltyCalResult calBuyerPenalty(Integer buyerUid, Long orderCode, OrderStatus orderStatus,
BigDecimal orderActualAmount,
OrderAttributes orderAttributes
){
if (Objects.isNull(orderAttributes)){
logger.warn("in calBuyerPenalty order Attributes illegal buyerUid {} orderCode {} orderAttributes {}", buyerUid, orderCode, orderAttributes);
throw new UfoServiceException(501, "order Attributes illegal");
}
BuyerPenalty buyerPenalty = metaConfigService.getBuyerPenalty();
if (Objects.isNull(buyerPenalty)){
logger.warn("in calBuyerPenalty order buyerPenalty config illegal buyerUid {} orderCode {} orderAttributes {}", buyerUid, orderCode, orderAttributes);
throw new UfoServiceException(501, "buyerPenalty config missed");
}
BuyerPenalty.Fee mfee = null;
//common order
if (Objects.equals(orderAttributes.getCode(), OrderAttributes.COMMON_IN_STOCK.getCode())){
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(orderCode, buyerUid);
Integer payTime;
if(Objects.isNull(ordersPay) || Objects.isNull(payTime = ordersPay.getCreateTime())){
logger.warn("in calBuyerPenalty not exist paid record.orderCode {}", orderCode);
throw new ServiceException(ServiceError.ORDER_HAS_NOT_PAID);
}
int currentTime = DateUtil.getCurrentTimeSecond();
int diffTime = currentTime - payTime;
//before seller deliver
if (Objects.equals(orderStatus.getCode(), OrderStatus.BUYER_CANCEL_BEFORE_SELLER_DELIVER.getCode())){
Collection<BuyerPenalty.Fee> fees = buyerPenalty.getStockCaseMap().get("goodsInStock")
.getTriggerCaseMap().get("beforeSellerDeliver").getStagedCollection();
mfee = findByTimeCompared(fees, diffTime, orderCode);
}
//after seller deliver
if (Objects.equals(orderStatus.getCode(), OrderStatus.BUYER_CANCEL_BEFORE_DEPOT_RECEIVE.getCode())){
Collection<BuyerPenalty.Fee> fees = buyerPenalty.getStockCaseMap().get("goodsInStock")
.getTriggerCaseMap().get("beforeDepotReceive").getStagedCollection();
mfee = fees.iterator().next();
}
if (Objects.isNull(mfee)){
logger.warn("in calBuyerPenalty can not match feeRate.orderCode {} diffTime {} buyerPenalty {}",
orderCode, diffTime, buyerPenalty);
throw new UfoServiceException(501, "not match calFee");
}
}
//TODO quick deliver
BuyerPenaltyCalResult bpcr = calBuyerPenalty( mfee,buyerUid, orderCode, orderActualAmount);
return bpcr;
}
public BuyerPenaltyCalResult calBuyerPenalty(BuyerPenalty.Fee mfee,Integer buyerUid, Long orderCode,
BigDecimal orderActualAmount){
BigDecimal penaltyRate = mfee.getRate();
BigDecimal penaltyAmount = BigDecimalHelper.halfUp(orderActualAmount.multiply(penaltyRate));
BuyerPenalty.Range moneyRange = mfee.getMoneyRange();
if (penaltyAmount.compareTo(moneyRange.getMin()) < 0){
penaltyAmount = moneyRange.getMin();
}
BigDecimal leftAmount = orderActualAmount.subtract(penaltyAmount);
BuyerPenaltyCalResult bpcr = BuyerPenaltyCalResult.builder().uid(buyerUid).orderCode(orderCode)
.orderActualAmount(orderActualAmount).penaltyRate(penaltyRate)
.penaltyAmount(penaltyAmount).leftAmount(leftAmount).build();
logger.info("calBuyerPenalty mfee {} BuyerPenaltyCalResult {}", mfee, bpcr);
return bpcr;
}
private BuyerPenalty.Fee findByTimeCompared(Collection<BuyerPenalty.Fee> fees, int diffTime, Long orderCode){
BuyerPenalty.Fee mfee = null;
for(BuyerPenalty.Fee fee : fees){
BuyerPenalty.Range timeRange = fee.getTimeRange();
BigDecimal min = timeRange.getMin();
BigDecimal max = timeRange.getMax();
boolean isgt = false;
if (Objects.nonNull(min)){
isgt = diffTime > min.intValue();
}
//可以不设置最大值,没有就默认为无穷大,所以默认为满足小于最大值
boolean islt = true;
if (Objects.nonNull(max)){
islt = diffTime <= max.intValue();
}
if (isgt && islt){
mfee = fee;
logger.info("findByTimeCompared orderCode {} diffTime {} mfee {}", orderCode, diffTime, mfee);
break;
}
}
return mfee;
}
public void cancel(BeforeSellerDeliverEvent bsdEvent){
OrderDynamicConfig.BuyerCancelCompensateNode compensate = orderDynamicConfig.getBeforeSellerDeliverBCCN();
... ...
... ... @@ -93,9 +93,6 @@ public class PaymentServiceImpl implements IPaymentService {
AlipayOuyinService alipayService;
@Autowired
OrdersPayMapper ordersPayDao;
@Autowired
OrdersPayMapper ordersPayMapper;
@Autowired
... ... @@ -134,12 +131,9 @@ public class PaymentServiceImpl implements IPaymentService {
@Autowired
private BuyerCancelCompensateComputeHandler buyerCancelCompensateComputeHandler;
@Autowired
OrdersPrePayMapper ordersPrePayMapper;
/**
* 获取主场的订单service
* @param codeMeta
... ... @@ -347,7 +341,7 @@ public class PaymentServiceImpl implements IPaymentService {
attach.put("order_type", orderType);
ordersPay.setAttach(attach.toJSONString());
ordersPayDao.insertSelective(ordersPay);
ordersPayMapper.insertSelective(ordersPay);
return ordersPay;
}
... ... @@ -582,10 +576,7 @@ public class PaymentServiceImpl implements IPaymentService {
record.setTradeStatus(0);
record.setCreateTime(now);
logger.info("transferMon参数检查成功!");
logger.info("transferMon插入初始化转账信息成功,接下来计算费用");
logger.info("transferMon参数检查成功!插入初始化转账信息成功,接下来计算费用");
// 算费
BigDecimal transferAmount = calcTransferAmount(sellerOrder.getUid(), sellerOrder.getSkup(), transferCase);
logger.info("transferMon计算费用结果为 {}", transferAmount);
... ...
package com.yohoufo.order.service;
import com.alibaba.fastjson.JSONObject;
import com.yohobuy.ufo.model.order.common.OrderAttributes;
import com.yohobuy.ufo.model.order.common.OrderStatus;
import com.yohoufo.dal.order.BuyerOrderMapper;
import com.yohoufo.dal.order.model.BuyerOrder;
import com.yohoufo.order.BaseWebTest;
import com.yohoufo.order.model.dto.BuyerPenaltyCalResult;
import com.yohoufo.order.service.impl.BuyerOrderCancelService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.math.BigDecimal;
/**
* Created by chao.chen on 2019/1/4.
*/
public class BuyerOrderCancelServiceTest extends BaseWebTest {
@Autowired
private BuyerOrderCancelService buyerOrderCancelService;
@Autowired
private BuyerOrderMapper buyerOrderMapper;
@Test
public void testCalBuyerPenalty(){
Long orderCode = 9832641421313L;
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
Integer buyerUid = buyerOrder.getUid();
OrderStatus orderStatus = OrderStatus.getOrderStatus(buyerOrder.getStatus());
BigDecimal orderActualAmount = buyerOrder.getAmount().subtract(buyerOrder.getShipFee());
OrderAttributes orderAttributes = OrderAttributes.getOrderAttributes(buyerOrder.getAttributes());
BuyerPenaltyCalResult bpcr = buyerOrderCancelService.calBuyerPenalty(buyerUid, orderCode, orderStatus, orderActualAmount, orderAttributes);
System.out.println(JSONObject.toJSONString(bpcr));
}
}
... ...