OrderServiceImpl.java 32 KB

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