OrderController.java 30 KB

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