ApiCartController.java 30 KB

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