OrderController.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. OrderProcessRecordEntity processRecordEntity = orderProcessRecordService.queryObjectByOrderSn(orderInfo.getOrderSn());
  284. if(org.apache.commons.lang.StringUtils.isNotEmpty(processRecordEntity.getIsCustomsSend())) {
  285. if (processRecordEntity.getIsCustomsSend().equalsIgnoreCase("2")) {
  286. return R.error("订单正在清关中,不能退款");
  287. }
  288. if (processRecordEntity.getIsCustomsSend().equalsIgnoreCase("1")) {
  289. return R.error("订单已清关完成,不能退款");
  290. }
  291. }
  292. String refundResult = "";
  293. if (orderInfo.getOrderStatus() != Integer.parseInt(Dict.orderStatus.item_0.getItem())) {
  294. if (orderInfo.getPayFlag().equalsIgnoreCase(Dict.payFlag.item_wxglobalpay.getItem())) {
  295. refundResult = wxGlobalRefund(orderInfo, totalActualPrice);
  296. if (StringUtils.isNotBlank(refundResult)) {
  297. return R.error(refundResult);
  298. }
  299. } else if (orderInfo.getPayFlag().equalsIgnoreCase(Dict.payFlag.item_pingan.getItem())) {
  300. refundResult = pinganRefund(orderInfo, refundId);
  301. if (StringUtils.isNotBlank(refundResult)) {
  302. return R.error(refundResult);
  303. }
  304. } else{
  305. refundResult = wxRefund(orderInfo,totalActualPrice);
  306. if(StringUtils.isNotBlank(refundResult)){
  307. return R.error(refundResult);
  308. }
  309. }
  310. }
  311. return R.ok("退款成功");
  312. }
  313. /**
  314. * 平安申请退款
  315. * @param orderInfo
  316. * @return
  317. */
  318. private String pinganRefund(OrderEntity orderInfo, String refundId){
  319. Integer refundAmount = orderInfo.getActualPrice().multiply(new BigDecimal(100)).intValue();
  320. PinganResponseDto responseDto = PinganUtil.pinganPayRefund(orderInfo.getMerchOrderSn(), refundId, refundAmount, null);
  321. if (!Objects.isNull(responseDto)) {
  322. responseDto.setOutNo(orderInfo.getMerchOrderSn());
  323. responseDto.setCreateTime(new Date());
  324. pinganResponseService.save(responseDto);
  325. if ("0".equals(responseDto.getErrcode())) {
  326. JSONObject tradeResult = JSONObject.fromObject(responseDto.getDatajson());
  327. if (Dict.pinganRefundStatus.item_1.getItem().equals(tradeResult.getString("status"))) {
  328. orderService.pinganRefund(orderInfo, tradeResult, refundId);
  329. } else {
  330. OrderRefundEntity mallOrderRefund = orderRefundService.queryObjectByOrderId(orderInfo.getId());
  331. OrderRefundEntity orderRefund = new OrderRefundEntity();
  332. orderRefund.setRefundType(Integer.parseInt(Dict.RefundType.item_1.getItem()));
  333. orderRefund.setRefundMoney(BigDecimal.valueOf(orderInfo.getActualPrice().doubleValue()));
  334. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  335. orderRefund.setModTime(new Date());
  336. orderRefund.setOutRefundNo(tradeResult.getString("ord_no"));
  337. if (mallOrderRefund != null) {
  338. orderRefund.setId(mallOrderRefund.getId());
  339. orderRefundService.update(orderRefund);//退款记录
  340. }
  341. OrderExceptionRecordEntity mallOrderExceptionRecord = new OrderExceptionRecordEntity();
  342. mallOrderExceptionRecord.setUserId(Integer.parseInt(orderInfo.getUserId() + ""));
  343. mallOrderExceptionRecord.setOrderSn(orderInfo.getOrderSn());
  344. mallOrderExceptionRecord.setExceptionStatus(Dict.exceptionStatus.item_03.getItem());
  345. mallOrderExceptionRecord.setExceptionContent("退款失败");
  346. mallOrderExceptionRecord.setCreateTime(new Date());
  347. orderExceptionRecordService.save(mallOrderExceptionRecord);
  348. return "发起平安支付退款失败!";
  349. }
  350. } else {
  351. return responseDto.getMsg();
  352. }
  353. }
  354. return "";
  355. }
  356. /**
  357. * 微信申请退款
  358. * @param orderInfo
  359. * @param totalActualPrice
  360. * @return
  361. */
  362. private String wxRefund(OrderEntity orderInfo,Double totalActualPrice){
  363. WechatRefundApiResult result = WechatUtil.wxRefund(orderInfo.getMerchOrderSn().toString(), totalActualPrice,
  364. orderInfo.getActualPrice().doubleValue());
  365. if (result.getResult_code().equals(WechatUtil.WXTradeState.SUCCESS.getCode())) {
  366. orderService.refund(orderInfo, result);
  367. } else {
  368. OrderRefundEntity mallOrderRefund = orderRefundService.queryObjectByOrderId(orderInfo.getId());
  369. OrderRefundEntity orderRefund = new OrderRefundEntity();
  370. orderRefund.setRefundType(Integer.parseInt(Dict.RefundType.item_1.getItem()));
  371. orderRefund.setRefundMoney(BigDecimal.valueOf(orderInfo.getActualPrice().doubleValue()));
  372. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  373. orderRefund.setModTime(new Date());
  374. orderRefund.setOutRefundNo(result.getOut_refund_no());
  375. if (mallOrderRefund != null) {
  376. orderRefund.setId(mallOrderRefund.getId());
  377. orderRefundService.update(orderRefund);//退款记录
  378. }
  379. OrderExceptionRecordEntity mallOrderExceptionRecord = new OrderExceptionRecordEntity();
  380. mallOrderExceptionRecord.setUserId(Integer.parseInt(orderInfo.getUserId() + ""));
  381. mallOrderExceptionRecord.setOrderSn(orderInfo.getOrderSn());
  382. mallOrderExceptionRecord.setExceptionStatus(Dict.exceptionStatus.item_03.getItem());
  383. mallOrderExceptionRecord.setExceptionContent("退款失败" + result.getErr_code_des());
  384. mallOrderExceptionRecord.setCreateTime(new Date());
  385. orderExceptionRecordService.save(mallOrderExceptionRecord);
  386. return result.getErr_code_des();
  387. }
  388. return "";
  389. }
  390. /**
  391. * 微信国际申请退款
  392. * @param orderInfo
  393. * @param totalActualPrice
  394. * @return
  395. */
  396. private String wxGlobalRefund(OrderEntity orderInfo,Double totalActualPrice){
  397. WechatGlobalRefundApiResult result = WechatGlobalUtil.wxRefund(orderInfo.getMerchOrderSn().toString(), totalActualPrice,
  398. orderInfo.getActualPrice().doubleValue());
  399. if (result.getResult_code().equals(WechatUtil.WXTradeState.SUCCESS.getCode())) {
  400. orderService.globalRefund(orderInfo, result);
  401. } else {
  402. OrderRefundEntity mallOrderRefund = orderRefundService.queryObjectByOrderId(orderInfo.getId());
  403. OrderRefundEntity orderRefund = new OrderRefundEntity();
  404. orderRefund.setRefundType(Integer.parseInt(Dict.RefundType.item_1.getItem()));
  405. orderRefund.setRefundMoney(BigDecimal.valueOf(orderInfo.getActualPrice().doubleValue()));
  406. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  407. orderRefund.setOutRefundNo(result.getOut_refund_no());
  408. orderRefund.setModTime(new Date());
  409. if (mallOrderRefund != null) {
  410. orderRefund.setId(mallOrderRefund.getId());
  411. orderRefundService.update(orderRefund);//退款记录
  412. }
  413. OrderExceptionRecordEntity mallOrderExceptionRecord = new OrderExceptionRecordEntity();
  414. mallOrderExceptionRecord.setUserId(Integer.parseInt(orderInfo.getUserId() + ""));
  415. mallOrderExceptionRecord.setOrderSn(orderInfo.getOrderSn());
  416. mallOrderExceptionRecord.setExceptionStatus(Dict.exceptionStatus.item_03.getItem());
  417. mallOrderExceptionRecord.setExceptionContent("退款失败" + result.getErr_code_des());
  418. mallOrderExceptionRecord.setCreateTime(new Date());
  419. orderExceptionRecordService.save(mallOrderExceptionRecord);
  420. return result.getErr_code_des();
  421. }
  422. return "";
  423. }
  424. /**
  425. * 订单退款请求
  426. */
  427. @RequiresPermissions(value = {"order:offilineRefund"})
  428. @RequestMapping(value = "offilineRefund", method = RequestMethod.POST)
  429. public Object offilineRefund(Long orderId) {
  430. OrderEntity orderInfo = orderService.queryObject(orderId);
  431. if (null == orderInfo) {
  432. return R.error("订单不存在");
  433. }
  434. if (orderInfo.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_401.getItem()) ||
  435. orderInfo.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_402.getItem())) {
  436. return R.error("订单已退款");
  437. }
  438. if (orderInfo.getOrderStatus() != Integer.parseInt(Dict.orderStatus.item_0.getItem())) {
  439. if (Dict.payFlag.item_cash.getItem().equals(orderInfo.getPayFlag())) {
  440. //现金支付,直接修改订单状态
  441. orderService.refund(orderInfo, null);
  442. } else if (Dict.payFlag.item_weixin.getItem().equals(orderInfo.getPayFlag())) {
  443. //微信线下扫码支付
  444. WechatRefundApiResult result = WechatUtil
  445. .wxRefund(orderInfo.getOrderSnWx(), orderInfo.getActualPrice().doubleValue(),
  446. orderInfo.getActualPrice().doubleValue());
  447. if (result.getResult_code().equals(WechatUtil.WXTradeState.SUCCESS.getCode())) {
  448. orderService.refund(orderInfo, result);
  449. } else {
  450. OrderRefundEntity mallOrderRefund = orderRefundService.queryObjectByOrderId(orderInfo.getId());
  451. OrderRefundEntity orderRefund = new OrderRefundEntity();
  452. orderRefund.setRefundType(Integer.parseInt(Dict.RefundType.item_1.getItem()));
  453. orderRefund.setRefundMoney(BigDecimal.valueOf(orderInfo.getActualPrice().doubleValue()));
  454. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  455. orderRefund.setOutRefundNo(result.getOut_refund_no());
  456. orderRefund.setModTime(new Date());
  457. if (mallOrderRefund != null) {
  458. orderRefund.setId(mallOrderRefund.getId());
  459. orderRefundService.update(orderRefund);//退款记录
  460. }
  461. OrderExceptionRecordEntity mallOrderExceptionRecord = new OrderExceptionRecordEntity();
  462. mallOrderExceptionRecord.setUserId(Integer.parseInt(orderInfo.getUserId() + ""));
  463. mallOrderExceptionRecord.setOrderSn(orderInfo.getOrderSn());
  464. mallOrderExceptionRecord.setExceptionStatus(Dict.exceptionStatus.item_03.getItem());
  465. mallOrderExceptionRecord.setExceptionContent("退款失败" + result.getErr_code_des());
  466. mallOrderExceptionRecord.setCreateTime(new Date());
  467. orderExceptionRecordService.save(mallOrderExceptionRecord);
  468. return R.error(result.getErr_code_des());
  469. }
  470. }
  471. }
  472. return R.ok("退款成功");
  473. }
  474. /**
  475. * 获取首页展示信息--会员购买率相关
  476. *
  477. * @param params
  478. * @return
  479. */
  480. @RequestMapping("/getUserOrderInfo")
  481. public R getUserOrderInfo(@RequestParam Map<String, Object> params) {
  482. int result = orderService.getUserOrderInfo(params);
  483. return R.ok().put("result", result);
  484. }
  485. @RequiresPermissions(value = {"order:orderSubmit"})
  486. @RequestMapping(value = "orderSubmit", method = RequestMethod.POST)
  487. @ResponseBody
  488. public R orderSubmit(@RequestBody List<OfflineCartEntity> offlineCartEntityList) {
  489. Map resultObj = null;
  490. try {
  491. SysUserEntity user = ShiroUtils.getUserEntity();
  492. resultObj = orderService.orderSubmit(offlineCartEntityList, user);
  493. if (((Integer) resultObj.get("errno")) != 0) {
  494. return R.error((String) resultObj.get("errmsg"));
  495. }
  496. } catch (Exception e) {
  497. e.printStackTrace();
  498. }
  499. return R.ok("订单提交成功");
  500. }
  501. /**
  502. * 确认付款
  503. *
  504. * @param id
  505. * @return
  506. */
  507. @RequestMapping("/confirmPay")
  508. @RequiresPermissions("order:confirmPay")
  509. public R confirmPay(@RequestBody Long id) {
  510. orderService.confirmPay(id, Dict.payFlag.item_cash.getItem(), null);
  511. return R.ok();
  512. }
  513. @RequestMapping("/offilineOrderList")
  514. @RequiresPermissions("order:offilineOrderList")
  515. public R offilineOrderList(@RequestParam Map<String, Object> params) {
  516. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);
  517. //查询列表数据
  518. Query query = new Query(params);
  519. query.put("isOnfiilineOrder", Dict.isOnfflineOrder.item_1.getItem());
  520. List<OrderEntity> orderList = orderService.queryOffilineOrderList(query);
  521. int total = orderService.queryTotal(query);
  522. PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());
  523. return R.ok().put("page", pageUtil);
  524. }
  525. @RequestMapping("/offlineInfos/{id}")
  526. @RequiresPermissions("order:offlineInfos")
  527. public R queryObjectBySysUser(@PathVariable("id") Long id) {
  528. OrderEntity order = orderService.queryObjectBySysUser(id);
  529. List<OrderWXPayRecordEntity> payRecords = orderWXPayRecordService.getRecordsByOutTradeNo(order.getOrderSn());
  530. order.setPayRecordList(payRecords);
  531. return R.ok().put("order", order);
  532. }
  533. @RequestMapping("/wxMicropayPay")
  534. @RequiresPermissions("order:wxMicropayPay")
  535. public R wxMicropayPay(Long id, String auth_code) {
  536. R r = null;
  537. SysUserEntity user = ShiroUtils.getUserEntity();
  538. if (user == null) {
  539. throw new RRException("用户登录超时,请重新登录");
  540. }
  541. if (!user.getRoleType().equalsIgnoreCase("2")) {
  542. throw new RRException("该操作只允许店员账户操作");
  543. }
  544. OrderEntity orderEntity = orderService.queryObject(id);
  545. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_201.getItem())) {
  546. throw new RRException("此订单已付款!");
  547. }
  548. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_301.getItem())) {
  549. throw new RRException("此订单已完成!");
  550. }
  551. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_101.getItem())) {
  552. throw new RRException("此订单已取消!");
  553. }
  554. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_102.getItem())) {
  555. throw new RRException("此订单已删除!");
  556. }
  557. //保存支付记录
  558. OrderWXPayRecordEntity orderWXPayRecordCurrent = orderWXPayRecordService.saveRecord(orderEntity);
  559. StoreEntity store = storeService.queryObject(orderEntity.getStoreId());
  560. WechatMicropayApiResult wechatMicropayApiResult = WechatUtil
  561. .wxMicropay(store.getMerchName() + "-" + store.getStoreName(), orderEntity.getOrderBizType(), null,
  562. orderWXPayRecordCurrent.getOutTradeNoWX(), orderEntity.getActualPrice().doubleValue(),
  563. "127.0.0.1", auth_code);
  564. orderWXPayRecordService.updateRecord(orderWXPayRecordCurrent.getId(), wechatMicropayApiResult);
  565. //当支付成功时,修改订单,并把其他支付记录撤销
  566. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatMicropayApiResult.getTrade_state())) {
  567. //查询当前订单所有的支付记录
  568. List<OrderWXPayRecordEntity> orderWXPayRecordEntitys =
  569. orderWXPayRecordService.getRecordsByOutTradeNo(orderEntity.getOrderSn());
  570. for (OrderWXPayRecordEntity orderWXPayRecordTemp : orderWXPayRecordEntitys) {
  571. //查询出来的记录不等于当前记录,并且未撤销,未关闭时,撤销订单
  572. if (orderWXPayRecordTemp.getId() != orderWXPayRecordCurrent.getId() &&
  573. (!WechatUtil.WXTradeState.REVOKED.getCode().equals(orderWXPayRecordTemp.getTradeState()) ||
  574. !WechatUtil.WXTradeState.CLOSED.getCode().equals(orderWXPayRecordTemp.getTradeState()))) {
  575. WechatReverseApiResult wechatReverseApiResult =
  576. WechatUtil.wxReverse(orderWXPayRecordTemp.getOutTradeNoWX());
  577. //撤销订单成功
  578. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatReverseApiResult.getReturn_code()) &&
  579. WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatReverseApiResult.getResult_code())) {
  580. //调用订单查询接口
  581. WechatRefundApiResult wechatRefundApiResult =
  582. WechatUtil.wxOrderQuery(orderWXPayRecordTemp.getOutTradeNoWX());
  583. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatRefundApiResult.getReturn_code()) &&
  584. WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatRefundApiResult.getResult_code())) {
  585. // 修改订单支付记录
  586. orderWXPayRecordService
  587. .updateWXPayRecordTradeState(orderWXPayRecordTemp.getId(), wechatRefundApiResult);
  588. }
  589. }
  590. }
  591. }
  592. orderService.confirmPay(id, Dict.payFlag.item_weixin.getItem(), orderWXPayRecordCurrent.getOutTradeNoWX());
  593. r = R.ok();
  594. //用户支付中
  595. } else if (WechatUtil.WXTradeState.USERPAYING.getCode().equals(wechatMicropayApiResult.getTrade_state())) {
  596. r = R.error(WechatUtil.WXTradeState.USERPAYING.getCodeZn() + ",稍等片刻后请刷新页面重新查看订单状态");
  597. //用户支付失败
  598. } else if (WechatUtil.WXTradeState.PAYERROR.getCode().equals(wechatMicropayApiResult.getTrade_state())) {
  599. WechatReverseApiResult wechatReverseApiResult =
  600. WechatUtil.wxReverse(orderWXPayRecordCurrent.getOutTradeNoWX());
  601. //撤销订单成功
  602. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatReverseApiResult.getReturn_code()) &&
  603. WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatReverseApiResult.getResult_code())) {
  604. //调用订单查询接口
  605. WechatRefundApiResult wechatRefundApiResult =
  606. WechatUtil.wxOrderQuery(orderWXPayRecordCurrent.getOutTradeNoWX());
  607. if (WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatRefundApiResult.getReturn_code()) &&
  608. WechatUtil.WXTradeState.SUCCESS.getCode().equals(wechatRefundApiResult.getResult_code())) {
  609. // 修改订单支付记录
  610. orderWXPayRecordService
  611. .updateWXPayRecordTradeState(orderWXPayRecordCurrent.getId(), wechatRefundApiResult);
  612. }
  613. r = R.error(orderWXPayRecordCurrent.getErrCodeDes());
  614. } else {
  615. r = R.error(wechatReverseApiResult.getErr_code_des());
  616. }
  617. }
  618. return r;
  619. }
  620. }