1
0

OrderServiceImpl.java 34 KB

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