Просмотр исходного кода

[订单][流程]:有价券与免费券分开

release_toaliyun_real
hanxueda 7 лет назад
Родитель
Сommit
f6ddac5990
6 измененных файлов: 336 добавлений и 14 удалений
  1. +16
    -1
      mallinkCApi/src/main/java/com/iformall/controller/WxOrderController.java
  2. +23
    -1
      mallinkCApi/src/main/java/com/iformall/controller/WxPayOrderController.java
  3. +8
    -1
      mallinkService/src/main/java/com/iformall/service/WxOrderService.java
  4. +7
    -0
      mallinkService/src/main/java/com/iformall/service/WxPayOrderService.java
  5. +251
    -6
      mallinkService/src/main/java/com/iformall/service/impl/WxOrderServiceImpl.java
  6. +31
    -5
      mallinkService/src/main/java/com/iformall/service/impl/WxPayOrderServiceImpl.java

+ 16
- 1
mallinkCApi/src/main/java/com/iformall/controller/WxOrderController.java Просмотреть файл

@@ -5,6 +5,7 @@ import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.po.WxCouponChannel;
import com.iformall.domain.po.WxOrder;
import com.iformall.domain.vo.WxOrderCouponPressVo;
@@ -12,6 +13,7 @@ import com.iformall.domain.vo.WxOrderCouponVo;
import com.iformall.enums.*;
import com.iformall.exception.MallinkException;
import com.iformall.service.WxCouponChannelService;
import com.iformall.service.WxCouponService;
import com.iformall.service.WxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -38,6 +40,9 @@ public class WxOrderController extends BaseController {
@Autowired
private WxOrderService wxOrderService;

@Autowired
private WxCouponService wxCouponService;

@ApiOperation(value = "免费领取", notes = "{\"couponChannelId\":\"String\",\"couponId\":\"String\",\"formId\":\"String\"}")
@PostMapping("freeCoupon")
public ResultData freeCoupon(@RequestBody Map<String, String> paramMap) {
@@ -166,7 +171,17 @@ public class WxOrderController extends BaseController {

WxCUser user = getUser();
try {
WxOrder order = wxOrderService.saveCouponOrder(user, couponChannelId, couponId, isPress, orderGroupId, formId);
//WxOrder order = wxOrderService.saveCouponOrder(user, couponChannelId, couponId, isPress, orderGroupId, formId);
WxOrder order = null;
WxCoupon coupon = wxCouponService.getById(couponId);
if (coupon == null) {
return new ResultData(ErrorCode.COUPON_IS_EMPTY);
}
if (wxOrderService.checkCouponIsFree(user, coupon)) {
order = wxOrderService.saveFreeOrderForCoupon(user, coupon, couponChannelId, formId);
} else {
order = wxOrderService.saveNoFreeOrderForCoupon(user, coupon, couponChannelId, isPress, orderGroupId, formId);
}
return new ResultData(order);
} catch (MallinkException e) {
logger.error(e.getMessage());


+ 23
- 1
mallinkCApi/src/main/java/com/iformall/controller/WxPayOrderController.java Просмотреть файл

@@ -113,7 +113,7 @@ public class WxPayOrderController extends BaseController {
WxOrder order = null;

try {
if(status.equals(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()) && !payOrderIdStr.equals("0")) {
/*if(status.equals(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()) && !payOrderIdStr.equals("0")) {
// 前端提示支付成功, 不作为支付成功标志,只检查支付订单状态
// 有价券不走update, callback更新
payOrder = wxPayOrderService.getById(payOrderId);
@@ -131,6 +131,28 @@ public class WxPayOrderController extends BaseController {
} else if (status.equals(EnumPayStatus.PAY_STATUS_FAIL.getCode())) {
wxPayOrderService.handlePayOrderStatusUpdate(payOrder, 2);
}
}*/
//支付成功
if (status.equals(EnumPayStatus.PAY_STATUS_SUCCESS.getCode())) {
//有价券
if(!payOrderIdStr.equals("0")){
// 前端提示支付成功, 不作为支付成功标志,只检查支付订单状态
// 有价券不走update, callback更新
payOrder = wxPayOrderService.getById(payOrderId);
if(payOrder.getPayOrderStatus().equals(EnumPayStatus.PAY_STATUS_SUCCESS.getCode())) {
order = wxOrderService.getById(orderId);
return new ResultData(Result.SUCCESS, "支付状态更新成功", order);
} else {
return new ResultData(ErrorCode.PAY_ORDER_ERROR, "支付未状态,请等待!!");
}
} else {
//免费券
//wxPayOrderService.handlePayOrderStatusUpdate(payOrder, EnumPayStatus.PAY_STATUS_SUCCESS.getCode());
wxPayOrderService.handlePayOrderStatusUpdateFree(payOrder);
}
} else if (status.equals(EnumPayStatus.PAY_STATUS_FAIL.getCode())){
//支付失败
wxPayOrderService.handlePayOrderStatusUpdate(payOrder, EnumPayStatus.PAY_STATUS_FAIL.getCode());
}
return new ResultData(Result.SUCCESS, "支付状态更新成功");
} catch (MallinkException e) {


+ 8
- 1
mallinkService/src/main/java/com/iformall/service/WxOrderService.java Просмотреть файл

@@ -83,6 +83,13 @@ public interface WxOrderService {
*/
int couponOrderSuccess(WxOrder updateOrder);

/**
* orderSuccess 订单已支付(免费券)
* @param updateOrder
* @return
*/
int couponOrderSuccessFree(WxOrder updateOrder);

/**
* microPayOrderSuccess 刷卡支付成功
* @param updateOrder
@@ -173,7 +180,7 @@ public interface WxOrderService {
boolean checkCouponIsFree(WxCUser user, WxCoupon coupon);

// 2. 创建免费订单, 领取 couponOrder
WxOrder saveFreeOrderForCoupon(WxCUser user, WxCoupon coupon, Long couponChannelId, boolean isPress, Long orderGroupId, String formId);
WxOrder saveFreeOrderForCoupon(WxCUser user, WxCoupon coupon, Long couponChannelId, String formId);

// 3. 创建有价订单
WxOrder saveNoFreeOrderForCoupon(WxCUser user, WxCoupon coupon, Long couponChannelId, boolean isPress, Long orderGroupId, String formId);


+ 7
- 0
mallinkService/src/main/java/com/iformall/service/WxPayOrderService.java Просмотреть файл

@@ -82,6 +82,13 @@ public interface WxPayOrderService {
*/
void handlePayOrderStatusUpdate(WxPayOrder record, int status);

/**
* 支付状态处理(免费券)
*
* @param record
*/
void handlePayOrderStatusUpdateFree(WxPayOrder record);

/**
* 保存或更新实体
*


+ 251
- 6
mallinkService/src/main/java/com/iformall/service/impl/WxOrderServiceImpl.java Просмотреть файл

@@ -944,8 +944,40 @@ public class WxOrderServiceImpl implements WxOrderService {
} catch (Exception e) {
logger.error("订单发券:" + e.getMessage());
}
// 增加成长值和积分
addScoreAndCredit(updateOrder, coupon, user);
return ret;
}

@Override
public int couponOrderSuccessFree(WxOrder updateOrder) {
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(updateOrder.getProductId());
if (coupon == null) {
logger.error("券不存在, couponId: " + updateOrder.getProductId());
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY);
}
if (coupon.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) {
logger.error("券已下架, couponId: " + updateOrder.getProductId());
throw new MallinkException(ErrorCode.COUPON_IS_TAKE_OFF);
}
WxCUser user = wxCUserMapper.selectByPrimaryKey(updateOrder.getcUserId());
if (user == null) {
logger.error("用户不存在, userId: " + updateOrder.getcUserId());
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
}
int ret = 0;
// 发券couponOrder
try {
wxCouponSendService.sendCouponToUser(EnumCouponSendSendType.C_ORDER,updateOrder);
} catch (Exception e) {
logger.error("订单发券:" + e.getMessage());
}
// 增加成长值和积分
addScoreAndCredit(updateOrder, coupon, user);
return ret;
}

private void addScoreAndCredit(WxOrder updateOrder, WxCoupon coupon, WxCUser user) {
// 成长值 计算
try {
wxScoreRulesService.addScore2(EnumScoreType.CONSUMPTION, updateOrder, coupon.getBusiness());
@@ -972,7 +1004,6 @@ public class WxOrderServiceImpl implements WxOrderService {
} catch (Exception e) {
logger.error("积分值:" + e.getMessage());
}
return ret;
}

private void setMerchantId(WxCoupon coupon, WxCreditHistory creditHistory) {
@@ -1391,25 +1422,239 @@ public class WxOrderServiceImpl implements WxOrderService {

@Override
public boolean checkCouponIsFree(WxCUser user, WxCoupon coupon) {
return false;
// 检查用户
if (user == null) {
logger.error("用户不存在");
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
}
// 检查券
if (coupon == null) {
logger.error("券不存在");
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY);
}
if (coupon.getStatus().equals(EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode())) {
logger.error("券已下架, couponId: " + coupon.getId());
throw new MallinkException(ErrorCode.COUPON_ORDER_IS_INVALID);
}
if (!isCouponMerchantValid(coupon.getId())) {
logger.error("商户不存在, couponId: " + coupon.getId());
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND);
}
return coupon.getSalePrice() == 0;
}

@Override
public WxOrder saveFreeOrderForCoupon(WxCUser user, WxCoupon coupon, Long couponChannelId, boolean isPress, Long orderGroupId, String formId) {
public WxOrder saveFreeOrderForCoupon(WxCUser user, WxCoupon coupon, Long couponChannelId, String formId) {
// 1. check user info and coupon info
// 2. 减库存
// 3. save order
// 4. 积分券 -- 扣减积分
// 10. coupon order
// 11. 生成完成后一系列操作
return null;
String couponIdStr = String.valueOf(coupon.getId());
// 减库存操作
stockReduce(user, coupon, couponIdStr);
Date curr = new Date();
// body
String bodyStr = "";
try {
bodyStr = Utility.substring(coupon.getTitle(), Math.min(62, coupon.getTitle().getBytes().length), "utf-8");
} catch (Exception e) {
logger.error("body:" + e.getMessage());
bodyStr = /*wxMerchant.getName() + */ "-" + EnumCouponType.getEnum(coupon.getType());
}
final IdWorker idWorker = IdWorker.get();
Long orderNumber = idWorker.nextId();
WxOrder record = new WxOrder();
record.setId(orderNumber);
record.setTenantId(user.getTenantId());
record.setOrderNumber(orderNumber);
record.setCouponChannelId(couponChannelId);
record.setProductId(coupon.getId());
//当积分券或者积分停车券时 则进行积分支付
if (coupon.getType() == EnumCouponType.COUPON_CREDIT.getCode() || coupon.getType() == EnumCouponType.COUPON_CREDIT_PARK.getCode()) {
//若用户剩余积分比积分售价低则不能支付
if (user.getCredit() == null || user.getCredit() < coupon.getCreditPrice()) {
throw new MallinkException(ErrorCode.CREDIT_NOT_ENOUGH);
}
//-------此处为【积分支付】记录增加积分操作-------
WxCreditHistory creditHistory = new WxCreditHistory();
//记录操作人类型 操作人id
if (user.getOperatorType() == EnumUserType.MALLUSER.getCode()) {
creditHistory.setOperatorType(EnumUserType.MALLUSER.getCode());
creditHistory.setOperatorId(user.getOperatorId());
} else {
creditHistory.setOperatorType(EnumUserType.CUSER.getCode());
creditHistory.setOperatorId(user.getId());
}
creditHistory.setCreditNum(coupon.getCreditPrice());
creditHistory.setCUserId(user.getId());
creditHistory.setTenantId(user.getTenantId());
creditHistory.setCreditType(EnumScoreType.CHANGE_CREDIT.getCode());
creditHistory.setCouponId(coupon.getId());
//如果券与商户一对一 则直接将消费商户更新为此商户 若一对多 则消费商户显示多商户
setMerchantId(coupon, creditHistory);
wxCreditHistoryService.saveOrUpdate(creditHistory);
record.setType(EnumOrderType.CREDIT.getCode());
} else {
record.setType(EnumOrderType.COUPON.getCode());
}
record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode());
record.setcUserId(user.getId());
record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode());
record.setPayment(0);
record.setDetail(bodyStr);
record.setCreateDate(curr);
record.setUpdateDate(curr);
record.setOrderGroupId(0L);
record.setFormId(formId);
try {
// 保存订单
wxOrderMapper.insertSelective(record);
} catch (RuntimeException e) {
// 库存恢复
stockBack(record);
logger.error("保存订单:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR);
}
// 创建couponOrder
try {
createCouponOrder(user, record, coupon);
} catch (Exception e) {
logger.error("保存订单:" + e.getMessage());
// 库存恢复
stockBack(record);
throw new MallinkException(ErrorCode.COUPON_ORDER_SAVE_ERR);
}
return record;
}

@Override
public WxOrder saveNoFreeOrderForCoupon(WxCUser user, WxCoupon coupon, Long couponChannelId, boolean isPress, Long orderGroupId, String formId) {
// 1. check user info and coupon info
// 2. save order
return null;
// 拼团
// 拼团
if (orderGroupId != null && !orderGroupId.equals(0L)) {
WxOrderGroup group = wxOrderGroupMapper.selectByPrimaryKey(orderGroupId);
if (group.getStatus().equals(EnumOrderStatus.ORDER_STATUS_COOPERATING_COMPLETE.getCode())) {
logger.error("下订单拼团人数已满>>>" + orderGroupId);
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FINISHED);
}
WxOrder order = new WxOrder();
order.setOrderGroupId(group.getId());
order.setCUserId(user.getId());
order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode());
long count = wxOrderMapper.selectCount(order);
if (count > 0) {
logger.error("重复的拼团>>>" + orderGroupId);
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_UNPAID);
}
}
String couponIdStr = String.valueOf(coupon.getId());
// 减库存操作
stockReduce(user, coupon, couponIdStr);
Date curr = new Date();
int payment = coupon.getSalePrice();
Integer orderStatus = EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode();
if (isPress || coupon.getType().equals(EnumCouponType.COUPON_PRESS.getCode())) {
// 砍价券 初始是全价即面额
payment = coupon.getPrice();
}
if (coupon.getType().equals(EnumCouponType.COUPON_GROUP.getCode())) {
if (orderGroupId == null) {
// 拼团券 初始是全价即面额
payment = coupon.getPrice();
} else {
orderStatus = EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode();
}
}
// body
// tenant_id + merchant_id + title + subtitle
String bodyStr = "";
try {
bodyStr = Utility.substring(coupon.getTitle(), Math.min(62, coupon.getTitle().getBytes().length), "utf-8");
} catch (Exception e) {
logger.error("body:" + e.getMessage());
bodyStr = /*wxMerchant.getName() + */ "-" + EnumCouponType.getEnum(coupon.getType());
}
final IdWorker idWorker = IdWorker.get();
Long orderNumber = idWorker.nextId();
WxOrder record = new WxOrder();
record.setId(orderNumber);
record.setTenantId(user.getTenantId());
record.setOrderNumber(orderNumber);
record.setCouponChannelId(couponChannelId);
record.setProductId(coupon.getId());
record.setType(EnumOrderType.COUPON.getCode());
record.setOrderStatus(orderStatus);
record.setcUserId(user.getId());
record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode());
record.setPayment(payment);
record.setDetail(bodyStr);
record.setCreateDate(curr);
record.setUpdateDate(curr);
record.setOrderGroupId(orderGroupId);
record.setFormId(formId);
if (isPress) {
// 初始砍价信息
record.setPressCurrentNum(0);
// 砍价券过期时间取券的过期时间和砍价限制时间中时间小的那个
Date pressEndDate = DateUtils.getHourTimeAfter(coupon.getPressLimitHours(), new Date());
if(pressEndDate.after(coupon.getValidEndDate()))
pressEndDate = coupon.getValidEndDate();
record.setPressEndDate(pressEndDate);
record.setPressCurrentValue(coupon.getPrice() - coupon.getSalePrice());
}
try {
// 保存订单
wxOrderMapper.insertSelective(record);
} catch (RuntimeException e) {
// 库存恢复
stockBack(record);
logger.error("保存订单:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR);
}
if (isPress) {
// 初始砍价
// 添加 wx_order_press
int total = coupon.getPrice() - coupon.getSalePrice();
int left_total = total;
WxOrderPress orderPress = new WxOrderPress();
orderPress.setId(idWorker.nextId());
orderPress.setOrderId(orderNumber);
orderPress.setUserId(user.getId());
orderPress.setCreateDate(new Date());
orderPress.setFirst(EnumOrderPressType.FIRST.getCode());
int lPressValue = PressUtils.stateLessPressValue(total, left_total, coupon.getPressLimitNum(), 0);
orderPress.setPressValue(lPressValue);
try {
// 保存订单
wxOrderPressMapper.insertSelective(orderPress);
} catch (RuntimeException e) {
// 库存恢复
stockBack(record);
logger.error("保存砍价记录Err:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR);
}
// 更新砍价信息
WxOrder orderUpdatePress = new WxOrder();
orderUpdatePress.setId(record.getId());
orderUpdatePress.setPressCurrentNum(1);
orderUpdatePress.setPressCurrentValue(left_total - orderPress.getPressValue());
orderUpdatePress.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PRESSING.getCode());
orderUpdatePress.setUpdateDate(new Date());
try {
// 保存订单
wxOrderMapper.updateByPrimaryKeySelective(orderUpdatePress);
} catch (RuntimeException e) {
// 库存恢复
stockBack(record);
logger.error("更新订单记录Err:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR);
}
}
return record;
}


}

+ 31
- 5
mallinkService/src/main/java/com/iformall/service/impl/WxPayOrderServiceImpl.java Просмотреть файл

@@ -1382,13 +1382,11 @@ public class WxPayOrderServiceImpl implements WxPayOrderService {
}
}

Date currentDate = new Date();
if (record.getId() > 0) {
// 有价券
// 1. get appinfo
WxAppinfo appInfo = wxAppinfoService.getCAppInfo(order.getTenantId());

if (2 == status) {
if (EnumPayStatus.PAY_STATUS_FAIL.getCode() == status) {
// 1. get appinfo
WxAppinfo appInfo = wxAppinfoService.getCAppInfo(order.getTenantId());
// 前端支付取消,
// 回调未返回,主动检查支付订单状态
try {
@@ -1436,6 +1434,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService {
// 修改订单状态
if (record.getPayOrderStatus() == EnumPayStatus.PAY_STATUS_SUCCESS.getCode()) {
try {

int _count = wxOrderService.couponOrderSuccess(order);
if (_count > 1) {
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
@@ -1470,6 +1469,33 @@ public class WxPayOrderServiceImpl implements WxPayOrderService {
*/
}

@Override
public void handlePayOrderStatusUpdateFree(WxPayOrder record) {
//检查数据合理性
WxOrder order = wxOrderMapper.selectByPrimaryKey(record.getOrderId());
if (order == null) {
logger.error("pay handle, order " + record.getOrderId() + " not found , payOrderid : " + record.getPayOrderNo());
throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND);
}
if (order.getType().equals(EnumOrderType.COUPON.getCode())) {
WxCUser user = wxCUserMapper.selectByPrimaryKey(order.getcUserId());
if (user == null) {
logger.error("pay handle, order " + record.getOrderId() + " not found , payOrderid : " + record.getPayOrderNo());
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
}
}
// 修改订单状态
try {
int _count = wxOrderService.couponOrderSuccessFree(order);
if (_count > 1) {
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
}
} catch (Exception e) {
logger.error(e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单更新");
}
}

private WxAppinfo getWxAppinfo(Integer orderType, String tenentId) {
WxAppinfo appInfo = null;
WxAppinfo appinfoQ = new WxAppinfo();


Загрузка…
Отмена
Сохранить