OrderController.java 30 KB

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