H5OrderController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package com.ruoyi.web.H5Api.api;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.commons.collections.map.HashedMap;
  8. import org.redisson.api.RLock;
  9. import org.redisson.api.RedissonClient;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.CrossOrigin;
  12. import org.springframework.web.bind.annotation.DeleteMapping;
  13. import org.springframework.web.bind.annotation.PathVariable;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.RequestBody;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import com.ruoyi.common.annotation.Log;
  19. import com.ruoyi.common.annotation.RepeatSubmit;
  20. import com.ruoyi.common.core.controller.BaseController;
  21. import com.ruoyi.common.core.domain.AjaxResult;
  22. import com.ruoyi.common.enums.BusinessType;
  23. import com.ruoyi.common.enums.OperatorType;
  24. import com.ruoyi.web.line.LineCallBankParam;
  25. import com.ruoyi.web.line.LinePay;
  26. import com.ruoyi.web.line.LinePayAPOD;
  27. import com.ruoyi.web.line.LinePayConfirmAPOD;
  28. import com.ruoyi.web.line.PayApi;
  29. import com.ruoyi.web.pcApi.domain.TCommodityManage;
  30. import com.ruoyi.web.pcApi.domain.TCommoditySchedul;
  31. import com.ruoyi.web.pcApi.domain.TCustomerCollectionOrFootprint;
  32. import com.ruoyi.web.pcApi.domain.TCustomerCoupon;
  33. import com.ruoyi.web.pcApi.domain.TCustomerShoppingCart;
  34. import com.ruoyi.web.pcApi.domain.TOrderEvaluate;
  35. import com.ruoyi.web.pcApi.domain.TOrderManage;
  36. import com.ruoyi.web.pcApi.domain.TOrderManageTotal;
  37. import com.ruoyi.web.pcApi.service.ITCommodityManageService;
  38. import com.ruoyi.web.pcApi.service.ITCommoditySchedulService;
  39. import com.ruoyi.web.pcApi.service.ITCustomerCollectionOrFootprintService;
  40. import com.ruoyi.web.pcApi.service.ITCustomerCouponService;
  41. import com.ruoyi.web.pcApi.service.ITCustomerShoppingCartService;
  42. import com.ruoyi.web.pcApi.service.ITOrderEvaluateService;
  43. import com.ruoyi.web.pcApi.service.ITOrderManageService;
  44. import com.ruoyi.web.pcApi.service.ITOrderManageTotalService;
  45. /**
  46. * h5端-订单信息api
  47. *
  48. * @author lzp
  49. * @date 2022-08-26
  50. */
  51. @RestController
  52. @RequestMapping("/h5/order")
  53. @CrossOrigin
  54. public class H5OrderController extends BaseController {
  55. @Autowired
  56. private ITCommodityManageService commodityManageService;
  57. @Autowired
  58. private ITCustomerCouponService customerCouponService;
  59. @Autowired
  60. private ITCustomerCollectionOrFootprintService customerCollectionOrFootprintService;
  61. @Autowired
  62. private ITCustomerShoppingCartService shoppingCartService;
  63. @Autowired
  64. private ITOrderManageService orderManageService;
  65. @Autowired
  66. private RedissonClient redissonClient;
  67. @Autowired
  68. private ITOrderEvaluateService orderEvaluateService;
  69. @Autowired
  70. private ITOrderManageTotalService orderManageTotalService;
  71. @Autowired
  72. private LinePay linePay;
  73. @Autowired
  74. private ITCommoditySchedulService commoditySchedulService;
  75. @Autowired
  76. private PayApi payApi;
  77. /**
  78. * 增加收藏
  79. *
  80. * @param customerCollectionOrFootprint
  81. * @return
  82. */
  83. @Log(title = "用户增加收藏", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  84. @RepeatSubmit
  85. @PostMapping("/addCollection")
  86. public AjaxResult selectPreferredList(@RequestBody TCustomerCollectionOrFootprint customerCollectionOrFootprint) {
  87. customerCollectionOrFootprint.setType("2");
  88. customerCollectionOrFootprint.setCustomerId(getLoginUser().getLoginCustomer().getId());
  89. customerCollectionOrFootprint.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  90. customerCollectionOrFootprint.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  91. return AjaxResult.success(customerCollectionOrFootprintService.insertTCustomerCollectionOrFootprint(customerCollectionOrFootprint));
  92. }
  93. /**
  94. * 商品详情-移除收藏
  95. *
  96. * @param commodityId 商品id
  97. * @return
  98. */
  99. @Log(title = "用户移除收藏", operatorType = OperatorType.MOBILE, businessType = BusinessType.DELETE)
  100. @RepeatSubmit
  101. @DeleteMapping("/delCollection/{commodityId}")
  102. public AjaxResult delCollection(@PathVariable("commodityId") Long commodityId) {
  103. int i = customerCollectionOrFootprintService.deleteByCustomerIdAndCommodityId(TCustomerCollectionOrFootprint.builder().commodityId(commodityId).customerId(getLoginUser().getLoginCustomer().getId()).type("2").build());
  104. return AjaxResult.success(i);
  105. }
  106. /**
  107. * 用户领取优惠券
  108. *
  109. * @param customerCoupon
  110. * @return
  111. */
  112. @Log(title = "用户领取优惠券", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  113. @RepeatSubmit
  114. @PostMapping("/receiveCoupon")
  115. public AjaxResult receiveCoupon(@RequestBody TCustomerCoupon customerCoupon) {
  116. customerCoupon.setCustomerId(getLoginUser().getLoginCustomer().getId());
  117. List<TCustomerCoupon> tCustomerCoupons = customerCouponService.selectTCustomerCouponList(customerCoupon);
  118. if (tCustomerCoupons.size() > 0) {
  119. return AjaxResult.error("该用户已有该商品优惠券,无法重复领取");
  120. }
  121. customerCoupon.setStatus("0");
  122. customerCoupon.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  123. customerCoupon.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  124. return AjaxResult.success(customerCouponService.insertTCustomerCoupon(customerCoupon));
  125. }
  126. /**
  127. * 增加购物车
  128. *
  129. * @param shoppingCart
  130. * @return
  131. */
  132. @Log(title = "用户添加购物车", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  133. @RepeatSubmit
  134. @PostMapping("/addShoppingCar")
  135. public AjaxResult addShoppingCar(@RequestBody TCustomerShoppingCart shoppingCart) {
  136. shoppingCart.setCustomerId(getLoginUser().getLoginCustomer().getId());
  137. shoppingCart.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  138. shoppingCart.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  139. // 商品信息/商品旅程信息
  140. TCommodityManage commodityManage = commodityManageService.selectTCommodityManageById(shoppingCart.getCommodityId());
  141. // 判断是否有未使用的优惠券
  142. shoppingCart.setIsCoupon("0");
  143. if (commodityManage.getCommodityCouponList() != null && commodityManage.getCommodityCouponList().size() > 0) {
  144. List<TCustomerCoupon> tCustomerCoupons = customerCouponService.selectTCustomerCouponList(TCustomerCoupon.builder().customerId(getLoginUser().getLoginCustomer().getId()).couponId(commodityManage.getCommodityCouponList().get(0).getId()).status("0").build());
  145. if (tCustomerCoupons.size() > 0) {
  146. shoppingCart.setIsCoupon("1");
  147. }
  148. }
  149. return AjaxResult.success(shoppingCartService.insertTCustomerShoppingCart(shoppingCart));
  150. }
  151. /**
  152. * 提交订单(分布式)
  153. *
  154. * @param orderManageTotal
  155. * @return
  156. */
  157. @Log(title = "用户提交订单", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  158. @RepeatSubmit
  159. @PostMapping("/submitOrder")
  160. public AjaxResult submitOrder(@RequestBody TOrderManageTotal orderManageTotal) {
  161. orderManageTotal.setStatus("0");// 已支付
  162. orderManageTotal.setPayDate(new Date());
  163. orderManageTotal.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  164. orderManageTotal.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  165. orderManageTotal.setAipayNo(String.valueOf(new Date().getTime()));
  166. orderManageTotalService.insertTOrderManageTotal(orderManageTotal);
  167. for (TOrderManage order : orderManageTotal.getOrderList()) {
  168. order.setCreateBy(getLoginUser().getLoginCustomer().getUsername());
  169. order.setOrderNo(String.valueOf(new Date().getTime()));
  170. order.setOrderTotalId(orderManageTotal.getId());
  171. order.setCustomerId(getLoginUser().getLoginCustomer().getId());
  172. order.setStatus("0");// 已支付
  173. order.setPayDate(new Date());
  174. order.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  175. order.setOrderMoney(order.getMoney());
  176. // 分布式redis锁
  177. // 商品信息/商品旅程信息
  178. TCommodityManage commodityManage = commodityManageService.selectTCommodityManageById(order.getCommodityId());
  179. RLock lock = redissonClient.getLock("submitOrder@" + commodityManage.getId());
  180. lock.lock();
  181. try {
  182. // 判断是否有未使用的优惠券
  183. TCustomerCoupon customerCoupon = null;
  184. if (commodityManage.getCommodityCouponList() != null && commodityManage.getCommodityCouponList().size() > 0) {
  185. List<TCustomerCoupon> tCustomerCoupons = customerCouponService.selectTCustomerCouponList(TCustomerCoupon.builder().customerId(getLoginUser().getLoginCustomer().getId()).couponId(commodityManage.getCommodityCouponList().get(0).getId()).status("0").build());
  186. if (tCustomerCoupons.size() > 0) {
  187. order.setIsCoupon("1");
  188. customerCoupon = tCustomerCoupons.get(0);
  189. }
  190. }
  191. // 减掉库存
  192. Integer orderNum = order.getAdultNum() + order.getChildNum();
  193. //座位信息取得。
  194. TCommoditySchedul schedulSeat = new TCommoditySchedul();
  195. schedulSeat.setCommodityId(commodityManage.getId());
  196. schedulSeat.setDepartureDate(order.getDepartureDate());
  197. List<TCommoditySchedul> seatList = commoditySchedulService.selectTCommoditySchedulList(schedulSeat);
  198. long stock = 0;
  199. for (TCommoditySchedul seatRcd : seatList) {
  200. stock = seatRcd.getSeatNum();
  201. if (stock - orderNum >= 0) {
  202. seatRcd.setSeatNum(stock - orderNum);
  203. commoditySchedulService.updateTCommoditySchedul(seatRcd);
  204. } else {
  205. return AjaxResult.error(commodityManage.getCommodityName() + "在庫がございません" + orderNum);
  206. }
  207. }
  208. /** 优惠券失效*/
  209. if (customerCoupon != null) {
  210. customerCoupon.setStatus("1");
  211. customerCoupon.setUpdateBy(order.getCreateBy());
  212. customerCouponService.updateTCustomerCoupon(customerCoupon);
  213. }
  214. orderManageService.insertTOrderManage(order);
  215. // 删除购物车信息
  216. if (order.getShopCarId() != null) {
  217. shoppingCartService.deleteTCustomerShoppingCartById(order.getShopCarId());
  218. }
  219. } catch (RuntimeException e) {
  220. e.printStackTrace();
  221. return AjaxResult.error("系统异常,请稍后再试");
  222. } finally {
  223. // 释放锁
  224. lock.unlock();
  225. }
  226. }
  227. Map<String,Object> map = new HashedMap();
  228. map.put("totalId",orderManageTotal.getId());
  229. map.put("orderList",orderManageTotal.getOrderList());
  230. return AjaxResult.success(map);
  231. }
  232. /**
  233. * 增加订单信息评价
  234. *
  235. * @param orderEvaluate
  236. * @return
  237. */
  238. @Log(title = "增加订单信息评价", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  239. @RepeatSubmit
  240. @PostMapping("/addOrderEvalute")
  241. public AjaxResult addOrderEvalute(@RequestBody TOrderEvaluate orderEvaluate) {
  242. // 更新订单状态
  243. orderManageService.updateTOrderManage(TOrderManage.builder().id(orderEvaluate.getOrderId()).status("9").build());
  244. orderEvaluate.setCustomerId(getLoginUser().getLoginCustomer().getId());
  245. orderEvaluate.setDeptId(getLoginUser().getLoginCustomer().getDeptId());
  246. return AjaxResult.success(orderEvaluateService.insertTOrderEvaluate(orderEvaluate));
  247. }
  248. /**
  249. * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付
  250. *
  251. * @param orderManageTotal
  252. * @return
  253. */
  254. @Log(title = "用户支付", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  255. @RepeatSubmit
  256. @PostMapping("/aiPay")
  257. public AjaxResult aiPay(@RequestBody TOrderManageTotal orderManageTotal) {
  258. //按分
  259. int len = orderManageTotal.getOrderList().size();
  260. //total
  261. BigDecimal total = orderManageTotal.getOrderMoney();
  262. BigDecimal sendAmout = BigDecimal.ZERO;
  263. //提交多条数据的时候要进行按分。
  264. if (len > 1) {
  265. int cnt = 0;
  266. BigDecimal anbun = BigDecimal.ZERO;
  267. //
  268. BigDecimal preTotal = BigDecimal.ZERO;
  269. for (TOrderManage orderManageTemp : orderManageTotal.getOrderList()) {
  270. preTotal=preTotal.add(orderManageTemp.getOrderMoney());
  271. }
  272. for (TOrderManage orderManage : orderManageTotal.getOrderList()) {
  273. cnt++;
  274. //amount
  275. BigDecimal amount = BigDecimal.ZERO;
  276. if (cnt == len) {
  277. amount = total.subtract(anbun);
  278. } else {
  279. amount = orderManage.getOrderMoney().multiply(total).divide(preTotal, BigDecimal.ROUND_UP);
  280. anbun = anbun.add(amount);
  281. }
  282. orderManage.setOrderMoney(amount);
  283. sendAmout = sendAmout.add(amount);
  284. }
  285. } else {
  286. orderManageTotal.getOrderList().get(0).setOrderMoney(total);
  287. sendAmout = total;
  288. }
  289. //0元LINE支付不要。
  290. if (sendAmout.intValue() == 0) {
  291. //
  292. payApi.pay(orderManageTotal, getLoginUser());
  293. return AjaxResult.success("Line支払不要");
  294. }
  295. // 接入支付接口
  296. LinePayAPOD resp = linePay.pay(orderManageTotal, orderManageTotalService.getDeptName(orderManageTotal.getId()));
  297. //ok
  298. if (resp != null && "0000".equals(resp.returnCode)) {
  299. //set the transactionId to the db.
  300. for (TOrderManage order : orderManageTotal.getOrderList()) {
  301. order.setTransactionId(resp.info.transactionId);
  302. orderManageService.updateTOrderManage(order);
  303. }
  304. //update ordermoney
  305. orderManageTotalService.updateTOrderManageTotal(orderManageTotal);
  306. return AjaxResult.success(resp.info);
  307. }
  308. //line支付error
  309. return AjaxResult.error("LINE PAY失敗しました。");
  310. }
  311. /**
  312. * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付
  313. *
  314. * @param orderManageTotal
  315. * @return
  316. */
  317. @Log(title = "キャンセルCallBack", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  318. @RepeatSubmit
  319. @PostMapping("/cancel")
  320. public AjaxResult apayCancelCallBack(@RequestBody LineCallBankParam param) {
  321. String[] orderIds = param.getOrderId().split("_");
  322. for (int i = 1; i < orderIds.length; i++) {
  323. //
  324. TOrderManage select = new TOrderManage();
  325. select.setTransactionId(param.getTransactionId());
  326. select.setId(Long.valueOf(orderIds[i]));
  327. select.setDelFlag("1");
  328. orderManageService.updateTOrderManage(select);
  329. }
  330. return AjaxResult.success("支払キャンセルしました。");
  331. }
  332. /**
  333. * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付
  334. *
  335. * @param orderManageTotal
  336. * @return
  337. */
  338. @Log(title = "用户支付CallBack", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT)
  339. @RepeatSubmit
  340. @PostMapping("/confirm")
  341. public AjaxResult aiPayCallBack(@RequestBody LineCallBankParam param) {
  342. String[] orderIds = param.getOrderId().split("_");
  343. List<TOrderManage> orderList = new ArrayList<TOrderManage>();
  344. for (int i = 1; i < orderIds.length; i++) {
  345. //
  346. TOrderManage order = orderManageService.selectTOrderManageById(Long.valueOf(orderIds[i]));
  347. orderList.add(order);
  348. }
  349. TOrderManageTotal orderManageTotal = orderManageTotalService.selectTOrderManageTotalById(Long.valueOf(orderIds[0]));
  350. orderManageTotal.setOrderList(orderList);
  351. // 接入支付接口
  352. LinePayConfirmAPOD resp = null;
  353. //0元支付不要。
  354. if (orderManageTotal.getOrderMoney().longValue() > 0) {
  355. resp = linePay.payConfirm(param.getTransactionId(), orderManageTotal.getOrderMoney().longValue());
  356. }
  357. if (resp == null || "0000".equals(resp.returnCode)) {
  358. payApi.pay(orderManageTotal, getLoginUser());
  359. return AjaxResult.success("支払成功");
  360. } else {
  361. return AjaxResult.success("支払失敗した");
  362. }
  363. }
  364. }