OrderServiceImpl.java 32 KB

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