OrderController.java 34 KB

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