OrderServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.amazonaws.services.codecommit.model.OrderEnum;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.fdkankan.common.constant.TbStatus;
  7. import com.fdkankan.common.exception.BusinessException;
  8. import com.fdkankan.ucenter.common.PageInfo;
  9. import com.fdkankan.common.util.FileUtils;
  10. import com.fdkankan.common.util.NumberUtils;
  11. import com.fdkankan.redis.util.RedisUtil;
  12. import com.fdkankan.ucenter.common.RedisKeyUtil;
  13. import com.fdkankan.ucenter.constant.*;
  14. import com.fdkankan.ucenter.entity.*;
  15. import com.fdkankan.ucenter.mapper.IOrderMapper;
  16. import com.fdkankan.ucenter.service.*;
  17. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  18. import com.fdkankan.ucenter.util.DateUserUtil;
  19. import com.fdkankan.ucenter.vo.request.CartParam;
  20. import com.fdkankan.ucenter.vo.request.DownNumParam;
  21. import com.fdkankan.ucenter.vo.request.OrderParam;
  22. import com.fdkankan.ucenter.vo.request.PlaceOrderParam;
  23. import com.fdkankan.ucenter.vo.response.GoodsSkuVo;
  24. import com.fdkankan.ucenter.vo.response.OrderVo;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.springframework.beans.BeanUtils;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.beans.factory.annotation.Value;
  29. import org.springframework.stereotype.Service;
  30. import java.math.BigDecimal;
  31. import java.util.*;
  32. import java.util.stream.Collectors;
  33. /**
  34. * <p>
  35. * 订单表 服务实现类
  36. * </p>
  37. *
  38. * @author
  39. * @since 2022-07-13
  40. */
  41. @Service
  42. public class OrderServiceImpl extends ServiceImpl<IOrderMapper, Order> implements IOrderService {
  43. @Autowired
  44. IUserService userService;
  45. @Autowired
  46. IOrderItemService orderItemService;
  47. @Autowired
  48. IInvoiceService invoiceService;
  49. @Autowired
  50. IIncrementOrderService incrementOrderService;
  51. @Autowired
  52. ICartService cartService;
  53. @Autowired
  54. IGoodsService goodsService;
  55. @Autowired
  56. IGoodsSkuService goodsSkuService;
  57. @Autowired
  58. RedisUtil redisUtil;
  59. @Autowired
  60. IDownloadOrderService downloadOrderService;
  61. @Autowired
  62. IIncrementTypeService incrementTypeService;
  63. @Autowired
  64. IOrderService orderService;
  65. @Override
  66. public PageInfo pageList(OrderParam param) {
  67. User user = userService.getByUserName(param.getUserName());
  68. LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
  69. wrapper.eq(Order::getUserId,user.getId());
  70. wrapper.eq(Order::getPaymentStatus,"paid");
  71. wrapper.orderByDesc(Order::getOrderTime);
  72. Page<Order> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
  73. List<OrderVo> orderVos = new ArrayList<>();
  74. for (Order record : page.getRecords()) {
  75. OrderVo orderVo = new OrderVo();
  76. BeanUtils.copyProperties(record,orderVo);
  77. Invoice invoice = invoiceService.getByOrderId(record.getId());
  78. orderVo.setInvoice(invoice);
  79. List<OrderItem> itemList = orderItemService.getByOrderId(record.getId());
  80. orderVo.setOrderItems(itemList);
  81. orderVos.add(orderVo);
  82. }
  83. Page<OrderVo> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
  84. pageVo.setRecords(orderVos);
  85. pageVo.setTotal(page.getTotal());
  86. return PageInfo.PageInfo(pageVo);
  87. }
  88. @Override
  89. public GoodsSkuVo getIncrementOrDownloadPrice(OrderParam param) {
  90. return incrementOrderService.getDownloadPrice(param);
  91. }
  92. @Override
  93. public void cancel(Long orderId) {
  94. LambdaUpdateWrapper<Order> wrapper = new LambdaUpdateWrapper<>();
  95. wrapper.set(Order::getOrderStatus, OrderStatus.invalid)
  96. .eq(Order::getId,orderId);
  97. this.update(wrapper);
  98. }
  99. @Override
  100. public void receipt(Long orderId) {
  101. LambdaUpdateWrapper<Order> wrapper = new LambdaUpdateWrapper<>();
  102. wrapper.set(Order::getOrderStatus, OrderStatus.completed)
  103. .set(Order::getShippingStatus, ShippingStatus.received)
  104. .eq(Order::getId,orderId);
  105. this.update(wrapper);
  106. }
  107. @Override
  108. public Order placeOrder(PlaceOrderParam param) {
  109. List<Long> id = param.getGoods().parallelStream().map(CartParam::getId).collect(Collectors.toList());
  110. List<String> ids = new ArrayList<>();
  111. for (Long aLong : id) {
  112. ids.add(String.valueOf(aLong));
  113. }
  114. User user = userService.getByUserName(param.getUserName());
  115. cartService.deleteIds(ids);
  116. // 下单
  117. List<OrderItem> orderItemEntities = new ArrayList<>();
  118. OrderItem orderItemEntity = null;
  119. int goodsTotalCount = 0;
  120. BigDecimal goodsAmount = new BigDecimal(0);
  121. for(CartParam requestCart : param.getGoods()){
  122. goodsTotalCount += requestCart.getGoodsCount();
  123. Goods responseGoods = goodsService.getById(requestCart.getGoodsId());
  124. if (responseGoods == null){
  125. continue;
  126. }
  127. GoodsSku sku = goodsSkuService.getBySku(requestCart.getSkuSn());
  128. if (sku == null){
  129. throw new BusinessException(500,LoginConstant.ERROR_MSG);
  130. }
  131. orderItemEntity = new OrderItem();
  132. orderItemEntity.setExpressNum(null);
  133. orderItemEntity.setGoodsPrice(param.getAbroad().intValue() == 0 ? sku.getPrice() : sku.getDollarPrice());
  134. orderItemEntity.setGoodsId(requestCart.getGoodsId());
  135. orderItemEntity.setShippingStatus(ShippingStatus.unshipped.name());
  136. orderItemEntity.setExpressName("顺丰速运");
  137. orderItemEntity.setGoodsCount(requestCart.getGoodsCount());
  138. orderItemEntity.setGoodsName(responseGoods.getName());
  139. orderItemEntity.setGoodsSn(responseGoods.getGoodsSn());
  140. orderItemEntity.setRecStatus("A");
  141. orderItemEntity.setCreateTime(DateUserUtil.getDate(new Date()));
  142. orderItemEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
  143. if (StringUtils.isNotEmpty(requestCart.getSkuSn())){
  144. orderItemEntity.setSkuSn(requestCart.getSkuSn());
  145. }
  146. orderItemEntities.add(orderItemEntity);
  147. goodsAmount = goodsAmount.add(orderItemEntity.getGoodsPrice().multiply(new BigDecimal(orderItemEntity.getGoodsCount())));
  148. }
  149. Order orderEntity = new Order();
  150. orderEntity.setOrderSn(NumberUtils.getOrderSn());
  151. orderEntity.setOrderTime(new Date());
  152. orderEntity.setOrderStatus(OrderStatus.unprocessed.name());
  153. orderEntity.setPaymentStatus(PaymentStatus.unpaid.name());
  154. orderEntity.setShippingStatus(ShippingStatus.unshipped.name());
  155. orderEntity.setPaymentTypeName(param.getPayType());
  156. orderEntity.setUserId(user.getId());
  157. orderEntity.setGoodsTotalCount(goodsTotalCount);
  158. orderEntity.setGoodsAmount(goodsAmount);
  159. orderEntity.setTotalAmount(goodsAmount);
  160. orderEntity.setPaidAmount(BigDecimal.ZERO);
  161. orderEntity.setExpressAmount(BigDecimal.ZERO);
  162. orderEntity.setDeliveryTypeName("快递运输");
  163. orderEntity.setRecStatus("A");
  164. orderEntity.setCreateTime(DateUserUtil.getDate(new Date()));
  165. orderEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
  166. if (param.getReceiver() != null){
  167. orderEntity.setShipAddress(param.getReceiver().getShipAddress());
  168. orderEntity.setShipAreaPath(param.getReceiver().getShipAreaPath());
  169. orderEntity.setShipMobile(param.getReceiver().getShipMobile());
  170. orderEntity.setShipName(param.getReceiver().getShipName());
  171. }
  172. orderEntity.setAbroad(param.getAbroad());
  173. this.save(orderEntity);
  174. for (OrderItem entity : orderItemEntities){
  175. entity.setOrderId(orderEntity.getId());
  176. }
  177. orderItemService.saveBatch(orderItemEntities);
  178. if(param.getInvoice()!=null && StringUtils.isNotBlank(param.getInvoice().getInvoiceType())){
  179. param.setOrderId(orderEntity.getId());
  180. param.getInvoice().setAmount(orderEntity.getTotalAmount().toString());
  181. invoiceService.saveByOrder(user.getId(),param);
  182. }
  183. return orderEntity;
  184. }
  185. @Override
  186. public boolean queryOrderStatus(OrderParam param) throws Exception {
  187. boolean success = false;
  188. String orderSn = param.getOrderSn();
  189. switch (param.getOrderType()){
  190. case 0:
  191. orderSn += "_entity";
  192. break;
  193. case 1:
  194. orderSn += "_recharge";
  195. break;
  196. case 2:
  197. orderSn += "_expansion";
  198. break;
  199. case 3:
  200. orderSn += "_commerce";
  201. break;
  202. case 4: //国内参数
  203. case 6: //国际服参数
  204. orderSn += "_increment";
  205. break;
  206. case 5:
  207. orderSn += "_download";
  208. break;
  209. }
  210. if(param.getPayType()!= 2 && !redisUtil.hasKey(RedisKeyUtil.QRCODE + orderSn)){
  211. String wxPngPath = QrCodeFilePath.WEIXIN_QRCODE_FOLDER + orderSn + ".png";
  212. String aliPngPath = QrCodeFilePath.ALI_QRCODE_FOLDER + orderSn + ".png";
  213. FileUtils.deleteFile(wxPngPath);
  214. FileUtils.deleteFile(aliPngPath);
  215. throw new BusinessException(LoginConstant.FAILURE_CODE_3035,LoginConstant.FAILURE_MSG_3035);
  216. }
  217. return success;
  218. }
  219. @Override
  220. public Order getByOrderSn(String orderSn) {
  221. LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
  222. wrapper.eq(Order::getOrderSn,orderSn);
  223. return this.getOne(wrapper);
  224. }
  225. @Override
  226. public DownloadOrder downloadOrder(DownNumParam param) {
  227. //支付美金,只能paypal支付
  228. if(param.getAbroad() == 1 && param.getPayType() != 2){
  229. throw new BusinessException(OrderConstant.FAILURE_CODE_8004, OrderConstant.FAILURE_MSG_8004);
  230. }
  231. IncrementType incrementType = incrementTypeService.getById(param.getIncrementType());
  232. if(incrementType == null){
  233. throw new BusinessException(OrderConstant.FAILURE_CODE_8004, OrderConstant.FAILURE_MSG_8004);
  234. }
  235. // GoodsSku goodsSku = goodsSkuService.getBySku(param.getSkuSn());
  236. // if(goodsSku == null){
  237. // throw new BusinessException(OrderConstant.FAILURE_CODE_8004, OrderConstant.FAILURE_MSG_8004);
  238. // }
  239. User user = userService.getByUserName(param.getUserName());
  240. if(user == null){
  241. throw new BusinessException(OrderConstant.FAILURE_CODE_8004, OrderConstant.FAILURE_MSG_8004);
  242. }
  243. return downloadOrderService.insertDownloadOrder(user.getId(),param,incrementType.getDownloadPrice());
  244. }
  245. @Override
  246. public IncrementOrder incrementOrder(DownNumParam param) {
  247. //支付美金,只能paypal支付
  248. if(param.getAbroad() == 1 && param.getPayType() != 2){
  249. throw new BusinessException(OrderConstant.FAILURE_CODE_8004, OrderConstant.FAILURE_MSG_8004);
  250. }
  251. User user = userService.getByUserName(param.getUserName());
  252. if(user == null){
  253. throw new BusinessException(OrderConstant.FAILURE_CODE_8004, OrderConstant.FAILURE_MSG_8004);
  254. }
  255. IncrementType incrementType = incrementTypeService.getById(param.getIncrementType());
  256. if(incrementType == null){
  257. throw new BusinessException(OrderConstant.FAILURE_CODE_8004, OrderConstant.FAILURE_MSG_8004);
  258. }
  259. return incrementOrderService.insertIncrementOrder(user.getId(), param,incrementType);
  260. }
  261. @Override
  262. public boolean paySuccessEntityOrder(String orderSn, String tradeNo, int paymentTypeName) {
  263. LambdaUpdateWrapper<Order> updateWrapper = new LambdaUpdateWrapper<>();
  264. updateWrapper.set(Order::getPaymentTypeName,paymentTypeName)
  265. .set(Order::getTradeNum,tradeNo)
  266. .set(Order::getPaymentStatus, PaymentStatus.paid.name())
  267. .eq(Order::getOrderSn,orderSn);
  268. return this.update(updateWrapper);
  269. }
  270. @Override
  271. public OrderVo getOrderDetail(Long orderId) {
  272. Order order = this.getById(orderId);
  273. OrderVo responseOrder = new OrderVo();
  274. if(order == null ){
  275. return responseOrder;
  276. }
  277. BeanUtils.copyProperties(order,responseOrder);
  278. List<OrderItem> responseOrderItems =orderItemService.getByOrderId(orderId);
  279. responseOrder.setOrderItems(responseOrderItems);
  280. Invoice invoice = invoiceService.getByOrderId(orderId);
  281. if(invoice != null){
  282. responseOrder.setInvoice(invoice);
  283. }
  284. return responseOrder;
  285. }
  286. @Override
  287. public void autoUpdateOrderStatus() {
  288. this.getBaseMapper().autoUpdateOrderStatus();
  289. }
  290. }