H5OrderController.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package com.ruoyi.web.H5Api.api;
  2. import java.math.BigDecimal;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.commons.collections.map.HashedMap;
  7. import org.redisson.api.RLock;
  8. import org.redisson.api.RedissonClient;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.CrossOrigin;
  11. import org.springframework.web.bind.annotation.DeleteMapping;
  12. import org.springframework.web.bind.annotation.PathVariable;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import com.ruoyi.common.annotation.Log;
  18. import com.ruoyi.common.annotation.RepeatSubmit;
  19. import com.ruoyi.common.core.controller.BaseController;
  20. import com.ruoyi.common.core.domain.AjaxResult;
  21. import com.ruoyi.common.enums.BusinessType;
  22. import com.ruoyi.common.enums.OperatorType;
  23. import com.ruoyi.web.line.LineCallBankParam;
  24. import com.ruoyi.web.line.LinePay;
  25. import com.ruoyi.web.line.LinePayAPOD;
  26. import com.ruoyi.web.line.LinePayConfirmAPOD;
  27. import com.ruoyi.web.pcApi.domain.TCommodityManage;
  28. import com.ruoyi.web.pcApi.domain.TCustomerCollectionOrFootprint;
  29. import com.ruoyi.web.pcApi.domain.TCustomerCoupon;
  30. import com.ruoyi.web.pcApi.domain.TCustomerDistributionRecord;
  31. import com.ruoyi.web.pcApi.domain.TCustomerDistributionSet;
  32. import com.ruoyi.web.pcApi.domain.TCustomerIntegralRecord;
  33. import com.ruoyi.web.pcApi.domain.TCustomerIntegralSet;
  34. import com.ruoyi.web.pcApi.domain.TCustomerManage;
  35. import com.ruoyi.web.pcApi.domain.TCustomerNotice;
  36. import com.ruoyi.web.pcApi.domain.TCustomerShoppingCart;
  37. import com.ruoyi.web.pcApi.domain.TOrderEvaluate;
  38. import com.ruoyi.web.pcApi.domain.TOrderManage;
  39. import com.ruoyi.web.pcApi.domain.TOrderManageTotal;
  40. import com.ruoyi.web.pcApi.mapper.TCommodityManageMapper;
  41. import com.ruoyi.web.pcApi.service.ITCommodityManageService;
  42. import com.ruoyi.web.pcApi.service.ITCustomerCollectionOrFootprintService;
  43. import com.ruoyi.web.pcApi.service.ITCustomerCouponService;
  44. import com.ruoyi.web.pcApi.service.ITCustomerDistributionRecordService;
  45. import com.ruoyi.web.pcApi.service.ITCustomerDistributionSetService;
  46. import com.ruoyi.web.pcApi.service.ITCustomerIntegralRecordService;
  47. import com.ruoyi.web.pcApi.service.ITCustomerIntegralSetService;
  48. import com.ruoyi.web.pcApi.service.ITCustomerManageService;
  49. import com.ruoyi.web.pcApi.service.ITCustomerNoticeService;
  50. import com.ruoyi.web.pcApi.service.ITCustomerShoppingCartService;
  51. import com.ruoyi.web.pcApi.service.ITOrderEvaluateService;
  52. import com.ruoyi.web.pcApi.service.ITOrderManageService;
  53. import com.ruoyi.web.pcApi.service.ITOrderManageTotalService;
  54. /**
  55. * h5端-订单信息api
  56. *
  57. * @author lzp
  58. * @date 2022-08-26
  59. */
  60. @RestController
  61. @RequestMapping("/h5/order")
  62. @CrossOrigin
  63. public class H5OrderController extends BaseController {
  64. @Autowired
  65. private ITCommodityManageService commodityManageService;
  66. @Autowired
  67. private ITCustomerCouponService customerCouponService;
  68. @Autowired
  69. private ITCustomerCollectionOrFootprintService customerCollectionOrFootprintService;
  70. @Autowired
  71. private ITCustomerShoppingCartService shoppingCartService;
  72. @Autowired
  73. private ITOrderManageService orderManageService;
  74. @Autowired
  75. private RedissonClient redissonClient;
  76. @Autowired
  77. private TCommodityManageMapper commodityManageMapper;
  78. @Autowired
  79. private ITOrderEvaluateService orderEvaluateService;
  80. @Autowired
  81. private ITCustomerDistributionRecordService customerDistributionRecordService;
  82. @Autowired
  83. private ITCustomerDistributionSetService customerDistributionSetService;
  84. @Autowired
  85. private ITCustomerManageService customerManageService;
  86. @Autowired
  87. private ITCustomerIntegralRecordService customerIntegralRecordService;
  88. @Autowired
  89. private ITCustomerNoticeService customerNoticeService;
  90. @Autowired
  91. private ITOrderManageTotalService orderManageTotalService;
  92. @Autowired
  93. private ITCustomerIntegralSetService customerIntegralSetService;
  94. @Autowired
  95. private LinePay linePay;
  96. /**
  97. * 增加收藏
  98. *
  99. * @param customerCollectionOrFootprint
  100. * @return
  101. */
  102. @Log(title = "用户增加收藏", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  103. @RepeatSubmit
  104. @PostMapping("/addCollection")
  105. public AjaxResult selectPreferredList(@RequestBody TCustomerCollectionOrFootprint customerCollectionOrFootprint) {
  106. customerCollectionOrFootprint.setType("2");
  107. customerCollectionOrFootprint.setCustomerId(getLoginUser().getLoginCustomer().getId());
  108. customerCollectionOrFootprint.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  109. customerCollectionOrFootprint.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  110. return AjaxResult.success(customerCollectionOrFootprintService.insertTCustomerCollectionOrFootprint(customerCollectionOrFootprint));
  111. }
  112. /**
  113. * 商品详情-移除收藏
  114. *
  115. * @param commodityId 商品id
  116. * @return
  117. */
  118. @Log(title = "用户移除收藏", operatorType = OperatorType.MOBILE, businessType = BusinessType.DELETE)
  119. @RepeatSubmit
  120. @DeleteMapping("/delCollection/{commodityId}")
  121. public AjaxResult delCollection(@PathVariable("commodityId") Long commodityId) {
  122. int i = customerCollectionOrFootprintService.deleteByCustomerIdAndCommodityId(TCustomerCollectionOrFootprint.builder().commodityId(commodityId).customerId(getLoginUser().getLoginCustomer().getId()).type("2").build());
  123. return AjaxResult.success(i);
  124. }
  125. /**
  126. * 用户领取优惠券
  127. *
  128. * @param customerCoupon
  129. * @return
  130. */
  131. @Log(title = "用户领取优惠券", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  132. @RepeatSubmit
  133. @PostMapping("/receiveCoupon")
  134. public AjaxResult receiveCoupon(@RequestBody TCustomerCoupon customerCoupon) {
  135. customerCoupon.setCustomerId(getLoginUser().getLoginCustomer().getId());
  136. List<TCustomerCoupon> tCustomerCoupons = customerCouponService.selectTCustomerCouponList(customerCoupon);
  137. if (tCustomerCoupons.size() > 0) {
  138. return AjaxResult.error("该用户已有该商品优惠券,无法重复领取");
  139. }
  140. customerCoupon.setStatus("0");
  141. customerCoupon.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  142. customerCoupon.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  143. return AjaxResult.success(customerCouponService.insertTCustomerCoupon(customerCoupon));
  144. }
  145. /**
  146. * 增加购物车
  147. *
  148. * @param shoppingCart
  149. * @return
  150. */
  151. @Log(title = "用户添加购物车", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  152. @RepeatSubmit
  153. @PostMapping("/addShoppingCar")
  154. public AjaxResult addShoppingCar(@RequestBody TCustomerShoppingCart shoppingCart) {
  155. shoppingCart.setCustomerId(getLoginUser().getLoginCustomer().getId());
  156. shoppingCart.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  157. shoppingCart.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  158. // 商品信息/商品旅程信息
  159. TCommodityManage commodityManage = commodityManageService.selectTCommodityManageById(shoppingCart.getCommodityId());
  160. // 判断是否有未使用的优惠券
  161. shoppingCart.setIsCoupon("0");
  162. if (commodityManage.getCommodityCouponList() != null && commodityManage.getCommodityCouponList().size() > 0) {
  163. List<TCustomerCoupon> tCustomerCoupons = customerCouponService.selectTCustomerCouponList(TCustomerCoupon.builder().customerId(getLoginUser().getLoginCustomer().getId()).couponId(commodityManage.getCommodityCouponList().get(0).getId()).status("0").build());
  164. if (tCustomerCoupons.size() > 0) {
  165. shoppingCart.setIsCoupon("1");
  166. }
  167. }
  168. return AjaxResult.success(shoppingCartService.insertTCustomerShoppingCart(shoppingCart));
  169. }
  170. /**
  171. * 提交订单(分布式)
  172. *
  173. * @param orderManageTotal
  174. * @return
  175. */
  176. @Log(title = "用户提交订单", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  177. @RepeatSubmit
  178. @PostMapping("/submitOrder")
  179. public AjaxResult submitOrder(@RequestBody TOrderManageTotal orderManageTotal) {
  180. orderManageTotal.setStatus("0");// 已支付
  181. orderManageTotal.setPayDate(new Date());
  182. orderManageTotal.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  183. orderManageTotal.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  184. orderManageTotal.setAipayNo(String.valueOf(new Date().getTime()));
  185. orderManageTotalService.insertTOrderManageTotal(orderManageTotal);
  186. for (TOrderManage order : orderManageTotal.getOrderList()) {
  187. order.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  188. order.setOrderNo(String.valueOf(new Date().getTime()));
  189. order.setOrderTotalId(orderManageTotal.getId());
  190. order.setCustomerId(getLoginUser().getLoginCustomer().getId());
  191. order.setStatus("0");// 已支付
  192. order.setPayDate(new Date());
  193. order.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  194. order.setOrderMoney(order.getMoney());
  195. // 分布式redis锁
  196. // 商品信息/商品旅程信息
  197. TCommodityManage commodityManage = commodityManageService.selectTCommodityManageById(order.getCommodityId());
  198. RLock lock = redissonClient.getLock("submitOrder@" + commodityManage.getId());
  199. lock.lock();
  200. try {
  201. // 判断是否有未使用的优惠券
  202. TCustomerCoupon customerCoupon = null;
  203. if (commodityManage.getCommodityCouponList() != null && commodityManage.getCommodityCouponList().size() > 0) {
  204. List<TCustomerCoupon> tCustomerCoupons = customerCouponService.selectTCustomerCouponList(TCustomerCoupon.builder().customerId(getLoginUser().getLoginCustomer().getId()).couponId(commodityManage.getCommodityCouponList().get(0).getId()).status("0").build());
  205. if (tCustomerCoupons.size() > 0) {
  206. order.setIsCoupon("1");
  207. customerCoupon = tCustomerCoupons.get(0);
  208. }
  209. }
  210. // 减掉库存
  211. Integer orderNum = order.getAdultNum() + order.getChildNum();
  212. if (commodityManage.getStock() - orderNum >= 0) {
  213. commodityManage.setStock(commodityManage.getStock() - orderNum);
  214. // 增加销售数量
  215. commodityManage.setSaleNum(commodityManage.getSaleNum() == null ? 0 + orderNum : commodityManage.getSaleNum() + orderNum);
  216. commodityManageMapper.updateTCommodityManage(commodityManage);
  217. } else {
  218. return AjaxResult.error(commodityManage.getCommodityName() + "库存不足" + orderNum);
  219. }
  220. /** 优惠券失效*/
  221. if (customerCoupon != null) {
  222. customerCoupon.setStatus("1");
  223. customerCoupon.setUpdateBy(order.getCreateBy());
  224. customerCouponService.updateTCustomerCoupon(customerCoupon);
  225. }
  226. orderManageService.insertTOrderManage(order);
  227. // 删除购物车信息
  228. if (order.getShopCarId() != null) {
  229. shoppingCartService.deleteTCustomerShoppingCartById(order.getShopCarId());
  230. }
  231. } catch (RuntimeException e) {
  232. e.printStackTrace();
  233. return AjaxResult.error("系统异常,请稍后再试");
  234. } finally {
  235. // 释放锁
  236. lock.unlock();
  237. }
  238. }
  239. Map<String,Object> map = new HashedMap();
  240. map.put("totalId",orderManageTotal.getId());
  241. map.put("orderList",orderManageTotal.getOrderList());
  242. return AjaxResult.success(map);
  243. }
  244. /**
  245. * 增加订单信息评价
  246. *
  247. * @param orderEvaluate
  248. * @return
  249. */
  250. @Log(title = "增加订单信息评价", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  251. @RepeatSubmit
  252. @PostMapping("/addOrderEvalute")
  253. public AjaxResult addOrderEvalute(@RequestBody TOrderEvaluate orderEvaluate) {
  254. // 更新订单状态
  255. orderManageService.updateTOrderManage(TOrderManage.builder().id(orderEvaluate.getOrderId()).status("9").build());
  256. orderEvaluate.setCustomerId(getLoginUser().getLoginCustomer().getId());
  257. orderEvaluate.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  258. return AjaxResult.success(orderEvaluateService.insertTOrderEvaluate(orderEvaluate));
  259. }
  260. /**
  261. * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付
  262. *
  263. * @param orderManageTotal
  264. * @return
  265. */
  266. @Log(title = "用户支付", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  267. @RepeatSubmit
  268. @PostMapping("/aiPay")
  269. public AjaxResult aiPay(@RequestBody TOrderManageTotal orderManageTotal) {
  270. // 接入支付接口
  271. LinePayAPOD resp = linePay.pay(orderManageTotal, orderManageTotalService.getDeptName(orderManageTotal.getId()));
  272. //ok
  273. if (resp != null && "0000".equals(resp.returnCode)) {
  274. //set the transactionId to the db.
  275. orderManageTotal.setTransactionId(resp.info.transactionId);
  276. orderManageTotalService.updateTOrderManageTotal(orderManageTotal);
  277. return AjaxResult.success(resp.info);
  278. }
  279. //line支付error
  280. return AjaxResult.error("LINE PAY失敗しました。");
  281. }
  282. /**
  283. * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付
  284. *
  285. * @param orderManageTotal
  286. * @return
  287. */
  288. @Log(title = "キャンセルCallBack", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  289. @RepeatSubmit
  290. @PostMapping("/cancel")
  291. public AjaxResult apayCancelCallBack(@RequestBody LineCallBankParam param) {
  292. //
  293. TOrderManageTotal select = new TOrderManageTotal();
  294. select.setTransactionId(param.getTransactionId());
  295. select.setId(param.getOrderId());
  296. select.setDelFlag("1");
  297. orderManageTotalService.updateTOrderManageTotal(select);
  298. return AjaxResult.success("支払キャンセルしました。");
  299. }
  300. /**
  301. * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付
  302. *
  303. * @param orderManageTotal
  304. * @return
  305. */
  306. @Log(title = "用户支付CallBack", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  307. @RepeatSubmit
  308. @PostMapping("/confirm")
  309. public AjaxResult aiPayCallBack(@RequestBody LineCallBankParam param) {
  310. //
  311. TOrderManageTotal select = new TOrderManageTotal();
  312. select.setTransactionId(param.getTransactionId());
  313. select.setId(param.getOrderId());
  314. List<TOrderManageTotal> lst = orderManageTotalService.selectTOrderManageTotalList(select);
  315. TOrderManageTotal orderManageTotal = lst.get(0);
  316. // 接入支付接口
  317. LinePayConfirmAPOD resp = linePay.payConfirm(orderManageTotal.getTransactionId(), orderManageTotal.getOrderMoney().longValue());
  318. if ("0000".equals(resp.returnCode)) {
  319. //set order list
  320. TOrderManage orderPram = new TOrderManage();
  321. orderPram.setOrderTotalId(param.getOrderId());
  322. List<TOrderManage> orderList = orderManageService.selectTOrderManageList(orderPram);
  323. orderManageTotal.setOrderList(orderList);
  324. TCustomerManage customer = customerManageService.selectTCustomerManageById(getLoginUser().getLoginCustomer().getId());
  325. // 多/单订单支付
  326. for (TOrderManage order : orderManageTotal.getOrderList()) {
  327. TOrderManage orderManage = orderManageService.selectTOrderManageById(order.getId());
  328. /** 通知待使用*/
  329. customerNoticeService.insertTCustomerNotice(TCustomerNotice.builder().customerId(customer.getId()).commodityId(order.getId())
  330. .content("您有一张" + orderManage.getCommodityName() + "商品待使用。").title("订单信息")
  331. .createBy(orderManage.getCreateBy()).status("0")
  332. .build());
  333. // 若使用积分 则订单金额减去积分金额
  334. if(orderManageTotal.getIntegralDeduction() != BigDecimal.ZERO && orderManageTotal.getIntegralDeduction() != null){
  335. order.setOrderMoney(orderManage.getOrderMoney().subtract(orderManageTotal.getIntegralDeduction()));
  336. order.setIntegralDeduction(orderManageTotal.getIntegralDeduction());
  337. }
  338. order.setStatus("1");// 已支付 支付状态,若接入支付回调,则变更为支付中状态-9,支付回调成功后变更为1-已支付
  339. order.setPayDate(new Date());
  340. orderManageService.updateTOrderManage(order);
  341. }
  342. // 获取场馆积分获取比例
  343. List<TCustomerIntegralSet> tCustomerIntegralSets = customerIntegralSetService.selectTCustomerIntegralSetList(TCustomerIntegralSet.builder().deptId(getLoginUser().getLoginCustomer().getDeptId()).build());
  344. BigDecimal orderIntegral = orderManageTotal.getOrderMoney();
  345. if (tCustomerIntegralSets.size() > 0) {
  346. // 计算分销人员奖励
  347. BigDecimal proportion = new BigDecimal(tCustomerIntegralSets.get(0).getProportion()).divide(new BigDecimal("100"));
  348. orderIntegral= (orderManageTotal.getOrderMoney().multiply(proportion)).setScale(0, BigDecimal.ROUND_DOWN);
  349. }
  350. // 记录用户积分
  351. customerIntegralRecordService.insertTCustomerIntegralRecord(TCustomerIntegralRecord.builder().
  352. supIntegral(customer.getIntegral()==null?"0":customer.getIntegral().toString()).integralNum(String.valueOf(orderIntegral.longValue())).integralType("1")
  353. .createBy(getLoginUser().getLoginCustomer().getUsername()).orderId(orderManageTotal.getId()).customerId(customer.getId()).deptId(getLoginUser().getDeptId())
  354. .build());
  355. /** 增加用户积分*/
  356. customer.setIntegral(customer.getIntegral()==null?0:customer.getIntegral() + orderIntegral.longValue());
  357. customerManageService.updateTCustomerManage(customer);
  358. // 会员减去积分
  359. if(orderManageTotal.getIntegralDeduction() != null && orderManageTotal.getIntegralDeduction() != BigDecimal.ZERO){
  360. customer.setIntegral(customer.getIntegral() - orderManageTotal.getIntegralDeduction().longValue());
  361. customerManageService.updateTCustomerManage(customer);
  362. // 记录减去用户积分
  363. customerIntegralRecordService.insertTCustomerIntegralRecord(TCustomerIntegralRecord.builder().
  364. supIntegral(customer.getIntegral().toString()).integralNum(orderManageTotal.getIntegralDeduction().toString()).integralType("4")
  365. .createBy(getLoginUser().getLoginCustomer().getUsername()).orderId(orderManageTotal.getOrderList().get(0).getId()).customerId(customer.getId()).deptId(getLoginUser().getDeptId())
  366. .build());
  367. }
  368. // 是否有分销
  369. if (orderManageTotal.getDistributionId() != null) {
  370. // 获取场馆分销比例
  371. List<TCustomerDistributionSet> tCustomerDistributionSets = customerDistributionSetService.selectTCustomerDistributionSetList(TCustomerDistributionSet.builder().deptId(getLoginUser().getLoginCustomer().getDeptId()).build());
  372. if (tCustomerDistributionSets.size() > 0) {
  373. // 计算分销人员奖励
  374. BigDecimal proportion = new BigDecimal(tCustomerDistributionSets.get(0).getProportion()).divide(new BigDecimal("100"));
  375. BigDecimal integral = (orderManageTotal.getOrderMoney().multiply(proportion)).setScale(0, BigDecimal.ROUND_DOWN);
  376. // 增加用户积分
  377. TCustomerManage customerManage = customerManageService.selectTCustomerManageById(orderManageTotal.getDistributionId());
  378. customerManage.setIntegral(customerManage.getIntegral() + integral.longValue());
  379. customerManageService.updateTCustomerManage(customerManage);
  380. // 记录分销
  381. customerDistributionRecordService.insertTCustomerDistributionRecord(
  382. TCustomerDistributionRecord.builder().customerId(orderManageTotal.getDistributionId()).deptId(getLoginUser().getLoginCustomer().getDeptId())
  383. .distributionIntegral(integral.toString()).createBy(getLoginUser().getLoginCustomer().getUsername()).orderId(orderManageTotal.getId()).build());
  384. }
  385. }
  386. // 查询总订单下-是否还有未支付的信息
  387. List<TOrderManage> orderManageList = orderManageService.selectTOrderManageList(TOrderManage.builder().
  388. orderTotalId(orderManageTotal.getId()).status("0").build());
  389. if(orderManageList.size() <= 0){
  390. orderManageTotal.setStatus("1");
  391. orderManageTotalService.updateTOrderManageTotal(orderManageTotal);
  392. }
  393. return AjaxResult.success("支払成功");
  394. } else {
  395. return AjaxResult.success("支払失敗した");
  396. }
  397. }
  398. }