ApiCartController.java 27 KB

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