1
0

ApiCartController.java 26 KB

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