ApiCartController.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. package com.kmall.api.api;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.kmall.api.annotation.LoginUser;
  4. import com.kmall.common.constant.Dict;
  5. import com.kmall.api.entity.*;
  6. import com.kmall.api.service.*;
  7. import com.kmall.api.util.ApiBaseAction;
  8. import com.kmall.common.utils.Constant;
  9. import com.kmall.common.utils.MapUtils;
  10. import com.qiniu.util.StringUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import java.math.BigDecimal;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * 作者: @author Scott <br>
  23. * 时间: 2017-08-11 08:32<br>
  24. * 描述: ApiIndexController <br>
  25. */
  26. @RestController
  27. @RequestMapping("/api/cart")
  28. public class ApiCartController extends ApiBaseAction {
  29. @Autowired
  30. private ApiCartService cartService;
  31. @Autowired
  32. private ApiGoodsService goodsService;
  33. @Autowired
  34. private ApiProductService productService;
  35. @Autowired
  36. private ApiAddressService addressService;
  37. @Autowired
  38. private ApiCouponService apiCouponService;
  39. @Autowired
  40. private ApiUserCouponService apiUserCouponService;
  41. @Autowired
  42. private ApiOrderGoodsService apiOrderGoodsService;
  43. @Autowired
  44. private ApiUserService apiUserService;
  45. @Autowired
  46. private ApiFreightService apiFreightService;
  47. /**
  48. * 获取购物车中的数据
  49. */
  50. @GetMapping("getCartMoney")
  51. public Object getCartMoney(@LoginUser UserVo loginUser, String checkCart) {
  52. Map<String, Object> resultObj = new HashMap();
  53. //查询列表数据
  54. Map param = new HashMap();
  55. param.put("user_id", loginUser.getId());
  56. Long storeId = getStoreId();
  57. param.put("store_id", storeId);
  58. param.put("checkCart",checkCart);
  59. List<CartVo> cartList = cartService.queryList(param);
  60. //获取购物车统计信息
  61. Integer goodsCount = 0;
  62. BigDecimal goodsAmount = new BigDecimal(0.00);
  63. Integer checkedGoodsCount = 0;
  64. BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
  65. for (CartVo cartItem : cartList) {
  66. goodsCount += cartItem.getNumber();
  67. goodsAmount = goodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  68. if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
  69. checkedGoodsCount += cartItem.getNumber();
  70. checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  71. }
  72. }
  73. resultObj.put("cartList", cartList);
  74. //
  75. Map<String, Object> cartTotal = new HashMap();
  76. cartTotal.put("goodsCount", goodsCount);
  77. cartTotal.put("goodsAmount", goodsAmount);
  78. cartTotal.put("checkedGoodsCount", checkedGoodsCount);
  79. cartTotal.put("checkedGoodsAmount", checkedGoodsAmount);
  80. //
  81. resultObj.put("cartTotal", cartTotal);
  82. return resultObj;
  83. }
  84. /**
  85. * 获取购物车中的数据
  86. */
  87. @GetMapping("getCart")
  88. public Object getCart(String checkCart) {
  89. UserVo loginUser = new UserVo();
  90. loginUser.setId(getUserId());
  91. Map<String, Object> resultObj = new HashMap();
  92. //查询列表数据
  93. Map param = new HashMap();
  94. param.put("user_id", loginUser.getId());
  95. Long storeId = getStoreId();
  96. param.put("store_id", storeId);
  97. param.put("merchSn", getMerchSn());
  98. param.put("checkCart", checkCart);
  99. List<CartVo> cartList = cartService.queryList(param);
  100. List<CartVo> validCartList = cartService.queryValidCartList(param);
  101. //获取购物车统计信息
  102. Integer goodsCount = 0;
  103. BigDecimal goodsAmount = new BigDecimal(0.00);
  104. Integer checkedGoodsCount = 0;
  105. BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
  106. List<CartVo> cart00List = new ArrayList<>();
  107. List<CartVo> cart02List = new ArrayList<>();
  108. List<CartVo> cart10List = new ArrayList<>();
  109. List<CartVo> cart11List = new ArrayList<>();
  110. for (CartVo cartItem : cartList) {
  111. goodsCount += cartItem.getNumber();
  112. goodsAmount = goodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  113. if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
  114. checkedGoodsCount += cartItem.getNumber();
  115. checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  116. }
  117. if(org.apache.commons.lang.StringUtils.isNotEmpty(cartItem.getGoodsBizType())){
  118. cartItem.setGoodsBizTypeName(Dict.orderBizType.valueOf("item_"+ cartItem.getGoodsBizType()).getItemName());
  119. }else{
  120. cartItem.setGoodsBizTypeName("");
  121. }
  122. if(Dict.orderBizType.item_00.getItem().equalsIgnoreCase(cartItem.getGoodsBizType())){
  123. CartVo cartVo00= cartItem;
  124. cart00List.add(cartVo00);
  125. }
  126. if(Dict.orderBizType.item_02.getItem().equalsIgnoreCase(cartItem.getGoodsBizType())){
  127. CartVo cartVo02= cartItem;
  128. cart02List.add(cartVo02);
  129. }
  130. if(Dict.orderBizType.item_10.getItem().equalsIgnoreCase(cartItem.getGoodsBizType())){
  131. CartVo cartVo10= cartItem;
  132. cart10List.add(cartVo10);
  133. }
  134. if(Dict.orderBizType.item_11.getItem().equalsIgnoreCase(cartItem.getGoodsBizType())){
  135. CartVo cartVo11= cartItem;
  136. cart11List.add(cartVo11);
  137. }
  138. }
  139. // 获取优惠信息提示 邮费
  140. CouponVo shippingCoupon = apiCouponService.matchShippingSign(loginUser.getId(), checkedGoodsAmount);
  141. // 获取优惠信息提示 满减
  142. CouponVo fullSubCoupon = apiCouponService.matchFullSubSign(loginUser.getId(), checkedGoodsAmount);
  143. List<CouponVo> couponInfoList = new ArrayList();
  144. if (null != shippingCoupon) {
  145. couponInfoList.add(shippingCoupon);
  146. }
  147. if (null != fullSubCoupon) {
  148. couponInfoList.add(fullSubCoupon);
  149. }
  150. resultObj.put("couponInfoList", couponInfoList);
  151. resultObj.put("cart00List", cart00List);//{'cartList':{'type': '保税备货','data':{}}}
  152. resultObj.put("cart02List", cart02List);
  153. resultObj.put("cart10List", cart10List);
  154. resultObj.put("cart11List", cart11List);
  155. resultObj.put("cartList", cartList);
  156. resultObj.put("validCartList", validCartList);
  157. //
  158. Map<String, Object> cartTotal = new HashMap();
  159. cartTotal.put("goodsCount", goodsCount);
  160. cartTotal.put("goodsAmount", goodsAmount);
  161. cartTotal.put("checkedGoodsCount", checkedGoodsCount);
  162. cartTotal.put("checkedGoodsAmount", checkedGoodsAmount);
  163. resultObj.put("cartTotal", cartTotal);
  164. param.put("checkCart","00");
  165. int total00 = cartService.queryTotal(param);
  166. param.put("checkCart","11");
  167. int total11 = cartService.queryTotal(param);
  168. resultObj.put("total00", total00);
  169. resultObj.put("total11", total11);
  170. return resultObj;
  171. }
  172. /**
  173. * 获取购物车中的数据,底部显示
  174. */
  175. @GetMapping("getFootCart")
  176. public Object getFootCart(@LoginUser UserVo loginUser) {
  177. Map<String, Object> resultObj = new HashMap();
  178. //查询列表数据
  179. Map param = new HashMap();
  180. param.put("user_id", loginUser.getId());
  181. Long storeId = getStoreId();
  182. param.put("store_id", storeId);
  183. List<CartVo> cartList = cartService.queryList(param);
  184. //获取购物车统计信息
  185. Integer goodsCount = 0;
  186. BigDecimal goodsAmount = new BigDecimal(0.00);
  187. Integer checkedGoodsCount = 0;
  188. BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
  189. for (CartVo cartItem : cartList) {
  190. goodsCount += 1;
  191. goodsAmount = goodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  192. if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
  193. checkedGoodsCount += cartItem.getNumber();
  194. checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  195. }
  196. }
  197. //
  198. resultObj.put("goodsCount", goodsCount);
  199. resultObj.put("goodsAmount", goodsAmount);
  200. resultObj.put("checkedGoodsCount", checkedGoodsCount);
  201. resultObj.put("checkedGoodsAmount", checkedGoodsAmount);
  202. //
  203. return toResponsSuccess(resultObj);
  204. }
  205. /**
  206. * 获取购物车信息,所有对购物车的增删改操作,都要重新返回购物车的信息
  207. */
  208. @GetMapping("index")
  209. public Object index(String checkCart) {
  210. return toResponsSuccess(getCart(checkCart));
  211. }
  212. /**
  213. * 添加商品到购物车
  214. */
  215. @PostMapping("add")
  216. public Object add(@LoginUser UserVo loginUser) {
  217. JSONObject jsonParam = getJsonRequest();
  218. Long goodsId = jsonParam.getLong("goodsId");
  219. Long productId = jsonParam.getLong("productId");
  220. Integer number = jsonParam.getInteger("number");
  221. String checkCart = jsonParam.getString("checkCart");
  222. //判断商品是否可以购买
  223. GoodsVo goodsInfo = goodsService.queryObject(goodsId);
  224. if (null == goodsInfo || goodsInfo.getIs_delete() == 1 || goodsInfo.getIs_on_sale() == 0) {
  225. return toResponsFail("商品已下架");
  226. }
  227. Long storeId = getStoreId();
  228. //取得规格的信息,判断规格库存
  229. /*ProductVo productInfo = productService.queryObjectByStoreId(productId, storeId);
  230. if (null == productInfo) {
  231. return toResponsFail("商品已下架");
  232. }
  233. if(productInfo.getStock_num() <= 0){
  234. return toResponsFail("库存不足");
  235. }*/
  236. ProductVo productInfo = productService.queryByStoreId(productId, storeId);
  237. if (null == productInfo) {
  238. return toResponsFail("商品已下架");
  239. }
  240. if(productInfo.getStock_num() == null || number > productInfo.getStock_num() || productInfo.getStock_num() <= 0){
  241. return toResponsFail("该商品库存不足");
  242. }
  243. if (null == productInfo.getRetail_price()) {
  244. productInfo.setRetail_price(goodsInfo.getRetail_price());
  245. productInfo.setMarket_price(goodsInfo.getMarket_price());
  246. }
  247. //判断购物车中是否存在此规格商品
  248. Map cartParam = new HashMap();
  249. cartParam.put("goods_id", goodsId);
  250. cartParam.put("product_id", productId);
  251. cartParam.put("user_id", loginUser.getId());
  252. cartParam.put("store_id", storeId);
  253. List<CartVo> cartInfoList = cartService.queryList(cartParam);
  254. CartVo cartInfo = null != cartInfoList && cartInfoList.size() > 0 ? cartInfoList.get(0) : null;
  255. if (null == cartInfo) {//添加规格名和值
  256. cartInfo = new CartVo();
  257. cartInfo.setGoods_id(goodsId);
  258. cartInfo.setProduct_id(productId);
  259. cartInfo.setGoods_sn(productInfo.getGoods_sn());
  260. cartInfo.setGoods_name(goodsInfo.getName());
  261. cartInfo.setList_pic_url(goodsInfo.getList_pic_url());
  262. cartInfo.setNumber(number);
  263. cartInfo.setStore_id(storeId);
  264. cartInfo.setUser_id(loginUser.getId());
  265. cartInfo.setRetail_price(productInfo.getRetail_price());
  266. cartInfo.setMarket_price(productInfo.getMarket_price());
  267. cartInfo.setGoods_specification_name_value(productInfo.getGoods_specification_name_value());
  268. cartInfo.setGoods_specification_ids(productInfo.getGoods_specification_ids());
  269. cartInfo.setChecked(1);
  270. cartInfo.setGoodsBizType(goodsInfo.getGoodsBizType());//业务类型
  271. cartInfo.setStockNum(productInfo.getStock_num());
  272. cartInfo.setMerchSn(goodsInfo.getMerchSn());
  273. cartService.save(cartInfo);
  274. } else {
  275. if(number + cartInfo.getNumber() > productInfo.getStock_num()){
  276. return toResponsFail("商品选购数量(含购物车已加购数量)已超过库存");
  277. }
  278. cartInfo.setNumber(cartInfo.getNumber() + number);
  279. cartInfo.setGoodsBizType(goodsInfo.getGoodsBizType());//业务类型
  280. cartInfo.setStockNum(productInfo.getStock_num());
  281. cartService.update(cartInfo);
  282. }
  283. return toResponsSuccess(getCart(checkCart));
  284. }
  285. /**
  286. * 添加商品到购物车
  287. */
  288. @PostMapping("addByGoodsId")
  289. public Object addByGoodsId(@LoginUser UserVo loginUser) {
  290. JSONObject jsonParam = getJsonRequest();
  291. Long goodsId = jsonParam.getLong("goodsId");
  292. String checkCart = jsonParam.getString("checkCart");
  293. //判断商品是否可以购买
  294. GoodsVo goodsInfo = goodsService.queryObject(goodsId);
  295. if (null == goodsInfo || goodsInfo.getIs_delete() == 1 || goodsInfo.getIs_on_sale() == 0) {
  296. return toResponsFail("商品已下架");
  297. }
  298. Long storeId = getStoreId();
  299. Map resultObj = cartService.addByGoodsId(getUserId(), goodsId, storeId);
  300. if (0 != MapUtils.getInteger("errno", resultObj)) {
  301. return toResponsObject(MapUtils.getInteger("errno", resultObj), MapUtils.getString("errmsg", resultObj), "");
  302. }
  303. return toResponsSuccess(getCart(checkCart));
  304. }
  305. /**
  306. * 根据订单号添加商品到购物车
  307. */
  308. @GetMapping("addByOrder")
  309. public Object addByOrder(@LoginUser UserVo loginUser, Long orderId) {
  310. JSONObject jsonParam = getJsonRequest();
  311. //
  312. Map params = new HashMap();
  313. params.put("order_id", orderId);
  314. List<OrderGoodsVo> orderGoodsVos = apiOrderGoodsService.queryList(params);
  315. for (OrderGoodsVo goodsVo : orderGoodsVos) {
  316. //判断商品是否可以购买
  317. GoodsVo goodsInfo = goodsService.queryObject(goodsVo.getGoods_id());
  318. if (null == goodsInfo || goodsInfo.getIs_delete() == 1 || goodsInfo.getIs_on_sale() == 0) {
  319. return toResponsFail("商品已下架");
  320. }
  321. }
  322. Long storeId = getStoreId();
  323. for (OrderGoodsVo goodsVo : orderGoodsVos) {
  324. ProductVo productInfo = productService.queryByStoreId(goodsVo.getProduct_id(), storeId);
  325. if (null == productInfo) {
  326. return toResponsFail("商品已下架");
  327. }
  328. if(productInfo.getStock_num() == null || goodsVo.getNumber() > productInfo.getStock_num() || productInfo.getStock_num() <= 0){
  329. return toResponsFail("该商品库存不足");
  330. }
  331. CartVo cartInfo = new CartVo();
  332. cartInfo.setGoods_id(goodsVo.getGoods_id());
  333. cartInfo.setProduct_id(goodsVo.getProduct_id());
  334. cartInfo.setGoods_sn(goodsVo.getGoods_sn());
  335. cartInfo.setGoods_name(goodsVo.getGoods_name());
  336. cartInfo.setList_pic_url(goodsVo.getList_pic_url());
  337. cartInfo.setNumber(goodsVo.getNumber());
  338. cartInfo.setStore_id(storeId);
  339. cartInfo.setUser_id(getUserId());
  340. cartInfo.setRetail_price(goodsVo.getRetail_price());
  341. cartInfo.setMarket_price(goodsVo.getMarket_price());
  342. cartInfo.setGoods_specification_name_value(goodsVo.getGoods_specification_name_value());
  343. cartInfo.setGoods_specification_ids(goodsVo.getGoods_specification_ids());
  344. cartInfo.setChecked(1);
  345. cartInfo.setGoodsBizType(goodsVo.getOrderBizType());
  346. cartInfo.setStockNum(productInfo.getStock_num());
  347. cartInfo.setMerchSn(goodsVo.getMerchSn());
  348. Map map = new HashMap();
  349. map.put("user_id", getUserId());
  350. map.put("store_id", getStoreId());
  351. map.put("goods_id",goodsVo.getGoods_id());
  352. map.put("product_id",goodsVo.getProduct_id());
  353. map.put("number",goodsVo.getNumber());
  354. List<CartVo> list= cartService.queryList(map);
  355. if(list != null && list.size() > 0){
  356. for (CartVo vo:list) {
  357. cartInfo.setId(vo.getId());
  358. cartService.update(cartInfo);
  359. }
  360. }else{
  361. cartService.save(cartInfo);
  362. }
  363. }
  364. return toResponsSuccess("添加成功");
  365. }
  366. /**
  367. * 减少商品到购物车
  368. */
  369. @PostMapping("minus")
  370. public Object minus(@LoginUser UserVo loginUser) {
  371. JSONObject jsonParam = getJsonRequest();
  372. Integer goodsId = jsonParam.getInteger("goodsId");
  373. Integer productId = jsonParam.getInteger("productId");
  374. Integer number = jsonParam.getInteger("number");
  375. //判断购物车中是否存在此规格商品
  376. Map cartParam = new HashMap();
  377. cartParam.put("goods_id", goodsId);
  378. cartParam.put("product_id", productId);
  379. Long storeId = getStoreId();
  380. cartParam.put("store_id", storeId);
  381. cartParam.put("user_id", loginUser.getId());
  382. List<CartVo> cartInfoList = cartService.queryList(cartParam);
  383. CartVo cartInfo = null != cartInfoList && cartInfoList.size() > 0 ? cartInfoList.get(0) : null;
  384. int cart_num = 0;
  385. if (null != cartInfo) {
  386. if (cartInfo.getNumber() > number) {
  387. cartInfo.setNumber(cartInfo.getNumber() - number);
  388. cartService.update(cartInfo);
  389. cart_num = cartInfo.getNumber();
  390. } else if (cartInfo.getNumber() == 1) {
  391. cartService.delete(cartInfo.getId());
  392. cart_num = 0;
  393. }
  394. }
  395. return toResponsSuccess(cart_num);
  396. }
  397. /**
  398. * 更新指定的购物车信息
  399. */
  400. @PostMapping("update")
  401. public Object update(@LoginUser UserVo loginUser) {
  402. JSONObject jsonParam = getJsonRequest();
  403. Long goodsId = jsonParam.getLong("goodsId");
  404. Long storeId = getStoreId();
  405. Long productId = jsonParam.getLong("productId");
  406. Integer number = jsonParam.getInteger("number");
  407. Integer id = jsonParam.getInteger("id");
  408. String checkCart = jsonParam.getString("checkCart");
  409. boolean isAdd = true;
  410. //取得规格的信息,判断规格库存
  411. ProductVo productInfo = productService.queryByStoreId(productId, storeId);
  412. if (null == productInfo) {
  413. return this.toResponsObject(400, "商品已下架", getCart(checkCart));
  414. }
  415. if(productInfo.getStock_num() == null || number > productInfo.getStock_num() || productInfo.getStock_num() <= 0){
  416. return this.toResponsObject(400, "该商品库存不足", getCart(checkCart));
  417. }
  418. String msg = "";
  419. // if (productInfo.getStock_num() < number) {
  420. // msg = "库存不足,仅剩余" + productInfo.getStock_num();
  421. // number = productInfo.getStock_num();
  422. //// return this.toResponsObject(400, "库存不足,仅剩余" + productInfo.getStock_num(), "");
  423. // }
  424. //判断是否已经存在product_id购物车商品
  425. CartVo cartInfo = cartService.queryObject(id);
  426. //只是更新number
  427. if (cartInfo.getProduct_id().equals(productId)) {
  428. cartInfo.setNumber(number);
  429. cartInfo.setStockNum(productInfo.getStock_num());
  430. cartService.update(cartInfo);
  431. return toResponsObject(0, msg, getCart(checkCart));
  432. }
  433. Map cartParam = new HashMap();
  434. cartParam.put("goodsId", goodsId);
  435. cartParam.put("productId", productId);
  436. cartParam.put("store_id", storeId);
  437. List<CartVo> cartInfoList = cartService.queryList(cartParam);
  438. CartVo newcartInfo = null != cartInfoList && cartInfoList.size() > 0 ? cartInfoList.get(0) : null;
  439. if (null == newcartInfo) {
  440. cartInfo.setProduct_id(productId);
  441. cartInfo.setGoods_sn(productInfo.getGoods_sn());
  442. cartInfo.setNumber(number);
  443. cartInfo.setRetail_price(productInfo.getRetail_price());
  444. cartInfo.setMarket_price(productInfo.getRetail_price());
  445. cartInfo.setGoods_specification_name_value(productInfo.getGoods_specification_name_value());
  446. cartInfo.setGoods_specification_ids(productInfo.getGoods_specification_ids());
  447. cartService.update(cartInfo);
  448. } else {
  449. //添加规格名和值
  450. cartInfo.setProduct_id(productId);
  451. cartInfo.setGoods_sn(productInfo.getGoods_sn());
  452. cartInfo.setNumber(number);
  453. cartInfo.setRetail_price(productInfo.getRetail_price());
  454. cartInfo.setMarket_price(productInfo.getRetail_price());
  455. cartInfo.setGoods_specification_name_value(productInfo.getGoods_specification_name_value());
  456. cartInfo.setGoods_specification_ids(productInfo.getGoods_specification_ids());
  457. cartService.update(cartInfo);
  458. }
  459. return toResponsObject(0, msg, getCart(checkCart));
  460. }
  461. /**
  462. * 是否选择商品,如果已经选择,则取消选择,批量操作
  463. */
  464. @PostMapping("checked")
  465. public Object checked(@LoginUser UserVo loginUser) {
  466. JSONObject jsonParam = getJsonRequest();
  467. String productIds = jsonParam.getString("productIds");
  468. Integer isChecked = jsonParam.getInteger("isChecked");
  469. String goodsBizType = jsonParam.getString("goodsBizType");
  470. String checkCart = jsonParam.getString("checkCart");
  471. String[] productIdArray = null;
  472. if(StringUtils.isNullOrEmpty(goodsBizType)) {
  473. if (StringUtils.isNullOrEmpty(productIds)) {//点击全选时
  474. return this.toResponsFail("删除出错");
  475. }
  476. productIdArray = productIds.split(",");
  477. }else{
  478. //根据业务类型查询购物车
  479. List<CartVo> cartVoList = cartService.queryCartByGoodsBizType(goodsBizType);
  480. productIdArray = new String[cartVoList.size()];
  481. for (int i = 0;i< cartVoList.size();i++){
  482. productIdArray[i] =cartVoList.get(i).getProduct_id()+"";
  483. }
  484. }
  485. cartService.updateCheck(productIdArray, isChecked, loginUser.getId(), getStoreId());
  486. return toResponsSuccess(getCart(checkCart));
  487. }
  488. //删除选中的购物车商品,批量删除
  489. @PostMapping("delete")
  490. public Object delete(@LoginUser UserVo loginUser) {
  491. JSONObject jsonObject = getJsonRequest();
  492. Long cartId = jsonObject.getLong("cartId");
  493. String checkCart = jsonObject.getString("checkCart");
  494. if (null == cartId) {
  495. return toResponsFail("删除出错");
  496. }
  497. cartService.delete(cartId);
  498. return toResponsSuccess(getCart(checkCart));
  499. }
  500. // 获取购物车商品的总件件数
  501. @GetMapping("goodscount")
  502. public Object goodscount(@LoginUser UserVo loginUser) {
  503. if (null == loginUser || null == loginUser.getId()) {
  504. return toResponsFail("未登录");
  505. }
  506. Map<String, Object> resultObj = new HashMap();
  507. //查询列表数据
  508. Map param = new HashMap();
  509. param.put("user_id", loginUser.getId());
  510. param.put("store_id", getStoreId());
  511. List<CartVo> cartList = cartService.queryList(param);
  512. //获取购物车统计信息
  513. Integer goodsCount = 0;
  514. for (CartVo cartItem : cartList) {
  515. goodsCount += cartItem.getNumber();
  516. }
  517. resultObj.put("cartList", cartList);
  518. //
  519. Map<String, Object> cartTotal = new HashMap();
  520. cartTotal.put("goodsCount", goodsCount);
  521. //
  522. resultObj.put("cartTotal", cartTotal);
  523. return toResponsSuccess(resultObj);
  524. }
  525. /**
  526. * 订单提交前的检验和填写相关订单信息
  527. */
  528. @GetMapping("checkout")
  529. public Object checkout(@LoginUser UserVo loginUser, Long userCouponId, String merchSn, String checkCart) {
  530. Map<String, Object> resultObj = new HashMap();
  531. //选择的收货地址
  532. Map param = new HashMap();
  533. param.put("is_default", 1);
  534. param.put("user_id", loginUser.getId());
  535. List<AddressVo> addressEntityList = addressService.queryList(param);
  536. //获取要购买的商品
  537. Map<String, Object> cartData = (Map<String, Object>) this.getCartMoney(loginUser,checkCart);
  538. List<CartVo> checkedGoodsList = new ArrayList();
  539. for (CartVo cartEntity : (List<CartVo>) cartData.get("cartList")) {
  540. if (cartEntity.getChecked() == 1) {
  541. checkedGoodsList.add(cartEntity);
  542. }
  543. }
  544. //计算订单的费用
  545. //商品总价
  546. BigDecimal goodsTotalPrice = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount");
  547. //获取可用的优惠券信息 抵用券
  548. List<UserCouponVo> enableCouponList = apiUserCouponService.matchUserCouponList(loginUser.getId(),merchSn,goodsTotalPrice);
  549. UserCouponVo checkedCoupon = null;
  550. BigDecimal couponPrice = new BigDecimal(0.00); //使用优惠券减免的金额
  551. if (null != enableCouponList && enableCouponList.size() > 0) {
  552. if (null != userCouponId) {
  553. for (UserCouponVo couponVo : enableCouponList) {
  554. if (null != userCouponId && userCouponId.equals(couponVo.getId())) {
  555. couponPrice = couponVo.getType_money();
  556. checkedCoupon = couponVo;
  557. }
  558. }
  559. }
  560. }
  561. // 根据收货地址计算运费
  562. // BigDecimal freightPrice = apiCouponService.matchShipping(loginUser.getId(), goodsTotalPrice);
  563. BigDecimal freightPrice00 = new BigDecimal("00");
  564. BigDecimal freightPrice02 = new BigDecimal("00");
  565. BigDecimal freightPrice10 = new BigDecimal("00");
  566. BigDecimal freightPrice11 = new BigDecimal("00");
  567. //查询运费模板
  568. for (CartVo vo: checkedGoodsList) {
  569. if(org.apache.commons.lang.StringUtils.isEmpty(checkCart)){
  570. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
  571. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
  572. freightPrice00 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice00;
  573. }
  574. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
  575. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
  576. freightPrice02 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice02;
  577. }
  578. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
  579. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
  580. freightPrice10 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice10;
  581. }
  582. if (vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
  583. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
  584. freightPrice11 = freightEntity != null ? freightEntity.getDefaultFreight() : freightPrice11;
  585. }
  586. }else{
  587. if(Dict.orderBizType.item_11.getItem().equalsIgnoreCase(checkCart)) {
  588. if (vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
  589. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
  590. freightPrice11 = freightEntity != null ? freightEntity.getDefaultFreight() : freightPrice11;
  591. }
  592. }else{
  593. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
  594. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
  595. freightPrice00 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice00;
  596. }
  597. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
  598. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
  599. freightPrice02 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice02;
  600. }
  601. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
  602. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
  603. freightPrice10 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice10;
  604. }
  605. }
  606. }
  607. }
  608. // 计算优惠券 todo 暂时不计算全场金额满减
  609. BigDecimal fullCutCouponDec = Constant.ZERO;
  610. Long fullCutCouponId = 0L;
  611. /*CouponVo fullSubCoupon = apiCouponService.matchFullSub(loginUser.getId(), goodsTotalPrice);
  612. if (null != fullSubCoupon && null != fullSubCoupon.getId()) {
  613. // 满减
  614. fullCutCouponDec = fullSubCoupon.getType_money();
  615. fullCutCouponId = fullSubCoupon.getId();
  616. }*/
  617. resultObj.put("fullCutCouponDec", fullCutCouponDec);
  618. resultObj.put("fullCutCouponId", fullCutCouponId);
  619. //订单的总价
  620. BigDecimal orderTotalPrice = goodsTotalPrice.add(freightPrice00).add(freightPrice02).add(freightPrice10).add(freightPrice11);
  621. BigDecimal actualPrice = orderTotalPrice.subtract(fullCutCouponDec).subtract(couponPrice); //减去其它支付的金额后,要实际支付的金额
  622. resultObj.put("freightPrice00", freightPrice00);
  623. resultObj.put("freightPrice02", freightPrice02);
  624. resultObj.put("freightPrice10", freightPrice10);
  625. resultObj.put("freightPrice11", freightPrice11);
  626. resultObj.put("checkedCoupon", checkedCoupon);
  627. resultObj.put("couponList", enableCouponList);
  628. resultObj.put("couponPrice", couponPrice);
  629. resultObj.put("checkedGoodsList", checkedGoodsList);
  630. resultObj.put("goodsTotalPrice", goodsTotalPrice);
  631. resultObj.put("orderTotalPrice", orderTotalPrice);
  632. resultObj.put("actualPrice", actualPrice);
  633. if (null != addressEntityList && addressEntityList.size() > 0) {
  634. resultObj.put("addressVo", addressEntityList.get(0));
  635. } else { // 没有默认地址,选择一个
  636. param = new HashMap();
  637. param.put("user_id", loginUser.getId());
  638. addressEntityList = addressService.queryList(param);
  639. if (null != addressEntityList && addressEntityList.size() > 0) {
  640. resultObj.put("addressVo", addressEntityList.get(0));
  641. }
  642. }
  643. UserVo userVo = apiUserService.queryObject(loginUser.getId());
  644. if(userVo != null){
  645. resultObj.put("idNo", userVo.getIdNo());
  646. resultObj.put("userName", userVo.getUsername());
  647. }
  648. return toResponsSuccess(resultObj);
  649. }
  650. /**
  651. * 选择优惠券列表
  652. */
  653. @GetMapping("checkedCouponList")
  654. public Object checkedCouponList(@LoginUser UserVo loginUser) {
  655. //查询列表数据
  656. Map param = new HashMap();
  657. param.put("user_id", loginUser.getId());
  658. Long storeId = getStoreId();
  659. param.put("store_id", storeId);
  660. List<CartVo> cartList = cartService.queryList(param);
  661. //获取购物车统计信息
  662. BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
  663. for (CartVo cartItem : cartList) {
  664. if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
  665. checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  666. }
  667. }
  668. // 匹配优惠券
  669. List<UserCouponVo> couponVos = apiUserCouponService.signUserCouponList(loginUser.getId(),getMerchSn(),checkedGoodsAmount);
  670. return toResponsSuccess(couponVos);
  671. }
  672. // select checked from mall_cart where goods_biz_type ='00'
  673. @PostMapping("getCheckedDataByType")
  674. public Object getCheckedDataByType() {
  675. JSONObject jsonParam = getJsonRequest();
  676. String goodsBizType = jsonParam.getString("goodsBizType");
  677. List<CartVo> cartVoList = cartService.queryCartByGoodsBizType(goodsBizType);
  678. for (int i=0;i<cartVoList.size();i++){
  679. if(cartVoList.get(i).getChecked()==0){//未选中
  680. Map map=new HashMap();
  681. map.put("isChecked",false);
  682. return toResponsSuccess(map);
  683. }
  684. }
  685. Map map=new HashMap();
  686. map.put("isChecked",true);
  687. return toResponsSuccess(map);
  688. }
  689. /**
  690. * 清空购物车失效商品
  691. * @return
  692. */
  693. @GetMapping("deleteValidCart")
  694. public Object deleteValidCart(String checkCart) {
  695. try{
  696. Map param = new HashMap();
  697. param.put("user_id", getUserId());
  698. Long storeId = getStoreId();
  699. param.put("store_id", storeId);
  700. List<CartVo> list = cartService.queryValidCartList(param);
  701. for (CartVo cart: list) {
  702. cartService.delete(cart.getId());
  703. }
  704. return toResponsSuccess(getCart(checkCart));
  705. }catch (Exception e){
  706. return toResponsFail("清空失败");
  707. }
  708. }
  709. }