OrderService.java 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. package com.shop.service;/**
  2. * Created by 17173 on 2018/3/13.
  3. */
  4. import com.alibaba.fastjson.JSON;
  5. import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
  6. import com.github.miemiedev.mybatis.paginator.domain.PageList;
  7. import com.github.miemiedev.mybatis.paginator.domain.Paginator;
  8. import com.shop.constant.DmConstant;
  9. import com.shop.constant.MessageModel;
  10. import com.shop.dao.OrderDao;
  11. import com.shop.dao.ProductDao;
  12. import com.shop.exception.ParamException;
  13. import com.shop.gto.IndexDto;
  14. import com.shop.gto.OrderDto;
  15. import com.shop.model.*;
  16. import com.shop.util.AssertUtil;
  17. import com.shop.util.MessageModelUtil;
  18. import com.shop.vo.LoginIdentity;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import java.math.BigDecimal;
  24. import java.util.*;
  25. /**
  26. * @author DY
  27. * @create 2018-03-13 18:20
  28. **/
  29. @Service
  30. @Transactional
  31. public class OrderService {
  32. @Autowired
  33. private OrderDao orderDao;
  34. @Autowired
  35. private ProductDao productDao;
  36. /**
  37. * @author DY
  38. * @create 2018/3/13 19:43
  39. * 订单总数,销售总额,昨日销售总额,近七天销售总额接口接口
  40. */
  41. public MessageModel order_total_amount(LoginIdentity loginIdentity) {
  42. MessageModel messageModel = new MessageModel();
  43. Map map = new HashMap();
  44. OrderDto orderDto = getOrderDto(loginIdentity, DmConstant.VERSION_ONE);
  45. //拿到今日订单总数和订单总额
  46. orderDto.setTime(0);
  47. IndexDto indexDto = orderDao.queryOrderCountAmountTotalByUserId(orderDto);
  48. map.put("orderTotalCount", indexDto.getOrderTotalCount());
  49. map.put("orderTotalPriceToday", indexDto.getOrderTotalPrice());
  50. //拿到昨日销售总额
  51. orderDto.setTime(1);
  52. indexDto = orderDao.queryOrderCountAmountTotalByUserId(orderDto);
  53. map.put("orderTotalPriceYesterday", indexDto.getOrderTotalPrice());
  54. //拿到近7天销售总额
  55. orderDto.setTime(7);
  56. indexDto = orderDao.queryOrderAmountTotalByUserId(orderDto);
  57. BigDecimal totalPriceAweek = indexDto.getOrderTotalPrice();
  58. map.put("orderTotalPriceAweek", totalPriceAweek);
  59. messageModel.setCode(DmConstant.OPS_SUCCESS_CODE);
  60. messageModel.setMsg(DmConstant.OPS_SUCCESS_MSG);
  61. messageModel.setData(map);
  62. return messageModel;
  63. }
  64. private OrderDto getOrderDto(LoginIdentity loginIdentity, Integer version) {
  65. Integer store = loginIdentity.getStore();
  66. Integer type = loginIdentity.getType();
  67. OrderDto orderDto = new OrderDto();
  68. orderDto.setStore(store);
  69. orderDto.setVersion(version);
  70. orderDto.setType(type);
  71. return orderDto;
  72. }
  73. private OrderDto getOrderDto(OrderDto orderDto, LoginIdentity loginIdentity) {
  74. Integer store = loginIdentity.getStore();
  75. Integer type = loginIdentity.getType();
  76. orderDto.setStore(store);
  77. orderDto.setType(type);
  78. return orderDto;
  79. }
  80. /**
  81. * @author DY
  82. * @create 2018/3/14 17:08
  83. * 统计各个订单状态数量
  84. */
  85. public MessageModel query_order_status(LoginIdentity loginIdentity) {
  86. MessageModel messageModel = new MessageModel();
  87. Map map = new HashMap();
  88. OrderDto orderDto = getOrderDto(loginIdentity, null);
  89. //查询订单个数以status分组统计
  90. List<OrderStatus> orders = orderDao.query_order_status(orderDto);
  91. AssertUtil.listIsNotEmpty(orders, "你还没订单哦...");
  92. Integer total_order = 0;
  93. Integer un_pay_order = 0;
  94. Integer have_pay_order = 0;
  95. Integer wait_receive_order = 0;
  96. Integer wait_show_order = 0;
  97. Integer have_show_order = 0;
  98. Integer close_order = 0;
  99. Integer refund_order = 0;
  100. Integer refund_order_over = 0;
  101. for (int i = 0; i < orders.size(); i++) {
  102. OrderStatus orderStatus = orders.get(i);
  103. total_order += orderStatus.getOrderTotalCount();
  104. //查询待付款订单个数
  105. if (orderStatus.getStatus().equals(DmConstant.UN_PAID_CODE)
  106. && orderStatus.getVersion().equals(1)
  107. && orderStatus.getExpire().after(new Date(System.currentTimeMillis()))
  108. && orderStatus.getRefundCode().equals(0)
  109. ) {
  110. un_pay_order += 1;
  111. }
  112. //拿到已付款订单
  113. if (orderStatus.getStatus().equals(DmConstant.UN_DELIVERY_CODE)
  114. && orderStatus.getVersion().equals(1)
  115. && (orderStatus.getRefundCode().equals(0)
  116. || orderStatus.getRefundCode().equals(4)
  117. || orderStatus.getRefundCode().equals(10))
  118. ) {
  119. have_pay_order += 1;
  120. }
  121. //拿到已发货订单
  122. if (orderStatus.getStatus().equals(DmConstant.UN_RECEIVE_CODE)
  123. && orderStatus.getVersion().equals(1)
  124. && (orderStatus.getRefundCode().equals(0)
  125. || orderStatus.getRefundCode().equals(4)
  126. || orderStatus.getRefundCode().equals(10))
  127. ) {
  128. wait_receive_order += 1;
  129. }
  130. //拿到待晒单订单
  131. if (orderStatus.getStatus().equals(DmConstant.UN_SHOW_CODE)
  132. && orderStatus.getVersion().equals(1)
  133. && (orderStatus.getRefundCode().equals(0)
  134. || orderStatus.getRefundCode().equals(4)
  135. || orderStatus.getRefundCode().equals(10))
  136. ) {
  137. wait_show_order += 1;
  138. }
  139. //拿到已晒单订单
  140. if (orderStatus.getStatus().equals(DmConstant.HAVE_SHOW_CODE)
  141. && orderStatus.getVersion().equals(1)
  142. && (orderStatus.getRefundCode().equals(0)
  143. || orderStatus.getRefundCode().equals(4)
  144. || orderStatus.getRefundCode().equals(10))
  145. ) {
  146. have_show_order += 1;
  147. }
  148. //拿到已关闭订单(主动取消或者过期订单)
  149. if (orderStatus.getVersion().equals(DmConstant.VERSION_CANCEL_ORDER)
  150. ) {
  151. close_order += 1;
  152. }
  153. //拿到退款订单
  154. if (orderStatus.getVersion().equals(1)
  155. && (
  156. orderStatus.getRefundCode().equals(1)
  157. || orderStatus.getRefundCode().equals(2)
  158. || orderStatus.getRefundCode().equals(3)
  159. || orderStatus.getRefundCode().equals(5)
  160. || orderStatus.getRefundCode().equals(8)
  161. || orderStatus.getRefundCode().equals(9)
  162. )
  163. ) {
  164. refund_order += 1;
  165. }
  166. //拿到退款完成订单
  167. if (orderStatus.getVersion().equals(1)
  168. && (orderStatus.getRefundCode().equals(6)
  169. || orderStatus.getRefundCode().equals(7)
  170. )
  171. ) {
  172. refund_order_over += 1;
  173. }
  174. }
  175. map.put("refund_order", refund_order);
  176. map.put("refund_order_over", refund_order_over);
  177. map.put("close_order", close_order);
  178. map.put("have_show_order", have_show_order);
  179. map.put("wait_show_order", wait_show_order);
  180. map.put("wait_receive_order", wait_receive_order);
  181. map.put("have_pay_order", have_pay_order);
  182. map.put("un_pay_order", un_pay_order);
  183. //总订单数
  184. map.put("total_order", total_order);
  185. messageModel.setCode(DmConstant.OPS_SUCCESS_CODE);
  186. messageModel.setMsg(DmConstant.OPS_SUCCESS_MSG);
  187. messageModel.setData(map);
  188. return messageModel;
  189. }
  190. /**
  191. * @author DY
  192. * @create 2018/3/19 14:22
  193. * 查看订单
  194. */
  195. public MessageModel showObligationOrder(OrderDto orderDto, LoginIdentity loginIdentity) {
  196. MessageModel messageModel = new MessageModel();
  197. orderDto = getOrderDto(orderDto, loginIdentity);
  198. Integer status = orderDto.getStatus();
  199. //默认refundCode=-1
  200. orderDto.setRefundCode(-1);
  201. //status 为null 时 查全部
  202. if (status == null) {
  203. } else {
  204. //status=0时 查待付款 筛选过期和退款订单
  205. if (status == 0) {
  206. orderDto.setRefundCode(0);
  207. orderDto.setExpire(1);
  208. orderDto.setVersion(1);
  209. }
  210. //status=1时 查已付款待发货 筛选过期和退款订单
  211. if (status == 1) {
  212. orderDto.setRefundCode(0);
  213. orderDto.setVersion(1);
  214. }
  215. //status=2时 查已发货待收货 筛选过期和退款订单
  216. if (status == 2) {
  217. orderDto.setRefundCode(0);
  218. orderDto.setVersion(1);
  219. }
  220. //status=3时 查已收货待晒单 筛选过期和退款订单
  221. if (status == 3) {
  222. orderDto.setRefundCode(0);
  223. orderDto.setVersion(1);
  224. }
  225. //status=4时 查晒单 筛选过期和退款订单
  226. if (status == 4) {
  227. orderDto.setRefundCode(0);
  228. orderDto.setVersion(1);
  229. }
  230. //status=5时 查已关闭订单
  231. if (status == 5) {
  232. orderDto.setStatus(-1);
  233. orderDto.setVersion(-1);
  234. }
  235. //status=6时 查退款订单
  236. if (status == 6) {
  237. orderDto.setStatus(null);
  238. orderDto.setRefundCode(1);
  239. orderDto.setVersion(1);
  240. }
  241. //status=7时 查退款完成订单
  242. if (status == 7) {
  243. orderDto.setStatus(null);
  244. orderDto.setRefundCode(2);
  245. }
  246. }
  247. Map map = new HashMap();
  248. // 構建pageBounds
  249. PageBounds pageBounds = orderDto.buildPageBounds();
  250. //此时拿到所有订单明细然后根据定制明细进行分类
  251. List<OrderDetail> order = orderDao.findOrderItemByUserId(orderDto, pageBounds);
  252. // 構建返回結果
  253. PageList<OrderDetail> orderListStr = (PageList<OrderDetail>) order;
  254. List<List<OrderDetail>> newOrderItem = getList(orderListStr);
  255. //構建paginator對象返回
  256. Paginator paginator = orderListStr.getPaginator();
  257. map.put("paginator", paginator);
  258. //当是未支付订单和全部订单时的时候把使用优惠券的大订单放在一起(暂时先把待支付放在一起)
  259. messageModel.setCode(DmConstant.OPS_SUCCESS_CODE);
  260. messageModel.setMsg(DmConstant.OPS_SUCCESS_MSG);
  261. map.put("listOrder", newOrderItem);
  262. messageModel.setData(map);
  263. return messageModel;
  264. }
  265. private List<List<OrderDetail>> getList(List<OrderDetail> orders) {
  266. Map<Integer, List> map = new LinkedHashMap();
  267. for (int i = 0; i < orders.size(); i++) {
  268. OrderDetail item = orders.get(i);
  269. //构建退款申请最晚同意时间和买家发货之后卖家最晚打款时间
  270. Integer refundCode = item.getOrderItemRefundCode();
  271. Date refundTime = item.getRefundTime();
  272. if (refundCode == DmConstant.REFUND_CODE_APPLY) {
  273. Date refundLateTime = new Date(refundTime.getTime() + DmConstant.APPLY_REFUND_IN_REFUND_APPLY);
  274. item.setAgreeTimeRefundTime(refundLateTime);
  275. }
  276. if (refundCode == DmConstant.REFUND_CODE_EXPRESS_NUMBER) {
  277. Date refundLateTime = new Date(refundTime.getTime() + DmConstant.APPLY_REFUND_IN_REFUND_HAVE_SEND_REFUND_GOODS);
  278. item.setAgreeConfirmTimeRefundTime(refundLateTime);
  279. }
  280. //构建自动确认收货时间
  281. Boolean isExtend = item.getIsExtend();
  282. if (item.getDeliveryTime() != null && (refundCode == DmConstant.REFUND_CODE_DEFAULT || refundCode == DmConstant.REFUND_CODE_CANCEL)) {
  283. Date time = new Date(item.getDeliveryTime().getTime() + DmConstant.AUTOMATICRECEIVING);
  284. if (isExtend) {
  285. time = new Date(time.getTime() + DmConstant.EXTENDTHERECEIVING * 24 * 60 * 60 * 1000);
  286. }
  287. item.setAutomaticReceivingDate(time);
  288. }
  289. Integer status = item.getOrderItemStatus();
  290. if (refundCode == DmConstant.REFUND_CODE_AGREE && status != 1) {
  291. item.setRefundSendDate(new Date(item.getModifyDate().getTime() + DmConstant.APPLY_REFUND_IN_REFUND_AGREE));
  292. }
  293. Integer key = item.getId();
  294. //当且仅当集合里有过 1//1才会进这个集合,第一次也不会进,第二次才会进
  295. if (map.containsKey(key)) {
  296. map.get(key).add(item);
  297. } else {
  298. List list = new ArrayList();
  299. list.add(item);
  300. map.put(key, list);
  301. }
  302. }
  303. List<List<OrderDetail>> data = new PageList<>();
  304. for (Map.Entry<Integer, List> entry : map.entrySet()) {
  305. data.add(entry.getValue());
  306. }
  307. return data;
  308. }
  309. public void update_receive_address (ReceiveAddress receiveAddress) {
  310. AssertUtil.intIsNotEmpty(receiveAddress.getId(), "请选择要修改的订单...");
  311. Integer code = orderDao.updateOrderReceiveAddressById(receiveAddress);
  312. AssertUtil.intIsNotEmpty(code, "修改失败,请联系客服....");
  313. }
  314. /* *//**
  315. * @author DY
  316. * @create 2018/3/27 18:31
  317. * 修改订单商品信息
  318. *//*
  319. public MessageModel update_order_property(ProductUpdate productNew) {
  320. //参数判断
  321. checkParams(productNew);
  322. //拿到修改之前订单明细表里的属性
  323. ProductUpdate productOld = orderDao.queryProductByOrderItemId(productNew.getOrderItemId());
  324. AssertUtil.notNull(productOld, "所查询的商品不存在");
  325. Boolean quantity = productNew.getQuantity().equals(productOld.getQuantity());
  326. //修改custom属性订单
  327. if (StringUtils.isNotBlank(productOld.getCustomNames())) {
  328. AssertUtil.isNotEmpty(productNew.getCustomIds(), "请传入定制ids");
  329. //默认全部修改--更新订单详情price quantity,custom_names 更改带大订单的价格,数量
  330. //拿到新定制的价格
  331. List<ProductCustom> productCustoms = orderDao.queryCustomsByCustomId(productNew.getCustomIds());
  332. BigDecimal customTotalPrice = BigDecimal.ZERO;
  333. StringBuffer sb = new StringBuffer();
  334. for (int i = 0; i < productCustoms.size(); i++) {
  335. ProductCustom productCustom = productCustoms.get(i);
  336. customTotalPrice = customTotalPrice.add(productCustom.getPrice());
  337. sb.append(" " + productCustom.getName());
  338. }
  339. String customNames = sb.toString();
  340. //价格=默认价格+新定制价格
  341. BigDecimal defaultProductPrice = orderDao.queryDefaultProductPriceByProductId(productOld.getProduct());
  342. AssertUtil.notNull(defaultProductPrice, "默认商品不存在,请联系客服...");
  343. BigDecimal newPriceOrderItem = defaultProductPrice.add(customTotalPrice);
  344. //更新订单详情表
  345. Integer code = orderDao.updateOrderItemCustom(newPriceOrderItem, productNew.getQuantity(), customNames, productNew.getOrderItemId());
  346. AssertUtil.intIsNotEmpty(code, "更新子订单属性失败,请联系客服....");
  347. //更新大订单--差价 差量
  348. BigDecimal newPriceTotal = newPriceOrderItem.multiply(new BigDecimal(productNew.getQuantity()));
  349. BigDecimal oldPriceTotal = productOld.getPrice().multiply(new BigDecimal(productOld.getQuantity()));
  350. BigDecimal differentPrice = newPriceTotal.subtract(oldPriceTotal);
  351. Integer differentQuantity = productNew.getQuantity() - productOld.getQuantity();
  352. //初始化订单修改对象
  353. UpdateOrderParams updateOrderParams = new UpdateOrderParams();
  354. updateOrderParams.setOrderId(productOld.getOrders());
  355. updateOrderParams.setAmountUpdate(differentPrice);
  356. updateOrderParams.setQuantityUpdate(differentQuantity);
  357. updateOrderPriceQuantity(updateOrderParams);
  358. Map map = new HashMap();
  359. map.put("price", newPriceOrderItem);
  360. map.put("quantity", productNew.getQuantity());
  361. return MessageModelUtil.getSuccessMessageModel(newPriceOrderItem);
  362. }
  363. //判断修改的是哪一项或者哪几项
  364. Boolean scm = productNew.getSize().equals(productOld.getSize()) && productNew.getColor().equals(productOld.getColor())
  365. && productNew.getMaterial().equals(productOld.getMaterial());
  366. //无任何操作
  367. if (scm && quantity) {
  368. return MessageModelUtil.getSuccessMessageModel();
  369. }
  370. //初始化订单修改对象
  371. UpdateOrderParams updateOrderParams = new UpdateOrderParams();
  372. updateOrderParams.setOrderId(productOld.getOrders());
  373. //初始化订单明细修改对象
  374. ProductUpdate productUpdate = new ProductUpdate();
  375. Integer orderItemId = productNew.getOrderItemId();
  376. productUpdate.setOrderItemId(orderItemId);
  377. //只修改属性
  378. if (!scm && quantity) {
  379. //拿到修改后的具体商品
  380. Product productGetByAttribute = updateProductAttribute(productOld, productNew);
  381. //把要修改的属性放进修改对象里
  382. productUpdate = updateOrderItemAttribute(productGetByAttribute, orderItemId);
  383. updateOrderItem(productUpdate);
  384. //单价改变修改大订单价格
  385. if (productOld.getPrice().compareTo(productGetByAttribute.getPrice()) != 0) {
  386. //拿到差价...
  387. BigDecimal differentPriceAmount = productGetByAttribute.getPrice().subtract(productOld.getPrice()).multiply(new BigDecimal(productOld.getQuantity()));
  388. //更新大订单的差价
  389. updateOrderParams.setAmountUpdate(differentPriceAmount);
  390. updateOrderPriceQuantity(updateOrderParams);
  391. }
  392. Map map = new HashMap();
  393. map.put("price", productGetByAttribute.getPrice());
  394. map.put("quantity", productNew.getQuantity());
  395. return MessageModelUtil.getSuccessMessageModel(map);
  396. }
  397. //只修改数量--修改子订单数量和大订单数量,金额
  398. if (scm && !quantity) {
  399. productUpdate.setQuantity(productNew.getQuantity());
  400. updateOrderItem(productUpdate);
  401. Integer differentQuantity = productNew.getQuantity() - productOld.getQuantity();
  402. //修改库存
  403. updateQuantityProduct(productOld.getProduct(), differentQuantity);
  404. BigDecimal differentPriceAmount = productOld.getPrice().multiply(new BigDecimal(differentQuantity));
  405. updateOrderParams.setAmountUpdate(differentPriceAmount);
  406. updateOrderParams.setQuantityUpdate(differentQuantity);
  407. updateOrderPriceQuantity(updateOrderParams);
  408. Map map = new HashMap();
  409. map.put("price", productOld.getPrice());
  410. map.put("quantity", productNew.getQuantity());
  411. return MessageModelUtil.getSuccessMessageModel(map);
  412. }
  413. //只修改属性和数量
  414. if (!scm && !quantity) {
  415. //拿到修改后的具体商品
  416. Product productGetByAttribute = updateProductAttribute(productOld, productNew);
  417. //把要修改的属性放进修改对象里
  418. productUpdate = updateOrderItemAttribute(productGetByAttribute, orderItemId);
  419. productUpdate.setQuantity(productNew.getQuantity());
  420. updateOrderItem(productUpdate);
  421. Integer differentQuantity = productNew.getQuantity() - productOld.getQuantity();
  422. //拿到修改后的价格
  423. BigDecimal priceNew = productGetByAttribute.getPrice().multiply(new BigDecimal(productNew.getQuantity()));
  424. //修改前的价格
  425. BigDecimal priceOld = productOld.getPrice().multiply(new BigDecimal(productOld.getQuantity()));
  426. BigDecimal differentPriceAmount = priceNew.subtract(priceOld);
  427. updateOrderParams.setAmountUpdate(differentPriceAmount);
  428. updateOrderParams.setQuantityUpdate(differentQuantity);
  429. updateOrderPriceQuantity(updateOrderParams);
  430. Map map = new HashMap();
  431. map.put("price", productGetByAttribute.getPrice());
  432. map.put("quantity", productNew.getQuantity());
  433. return MessageModelUtil.getSuccessMessageModel(map);
  434. }
  435. return MessageModelUtil.getSuccessMessageModel();
  436. }*/
  437. /**
  438. * @author DY
  439. * @create 2018/3/27 18:31
  440. * 修改订单商品信息--更改
  441. */
  442. public MessageModel update_order_property(ProductUpdate productNew) {
  443. //参数判断
  444. checkParams(productNew);
  445. Map map = new HashMap();
  446. //拿到修改之前订单明细表里的属性
  447. ProductUpdate productOld = orderDao.queryProductByOrderItemId(productNew.getOrderItemId());
  448. AssertUtil.notNull(productOld, "所查询的商品不存在");
  449. if (productOld.getIsCustom()) {
  450. Integer a = 0;
  451. if (null != productNew.getMeasureId()) {
  452. a = a + 1;
  453. }
  454. if (StringUtils.isNotBlank(productNew.getSize())) {
  455. a = a + 1;
  456. }
  457. AssertUtil.isTrue(a == 2, "定制尺码和套码只能而二选一");
  458. }
  459. Boolean quantity = productNew.getQuantity() == productOld.getQuantity();
  460. //判断是否修改细节定制
  461. Boolean scmDetail = false;
  462. if (StringUtils.isNotBlank(productNew.getCustomNamesIds())) {
  463. scmDetail = productNew.getCustomNamesIds().equals(productOld.getCustomNamesIds());
  464. }
  465. // 1).定制修改(当且仅当含有细节定制走这一块)
  466. if (scmDetail == false) {
  467. //默认全部修改--更新订单详情price quantity,custom_names 更改带大订单的价格,数量
  468. //拿到细节定制的新价格
  469. String customNames = null;
  470. BigDecimal customTotalPriceOld = BigDecimal.ZERO;
  471. BigDecimal customTotalPriceNew = BigDecimal.ZERO;
  472. if (productOld.getCustomNamesIds() != null) {
  473. List<ProductCustom> productCustomsOld = orderDao.queryCustomsByCustomId(productOld.getCustomNamesIds());
  474. //拿到细节定制的新价格
  475. List<ProductCustom> productCustomsNew = orderDao.queryCustomsByCustomId(productNew.getCustomNamesIds());
  476. StringBuffer sb = new StringBuffer();
  477. for (int i = 0; i < productCustomsOld.size(); i++) {
  478. ProductCustom productCustom = productCustomsOld.get(i);
  479. customTotalPriceOld = customTotalPriceOld.add(productCustom.getPrice());
  480. }
  481. for (int i = 0; i < productCustomsNew.size(); i++) {
  482. ProductCustom productCustom = productCustomsNew.get(i);
  483. customTotalPriceNew = customTotalPriceNew.add(productCustom.getPrice());
  484. sb.append(" " + productCustom.getName());
  485. }
  486. customNames = sb.toString();
  487. }
  488. //初始化订单修改对象
  489. UpdateOrderParams updateOrderParams = new UpdateOrderParams();
  490. updateOrderParams.setOrderId(productOld.getOrders());
  491. //初始化订单明细修改对象
  492. ProductUpdate productUpdate = new ProductUpdate();
  493. Integer orderItemId = productNew.getOrderItemId();
  494. productUpdate.setOrderItemId(orderItemId);
  495. //拿到修改后的具体商品
  496. Product newProductUpdate = updateProductAttribute(productOld, productNew);
  497. //把要修改的属性放进修改对象里
  498. productUpdate = updateOrderItemAttribute(newProductUpdate, orderItemId);
  499. productUpdate.setQuantity(productNew.getQuantity());
  500. productUpdate.setCustomNames(customNames);
  501. productUpdate.setCustomNamesIds(productNew.getCustomNamesIds());
  502. productUpdate.setCustomDetailPrice(customTotalPriceNew);
  503. //若定制修改量身Id把size置为空,反之亦然
  504. if (StringUtils.isNotBlank(productNew.getSize())) {
  505. productUpdate.setMeasureId(null);
  506. }
  507. if (productNew.getMeasureId() != null) {
  508. productUpdate.setSize(null);
  509. }
  510. updateOrderItem(productUpdate);
  511. Integer differentQuantity = productNew.getQuantity() - productOld.getQuantity();
  512. //拿到修改后的价格
  513. BigDecimal priceNew = (newProductUpdate.getPrice().add(customTotalPriceNew)).multiply(new BigDecimal(productNew.getQuantity()));
  514. //修改前的价格
  515. BigDecimal priceOld = (productOld.getPrice().add(customTotalPriceOld)).multiply(new BigDecimal(productOld.getQuantity()));
  516. BigDecimal differentPriceAmount = priceNew.subtract(priceOld);
  517. updateOrderParams.setAmountUpdate(differentPriceAmount);
  518. updateOrderParams.setQuantityUpdate(differentQuantity);
  519. updateOrderPriceQuantity(updateOrderParams);
  520. map.put("price", newProductUpdate.getPrice().add(customTotalPriceNew));
  521. map.put("quantity", productNew.getQuantity());
  522. return MessageModelUtil.getSuccessMessageModel(map);
  523. }
  524. //判断修改的是哪一项或者哪几项
  525. Boolean scm = productNew.getSize().equals(productOld.getSize()) && productNew.getColor().equals(productOld.getColor());
  526. //无任何操作
  527. if (scm && quantity) {
  528. map.put("price", productOld.getPrice());
  529. map.put("quantity", productOld.getQuantity());
  530. return MessageModelUtil.getSuccessMessageModel(map);
  531. }
  532. //只修改属性
  533. if (!scm && quantity) {
  534. map = updateAttributeProductOnly(productNew, productOld);
  535. return MessageModelUtil.getSuccessMessageModel(map);
  536. }
  537. //只修改数量--修改子订单数量和大订单数量,金额
  538. if (scm && !quantity) {
  539. map = updateQuantityProductOnly(productNew, productOld);
  540. return MessageModelUtil.getSuccessMessageModel(map);
  541. }
  542. if (!scm && !quantity) {
  543. map = updateAttributeAndQuantityProductOnly(productNew, productOld);
  544. return MessageModelUtil.getSuccessMessageModel(map);
  545. }
  546. return MessageModelUtil.getSuccessMessageModel();
  547. }
  548. private Map updateAttributeProductOnly(ProductUpdate productNew, ProductUpdate productOld) {
  549. //初始化订单修改对象
  550. UpdateOrderParams updateOrderParams = new UpdateOrderParams();
  551. updateOrderParams.setOrderId(productOld.getOrders());
  552. //初始化订单明细修改对象
  553. ProductUpdate productUpdate = new ProductUpdate();
  554. Integer orderItemId = productNew.getOrderItemId();
  555. productUpdate.setOrderItemId(orderItemId);
  556. //拿到修改后的具体商品
  557. Product newProductUpdate = updateProductAttribute(productOld, productNew);
  558. //把要修改的属性放进修改对象里
  559. productUpdate = updateOrderItemAttribute(newProductUpdate, orderItemId);
  560. productUpdate.setMeasureId(productNew.getMeasureId());
  561. //若定制修改量身Id把size置为空,反之亦然
  562. if (StringUtils.isNotBlank(productNew.getSize())) {
  563. productUpdate.setMeasureId(null);
  564. }
  565. if (productNew.getMeasureId() != null) {
  566. productUpdate.setSize(null);
  567. }
  568. updateOrderItem(productUpdate);
  569. //单价改变修改大订单价格
  570. if (productOld.getPrice().compareTo(newProductUpdate.getPrice()) != 0) {
  571. //拿到差价...
  572. BigDecimal differentPriceAmount = newProductUpdate.getPrice().subtract(productOld.getPrice()).multiply(new BigDecimal(productOld.getQuantity()));
  573. //更新大订单的差价
  574. updateOrderParams.setAmountUpdate(differentPriceAmount);
  575. updateOrderPriceQuantity(updateOrderParams);
  576. }
  577. Map map = new HashMap();
  578. map.put("price", newProductUpdate.getPrice());
  579. map.put("quantity", productNew.getQuantity());
  580. return map;
  581. }
  582. private Map updateQuantityProductOnly(ProductUpdate productNew, ProductUpdate productOld) {
  583. //初始化订单修改对象
  584. UpdateOrderParams updateOrderParams = new UpdateOrderParams();
  585. updateOrderParams.setOrderId(productOld.getOrders());
  586. //初始化订单明细修改对象
  587. ProductUpdate productUpdate = new ProductUpdate();
  588. Integer orderItemId = productNew.getOrderItemId();
  589. productUpdate.setOrderItemId(orderItemId);
  590. productUpdate.setQuantity(productNew.getQuantity());
  591. productUpdate.setMeasureId(productNew.getMeasureId());
  592. updateOrderItem(productUpdate);
  593. Integer differentQuantity = productNew.getQuantity() - productOld.getQuantity();
  594. //套码修改库存
  595. if (productOld.getIsCustom() == false) {
  596. updateQuantityProduct(productOld.getProduct(), differentQuantity);
  597. }
  598. BigDecimal differentPriceAmount = productOld.getPrice().multiply(new BigDecimal(differentQuantity));
  599. updateOrderParams.setAmountUpdate(differentPriceAmount);
  600. updateOrderParams.setQuantityUpdate(differentQuantity);
  601. updateOrderPriceQuantity(updateOrderParams);
  602. Map map = new HashMap();
  603. map.put("price", productOld.getPrice());
  604. map.put("quantity", productNew.getQuantity());
  605. return map;
  606. }
  607. private Map updateAttributeAndQuantityProductOnly(ProductUpdate productNew, ProductUpdate productOld) {
  608. //初始化订单修改对象
  609. UpdateOrderParams updateOrderParams = new UpdateOrderParams();
  610. updateOrderParams.setOrderId(productOld.getOrders());
  611. //初始化订单明细修改对象
  612. ProductUpdate productUpdate = new ProductUpdate();
  613. Integer orderItemId = productNew.getOrderItemId();
  614. productUpdate.setOrderItemId(orderItemId);
  615. //拿到修改后的具体商品
  616. Product newProductUpdate = updateProductAttribute(productOld, productNew);
  617. //把要修改的属性放进修改对象里
  618. productUpdate = updateOrderItemAttribute(newProductUpdate, orderItemId);
  619. productUpdate.setQuantity(productNew.getQuantity());
  620. productUpdate.setMeasureId(productNew.getMeasureId());
  621. //若定制修改量身Id把size置为空,反之亦然
  622. if (StringUtils.isNotBlank(productNew.getSize())) {
  623. productUpdate.setMeasureId(null);
  624. }
  625. if (productNew.getMeasureId() != null) {
  626. productUpdate.setSize(null);
  627. }
  628. updateOrderItem(productUpdate);
  629. Integer differentQuantity = productNew.getQuantity() - productOld.getQuantity();
  630. //拿到修改后的价格
  631. BigDecimal priceNew = newProductUpdate.getPrice().multiply(new BigDecimal(productNew.getQuantity()));
  632. //修改前的价格
  633. BigDecimal priceOld = productOld.getPrice().multiply(new BigDecimal(productOld.getQuantity()));
  634. BigDecimal differentPriceAmount = priceNew.subtract(priceOld);
  635. updateOrderParams.setAmountUpdate(differentPriceAmount);
  636. updateOrderParams.setQuantityUpdate(differentQuantity);
  637. updateOrderPriceQuantity(updateOrderParams);
  638. Map map = new HashMap();
  639. map.put("price", newProductUpdate.getPrice());
  640. map.put("quantity", productNew.getQuantity());
  641. return map;
  642. }
  643. private void updateQuantityProduct(Integer productId, Integer quantity) {
  644. Integer code = productDao.updateStockOldProduct(productId, quantity);
  645. AssertUtil.intIsNotEmpty(code, "修改数量失败....");
  646. }
  647. private void updateOrderItem(ProductUpdate productUpdate) {
  648. //更改子订单
  649. Integer updateOrderItemCode = orderDao.updateOrderItemByProduct(productUpdate);
  650. AssertUtil.intIsNotEmpty(updateOrderItemCode, "更改子订单属性smc失败...");
  651. }
  652. private void updateOrderPriceQuantity(UpdateOrderParams updateOrderParams) {
  653. Integer updateOrderCode = orderDao.updateOrderByProduct(updateOrderParams);
  654. AssertUtil.intIsNotEmpty(updateOrderCode, "修改失败,请联系客服....");
  655. }
  656. private ProductUpdate updateOrderItemAttribute(Product productGetByAttribute, Integer orderItemId) {
  657. ProductUpdate productUpdate = new ProductUpdate();
  658. productUpdate.setProduct(productGetByAttribute.getId());
  659. productUpdate.setOrderItemId(orderItemId);
  660. productUpdate.setPrice(productGetByAttribute.getPrice());
  661. productUpdate.setColor(productGetByAttribute.getColor());
  662. productUpdate.setSize(productGetByAttribute.getSize());
  663. return productUpdate;
  664. }
  665. private Product updateProductAttribute(ProductUpdate productOld, ProductUpdate productNew) {
  666. Integer quantityOld = productOld.getQuantity();
  667. Integer quantityNew = productNew.getQuantity();
  668. //拿到商品原信息
  669. List<Product> updateProduct = null;
  670. Product product = null;
  671. //套码
  672. Boolean isCustom = productNew.getIsCustom();
  673. //定制的话尺寸置为空
  674. if (isCustom) {
  675. String size = productNew.getSize();
  676. productNew.setSize(null);
  677. updateProduct = productDao.queryProductBySMC(productNew);
  678. AssertUtil.isTrue(updateProduct.isEmpty(), "更改的商品不存在,请稍后重试...");
  679. product = updateProduct.get(0);
  680. product.setSize(size);
  681. } else {
  682. updateProduct = productDao.queryProductBySMC(productNew);
  683. AssertUtil.isTrue(updateProduct.isEmpty(), "更改的商品不存在,请稍后重试...");
  684. product = updateProduct.get(0);
  685. //回滚元商品库存,扣去现在商品库存
  686. Integer codeOld = productDao.updateStockOldProduct(productOld.getProduct(), -quantityOld);
  687. AssertUtil.intIsNotEmpty(codeOld, "回滚库存失败,请联系客服...");
  688. Integer codeNew = productDao.updateStockOldProduct(product.getId(), quantityNew);
  689. AssertUtil.intIsNotEmpty(codeNew, "扣除库存失败,请联系客服...");
  690. }
  691. return product;
  692. }
  693. private void checkParams(ProductUpdate product) {
  694. AssertUtil.intIsNotEmpty(product.getOrderItemId(), "请传入订单明细id...");
  695. AssertUtil.intIsNotEmpty(product.getQuantity(), "请传入修改之后的数量...");
  696. }
  697. public void update_order_totalPrice(BigDecimal totalPrice, Integer id) {
  698. AssertUtil.intIsNotEmpty(id, "请选择订单...");
  699. AssertUtil.notNull(totalPrice, "请传入要修改的价格...");
  700. Integer code = orderDao.updateOrderPriceByPrice(totalPrice, id);
  701. AssertUtil.intIsNotEmpty(code, "价格修改失败,请联系客服.....");
  702. }
  703. public void updateOrderStatusToSend(String deliverySend) {
  704. AssertUtil.isNotEmpty(deliverySend, "请传入发货信息...");
  705. //格式化发货大字段
  706. List<DeliverySend> deliverySends = null;
  707. if (StringUtils.isNotBlank(deliverySend)) {
  708. deliverySends = JSON.parseArray(deliverySend, DeliverySend.class);
  709. }
  710. //更改子订单
  711. Integer codeOrderItem = orderDao.updateOrderItemStatusCode(deliverySends);
  712. AssertUtil.intIsNotEmpty(codeOrderItem, "更改订单状态失败,请联系客服...");
  713. //更改大订单状态,若有多个小订单判断是否全部发货,有一个未发货都不更改大订单
  714. for (int i = 0; i < deliverySends.size(); i++) {
  715. Integer orderId=deliverySends.get(i).getOrderId();
  716. AssertUtil.intIsNotEmpty(orderId, "请传入父订单Id...");
  717. Integer isHaveNoSend=orderDao.findOrdersNoSend(orderId,DmConstant.UN_DELIVERY_CODE);
  718. if(isHaveNoSend==null||isHaveNoSend<0){
  719. Integer code = orderDao.updateOrderStatusAndDelivery(orderId,DmConstant.UN_RECEIVE_CODE);
  720. AssertUtil.intIsNotEmpty(code, "更新父订单失败,请稍后重试..."+orderId);
  721. }
  722. }
  723. }
  724. /*private void updateOrderStatus(Integer orderId, Integer unReceiveCode) {
  725. //更改大订单状态
  726. Integer codeOrder=orderDao.updateOrderStatus(unReceiveCode,orderId);
  727. AssertUtil.intIsNotEmpty(codeOrder,"更改订单状态失败,请联系客服...");
  728. //更改子订单
  729. Integer codeOrderItem=orderDao.updateOrderItemStatusCode(unReceiveCode,orderId);
  730. AssertUtil.intIsNotEmpty(codeOrderItem,"更改订单状态失败,请联系客服...");
  731. }*/
  732. public List<DeliveryCode> query_delivery_code() {
  733. List<DeliveryCode> deliveryCodeList = orderDao.queryDeliveryCode();
  734. return deliveryCodeList;
  735. }
  736. public void deal_with_return_order(Integer orderItemId, Boolean isAgree,Integer refundCodeF) {
  737. AssertUtil.intIsNotEmpty(orderItemId, "请选择带操作的订单");
  738. AssertUtil.intIsNotEmpty(refundCodeF, "请传入待退款订单的状态refundCode");
  739. AssertUtil.notNull(isAgree, "请选择同意或者拒绝...");
  740. Integer refundCode = null;
  741. if (isAgree) {
  742. refundCode = DmConstant.REFUND_CODE_AGREE;
  743. // 3.更新库存
  744. //查询待更新订单数量
  745. OrderItemDetail orderDetail1 = orderDao.findOrderItemById(orderItemId);
  746. //拿到定制类属性,若为空则是套码更新库存,否则不更新
  747. Boolean isCustom = orderDetail1.getIsCustom();
  748. //套码回滚库存...
  749. if (isCustom == false) {
  750. Integer quantity = orderDetail1.getQuantity();
  751. Integer product = orderDetail1.getProduct();
  752. Integer updateProductStock = orderDao.updateProductStock(-quantity, product);
  753. AssertUtil.intIsNotEmpty(updateProductStock, "取消订单失败003,请联系客服...");
  754. }
  755. } else {
  756. if(refundCodeF==DmConstant.REFUND_CODE_APPLY||refundCodeF==DmConstant.REFUND_CODE_APPLY_CANCEL_AGAIN){
  757. refundCode = DmConstant.REFUND_CODE_REFUSE;
  758. }
  759. if(refundCodeF==DmConstant.REFUND_CODE_APPLY_REFUSE_AGAIN){
  760. refundCode = DmConstant.REFUND_CODE_REFUSE_AGAIN;
  761. }
  762. }
  763. Integer code = orderDao.updateOrderItemRefundStatus(orderItemId, refundCode);
  764. AssertUtil.intIsNotEmpty(code, "操作失败,请联系客服...");
  765. }
  766. public void updateOrderStatusToAgree() {
  767. //更新到期退款时间订单
  768. OrderDto orderDto = new OrderDto();
  769. orderDto.setRefundCode(DmConstant.REFUND_CODE_AGREE);
  770. orderDto.setRefundTime(DmConstant.REFUND_AGREE_TIME);
  771. //更新子订单
  772. orderDao.updateOrderItemStatusToAgree(orderDto);
  773. //查询待退款订单 refundCode=3子订单
  774. orderDto.setRefundCode(DmConstant.REFUND_CODE_AGREE);
  775. List<OrderItemQuery> orderDetails = orderDao.findOrderItemByOrderDto(orderDto);
  776. //AssertUtil.listIsNotEmpty(orderDetails, "查询失败,请联系客服...");
  777. //回滚库存
  778. /* for (int i = 0; i < orderDetails.size(); i++) {
  779. OrderItemQuery orderItemQuery = orderDetails.get(i);
  780. //拿到定制类属性,若为空则是套码更新库存,否则不更新
  781. Boolean isCustom = orderItemQuery.getIsCustom();
  782. //套码回滚库存...
  783. if (isCustom == false) {
  784. Integer quantity = orderItemQuery.getOrderItemQuantity();
  785. Integer product = orderItemQuery.getProduct();
  786. Integer updateProductStock = orderDao.updateProductStock(-quantity, product);
  787. AssertUtil.intIsNotEmpty(updateProductStock, "取消订单失败003,请联系客服...");
  788. }
  789. }*/
  790. StringBuffer orders = new StringBuffer();
  791. if (orderDetails.size() != 0) {
  792. for (int i = 0; i < orderDetails.size(); i++) {
  793. OrderItemQuery orderItemQuery = orderDetails.get(i);
  794. Integer orderId = orderItemQuery.getOrderId();
  795. //第一步过滤
  796. if (orderItemQuery.getOrderQuantity().equals(orderItemQuery.getOrderItemQuantity())) {
  797. orders.append(orderId + ",");
  798. } else {
  799. //查询大订单下子订单是否都是已同意状态,是的话把大订单更改为已同意
  800. String refundCodes = DmConstant.REFUND_CODE_APPLY + "," + DmConstant.REFUND_CODE_REFUSE;
  801. List<Integer> integers = orderDao.findOrderItemByOrderId(orderId, refundCodes);
  802. if (integers.size() == 0) {
  803. orders.append(orderId + ",");
  804. }
  805. }
  806. }
  807. }
  808. orders.deleteCharAt(orders.length() - 1);
  809. String ordersStr = orders.toString();
  810. //批量更新大订单状态
  811. orderDao.updateOrderStatusToAgree(ordersStr, DmConstant.REFUND_CODE_AGREE);
  812. }
  813. public Integer query_order_refund_status(LoginIdentity loginIdentity) {
  814. OrderDto orderDto = getOrderDto(loginIdentity, null);
  815. //拿到退款退货订单
  816. List<Integer> return_order = orderDao.queryReturnOrder(orderDto);
  817. return return_order.size();
  818. }
  819. public void return_order_to_last(Integer orderId,Integer orderItemId,Integer isAgree) {
  820. AssertUtil.intIsNotEmpty(orderId, "请确认打款的父订单...");
  821. AssertUtil.intIsNotEmpty(orderItemId, "请确认打款的子订单...");
  822. AssertUtil.intIsNotEmpty(isAgree, "请确认是否同意...");
  823. Integer refundCode=null;
  824. if(isAgree==1){
  825. refundCode=DmConstant.REFUND_CODE_CONFIRM_RECEIVING;
  826. }
  827. else if(isAgree==2){
  828. refundCode=DmConstant.REFUND_CODE_REFUSE_AGAIN;
  829. }
  830. else{
  831. throw new ParamException("请传入正确的状态值");
  832. }
  833. //更新订单状态
  834. Integer code=orderDao.updateOrderItemRefundStatus(orderItemId,refundCode);
  835. AssertUtil.intIsNotEmpty(code,"子订单更新失败");
  836. if(isAgree==1){
  837. //同步大订单状态
  838. String refundCodes="1,2,3,5,6,8,9,11";
  839. Integer code2=orderDao.findIsHaveRefundOrder(orderId,refundCodes);
  840. //没有退款处理中订单更新大订单状态
  841. if(code2==null||code2<0){
  842. Integer code3=orderDao.updateOrderRefundStatus(orderId,refundCode);
  843. AssertUtil.intIsNotEmpty(code3,"父订单更新失败");
  844. }
  845. }
  846. }
  847. }