OrderController.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package com.kmall.admin.controller;
  2. import com.kmall.admin.entity.*;
  3. import com.kmall.admin.service.*;
  4. import com.kmall.admin.service.OrderExceptionRecordService;
  5. import com.kmall.admin.service.OrderProcessRecordService;
  6. import com.kmall.admin.service.OrderService;
  7. import com.kmall.admin.utils.ParamUtils;
  8. import com.kmall.common.constant.Dict;
  9. import com.kmall.common.entity.SysUserEntity;
  10. import com.kmall.common.utils.*;
  11. import com.kmall.common.utils.print.ticket.item.Ticket;
  12. import com.kmall.common.utils.wechat.WechatMicropayApiResult;
  13. import com.kmall.common.utils.wechat.WechatRefundApiResult;
  14. import com.kmall.common.utils.wechat.WechatReverseApiResult;
  15. import com.kmall.common.utils.wechat.WechatUtil;
  16. import com.kmall.common.utils.wechat.wxglobal.WechatGlobalUtil;
  17. import com.kmall.common.utils.wechat.wxglobal.dto.WechatGlobalRefundApiResult;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.apache.shiro.authz.annotation.RequiresPermissions;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.web.bind.annotation.*;
  22. import java.math.BigDecimal;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * @author Scott
  28. * @email
  29. * @date 2017-08-13 10:41:09
  30. */
  31. @RestController
  32. @RequestMapping("order")
  33. public class OrderController {
  34. @Autowired
  35. private OrderService orderService;
  36. @Autowired
  37. private OrderProcessRecordService orderProcessRecordService;
  38. @Autowired
  39. private OrderRefundService orderRefundService;
  40. @Autowired
  41. private OrderExceptionRecordService orderExceptionRecordService;
  42. @Autowired
  43. private StoreService storeService;
  44. @Autowired
  45. private OrderWXPayRecordService orderWXPayRecordService;
  46. /**
  47. * 列表
  48. */
  49. @RequestMapping("/list")
  50. @RequiresPermissions("order:list")
  51. public R list(@RequestParam Map<String, Object> params) {
  52. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);
  53. //查询列表数据
  54. Query query = new Query(params);
  55. query.put("isOnfiilineOrder", Dict.isOnfflineOrder.item_0.getItem());
  56. List<OrderEntity> orderList = orderService.queryList(query);
  57. int total = orderService.queryTotal(query);
  58. PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());
  59. return R.ok().put("page", pageUtil);
  60. }
  61. /**
  62. * 信息
  63. */
  64. @RequestMapping("/info/{id}")
  65. @RequiresPermissions("order:info")
  66. public R info(@PathVariable("id") Long id) {
  67. OrderEntity order = orderService.queryObject(id);
  68. return R.ok().put("order", order);
  69. }
  70. /**
  71. * 信息
  72. */
  73. @RequestMapping("/infos/{id}")
  74. @RequiresPermissions("order:infos")
  75. public R infos(@PathVariable("id") Long id) {
  76. OrderEntity order = orderService.queryInfos(id);
  77. return R.ok().put("order", order);
  78. }
  79. /**
  80. * 保存
  81. */
  82. @RequestMapping("/save")
  83. @RequiresPermissions("order:save")
  84. public R save(@RequestBody OrderEntity order) {
  85. orderService.save(order);
  86. return R.ok();
  87. }
  88. /**
  89. * 修改
  90. */
  91. @RequestMapping("/update")
  92. @RequiresPermissions("order:update")
  93. public R update(@RequestBody OrderEntity order) {
  94. orderService.update(order);
  95. return R.ok();
  96. }
  97. /**
  98. * 删除
  99. */
  100. @RequestMapping("/delete")
  101. @RequiresPermissions("order:delete")
  102. public R delete(@RequestBody Long[] ids) {
  103. orderService.deleteBatch(ids);
  104. return R.ok();
  105. }
  106. /**
  107. * 查看所有列表
  108. */
  109. @RequestMapping("/queryAll")
  110. public R queryAll(@RequestParam Map<String, Object> params) {
  111. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);
  112. List<OrderEntity> list = orderService.queryList(params);
  113. return R.ok().put("list", list);
  114. }
  115. /**
  116. * 总计
  117. */
  118. @RequestMapping("/queryTotal")
  119. public R queryTotal(@RequestParam Map<String, Object> params) {
  120. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);
  121. int sum = orderService.queryTotal(params);
  122. return R.ok().put("sum", sum);
  123. }
  124. /**
  125. * 确定收货
  126. *
  127. * @param id
  128. * @return
  129. */
  130. @RequestMapping("/confirm")
  131. @RequiresPermissions("order:confirm")
  132. public R confirm(@RequestBody Long id) {
  133. orderService.confirm(id);
  134. return R.ok();
  135. }
  136. /**
  137. * 发货
  138. *
  139. * @param order
  140. * @return
  141. */
  142. @RequestMapping("/sendGoods")
  143. @RequiresPermissions("order:sendGoods")
  144. public R sendGoods(@RequestBody OrderEntity order) {
  145. orderService.sendGoods(order);
  146. return R.ok();
  147. }
  148. /**
  149. * 跟踪快递轨迹
  150. *
  151. * @param id
  152. * @return
  153. */
  154. @RequestMapping("/getLogistics/{id}")
  155. @RequiresPermissions("order:getLogistics")
  156. public R getLogistics(@PathVariable("id") Long id) {
  157. Map result = orderService.getLogistics(id);
  158. return R.ok().put("result", result);
  159. }
  160. /**
  161. * 获取订单清关信息
  162. *
  163. * @param orderSn
  164. * @return
  165. */
  166. @RequestMapping("/getProcess/{orderSn}")
  167. @RequiresPermissions("order:getProcess")
  168. public R getProcess(@PathVariable("orderSn") String orderSn) {
  169. OrderProcessRecordEntity orderProcessRecordEntity = orderProcessRecordService.queryObjectByOrderSn(orderSn);
  170. if (orderProcessRecordEntity != null) {
  171. if (orderProcessRecordEntity.getShipmentStartTime() != null) {
  172. orderProcessRecordEntity.setShipmentStartTimeStr(
  173. DateUtils.format(orderProcessRecordEntity.getShipmentStartTime(), DateUtils.DATE_TIME_PATTERN));
  174. }
  175. if (orderProcessRecordEntity.getShipmentSuccTime() != null) {
  176. orderProcessRecordEntity.setShipmentSuccTimeStr(
  177. DateUtils.format(orderProcessRecordEntity.getShipmentSuccTime(), DateUtils.DATE_TIME_PATTERN));
  178. }
  179. if (orderProcessRecordEntity.getCustomsStartTime() != null) {
  180. orderProcessRecordEntity.setCustomsStartTimeStr(
  181. DateUtils.format(orderProcessRecordEntity.getCustomsStartTime(), DateUtils.DATE_TIME_PATTERN));
  182. }
  183. if (orderProcessRecordEntity.getCustomsSuccTime() != null) {
  184. orderProcessRecordEntity.setCustomsSuccTimeStr(
  185. DateUtils.format(orderProcessRecordEntity.getCustomsSuccTime(), DateUtils.DATE_TIME_PATTERN));
  186. }
  187. if (orderProcessRecordEntity.getWaybillStartTime() != null) {
  188. orderProcessRecordEntity.setWaybillStartTimeStr(
  189. DateUtils.format(orderProcessRecordEntity.getWaybillStartTime(), DateUtils.DATE_TIME_PATTERN));
  190. }
  191. if (orderProcessRecordEntity.getWaybillSuccTime() != null) {
  192. orderProcessRecordEntity.setWaybillSuccTimeStr(
  193. DateUtils.format(orderProcessRecordEntity.getWaybillSuccTime(), DateUtils.DATE_TIME_PATTERN));
  194. }
  195. if (orderProcessRecordEntity.getEleOrderStartTime() != null) {
  196. orderProcessRecordEntity.setEleOrderStartTimeStr(
  197. DateUtils.format(orderProcessRecordEntity.getEleOrderStartTime(), DateUtils.DATE_TIME_PATTERN));
  198. }
  199. if (orderProcessRecordEntity.getEleOrderSuccTime() != null) {
  200. orderProcessRecordEntity.setEleOrderSuccTimeStr(
  201. DateUtils.format(orderProcessRecordEntity.getEleOrderSuccTime(), DateUtils.DATE_TIME_PATTERN));
  202. }
  203. if (orderProcessRecordEntity.getAddOrderStartTime() != null) {
  204. orderProcessRecordEntity.setAddOrderStartTimeStr(
  205. DateUtils.format(orderProcessRecordEntity.getAddOrderStartTime(), DateUtils.DATE_TIME_PATTERN));
  206. }
  207. if (orderProcessRecordEntity.getAddOrderSuccTime() != null) {
  208. orderProcessRecordEntity.setAddOrderSuccTimeStr(
  209. DateUtils.format(orderProcessRecordEntity.getAddOrderSuccTime(), DateUtils.DATE_TIME_PATTERN));
  210. }
  211. if (orderProcessRecordEntity.getPaySuccTime() != null) {
  212. orderProcessRecordEntity.setPaySuccTimeStr(
  213. DateUtils.format(orderProcessRecordEntity.getPaySuccTime(), DateUtils.DATE_TIME_PATTERN));
  214. }
  215. if (orderProcessRecordEntity.getPayStartTime() != null) {
  216. orderProcessRecordEntity.setPayStartTimeStr(
  217. DateUtils.format(orderProcessRecordEntity.getPayStartTime(), DateUtils.DATE_TIME_PATTERN));
  218. }
  219. if (orderProcessRecordEntity.getPaymentStartTime() != null) {
  220. orderProcessRecordEntity.setPaymentStartTimeStr(
  221. DateUtils.format(orderProcessRecordEntity.getPaymentStartTime(), DateUtils.DATE_TIME_PATTERN));
  222. }
  223. if (orderProcessRecordEntity.getPaymentSuccTime() != null) {
  224. orderProcessRecordEntity.setPaymentSuccTimeStr(
  225. DateUtils.format(orderProcessRecordEntity.getPaymentSuccTime(), DateUtils.DATE_TIME_PATTERN));
  226. }
  227. }
  228. return R.ok().put("orderProcessRecordEntity", orderProcessRecordEntity);
  229. }
  230. /**
  231. * 打印小票
  232. *
  233. * @param id
  234. * @return
  235. */
  236. @RequestMapping("/printMsg")
  237. public R printMsg(@RequestBody Long id) {
  238. Ticket ticket = orderService.printMsg(id);
  239. return R.ok().put("ticket", ticket);
  240. }
  241. /**
  242. * 订单取消请求
  243. */
  244. @RequiresPermissions(value = {"order:refund"})
  245. @RequestMapping(value = "cancel", method = RequestMethod.POST)
  246. public Object cancel(Long orderId) {
  247. OrderEntity orderInfo = orderService.queryObject(orderId);
  248. if (null == orderInfo) {
  249. return R.error("订单不存在");
  250. }
  251. if (orderInfo.getOrderStatus() != 0) {
  252. return R.error("订单状态不支持取消");
  253. }
  254. orderService.cancelOrder(orderInfo);
  255. return R.ok();
  256. }
  257. /**
  258. * 订单退款请求
  259. */
  260. @RequiresPermissions(value = {"order:refund"})
  261. @RequestMapping(value = "refund", method = RequestMethod.POST)
  262. public Object refund(Long orderId, BigDecimal refundMoney) {
  263. OrderEntity orderInfo = orderService.queryObject(orderId);
  264. if (null == orderInfo) {
  265. return R.error("订单不存在");
  266. }
  267. if (orderInfo.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_401.getItem()) ||
  268. orderInfo.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_402.getItem())) {
  269. return R.error("订单已退款");
  270. }
  271. Double totalActualPrice = orderService.getTotalActualPrice(orderInfo.getMerchOrderSn());
  272. if (totalActualPrice == null) {
  273. totalActualPrice = 0d;
  274. }
  275. String refundResult = "";
  276. if (orderInfo.getOrderStatus() != Integer.parseInt(Dict.orderStatus.item_0.getItem())) {
  277. if(orderInfo.getPayFlag().equalsIgnoreCase(Dict.payFlag.item_wxglobalpay.getItem())){
  278. refundResult = wxGlobalRefund(orderInfo,totalActualPrice);
  279. if(StringUtils.isNotBlank(refundResult)){
  280. return R.error(refundResult);
  281. }
  282. }else{
  283. refundResult = wxRefund(orderInfo,totalActualPrice);
  284. if(StringUtils.isNotBlank(refundResult)){
  285. return R.error(refundResult);
  286. }
  287. }
  288. }
  289. return R.ok("退款成功");
  290. }
  291. /**
  292. * 微信申请退款
  293. * @param orderInfo
  294. * @param totalActualPrice
  295. * @return
  296. */
  297. private String wxRefund(OrderEntity orderInfo,Double totalActualPrice){
  298. WechatRefundApiResult result = WechatUtil.wxRefund(orderInfo.getMerchOrderSn().toString(), totalActualPrice,
  299. orderInfo.getActualPrice().doubleValue());
  300. if (result.getResult_code().equals(WechatUtil.WXTradeState.SUCCESS.getCode())) {
  301. orderService.refund(orderInfo, result);
  302. } else {
  303. OrderRefundEntity mallOrderRefund = orderRefundService.queryObjectByOrderId(orderInfo.getId());
  304. OrderRefundEntity orderRefund = new OrderRefundEntity();
  305. orderRefund.setRefundType(Integer.parseInt(Dict.RefundType.item_1.getItem()));
  306. orderRefund.setRefundMoney(BigDecimal.valueOf(orderInfo.getActualPrice().doubleValue()));
  307. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  308. orderRefund.setModTime(new Date());
  309. orderRefund.setOutRefundNo(result.getOut_refund_no());
  310. if (mallOrderRefund != null) {
  311. orderRefund.setId(mallOrderRefund.getId());
  312. orderRefundService.update(orderRefund);//退款记录
  313. }
  314. OrderExceptionRecordEntity mallOrderExceptionRecord = new OrderExceptionRecordEntity();
  315. mallOrderExceptionRecord.setUserId(Integer.parseInt(orderInfo.getUserId() + ""));
  316. mallOrderExceptionRecord.setOrderSn(orderInfo.getOrderSn());
  317. mallOrderExceptionRecord.setExceptionStatus(Dict.exceptionStatus.item_03.getItem());
  318. mallOrderExceptionRecord.setExceptionContent("退款失败" + result.getErr_code_des());
  319. mallOrderExceptionRecord.setCreateTime(new Date());
  320. orderExceptionRecordService.save(mallOrderExceptionRecord);
  321. return result.getErr_code_des();
  322. }
  323. return "";
  324. }
  325. /**
  326. * 微信国际申请退款
  327. * @param orderInfo
  328. * @param totalActualPrice
  329. * @return
  330. */
  331. private String wxGlobalRefund(OrderEntity orderInfo,Double totalActualPrice){
  332. WechatGlobalRefundApiResult result = WechatGlobalUtil.wxRefund(orderInfo.getMerchOrderSn().toString(), totalActualPrice,
  333. orderInfo.getActualPrice().doubleValue());
  334. if (result.getResult_code().equals(WechatUtil.WXTradeState.SUCCESS.getCode())) {
  335. orderService.globalRefund(orderInfo, result);
  336. } else {
  337. OrderRefundEntity mallOrderRefund = orderRefundService.queryObjectByOrderId(orderInfo.getId());
  338. OrderRefundEntity orderRefund = new OrderRefundEntity();
  339. orderRefund.setRefundType(Integer.parseInt(Dict.RefundType.item_1.getItem()));
  340. orderRefund.setRefundMoney(BigDecimal.valueOf(orderInfo.getActualPrice().doubleValue()));
  341. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  342. orderRefund.setOutRefundNo(result.getOut_refund_no());
  343. orderRefund.setModTime(new Date());
  344. if (mallOrderRefund != null) {
  345. orderRefund.setId(mallOrderRefund.getId());
  346. orderRefundService.update(orderRefund);//退款记录
  347. }
  348. OrderExceptionRecordEntity mallOrderExceptionRecord = new OrderExceptionRecordEntity();
  349. mallOrderExceptionRecord.setUserId(Integer.parseInt(orderInfo.getUserId() + ""));
  350. mallOrderExceptionRecord.setOrderSn(orderInfo.getOrderSn());
  351. mallOrderExceptionRecord.setExceptionStatus(Dict.exceptionStatus.item_03.getItem());
  352. mallOrderExceptionRecord.setExceptionContent("退款失败" + result.getErr_code_des());
  353. mallOrderExceptionRecord.setCreateTime(new Date());
  354. orderExceptionRecordService.save(mallOrderExceptionRecord);
  355. return result.getErr_code_des();
  356. }
  357. return "";
  358. }
  359. /**
  360. * 订单退款请求
  361. */
  362. @RequiresPermissions(value = {"order:offilineRefund"})
  363. @RequestMapping(value = "offilineRefund", method = RequestMethod.POST)
  364. public Object offilineRefund(Long orderId) {
  365. OrderEntity orderInfo = orderService.queryObject(orderId);
  366. if (null == orderInfo) {
  367. return R.error("订单不存在");
  368. }
  369. if (orderInfo.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_401.getItem()) ||
  370. orderInfo.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_402.getItem())) {
  371. return R.error("订单已退款");
  372. }
  373. if (orderInfo.getOrderStatus() != Integer.parseInt(Dict.orderStatus.item_0.getItem())) {
  374. if (Dict.payFlag.item_cash.getItem().equals(orderInfo.getPayFlag())) {
  375. //现金支付,直接修改订单状态
  376. orderService.refund(orderInfo, null);
  377. } else if (Dict.payFlag.item_weixin.getItem().equals(orderInfo.getPayFlag())) {
  378. //微信线下扫码支付
  379. WechatRefundApiResult result = WechatUtil
  380. .wxRefund(orderInfo.getOrderSnWx(), orderInfo.getActualPrice().doubleValue(),
  381. orderInfo.getActualPrice().doubleValue());
  382. if (result.getResult_code().equals(WechatUtil.WXTradeState.SUCCESS.getCode())) {
  383. orderService.refund(orderInfo, result);
  384. } else {
  385. OrderRefundEntity mallOrderRefund = orderRefundService.queryObjectByOrderId(orderInfo.getId());
  386. OrderRefundEntity orderRefund = new OrderRefundEntity();
  387. orderRefund.setRefundType(Integer.parseInt(Dict.RefundType.item_1.getItem()));
  388. orderRefund.setRefundMoney(BigDecimal.valueOf(orderInfo.getActualPrice().doubleValue()));
  389. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  390. orderRefund.setOutRefundNo(result.getOut_refund_no());
  391. orderRefund.setModTime(new Date());
  392. if (mallOrderRefund != null) {
  393. orderRefund.setId(mallOrderRefund.getId());
  394. orderRefundService.update(orderRefund);//退款记录
  395. }
  396. OrderExceptionRecordEntity mallOrderExceptionRecord = new OrderExceptionRecordEntity();
  397. mallOrderExceptionRecord.setUserId(Integer.parseInt(orderInfo.getUserId() + ""));
  398. mallOrderExceptionRecord.setOrderSn(orderInfo.getOrderSn());
  399. mallOrderExceptionRecord.setExceptionStatus(Dict.exceptionStatus.item_03.getItem());
  400. mallOrderExceptionRecord.setExceptionContent("退款失败" + result.getErr_code_des());
  401. mallOrderExceptionRecord.setCreateTime(new Date());
  402. orderExceptionRecordService.save(mallOrderExceptionRecord);
  403. return R.error(result.getErr_code_des());
  404. }
  405. }
  406. }
  407. return R.ok("退款成功");
  408. }
  409. /**
  410. * 获取首页展示信息--会员购买率相关
  411. *
  412. * @param params
  413. * @return
  414. */
  415. @RequestMapping("/getUserOrderInfo")
  416. public R getUserOrderInfo(@RequestParam Map<String, Object> params) {
  417. int result = orderService.getUserOrderInfo(params);
  418. return R.ok().put("result", result);
  419. }
  420. @RequiresPermissions(value = {"order:orderSubmit"})
  421. @RequestMapping(value = "orderSubmit", method = RequestMethod.POST)
  422. @ResponseBody
  423. public R orderSubmit(@RequestBody List<OfflineCartEntity> offlineCartEntityList) {
  424. Map resultObj = null;
  425. try {
  426. SysUserEntity user = ShiroUtils.getUserEntity();
  427. resultObj = orderService.orderSubmit(offlineCartEntityList, user);
  428. if (((Integer) resultObj.get("errno")) != 0) {
  429. return R.error((String) resultObj.get("errmsg"));
  430. }
  431. } catch (Exception e) {
  432. e.printStackTrace();
  433. }
  434. return R.ok("订单提交成功");
  435. }
  436. /**
  437. * 确认付款
  438. *
  439. * @param id
  440. * @return
  441. */
  442. @RequestMapping("/confirmPay")
  443. @RequiresPermissions("order:confirmPay")
  444. public R confirmPay(@RequestBody Long id) {
  445. orderService.confirmPay(id, Dict.payFlag.item_cash.getItem(), null);
  446. return R.ok();
  447. }
  448. @RequestMapping("/offilineOrderList")
  449. @RequiresPermissions("order:offilineOrderList")
  450. public R offilineOrderList(@RequestParam Map<String, Object> params) {
  451. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);
  452. //查询列表数据
  453. Query query = new Query(params);
  454. query.put("isOnfiilineOrder", Dict.isOnfflineOrder.item_1.getItem());
  455. List<OrderEntity> orderList = orderService.queryOffilineOrderList(query);
  456. int total = orderService.queryTotal(query);
  457. PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());
  458. return R.ok().put("page", pageUtil);
  459. }
  460. @RequestMapping("/offlineInfos/{id}")
  461. @RequiresPermissions("order:offlineInfos")
  462. public R queryObjectBySysUser(@PathVariable("id") Long id) {
  463. OrderEntity order = orderService.queryObjectBySysUser(id);
  464. List<OrderWXPayRecordEntity> payRecords = orderWXPayRecordService.getRecordsByOutTradeNo(order.getOrderSn());
  465. order.setPayRecordList(payRecords);
  466. return R.ok().put("order", order);
  467. }
  468. @RequestMapping("/wxMicropayPay")
  469. @RequiresPermissions("order:wxMicropayPay")
  470. public R wxMicropayPay(Long id, String auth_code) {
  471. R r = null;
  472. SysUserEntity user = ShiroUtils.getUserEntity();
  473. if (user == null) {
  474. throw new RRException("用户登录超时,请重新登录");
  475. }
  476. if (!user.getRoleType().equalsIgnoreCase("2")) {
  477. throw new RRException("该操作只允许店员账户操作");
  478. }
  479. OrderEntity orderEntity = orderService.queryObject(id);
  480. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_201.getItem())) {
  481. throw new RRException("此订单已付款!");
  482. }
  483. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_301.getItem())) {
  484. throw new RRException("此订单已完成!");
  485. }
  486. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_101.getItem())) {
  487. throw new RRException("此订单已取消!");
  488. }
  489. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_102.getItem())) {
  490. throw new RRException("此订单已删除!");
  491. }
  492. //保存支付记录
  493. OrderWXPayRecordEntity orderWXPayRecordCurrent = orderWXPayRecordService.saveRecord(orderEntity);
  494. StoreEntity store = storeService.queryObject(orderEntity.getStoreId());
  495. WechatMicropayApiResult wechatMicropayApiResult = WechatUtil
  496. .wxMicropay(store.getMerchName() + "-" + store.getStoreName(), orderEntity.getOrderBizType(), null,
  497. orderWXPayRecordCurrent.getOutTradeNoWX(), orderEntity.getActualPrice().doubleValue(),
  498. "127.0.0.1", auth_code);
  499. orderWXPayRecordService.updateRecord(orderWXPayRecordCurrent.getId(), wechatMicropayApiResult);
  500. //当支付成功时,修改订单,并把其他支付记录撤销
  501. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatMicropayApiResult.getTrade_state())) {
  502. //查询当前订单所有的支付记录
  503. List<OrderWXPayRecordEntity> orderWXPayRecordEntitys =
  504. orderWXPayRecordService.getRecordsByOutTradeNo(orderEntity.getOrderSn());
  505. for (OrderWXPayRecordEntity orderWXPayRecordTemp : orderWXPayRecordEntitys) {
  506. //查询出来的记录不等于当前记录,并且未撤销,未关闭时,撤销订单
  507. if (orderWXPayRecordTemp.getId() != orderWXPayRecordCurrent.getId() &&
  508. (!WechatUtil.WXTradeState.REVOKED.getCode().equals(orderWXPayRecordTemp.getTradeState()) ||
  509. !WechatUtil.WXTradeState.CLOSED.getCode().equals(orderWXPayRecordTemp.getTradeState()))) {
  510. WechatReverseApiResult wechatReverseApiResult =
  511. WechatUtil.wxReverse(orderWXPayRecordTemp.getOutTradeNoWX());
  512. //撤销订单成功
  513. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatReverseApiResult.getReturn_code()) &&
  514. WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatReverseApiResult.getResult_code())) {
  515. //调用订单查询接口
  516. WechatRefundApiResult wechatRefundApiResult =
  517. WechatUtil.wxOrderQuery(orderWXPayRecordTemp.getOutTradeNoWX());
  518. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatRefundApiResult.getReturn_code()) &&
  519. WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatRefundApiResult.getResult_code())) {
  520. // 修改订单支付记录
  521. orderWXPayRecordService
  522. .updateWXPayRecordTradeState(orderWXPayRecordTemp.getId(), wechatRefundApiResult);
  523. }
  524. }
  525. }
  526. }
  527. orderService.confirmPay(id, Dict.payFlag.item_weixin.getItem(), orderWXPayRecordCurrent.getOutTradeNoWX());
  528. r = R.ok();
  529. //用户支付中
  530. } else if (WechatUtil.WXTradeState.USERPAYING.getCode().equals(wechatMicropayApiResult.getTrade_state())) {
  531. r = R.error(WechatUtil.WXTradeState.USERPAYING.getCodeZn() + ",稍等片刻后请刷新页面重新查看订单状态");
  532. //用户支付失败
  533. } else if (WechatUtil.WXTradeState.PAYERROR.getCode().equals(wechatMicropayApiResult.getTrade_state())) {
  534. WechatReverseApiResult wechatReverseApiResult =
  535. WechatUtil.wxReverse(orderWXPayRecordCurrent.getOutTradeNoWX());
  536. //撤销订单成功
  537. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatReverseApiResult.getReturn_code()) &&
  538. WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatReverseApiResult.getResult_code())) {
  539. //调用订单查询接口
  540. WechatRefundApiResult wechatRefundApiResult =
  541. WechatUtil.wxOrderQuery(orderWXPayRecordCurrent.getOutTradeNoWX());
  542. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatRefundApiResult.getReturn_code()) &&
  543. WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatRefundApiResult.getResult_code())) {
  544. // 修改订单支付记录
  545. orderWXPayRecordService
  546. .updateWXPayRecordTradeState(orderWXPayRecordCurrent.getId(), wechatRefundApiResult);
  547. }
  548. r = R.error(orderWXPayRecordCurrent.getErrCodeDes());
  549. } else {
  550. r = R.error(wechatReverseApiResult.getErr_code_des());
  551. }
  552. }
  553. return r;
  554. }
  555. }