OrderServiceImpl.java 33 KB

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