嗨森逛服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hai-server/hai-service/src/main/java/com/hai/service/impl/HighOrderServiceImpl.java

277 lines
12 KiB

package com.hai.service.impl;
import com.hai.common.QRCodeGenerator;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.IDGenerator;
import com.hai.dao.HighChildOrderMapper;
import com.hai.dao.HighOrderMapper;
import com.hai.dao.HighOrderMapperExt;
import com.hai.dao.HighUserCouponMapper;
import com.hai.entity.*;
import com.hai.model.HighCouponHandselModel;
import com.hai.model.HighCouponModel;
import com.hai.service.*;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/3/26 23:06
*/
@Service("highOrderService")
public class HighOrderServiceImpl implements HighOrderService {
@Resource
private HighOrderMapper highOrderMapper;
@Resource
private HighOrderMapperExt highOrderMapperExt;
@Resource
private HighChildOrderMapper highChildOrderMapper;
@Resource
private HighCouponCodeService highCouponCodeService;
@Resource
private HighCouponService highCouponService;
@Resource
private HighCouponHandselService highCouponHandselService;
@Resource
private HighUserService highUserService;
@Resource
private HighUserCouponMapper highUserCouponMapper;
@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public void insertOrder(HighOrder highOrder) throws Exception {
highOrderMapper.insert(highOrder);
for (int i = 0; i < highOrder.getHighChildOrderList().size();i++) {
HighChildOrder childOrder = highOrder.getHighChildOrderList().get(i);
childOrder.setOrderId(highOrder.getId());
highChildOrderMapper.insert(childOrder);
// 商品类型 1:卡卷
if (childOrder.getGoodsType() == 1) {
// 查询待销售
List<HighCouponCode> list = highCouponCodeService.getNoSaleCode(childOrder.getGoodsId());
if (list == null || list.size() == 0) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_STOCK_INSUFFICIENT, "");
}
list.get(0).setChildOrderId(childOrder.getId());
list.get(0).setStatus(99); // 状态:1.待销售 2.未使用 3.已使用 99.预支付
highCouponCodeService.updateCouponCode(list.get(0));
childOrder.setExt1(list.get(0).getExt1());
highChildOrderMapper.updateByPrimaryKey(childOrder);
if(childOrder.getGiveawayType() == false) {
// 查看是否需要赠送卡卷
List<HighCouponHandselModel> handselListByCoupon = highCouponHandselService.getHandselListByCoupon(childOrder.getGoodsId());
if (handselListByCoupon != null && handselListByCoupon.size() > 0) {
for (HighCouponHandsel highCouponHandsel : handselListByCoupon) {
// 查询卡卷信息
HighCouponModel coupon = highCouponService.getCouponById(highCouponHandsel.getHandselCouponId());
if (coupon == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, "");
}
// 查询赠送卡卷 是否有库存,没有就不赠送
if (highCouponCodeService.getStockCountByCoupon(coupon.getId()) > 0) {
HighChildOrder highChildOrder = new HighChildOrder();
highChildOrder.setOrderId(highOrder.getId());
highChildOrder.setGoodsType(1);
highChildOrder.setGoodsId(coupon.getId());
highChildOrder.setGoodsName(coupon.getCouponName());
highChildOrder.setGoodsImg(coupon.getCouponImg());
highChildOrder.setGoodsSpecName("默认");
highChildOrder.setGoodsPrice(new BigDecimal(0));
highChildOrder.setSaleCount(1);
highChildOrder.setTotalPrice(new BigDecimal(0));
highChildOrder.setGiveawayType(true); // 是否是赠品 0:否 1:是
highChildOrder.setChildOrdeStatus(1); // 1 待支付 2 已支付 3.已完成 4. 已退款 5.已取消
highChildOrder.setPraiseStatus(0);
highOrder.getHighChildOrderList().add(highChildOrder);
}
}
}
}
}
}
}
@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public void goldPayOrder(Long userId, Long orderId) {
HighOrder highOrder = getOrderById(orderId);
if(highOrder == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_ORDER, "");
}
// 金币 1:100
Integer goldNum = new BigDecimal(highOrder.getTotalPrice().toString()).multiply(new BigDecimal("100")).intValue();
highUserService.goldHandle(userId, goldNum, 2, 2, highOrder.getId());
highOrder.setPayTime(new Date()); // 支付时间
highOrder.setPayModel(1); // 支付模式:1 金币,2 第三方平台,3 混合
highOrder.setPayType(3); // 支付方式: 1:支付宝 2:微信 3:金币
highOrder.setOrderStatus(2); // 订单状态:1 待支付 2 已支付 3.已完成 4. 已退款 5.已取消
for (HighChildOrder highChildOrder : highOrder.getHighChildOrderList()) {
highChildOrder.setChildOrdeStatus(2); // 子订单状态:1 待支付 2 已支付 3.已完成 4. 已退款 5.已取消
// 商品类型 商品类型 1:卡卷 2:金币充值
if (highChildOrder.getGoodsType() == 1) {
HighCouponCode code = highCouponCodeService.getCouponCodeByOrderId(highChildOrder.getId());
code.setStatus(2); // 状态:1.待销售 2.未使用 3.已使用 99.预支付
code.setReceiveTime(new Date());
highCouponCodeService.updateCouponCode(code);
// 卡卷关联用户
HighUserCoupon highUserCoupon = new HighUserCoupon();
highUserCoupon.setMerchantId(code.getMerchantId());
highUserCoupon.setCouponId(code.getCouponId());
highUserCoupon.setUserId(highOrder.getMemId());
highUserCoupon.setCouponCodeId(code.getId());
highUserCoupon.setCreateTime(new Date());
highUserCoupon.setUseEndTime(code.getUseEndTime());
highUserCoupon.setStatus(1); // 状态 0:已过期 1:未使用 2:已使用
highUserCouponMapper.insert(highUserCoupon);
}
}
updateOrder(highOrder);
}
@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public void updateOrder(HighOrder highOrder) {
highOrderMapper.updateByPrimaryKey(highOrder);
for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
highChildOrderMapper.updateByPrimaryKey(childOrder);
}
}
@Override
public Integer getUndoneChildOrder(Long orderId) {
HighOrder order = getOrderById(orderId);
Integer count = 0;
for (HighChildOrder childOrder : order.getHighChildOrderList()) {
if (childOrder.getChildOrdeStatus() != 3) {
count++;
}
}
return count;
}
@Override
public HighOrder getOrderById(Long id) {
HighOrder order = highOrderMapper.selectByPrimaryKey(id);
if (order == null) {
return null;
}
HighChildOrderExample example = new HighChildOrderExample();
example.createCriteria().andOrderIdEqualTo(order.getId());
order.setHighChildOrderList(highChildOrderMapper.selectByExample(example));
return order;
}
@Override
public HighOrder getOrderByOrderNo(String orderNo) {
HighOrderExample example = new HighOrderExample();
example.createCriteria().andOrderNoEqualTo(orderNo);
List<HighOrder> list = highOrderMapper.selectByExample(example);
if (list != null && list.size() > 0) {
return getOrderById(list.get(0).getId());
}
return null;
}
@Override
public List<HighOrder> getOrderList(Map<String, Object> map) {
HighOrderExample example = new HighOrderExample();
HighOrderExample.Criteria criteria = example.createCriteria();
if (MapUtils.getLong(map, "memId") != null) {
criteria.andMemIdEqualTo(MapUtils.getLong(map, "memId"));
}
if (MapUtils.getInteger(map, "status") != null) {
criteria.andOrderStatusEqualTo(MapUtils.getInteger(map, "status"));
}
if (MapUtils.getString(map, "orderNo") != null) {
criteria.andOrderNoEqualTo(MapUtils.getString(map, "orderNo"));
}
example.setOrderByClause("create_time desc");
return highOrderMapper.selectByExample(example);
}
@Override
public List<HighOrder> getCloseOrder() {
return highOrderMapperExt.getCloseOrder();
}
@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public void childOrderComplete(Long childOrderId) {
HighChildOrder childOrder = highChildOrderMapper.selectByPrimaryKey(childOrderId);
if (childOrder == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
childOrder.setChildOrdeStatus(3);
highChildOrderMapper.updateByPrimaryKey(childOrder);
// 查询未完成的子订单数量
Integer count = getUndoneChildOrder(childOrder.getOrderId());
if (count == 0) {
HighOrder order = getOrderById(childOrder.getOrderId());
order.setOrderStatus(3);
order.setFinishTime(new Date());
highOrderMapper.updateByPrimaryKey(order);
}
}
@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public void cancelOrder(Long orderId) {
HighOrder order = getOrderById(orderId);
if (order != null) {
order.setOrderStatus(5); // 订单状态:1 待支付 2 已支付 3.已完成 4. 已退款 5.已取消
order.setCancelTime(new Date());
for (HighChildOrder highChildOrder : order.getHighChildOrderList()) {
// 商品类型 1:卡卷 2:金币充值
if (highChildOrder.getGoodsType() == 1) {
// 查询卡卷详情
HighCouponCode couponCode = highCouponCodeService.getCouponCodeByOrderId(highChildOrder.getId());
couponCode.setChildOrderId(null);
couponCode.setStatus(1); // 状态:1.待销售 2.未使用 3.已使用 99.预支付
highCouponCodeService.updateCouponCode(couponCode);
}
highChildOrder.setChildOrdeStatus(5); // 子订单状态:1 待支付 2 已支付 3.已完成 4. 已退款 5.已取消
}
updateOrder(order);
}
}
}