OrderServiceImpl.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. package com.kmall.admin.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.kmall.admin.dao.*;
  4. import com.kmall.admin.entity.*;
  5. import com.kmall.admin.entity.OrderProcessRecordEntity;
  6. import com.kmall.admin.service.OrderService;
  7. import com.kmall.common.constant.Dict;
  8. import com.kmall.api.service.merch.OmsMerchPropertiesBuilder;
  9. import com.kmall.api.util.CommonUtil;
  10. import com.kmall.common.entity.SysUserEntity;
  11. import com.kmall.common.service.print.ticket.PrintTicketPropertiesBuilder;
  12. import com.kmall.common.utils.Constant;
  13. import com.kmall.common.utils.ShiroUtils;
  14. import com.kmall.common.utils.print.ticket.TicketPrintUtil;
  15. import com.kmall.common.utils.print.ticket.item.*;
  16. import com.kmall.common.utils.RRException;
  17. import com.kmall.common.utils.StringUtils;
  18. import com.kmall.common.utils.express.kdn.KdniaoUtil;
  19. import com.kmall.common.utils.wechat.WechatRefundApiResult;
  20. import com.kmall.common.utils.wechat.wxglobal.dto.WechatGlobalRefundApiResult;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import java.math.BigDecimal;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. @Service("orderService")
  28. public class OrderServiceImpl
  29. implements OrderService {
  30. @Autowired
  31. private OrderDao orderDao;
  32. @Autowired
  33. private ShippingDao shippingDao;
  34. @Autowired
  35. private OrderGoodsDao orderGoodsDao;
  36. @Autowired
  37. private SysPrinterDao printerDao;
  38. @Autowired
  39. private UserDao userDao;
  40. @Autowired
  41. private UserCouponDao userCouponDao;
  42. @Autowired
  43. private ProductStoreRelaDao productStoreRelaDao;
  44. @Autowired
  45. private OrderProcessRecordDao orderProcessRecordDao;
  46. @Autowired
  47. private OrderRefundDao orderRefundDao;
  48. @Autowired
  49. private StoreDao storeDao;
  50. @Autowired
  51. private OfflineCartDao offlineCartDao;
  52. @Autowired
  53. private GoodsDao goodsDao;
  54. @Autowired
  55. private MerchDao merchDao;
  56. @Override
  57. public OrderEntity queryObject(Long id) {
  58. return orderDao.queryObject(id);
  59. }
  60. @Override
  61. public Double getTotalActualPrice(String merchOrderSn) {
  62. return orderDao.getTotalActualPrice(merchOrderSn);
  63. }
  64. @Override
  65. public List<OrderEntity> queryList(Map<String, Object> map) {
  66. return orderDao.queryList(map);
  67. }
  68. @Override
  69. public int queryTotal(Map<String, Object> map) {
  70. return orderDao.queryTotal(map);
  71. }
  72. @Override
  73. public int save(OrderEntity order) {
  74. return orderDao.save(order);
  75. }
  76. @Override
  77. public int update(OrderEntity order) {
  78. return orderDao.update(order);
  79. }
  80. /**
  81. * 取消订单
  82. *
  83. * @param order
  84. */
  85. @Override
  86. public void cancelOrder(OrderEntity order) {
  87. boolean needUpdateStock = true;
  88. if (order.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_0.getItem())) {
  89. order.setOrderStatus(Integer.parseInt(Dict.orderStatus.item_101.getItem()));
  90. }
  91. // 判断是否有优惠券
  92. UserCouponEntity couponVo = userCouponDao.queryByOrderId(order.getId());
  93. if (null != couponVo) {
  94. userCouponDao.cancelOrder(couponVo);
  95. }
  96. orderDao.update(order);
  97. if (!needUpdateStock) {
  98. return;
  99. }
  100. // 更新库存
  101. updateStock(order);
  102. }
  103. @Override
  104. public int delete(Long id) {
  105. return orderDao.delete(id);
  106. }
  107. @Override
  108. public int deleteBatch(Long[] ids) {
  109. return orderDao.deleteBatch(ids);
  110. }
  111. @Override
  112. public int confirm(Long id) {
  113. OrderEntity orderEntity = queryObject(id);
  114. Integer shippingStatus = orderEntity.getShippingStatus();//发货状态
  115. Integer payStatus = orderEntity.getPayStatus();//付款状态
  116. if (2 != payStatus) {
  117. throw new RRException("此订单未付款,不能确认收货!");
  118. }
  119. if (4 == shippingStatus) {
  120. throw new RRException("此订单处于退货状态,不能确认收货!");
  121. }
  122. if (0 == shippingStatus) {
  123. throw new RRException("此订单未发货,不能确认收货!");
  124. }
  125. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_301.getItem())) {
  126. throw new RRException("此订单已完成!");
  127. }
  128. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_0.getItem())) {
  129. throw new RRException("此订单待付款!");
  130. }
  131. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_100.getItem())) {
  132. throw new RRException("此订单付款中!");
  133. }
  134. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_101.getItem())) {
  135. throw new RRException("此订单已取消!");
  136. }
  137. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_102.getItem())) {
  138. throw new RRException("此订单已删除!");
  139. }
  140. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_201.getItem())) {
  141. throw new RRException("此订单还未发货!");
  142. }
  143. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_401.getItem())) {
  144. throw new RRException("此订单已退款!");
  145. }
  146. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_402.getItem())) {
  147. throw new RRException("此订单已退款!");
  148. }
  149. orderEntity.setShippingStatus(2);
  150. orderEntity.setOrderStatus(301);
  151. orderEntity.setConfirmTime(new Date());
  152. orderDao.update(orderEntity);
  153. return 0;
  154. }
  155. @Override
  156. public int sendGoods(OrderEntity order) {
  157. Integer payStatus = order.getPayStatus();//付款状态
  158. if (2 != payStatus) {
  159. throw new RRException("此订单未付款!");
  160. }
  161. ShippingEntity shippingEntity = shippingDao.queryObject(order.getShippingId());
  162. if (null != shippingEntity) {
  163. order.setShippingName(shippingEntity.getName());
  164. }
  165. order.setOrderStatus(300);//订单已发货
  166. order.setShippingStatus(1);//已发货
  167. return orderDao.update(order);
  168. }
  169. @Override
  170. public Ticket printMsg(Long id) {
  171. OrderEntity orderEntity = queryInfos(id);
  172. List<OrderGoodsEntity> orderGoodsEntityList = orderEntity.getOrderGoodsEntityList();
  173. // 获取门店
  174. StoreEntity storeEntity = storeDao.queryObject(orderEntity.getStoreId());
  175. // 获取清关信息
  176. OrderProcessRecordEntity orderProcessRecordEntity =
  177. orderProcessRecordDao.queryObjectByOrderSn(orderEntity.getOrderSn());
  178. // 小票头
  179. TicketHead head = new TicketHead();
  180. head.setTitle(OmsMerchPropertiesBuilder.instance().getMerchName() + storeEntity.getStoreName());
  181. // head.setMemberId(orderEntity.getUserName().toString());
  182. head.setOrderId(orderEntity.getOrderSn());
  183. head.setTradeTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE").format(new Date()));
  184. // 商品信息
  185. Integer goodsTotal = 0; // 商品总个数
  186. BigDecimal total = Constant.ZERO; // 商品总计
  187. List<Goods> goodsList = new ArrayList<>();
  188. for (OrderGoodsEntity orderGoods : orderGoodsEntityList) {
  189. goodsTotal += orderGoods.getNumber();
  190. total = total.add(orderGoods.getRetailPrice().multiply(new BigDecimal(orderGoods.getNumber())))
  191. .setScale(2, BigDecimal.ROUND_HALF_UP);
  192. Goods goods = new Goods(orderGoods.getGoodsName(), orderGoods.getRetailPrice().toString(),
  193. orderGoods.getNumber().toString(),
  194. orderGoods.getRetailPrice().multiply(new BigDecimal(orderGoods.getNumber()))
  195. .setScale(2, BigDecimal.ROUND_HALF_UP).toString());
  196. goodsList.add(goods);
  197. }
  198. // 收银信息
  199. CashInfo cashInfo = new CashInfo();
  200. cashInfo.setGoodsTotal(goodsTotal.toString());
  201. cashInfo.setTotal(total.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
  202. cashInfo.setReceipts(orderEntity.getActualPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
  203. cashInfo.setOddChange("0.00");
  204. cashInfo.setCoupon(orderEntity.getCouponPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
  205. cashInfo.setFreight(
  206. new BigDecimal(orderEntity.getFreightPrice()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
  207. cashInfo.setPaymentMode("微信支付");
  208. // 海关清单
  209. CusListing cusListing = new CusListing();
  210. cusListing.setOrderId(orderEntity.getOrderSn());
  211. if (!orderEntity.getOrderBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
  212. cusListing.setWaybillId(orderProcessRecordEntity.getLogisticsNo());
  213. cusListing.setInvtNo(orderProcessRecordEntity.getInvtNo());
  214. cusListing.setConsignee(orderEntity.getConsignee());
  215. cusListing.setConsigneeTel(orderEntity.getMobile());
  216. }
  217. cusListing.setOriginAddress(PrintTicketPropertiesBuilder.instance().getAddress());
  218. cusListing.setDeliveryAddress(storeEntity.getStoreAddress());
  219. return TicketPrintUtil.print(head, goodsList, cashInfo, cusListing);
  220. }
  221. /**
  222. * 退款
  223. */
  224. @Transactional
  225. public void refund(OrderEntity order, WechatRefundApiResult result) {
  226. boolean needUpdateStock = true;
  227. if (order.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_201.getItem())) {
  228. order.setOrderStatus(Integer.parseInt(Dict.orderStatus.item_401.getItem()));
  229. }
  230. if (Dict.payFlag.item_cash.getItem().equals(order.getPayFlag())) {
  231. order.setPayStatus(Integer.parseInt(Dict.payStatus.item_4.getItem()));
  232. }else{
  233. order.setPayStatus(Integer.parseInt(Dict.payStatus.item_3.getItem()));
  234. OrderRefundEntity mallOrderRefund = orderRefundDao.queryObjectByOrderId(order.getId());
  235. OrderRefundEntity orderRefund = new OrderRefundEntity();
  236. orderRefund.setRefundId(result.getRefund_id());
  237. orderRefund.setOutRefundNo(result.getOut_refund_no());
  238. orderRefund
  239. .setRefundMoney(BigDecimal.valueOf(Long.valueOf(result.getRefund_fee())).divide(Constant.ONE_HUNDRED));
  240. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_5.getItem()));//退款处理中
  241. orderRefund.setModTime(new Date());
  242. if (mallOrderRefund != null) {
  243. orderRefund.setId(mallOrderRefund.getId());
  244. orderRefund.setRefundType(mallOrderRefund.getRefundType());
  245. orderRefundDao.update(orderRefund);
  246. } else {
  247. orderRefund.setOrderId(Integer.parseInt(order.getId() + ""));
  248. orderRefund.setUserId(Integer.parseInt(order.getUserId() + ""));
  249. orderRefund.setCreateTime(new Date());
  250. orderRefundDao.save(orderRefund);//退款记录
  251. }
  252. }
  253. orderDao.update(order);//修改为退款中
  254. // 判断是否有优惠券
  255. UserCouponEntity couponVo = userCouponDao.queryByOrderId(order.getId());
  256. if (null != couponVo) {
  257. userCouponDao.cancelOrder(couponVo);
  258. }
  259. if (!needUpdateStock) {
  260. return;
  261. }
  262. // 更新库存
  263. updateStock(order);
  264. }
  265. /**
  266. * 微信国际退款逻辑处理
  267. * @param order
  268. * @param result
  269. */
  270. @Transactional
  271. public void globalRefund(OrderEntity order, WechatGlobalRefundApiResult result) {
  272. boolean needUpdateStock = true;
  273. if (order.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_201.getItem())) {
  274. order.setOrderStatus(Integer.parseInt(Dict.orderStatus.item_401.getItem()));
  275. }
  276. order.setPayStatus(Integer.parseInt(Dict.payStatus.item_3.getItem()));
  277. OrderRefundEntity orderRefund = new OrderRefundEntity();
  278. orderRefund.setRefundId(result.getRefund_id());
  279. orderRefund.setOutRefundNo(result.getOut_refund_no());
  280. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_5.getItem()));//退款处理中
  281. orderRefund.setModTime(new Date());
  282. orderRefund.setRefundMoney(BigDecimal.valueOf(Long.valueOf(result.getRefund_fee())).divide(Constant.ONE_HUNDRED));
  283. orderRefund.setRefundFeeType(result.getRefund_fee_type());
  284. if(org.apache.commons.lang3.StringUtils.isNotEmpty(result.getTotal_fee())) {
  285. orderRefund.setTotalFee(Integer.parseInt(BigDecimal.valueOf(Long.valueOf(result.getTotal_fee())).divide(Constant.ONE_HUNDRED).toString()));
  286. }
  287. orderRefund.setFeeType(result.getFee_type());
  288. if(org.apache.commons.lang3.StringUtils.isNotEmpty(result.getCash_fee())) {
  289. orderRefund.setCashFee(Integer.parseInt(BigDecimal.valueOf(Long.valueOf(result.getCash_fee())).divide(Constant.ONE_HUNDRED).toString()));
  290. }
  291. orderRefund.setCashFeeType(result.getCash_fee_type());
  292. if(org.apache.commons.lang3.StringUtils.isNotEmpty(result.getCash_refund_fee())) {
  293. orderRefund.setCashRefundFee(Integer.parseInt(BigDecimal.valueOf(Long.valueOf(result.getCash_refund_fee())).divide(Constant.ONE_HUNDRED).toString()));
  294. }
  295. orderRefund.setCashRefundFeeType(result.getCash_refund_fee_type());
  296. OrderRefundEntity mallOrderRefund = orderRefundDao.queryObjectByOrderId(order.getId());
  297. if (mallOrderRefund != null) {
  298. orderRefund.setRefundType(mallOrderRefund.getRefundType());
  299. orderRefund.setId(mallOrderRefund.getId());
  300. orderRefundDao.update(orderRefund);
  301. } else {//退款记录不存在
  302. orderRefund.setOrderId(Integer.parseInt(order.getId() + ""));
  303. orderRefund.setUserId(Integer.parseInt(order.getUserId() + ""));
  304. orderRefund.setCreateTime(new Date());
  305. orderRefundDao.save(orderRefund);//退款记录
  306. }
  307. orderDao.update(order);//修改为退款中
  308. // 判断是否有优惠券
  309. UserCouponEntity couponVo = userCouponDao.queryByOrderId(order.getId());
  310. if (null != couponVo) {
  311. userCouponDao.cancelOrder(couponVo);
  312. }
  313. if (!needUpdateStock) {
  314. return;
  315. }
  316. // 更新库存
  317. updateStock(order);
  318. }
  319. private void updateStock(OrderEntity order){
  320. Map<String, Object> map = new HashMap<String, Object>();
  321. map.put("orderId", order.getId());
  322. List<OrderGoodsEntity> orderGoodsVoList = orderGoodsDao.queryList(map);
  323. for (OrderGoodsEntity orderGoodsEntity : orderGoodsVoList) {
  324. ProductStoreRelaEntity storeRelaEntity = productStoreRelaDao
  325. .queryByStoreIdProductId(Long.valueOf(order.getStoreId()),
  326. Long.valueOf(orderGoodsEntity.getProductId()));
  327. if (null == storeRelaEntity || null == storeRelaEntity.getSellVolume()) {
  328. storeRelaEntity.setSellVolume(0);
  329. }
  330. BigDecimal sellVolume = new BigDecimal(storeRelaEntity.getSellVolume() - orderGoodsEntity.getNumber());//销售量
  331. if (sellVolume.compareTo(Constant.ZERO) < 0) {
  332. sellVolume = Constant.ZERO;
  333. }
  334. storeRelaEntity.setSellVolume(Integer.parseInt(sellVolume.toString()));
  335. if (null == storeRelaEntity.getStockNum()) {
  336. storeRelaEntity.setStockNum(0);
  337. }
  338. storeRelaEntity.setStockNum(storeRelaEntity.getStockNum() + orderGoodsEntity.getNumber());//库存数量
  339. productStoreRelaDao.update(storeRelaEntity);
  340. }
  341. }
  342. /**
  343. * 处理用户退款申请
  344. *
  345. * @param orderInfo
  346. */
  347. @Transactional
  348. public void applyRefundDeal(OrderEntity orderInfo, OrderRefundEntity refundEntity) {
  349. refundEntity.setApprovalTime(new Date());
  350. // refundEntity.setApprover(ShiroUtils.getUserId());
  351. // 退积分
  352. try {
  353. Integer integral = 1;
  354. if (orderInfo.getActualPrice().intValue() > 0) {
  355. integral = orderInfo.getActualPrice().intValue();
  356. }
  357. if (refundEntity.getRefundStatus() == 2) {
  358. orderInfo.setOrderStatus(401);
  359. UserEntity entity = userDao.queryObject(orderInfo.getUserId());
  360. } else {
  361. orderInfo.setOrderStatus(201);
  362. }
  363. } catch (Exception e) {
  364. e.printStackTrace();
  365. }
  366. orderRefundDao.update(refundEntity);
  367. orderDao.update(orderInfo);
  368. }
  369. @Override
  370. public OrderRefundEntity queryRefundObject(Long refundId) {
  371. return orderRefundDao.queryObject(refundId);
  372. }
  373. @Override
  374. public List<OrderRefundEntity> queryRefundList(Map<String, Object> map) {
  375. return orderRefundDao.queryList(map);
  376. }
  377. @Override
  378. public int queryRefundTotal(Map<String, Object> map) {
  379. return orderRefundDao.queryTotal(map);
  380. }
  381. @Override
  382. public int getUserOrderInfo(Map<String, Object> params) {
  383. int result = 0;
  384. String type = (String) params.get("type");
  385. if ("yfkOrderUserSum".equals(type)) {
  386. result = orderDao.getYfkOrderUserSum();
  387. } else if ("oderUserSum".equals(type)) {
  388. result = orderDao.getOderUserSum();
  389. } else if ("todayUserOrder".equals(type)) {
  390. result = orderDao.getTodayUserOrder();
  391. } else if ("todayUserSales".equals(type)) {
  392. result = orderDao.getTodayUserSales();
  393. } else if ("incomeSum".equals(type)) {
  394. result = orderDao.getIncomeSum();
  395. } else if ("payedOrderCount".equals(type)) {
  396. result = orderDao.getPayedOrderCount();
  397. }
  398. return result;
  399. }
  400. @Override
  401. public Map getLogistics(Long id) {
  402. OrderEntity orderEntity = queryObject(id);
  403. if (orderEntity == null) {
  404. throw new RRException("此订单不存在!");
  405. }
  406. if (orderEntity.getShippingStatus() == 0) {
  407. if (orderEntity.getOrderBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem()) ||
  408. orderEntity.getOrderBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())) {
  409. throw new RRException("此订单还未发货!");
  410. } else {
  411. if (StringUtils.isNullOrEmpty(orderEntity.getShippingNo())) {
  412. throw new RRException("此订单还未发货!");
  413. }
  414. }
  415. }
  416. Map logisticsInfo = new HashMap();
  417. List<WuliuEntity> wuliuEntityList = new ArrayList<>();
  418. OrderProcessRecordEntity orderProcessRecordEntity =
  419. orderProcessRecordDao.queryObjectByOrderSn(orderEntity.getOrderSn());
  420. if (orderProcessRecordEntity != null) {
  421. if (orderEntity.getOrderBizType().equals(Dict.orderBizType.item_10.getItem()) ||
  422. orderEntity.getOrderBizType().equals(Dict.orderBizType.item_02.getItem())) {
  423. WuliuEntity wuliuEntity = new WuliuEntity();
  424. wuliuEntity.setAcceptTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  425. .format(orderProcessRecordEntity.getCustomsSuccTime()));
  426. wuliuEntity.setAcceptStation(PrintTicketPropertiesBuilder.instance().getAddress() + "送达" +
  427. storeDao.queryObject(orderEntity.getStoreId()).getStoreAddress() +
  428. ";交易完成,用户已提走");
  429. wuliuEntityList.add(wuliuEntity);
  430. }
  431. if (StringUtils.isNotEmpty(orderEntity.getShippingCode()) &&
  432. StringUtils.isNotEmpty(orderEntity.getShippingNo()) &&
  433. orderProcessRecordEntity.getShipmentStartTime() != null) {
  434. JSONObject traces =
  435. KdniaoUtil.getOrderTracesByJson(orderEntity.getShippingCode(), orderEntity.getShippingNo());
  436. List<Map> mapList = (List<Map>) traces.get("Traces");
  437. if (mapList != null && mapList.size() > 0) {
  438. for (Map map : mapList) {
  439. WuliuEntity wuliuEntity = new WuliuEntity();
  440. wuliuEntity.setAcceptTime(map.get("AcceptTime").toString());
  441. wuliuEntity.setAcceptStation(map.get("AcceptStation").toString());
  442. wuliuEntityList.add(wuliuEntity);
  443. }
  444. }
  445. String state = traces.get("State") + "";
  446. if (Dict.logisticsStatus.item_0.getItem().equals(state) && traces.getBoolean("Success")) {
  447. String reason = traces.get("Reason") + "";
  448. WuliuEntity wuliuEntity = new WuliuEntity();
  449. wuliuEntity.setAcceptStation(reason);
  450. wuliuEntityList.add(wuliuEntity);
  451. }
  452. }
  453. if (Dict.isSend.item_1.getItem().equalsIgnoreCase(orderProcessRecordEntity.getIsCustomsSend()) &&
  454. orderProcessRecordEntity.getShipmentStartTime() != null) {
  455. WuliuEntity wuliuEntity = new WuliuEntity();
  456. wuliuEntity.setAcceptStation("订单已出库");
  457. wuliuEntity.setAcceptTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  458. .format(orderProcessRecordEntity.getShipmentStartTime()));
  459. wuliuEntityList.add(wuliuEntity);
  460. }
  461. if (Dict.isSend.item_1.getItem().equals(orderProcessRecordEntity.getIsCustomsSend())) {
  462. WuliuEntity wuliuEntity = new WuliuEntity();
  463. wuliuEntity.setAcceptStation("订单清关完成,等待仓库发货");
  464. wuliuEntity.setAcceptTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  465. .format(orderProcessRecordEntity.getCustomsSuccTime()));
  466. wuliuEntityList.add(wuliuEntity);
  467. }
  468. if (Dict.isSend.item_0.getItem().equalsIgnoreCase(orderProcessRecordEntity.getIsCustomsSend())) {
  469. WuliuEntity wuliuEntity = new WuliuEntity();
  470. wuliuEntity.setAcceptStation("订单清关失败");
  471. wuliuEntityList.add(wuliuEntity);
  472. }
  473. if (Dict.isSend.item_1.getItem().equalsIgnoreCase(orderProcessRecordEntity.getIsEleOrderSend()) &&
  474. orderProcessRecordEntity.getWaybillSuccTime() != null) {
  475. WuliuEntity wuliuEntity = new WuliuEntity();
  476. wuliuEntity.setAcceptStation("海关三单发送成功");
  477. wuliuEntity.setAcceptTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  478. .format(orderProcessRecordEntity.getWaybillSuccTime()));
  479. wuliuEntityList.add(wuliuEntity);
  480. }
  481. if (Dict.isSend.item_1.getItem().equals(orderProcessRecordEntity.getIsPaymentSend())) {
  482. WuliuEntity wuliuEntity = new WuliuEntity();
  483. wuliuEntity.setAcceptStation("订单支付成功");
  484. wuliuEntity.setAcceptTime(
  485. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(orderProcessRecordEntity.getPaySuccTime()));
  486. wuliuEntityList.add(wuliuEntity);
  487. }
  488. if (Dict.isSend.item_1.getItem().equalsIgnoreCase(orderProcessRecordEntity.getIsAddOrderSend())) {
  489. WuliuEntity wuliuEntity = new WuliuEntity();
  490. wuliuEntity.setAcceptStation("订单下单成功");
  491. wuliuEntity.setAcceptTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  492. .format(orderProcessRecordEntity.getAddOrderSuccTime()));
  493. wuliuEntityList.add(wuliuEntity);
  494. }
  495. }
  496. logisticsInfo.put("tracesList", wuliuEntityList);
  497. logisticsInfo.put("logisticCode", orderEntity.getShippingNo());
  498. logisticsInfo.put("shipperCode", orderEntity.getShippingCode());
  499. return logisticsInfo;
  500. }
  501. @Override
  502. public OrderEntity queryInfos(Long id) {
  503. OrderEntity orderEntity = orderDao.queryObject(id);
  504. Map<String, Object> map = new HashMap<String, Object>();
  505. map.put("orderId", id);
  506. List<OrderGoodsEntity> orderGoodsEntityList = orderGoodsDao.queryList(map);
  507. orderEntity.setOrderGoodsEntityList(orderGoodsEntityList);
  508. return orderEntity;
  509. }
  510. /**
  511. * 普货商品生成线下订单
  512. * @param offlineCartList
  513. * @param user
  514. * @return
  515. */
  516. @Transactional
  517. @Override
  518. public Map orderSubmit(List<OfflineCartEntity> offlineCartList, SysUserEntity user) {
  519. Map resultObj = new HashMap();
  520. try {
  521. if (user == null) {
  522. resultObj.put("errno", 400);
  523. resultObj.put("errmsg", "用户登录超时,请重新登录");
  524. return resultObj;
  525. }
  526. if (!user.getRoleType().equalsIgnoreCase("2")) {
  527. resultObj.put("errno", 400);
  528. resultObj.put("errmsg", "该操作只允许店员账户操作");
  529. return resultObj;
  530. }
  531. if (org.apache.commons.lang3.StringUtils.isEmpty(user.getMerchSn())) {
  532. resultObj.put("errno", 400);
  533. resultObj.put("errmsg", "操作用户的商户编号为空,请先维护用户商户编号信息再来操作");
  534. return resultObj;
  535. }
  536. Long userId = user.getUserId();
  537. Integer storeId = user.getStoreId();
  538. //获取要购买的商品
  539. Map param = new HashMap();
  540. param.put("userId", userId);
  541. param.put("storeId", storeId);
  542. if (null == offlineCartList && offlineCartList.size() == 0) {
  543. resultObj.put("errno", 400);
  544. resultObj.put("errmsg", "购买商品数据为空");
  545. return resultObj;
  546. }
  547. List<OfflineCartEntity> offlineCartEntityList = offlineCartDao.queryOfflineCartByBizType(param);
  548. if (offlineCartEntityList.size() == 0 && offlineCartList.size() > 0) {
  549. resultObj.put("errno", 0);
  550. resultObj.put("errmsg", "该单已下单成功,请重新扫描商品进行购买");
  551. return resultObj;
  552. }
  553. // 检查库存和更新库存
  554. for (OfflineCartEntity cartEntity : offlineCartEntityList) {
  555. if (goodsDao.queryObject(cartEntity.getGoodsId()) == null) {
  556. resultObj.put("errno", 400);
  557. resultObj.put("errmsg", "订单提交失败:商品不存在");
  558. return resultObj;
  559. }
  560. //取得规格的信息,判断规格库存
  561. ProductStoreRelaEntity productInfo = productStoreRelaDao
  562. .queryByGoodsIdAndStoreId(Long.valueOf(storeId), Long.valueOf(cartEntity.getGoodsId()));
  563. synchronized (productInfo) {
  564. if (null == productInfo || null == productInfo.getStockNum() ||
  565. productInfo.getStockNum() < cartEntity.getNumber()) {
  566. resultObj.put("errno", 400);
  567. resultObj.put("errmsg", "库存不足,仅剩余" + productInfo.getStockNum());
  568. return resultObj;
  569. } else {
  570. productInfo.setStockNum(productInfo.getStockNum() - cartEntity.getNumber());
  571. productInfo.setStoreId(Long.valueOf(storeId));
  572. productInfo.addSellVolume();
  573. productStoreRelaDao.updateStockNum(productInfo);
  574. }
  575. }
  576. }
  577. String merchOrderSn = "EMATO" + CommonUtil.generateOrderNumber();
  578. OrderEntity order = setOrderVo(user, offlineCartEntityList, user.getMerchSn());
  579. order.setStoreId(storeId);
  580. order.setMerchOrderSn(merchOrderSn);
  581. //开启事务,插入订单信息和订单商品
  582. if (order != null) {
  583. orderDao.save(order);
  584. if (null == order.getId()) {
  585. resultObj.put("errno", 400);
  586. resultObj.put("errmsg", "订单提交失败");
  587. return resultObj;
  588. }
  589. for (OfflineCartEntity cartEntity : offlineCartEntityList) {
  590. OrderGoodsEntity orderGoodsEntity = setOrderGoodsVo(order, cartEntity);
  591. //新增订单详情
  592. orderGoodsDao.save(orderGoodsEntity);
  593. }
  594. //清空预订单商品临时表
  595. offlineCartDao.deleteByUserId(userId);
  596. Map orderInfoMap = new HashMap();
  597. orderInfoMap.put("orderInfo", order);
  598. resultObj.put("errno", 0);
  599. resultObj.put("errmsg", "订单提交成功");
  600. resultObj.put("data", orderInfoMap);
  601. }
  602. } catch (Exception e) {
  603. e.printStackTrace();
  604. resultObj.put("errno", 400);
  605. resultObj.put("errmsg", "订单异常");
  606. return resultObj;
  607. }
  608. return resultObj;
  609. }
  610. /**
  611. * 设置订单数据
  612. *
  613. * @param loginUser
  614. * @return
  615. */
  616. public OrderEntity setOrderVo(SysUserEntity loginUser, List<OfflineCartEntity> offlineCartEntityList,
  617. String merchSn) {
  618. OrderEntity orderInfo = new OrderEntity();
  619. BigDecimal freightPrice = new BigDecimal(0.00);
  620. BigDecimal goodsTotalPrice = new BigDecimal(0.00);
  621. for (OfflineCartEntity offlineCartEntity : offlineCartEntityList) {
  622. goodsTotalPrice = goodsTotalPrice
  623. .add(offlineCartEntity.getRetailPrice().multiply(new BigDecimal(offlineCartEntity.getNumber())));
  624. }
  625. //订单价格计算:订单的总价+运费
  626. BigDecimal orderTotalPrice = goodsTotalPrice.add(freightPrice);
  627. //查询未使用的优惠券
  628. /*String couponName = "";
  629. BigDecimal fullCutCouponDec = Constant.ZERO;
  630. UserCouponVo couponVo = null;*/
  631. BigDecimal couponPrice = new BigDecimal(0.00);
  632. //减去其它支付的金额后,要实际支付的金额 订单的总价+运费-优惠券金额
  633. BigDecimal actualPrice = orderTotalPrice.subtract(couponPrice);
  634. //商户(拼音首字母)+业务类型+编号
  635. // String merchSn = OmsMerchPropertiesBuilder.instance().getMerchSn();
  636. // String merchShortName = OmsMerchPropertiesBuilder.instance().getMerchShortName();
  637. MerchEntity merchEntity = merchDao.findByMerchSn(merchSn);
  638. String merchShortName = "";
  639. if (merchEntity != null) {
  640. merchShortName = merchEntity.getMerchShortName();
  641. }
  642. String orderSn = merchShortName + Dict.orderBizType.item_11.getItem() + CommonUtil.generateOrderNumber();
  643. orderInfo.setOrderSn(orderSn);
  644. orderInfo.setMerchSn(merchSn);
  645. orderInfo.setUserId(Integer.parseInt(loginUser.getUserId() + ""));
  646. orderInfo.setFreightPrice(freightPrice.intValue());
  647. orderInfo.setOrderBizType(Dict.orderBizType.item_11.getItem());
  648. /*orderInfo.setCoupon_id(userCouponId);
  649. orderInfo.setCoupon_name(couponName);*/
  650. // orderInfo.setFullCutPrice(fullCutCouponDec);//使用的优惠券
  651. orderInfo.setCouponPrice(couponPrice);
  652. orderInfo.setPostscript("");//留言
  653. orderInfo.setAddTime(new Date());
  654. orderInfo.setGoodsPrice(goodsTotalPrice);
  655. orderInfo.setOrderPrice(orderTotalPrice);
  656. orderInfo.setActualPrice(actualPrice);
  657. orderInfo.setOrderType("1");
  658. orderInfo.setOrderStatus(0);// 待付款
  659. orderInfo.setShippingStatus(0);
  660. orderInfo.setPayStatus(0);
  661. orderInfo.setShippingId(0L);
  662. orderInfo.setShippingFee(Constant.ZERO);
  663. orderInfo.setIntegral(0);
  664. orderInfo.setIntegralMoney(Constant.ZERO);
  665. orderInfo.setCreateTime(new Date());
  666. orderInfo.setModTime(new Date());
  667. orderInfo.setIsOnfflineOrder(Dict.isOnfflineOrder.item_1.getItem());//线下购买
  668. /*//标记该订单已使用优惠券
  669. if(couponVo != null){
  670. couponVo.setUsed_time(new Date());
  671. couponVo.setIsUsed(Dict.isUsed.item_1.getItem());
  672. apiUserCouponMapper.update(couponVo);
  673. }*/
  674. return orderInfo;
  675. }
  676. public OrderGoodsEntity setOrderGoodsVo(OrderEntity orderInfo, OfflineCartEntity goodsItem) {
  677. GoodsEntity goodsVo = goodsDao.queryObject(goodsItem.getGoodsId());
  678. OrderGoodsEntity orderGoodsVo = new OrderGoodsEntity();
  679. orderGoodsVo.setOrderId(Integer.parseInt(orderInfo.getId() + ""));
  680. orderGoodsVo.setGoodsId(goodsItem.getGoodsId());
  681. orderGoodsVo.setGoodsSn(goodsItem.getGoodsSn());
  682. orderGoodsVo.setProductId(goodsItem.getProductId());
  683. orderGoodsVo.setGoodsName(goodsItem.getGoodsName());
  684. orderGoodsVo.setListPicUrl(goodsItem.getListPicUrl());
  685. orderGoodsVo.setMarketPrice(goodsItem.getMarketPrice());
  686. orderGoodsVo.setRetailPrice(goodsItem.getRetailPrice());
  687. orderGoodsVo.setNumber(goodsItem.getNumber());
  688. orderGoodsVo.setOrderBizType(Dict.orderBizType.item_11.getItem());
  689. orderGoodsVo.setCreateTime(new Date());
  690. orderGoodsVo.setModTime(new Date());
  691. orderGoodsVo.setGoodsRate(goodsVo.getGoodsRate());
  692. orderGoodsVo.setSku(goodsVo.getSku());
  693. return orderGoodsVo;
  694. }
  695. @Override
  696. public OrderEntity queryObjectBySysUser(Long id) {
  697. OrderEntity orderEntity = orderDao.queryObjectBySysUser(id);
  698. Map<String, Object> map = new HashMap<String, Object>();
  699. map.put("orderId", id);
  700. List<OrderGoodsEntity> orderGoodsEntityList = orderGoodsDao.queryList(map);
  701. orderEntity.setOrderGoodsEntityList(orderGoodsEntityList);
  702. return orderEntity;
  703. }
  704. @Override
  705. public int confirmPay(Long id, String payFlag,String orderSnWx) {
  706. SysUserEntity user = ShiroUtils.getUserEntity();
  707. if (user == null) {
  708. throw new RRException("用户登录超时,请重新登录");
  709. }
  710. if (!user.getRoleType().equalsIgnoreCase("2")) {
  711. throw new RRException("该操作只允许店员账户操作");
  712. }
  713. OrderEntity orderEntity = queryObject(id);
  714. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_201.getItem())) {
  715. throw new RRException("此订单已付款!");
  716. }
  717. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_301.getItem())) {
  718. throw new RRException("此订单已完成!");
  719. }
  720. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_101.getItem())) {
  721. throw new RRException("此订单已取消!");
  722. }
  723. if (orderEntity.getOrderStatus() == Integer.parseInt(Dict.orderStatus.item_102.getItem())) {
  724. throw new RRException("此订单已删除!");
  725. }
  726. orderEntity.setOrderStatus(Integer.parseInt(Dict.orderStatus.item_201.getItem()));
  727. orderEntity.setPayStatus(Integer.parseInt(Dict.payStatus.item_2.getItem()));
  728. orderEntity.setPayFlag(payFlag);
  729. orderEntity.setPayTime(new Date());
  730. if(orderSnWx != null){
  731. orderEntity.setOrderSnWx(orderSnWx);
  732. }
  733. orderDao.update(orderEntity);
  734. return 0;
  735. }
  736. @Override
  737. public List<OrderEntity> queryOffilineOrderList(Map<String, Object> map) {
  738. return orderDao.queryOffilineOrderList(map);
  739. }
  740. }