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.*; import com.hai.entity.*; import com.hai.model.HighCouponHandselModel; import com.hai.model.HighCouponModel; import com.hai.model.HighOrderData; 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; @Resource private HighDiscountUserRelMapper highDiscountUserRelMapper; @Resource private HighDiscountUserRelService highDiscountUserRelService; @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void insertOrder(HighOrder highOrder) throws Exception { highOrderMapper.insert(highOrder); // 使用优惠券 if (highOrder.getMemDiscountId() != null) { HighDiscountUserRel discountUserRel = highDiscountUserRelService.getRelById(highOrder.getMemDiscountId()); discountUserRel.setUseTime(new Date()); // 使用时间 discountUserRel.setStatus(2); //状态 0:已过期 1:未使用 2:已使用 highDiscountUserRelService.updateDiscountUserRel(discountUserRel); } 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 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()) { // 查看是否需要赠送卡卷 List handselListByCoupon = highCouponHandselService.getHandselListByCoupon(childOrder.getGoodsId()); if (handselListByCoupon != null && handselListByCoupon.size() > 0) { for (HighCouponHandsel highCouponHandsel : handselListByCoupon) { // 查询卡卷信息 HighCoupon 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.setPayGold(goldNum); 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); HighCoupon coupon = highCouponService.getCouponById(highChildOrder.getGoodsId()); // 卡卷关联用户 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.setQrCodeImg(code.getExt1()); // 计算使用有效期 Calendar userEndTime = Calendar.getInstance(); userEndTime.setTime(new Date()); userEndTime.set(Calendar.HOUR_OF_DAY, 23); userEndTime.set(Calendar.MINUTE, 59); userEndTime.set(Calendar.SECOND, 59); userEndTime.add(Calendar.DATE, coupon.getRecycleDay()); if (userEndTime.getTime().compareTo(code.getUseEndTime()) == 1) { highUserCoupon.setUseEndTime(code.getUseEndTime()); } else { highUserCoupon.setUseEndTime(userEndTime.getTime()); } 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 HighChildOrder getChildOrderById(Long childOrderId) { return highChildOrderMapper.selectByPrimaryKey(childOrderId); } @Override public List getChildOrderByOrder(Long orderId) { HighChildOrderExample example = new HighChildOrderExample(); example.createCriteria().andOrderIdEqualTo(orderId); return highChildOrderMapper.selectByExample(example); } @Override public List getChildOrderList(Map map) { HighChildOrderExample example = new HighChildOrderExample(); HighChildOrderExample.Criteria criteria = example.createCriteria(); if (MapUtils.getLong(map, "memId") != null) { criteria.andOrderIdEqualTo(MapUtils.getLong(map, "memId")); } if (MapUtils.getLong(map, "childOrdeStatus") != null) { criteria.andChildOrdeStatusEqualTo(MapUtils.getInteger(map, "childOrdeStatus")); } if (MapUtils.getString(map, "childOrdeStatusList") != null) { String[] childOrdeStatusLists = MapUtils.getString(map, "childOrdeStatusList").split(","); List integerList = new ArrayList<>(); for (String status :childOrdeStatusLists) { integerList.add(Integer.parseInt(status)); } criteria.andChildOrdeStatusIn(integerList); } if (MapUtils.getLong(map, "goodsType") != null) { criteria.andGoodsTypeEqualTo(MapUtils.getInteger(map, "goodsType")); } if (MapUtils.getLong(map, "goodsId") != null) { criteria.andGoodsIdEqualTo(MapUtils.getLong(map, "goodsId")); } if (MapUtils.getLong(map, "giveawayType") != null) { criteria.andGiveawayTypeEqualTo(MapUtils.getBoolean(map, "giveawayType")); } return highChildOrderMapper.selectByExample(example); } @Override public HighOrder getOrderById(Long id) { HighOrder order = highOrderMapper.selectByPrimaryKey(id); if (order == null) { return null; } order.setHighChildOrderList(getChildOrderByOrder(order.getId())); return order; } @Override public HighOrder getOrderByOrderNo(String orderNo) { HighOrderExample example = new HighOrderExample(); example.createCriteria().andOrderNoEqualTo(orderNo); List list = highOrderMapper.selectByExample(example); if (list != null && list.size() > 0) { return getOrderById(list.get(0).getId()); } return null; } @Override public List getOrderBList(Map map) { return highOrderMapperExt.selectOrderDataList(map); } @Override public List getOrderList(Map 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")); } if (MapUtils.getString(map, "phone") != null) { criteria.andMemPhoneEqualTo(MapUtils.getString(map, "phone")); } example.setOrderByClause("create_time desc"); List list = highOrderMapper.selectByExample(example); if (list.size() > 0) { for (HighOrder order : list) { order.setHighChildOrderList(getChildOrderByOrder(order.getId())); } } return list; } @Override public List 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()); if (couponCode != null) { couponCode.setChildOrderId(null); couponCode.setStatus(1); // 状态:1.待销售 2.未使用 3.已使用 99.预支付 highCouponCodeService.updateCouponCode(couponCode); } } highChildOrder.setChildOrdeStatus(5); // 子订单状态:1 待支付 2 已支付 3.已完成 4. 已退款 5.已取消 } if (order.getMemDiscountId() != null) { HighDiscountUserRel rel = highDiscountUserRelService.getRelById(order.getMemDiscountId()); if (rel != null) { rel.setStatus(1); // 状态 0:已过期 1:未使用 2:已使用 rel.setUseTime(new Date()); highDiscountUserRelService.updateDiscountUserRel(rel); } } updateOrder(order); } } @Override public Integer countOrderByUserId(Long memId , Integer status) { HighOrderExample example = new HighOrderExample(); HighOrderExample.Criteria criteria = example.createCriteria(); criteria.andMemIdEqualTo(memId); criteria.andOrderStatusEqualTo(status); return highOrderMapper.selectByExample(example).size(); } @Override public Integer countUnusedDiscountByUserId(Long userId, Integer status) { HighDiscountUserRelExample example = new HighDiscountUserRelExample(); HighDiscountUserRelExample.Criteria criteria = example.createCriteria(); if (userId != null) { criteria.andUserIdEqualTo(userId); } if (status != null) { criteria.andStatusEqualTo(status); } example.setOrderByClause("create_time desc"); return highDiscountUserRelMapper.selectByExample(example).size(); } }