123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- package com.kmall.api.api;
- import com.alibaba.fastjson.JSONObject;
- import com.kmall.api.annotation.LoginUser;
- import com.kmall.common.constant.Dict;
- import com.kmall.api.entity.*;
- import com.kmall.api.service.*;
- import com.kmall.api.util.ApiBaseAction;
- import com.kmall.common.utils.Constant;
- import com.kmall.common.utils.MapUtils;
- import com.qiniu.util.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * 作者: @author Scott <br>
- * 时间: 2017-08-11 08:32<br>
- * 描述: ApiIndexController <br>
- */
- @RestController
- @RequestMapping("/api/cart")
- public class ApiCartController extends ApiBaseAction {
- @Autowired
- private ApiCartService cartService;
- @Autowired
- private ApiGoodsService goodsService;
- @Autowired
- private ApiProductService productService;
- @Autowired
- private ApiAddressService addressService;
- @Autowired
- private ApiCouponService apiCouponService;
- @Autowired
- private ApiUserCouponService apiUserCouponService;
- @Autowired
- private ApiOrderGoodsService apiOrderGoodsService;
- @Autowired
- private ApiUserService apiUserService;
- @Autowired
- private ApiFreightService apiFreightService;
- /**
- * 获取购物车中的数据
- */
- @GetMapping("getCartMoney")
- public Object getCartMoney(@LoginUser UserVo loginUser, String checkCart) {
- Map<String, Object> resultObj = new HashMap();
- //查询列表数据
- Map param = new HashMap();
- param.put("user_id", loginUser.getId());
- Long storeId = getStoreId();
- param.put("store_id", storeId);
- param.put("checkCart",checkCart);
- List<CartVo> cartList = cartService.queryList(param);
- //获取购物车统计信息
- Integer goodsCount = 0;
- BigDecimal goodsAmount = new BigDecimal(0.00);
- Integer checkedGoodsCount = 0;
- BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
- for (CartVo cartItem : cartList) {
- goodsCount += cartItem.getNumber();
- goodsAmount = goodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
- if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
- checkedGoodsCount += cartItem.getNumber();
- checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
- }
- }
- resultObj.put("cartList", cartList);
- //
- Map<String, Object> cartTotal = new HashMap();
- cartTotal.put("goodsCount", goodsCount);
- cartTotal.put("goodsAmount", goodsAmount);
- cartTotal.put("checkedGoodsCount", checkedGoodsCount);
- cartTotal.put("checkedGoodsAmount", checkedGoodsAmount);
- //
- resultObj.put("cartTotal", cartTotal);
- return resultObj;
- }
- /**
- * 获取购物车中的数据
- */
- @GetMapping("getCart")
- public Object getCart(String checkCart) {
- UserVo loginUser = new UserVo();
- loginUser.setId(getUserId());
- Map<String, Object> resultObj = new HashMap();
- //查询列表数据
- Map param = new HashMap();
- param.put("user_id", loginUser.getId());
- Long storeId = getStoreId();
- param.put("store_id", storeId);
- param.put("merchSn", getMerchSn());
- param.put("checkCart", checkCart);
- List<CartVo> cartList = cartService.queryList(param);
- List<CartVo> validCartList = cartService.queryValidCartList(param);
- //获取购物车统计信息
- Integer goodsCount = 0;
- BigDecimal goodsAmount = new BigDecimal(0.00);
- Integer checkedGoodsCount = 0;
- BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
- List<CartVo> cart00List = new ArrayList<>();
- List<CartVo> cart02List = new ArrayList<>();
- List<CartVo> cart10List = new ArrayList<>();
- List<CartVo> cart11List = new ArrayList<>();
- for (CartVo cartItem : cartList) {
- goodsCount += cartItem.getNumber();
- goodsAmount = goodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
- if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
- checkedGoodsCount += cartItem.getNumber();
- checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
- }
- if(org.apache.commons.lang.StringUtils.isNotEmpty(cartItem.getGoodsBizType())){
- cartItem.setGoodsBizTypeName(Dict.orderBizType.valueOf("item_"+ cartItem.getGoodsBizType()).getItemName());
- }else{
- cartItem.setGoodsBizTypeName("");
- }
- if(Dict.orderBizType.item_00.getItem().equalsIgnoreCase(cartItem.getGoodsBizType())){
- CartVo cartVo00= cartItem;
- cart00List.add(cartVo00);
- }
- if(Dict.orderBizType.item_02.getItem().equalsIgnoreCase(cartItem.getGoodsBizType())){
- CartVo cartVo02= cartItem;
- cart02List.add(cartVo02);
- }
- if(Dict.orderBizType.item_10.getItem().equalsIgnoreCase(cartItem.getGoodsBizType())){
- CartVo cartVo10= cartItem;
- cart10List.add(cartVo10);
- }
- if(Dict.orderBizType.item_11.getItem().equalsIgnoreCase(cartItem.getGoodsBizType())){
- CartVo cartVo11= cartItem;
- cart11List.add(cartVo11);
- }
- }
- // 获取优惠信息提示 邮费
- CouponVo shippingCoupon = apiCouponService.matchShippingSign(loginUser.getId(), checkedGoodsAmount);
- // 获取优惠信息提示 满减
- CouponVo fullSubCoupon = apiCouponService.matchFullSubSign(loginUser.getId(), checkedGoodsAmount);
- List<CouponVo> couponInfoList = new ArrayList();
- if (null != shippingCoupon) {
- couponInfoList.add(shippingCoupon);
- }
- if (null != fullSubCoupon) {
- couponInfoList.add(fullSubCoupon);
- }
- resultObj.put("couponInfoList", couponInfoList);
- resultObj.put("cart00List", cart00List);//{'cartList':{'type': '保税备货','data':{}}}
- resultObj.put("cart02List", cart02List);
- resultObj.put("cart10List", cart10List);
- resultObj.put("cart11List", cart11List);
- resultObj.put("cartList", cartList);
- resultObj.put("validCartList", validCartList);
- //
- Map<String, Object> cartTotal = new HashMap();
- cartTotal.put("goodsCount", goodsCount);
- cartTotal.put("goodsAmount", goodsAmount);
- cartTotal.put("checkedGoodsCount", checkedGoodsCount);
- cartTotal.put("checkedGoodsAmount", checkedGoodsAmount);
- resultObj.put("cartTotal", cartTotal);
- param.put("checkCart","00");
- int total00 = cartService.queryTotal(param);
- param.put("checkCart","11");
- int total11 = cartService.queryTotal(param);
- resultObj.put("total00", total00);
- resultObj.put("total11", total11);
- return resultObj;
- }
- /**
- * 获取购物车中的数据,底部显示
- */
- @GetMapping("getFootCart")
- public Object getFootCart(@LoginUser UserVo loginUser) {
- Map<String, Object> resultObj = new HashMap();
- //查询列表数据
- Map param = new HashMap();
- param.put("user_id", loginUser.getId());
- Long storeId = getStoreId();
- param.put("store_id", storeId);
- List<CartVo> cartList = cartService.queryList(param);
- //获取购物车统计信息
- Integer goodsCount = 0;
- BigDecimal goodsAmount = new BigDecimal(0.00);
- Integer checkedGoodsCount = 0;
- BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
- for (CartVo cartItem : cartList) {
- goodsCount += 1;
- goodsAmount = goodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
- if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
- checkedGoodsCount += cartItem.getNumber();
- checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
- }
- }
- //
- resultObj.put("goodsCount", goodsCount);
- resultObj.put("goodsAmount", goodsAmount);
- resultObj.put("checkedGoodsCount", checkedGoodsCount);
- resultObj.put("checkedGoodsAmount", checkedGoodsAmount);
- //
- return toResponsSuccess(resultObj);
- }
- /**
- * 获取购物车信息,所有对购物车的增删改操作,都要重新返回购物车的信息
- */
- @GetMapping("index")
- public Object index(String checkCart) {
- return toResponsSuccess(getCart(checkCart));
- }
- /**
- * 添加商品到购物车
- */
- @PostMapping("add")
- public Object add(@LoginUser UserVo loginUser) {
- JSONObject jsonParam = getJsonRequest();
- Long goodsId = jsonParam.getLong("goodsId");
- Long productId = jsonParam.getLong("productId");
- Integer number = jsonParam.getInteger("number");
- String checkCart = jsonParam.getString("checkCart");
- //判断商品是否可以购买
- GoodsVo goodsInfo = goodsService.queryObject(goodsId);
- if (null == goodsInfo || goodsInfo.getIs_delete() == 1 || goodsInfo.getIs_on_sale() == 0) {
- return toResponsFail("商品已下架");
- }
- Long storeId = getStoreId();
- //取得规格的信息,判断规格库存
- /*ProductVo productInfo = productService.queryObjectByStoreId(productId, storeId);
- if (null == productInfo) {
- return toResponsFail("商品已下架");
- }
- if(productInfo.getStock_num() <= 0){
- return toResponsFail("库存不足");
- }*/
- ProductVo productInfo = productService.queryByStoreId(productId, storeId);
- if (null == productInfo) {
- return toResponsFail("商品已下架");
- }
- if(productInfo.getStock_num() == null || number > productInfo.getStock_num() || productInfo.getStock_num() <= 0){
- return toResponsFail("该商品库存不足");
- }
- if (null == productInfo.getRetail_price()) {
- productInfo.setRetail_price(goodsInfo.getRetail_price());
- productInfo.setMarket_price(goodsInfo.getMarket_price());
- }
- //判断购物车中是否存在此规格商品
- Map cartParam = new HashMap();
- cartParam.put("goods_id", goodsId);
- cartParam.put("product_id", productId);
- cartParam.put("user_id", loginUser.getId());
- cartParam.put("store_id", storeId);
- List<CartVo> cartInfoList = cartService.queryList(cartParam);
- CartVo cartInfo = null != cartInfoList && cartInfoList.size() > 0 ? cartInfoList.get(0) : null;
- if (null == cartInfo) {//添加规格名和值
- cartInfo = new CartVo();
- cartInfo.setGoods_id(goodsId);
- cartInfo.setProduct_id(productId);
- cartInfo.setGoods_sn(productInfo.getGoods_sn());
- cartInfo.setGoods_name(goodsInfo.getName());
- cartInfo.setList_pic_url(goodsInfo.getList_pic_url());
- cartInfo.setNumber(number);
- cartInfo.setStore_id(storeId);
- cartInfo.setUser_id(loginUser.getId());
- cartInfo.setRetail_price(productInfo.getRetail_price());
- cartInfo.setMarket_price(productInfo.getMarket_price());
- cartInfo.setGoods_specification_name_value(productInfo.getGoods_specification_name_value());
- cartInfo.setGoods_specification_ids(productInfo.getGoods_specification_ids());
- cartInfo.setChecked(1);
- cartInfo.setGoodsBizType(goodsInfo.getGoodsBizType());//业务类型
- cartInfo.setStockNum(productInfo.getStock_num());
- cartInfo.setMerchSn(goodsInfo.getMerchSn());
- cartService.save(cartInfo);
- } else {
- if(number + cartInfo.getNumber() > productInfo.getStock_num()){
- return toResponsFail("商品选购数量(含购物车已加购数量)已超过库存");
- }
- cartInfo.setNumber(cartInfo.getNumber() + number);
- cartInfo.setGoodsBizType(goodsInfo.getGoodsBizType());//业务类型
- cartInfo.setStockNum(productInfo.getStock_num());
- cartService.update(cartInfo);
- }
- return toResponsSuccess(getCart(checkCart));
- }
- /**
- * 添加商品到购物车
- */
- @PostMapping("addByGoodsId")
- public Object addByGoodsId(@LoginUser UserVo loginUser) {
- JSONObject jsonParam = getJsonRequest();
- Long goodsId = jsonParam.getLong("goodsId");
- String checkCart = jsonParam.getString("checkCart");
- //判断商品是否可以购买
- GoodsVo goodsInfo = goodsService.queryObject(goodsId);
- if (null == goodsInfo || goodsInfo.getIs_delete() == 1 || goodsInfo.getIs_on_sale() == 0) {
- return toResponsFail("商品已下架");
- }
- Long storeId = getStoreId();
- Map resultObj = cartService.addByGoodsId(getUserId(), goodsId, storeId);
- if (0 != MapUtils.getInteger("errno", resultObj)) {
- return toResponsObject(MapUtils.getInteger("errno", resultObj), MapUtils.getString("errmsg", resultObj), "");
- }
- return toResponsSuccess(getCart(checkCart));
- }
- /**
- * 根据订单号添加商品到购物车
- */
- @GetMapping("addByOrder")
- public Object addByOrder(@LoginUser UserVo loginUser, Long orderId) {
- JSONObject jsonParam = getJsonRequest();
- //
- Map params = new HashMap();
- params.put("order_id", orderId);
- List<OrderGoodsVo> orderGoodsVos = apiOrderGoodsService.queryList(params);
- for (OrderGoodsVo goodsVo : orderGoodsVos) {
- //判断商品是否可以购买
- GoodsVo goodsInfo = goodsService.queryObject(goodsVo.getGoods_id());
- if (null == goodsInfo || goodsInfo.getIs_delete() == 1 || goodsInfo.getIs_on_sale() == 0) {
- return toResponsFail("商品已下架");
- }
- }
- Long storeId = getStoreId();
- for (OrderGoodsVo goodsVo : orderGoodsVos) {
- ProductVo productInfo = productService.queryByStoreId(goodsVo.getProduct_id(), storeId);
- if (null == productInfo) {
- return toResponsFail("商品已下架");
- }
- if(productInfo.getStock_num() == null || goodsVo.getNumber() > productInfo.getStock_num() || productInfo.getStock_num() <= 0){
- return toResponsFail("该商品库存不足");
- }
- CartVo cartInfo = new CartVo();
- cartInfo.setGoods_id(goodsVo.getGoods_id());
- cartInfo.setProduct_id(goodsVo.getProduct_id());
- cartInfo.setGoods_sn(goodsVo.getGoods_sn());
- cartInfo.setGoods_name(goodsVo.getGoods_name());
- cartInfo.setList_pic_url(goodsVo.getList_pic_url());
- cartInfo.setNumber(goodsVo.getNumber());
- cartInfo.setStore_id(storeId);
- cartInfo.setUser_id(getUserId());
- cartInfo.setRetail_price(goodsVo.getRetail_price());
- cartInfo.setMarket_price(goodsVo.getMarket_price());
- cartInfo.setGoods_specification_name_value(goodsVo.getGoods_specification_name_value());
- cartInfo.setGoods_specification_ids(goodsVo.getGoods_specification_ids());
- cartInfo.setChecked(1);
- cartInfo.setGoodsBizType(goodsVo.getOrderBizType());
- cartInfo.setStockNum(productInfo.getStock_num());
- cartInfo.setMerchSn(goodsVo.getMerchSn());
- Map map = new HashMap();
- map.put("user_id", getUserId());
- map.put("store_id", getStoreId());
- map.put("goods_id",goodsVo.getGoods_id());
- map.put("product_id",goodsVo.getProduct_id());
- map.put("number",goodsVo.getNumber());
- List<CartVo> list= cartService.queryList(map);
- if(list != null && list.size() > 0){
- for (CartVo vo:list) {
- cartInfo.setId(vo.getId());
- cartService.update(cartInfo);
- }
- }else{
- cartService.save(cartInfo);
- }
- }
- return toResponsSuccess("添加成功");
- }
- /**
- * 减少商品到购物车
- */
- @PostMapping("minus")
- public Object minus(@LoginUser UserVo loginUser) {
- JSONObject jsonParam = getJsonRequest();
- Integer goodsId = jsonParam.getInteger("goodsId");
- Integer productId = jsonParam.getInteger("productId");
- Integer number = jsonParam.getInteger("number");
- //判断购物车中是否存在此规格商品
- Map cartParam = new HashMap();
- cartParam.put("goods_id", goodsId);
- cartParam.put("product_id", productId);
- Long storeId = getStoreId();
- cartParam.put("store_id", storeId);
- cartParam.put("user_id", loginUser.getId());
- List<CartVo> cartInfoList = cartService.queryList(cartParam);
- CartVo cartInfo = null != cartInfoList && cartInfoList.size() > 0 ? cartInfoList.get(0) : null;
- int cart_num = 0;
- if (null != cartInfo) {
- if (cartInfo.getNumber() > number) {
- cartInfo.setNumber(cartInfo.getNumber() - number);
- cartService.update(cartInfo);
- cart_num = cartInfo.getNumber();
- } else if (cartInfo.getNumber() == 1) {
- cartService.delete(cartInfo.getId());
- cart_num = 0;
- }
- }
- return toResponsSuccess(cart_num);
- }
- /**
- * 更新指定的购物车信息
- */
- @PostMapping("update")
- public Object update(@LoginUser UserVo loginUser) {
- JSONObject jsonParam = getJsonRequest();
- Long goodsId = jsonParam.getLong("goodsId");
- Long storeId = getStoreId();
- Long productId = jsonParam.getLong("productId");
- Integer number = jsonParam.getInteger("number");
- Integer id = jsonParam.getInteger("id");
- String checkCart = jsonParam.getString("checkCart");
- boolean isAdd = true;
- //取得规格的信息,判断规格库存
- ProductVo productInfo = productService.queryByStoreId(productId, storeId);
- if (null == productInfo) {
- return this.toResponsObject(400, "商品已下架", getCart(checkCart));
- }
- if(productInfo.getStock_num() == null || number > productInfo.getStock_num() || productInfo.getStock_num() <= 0){
- return this.toResponsObject(400, "该商品库存不足", getCart(checkCart));
- }
- String msg = "";
- // if (productInfo.getStock_num() < number) {
- // msg = "库存不足,仅剩余" + productInfo.getStock_num();
- // number = productInfo.getStock_num();
- //// return this.toResponsObject(400, "库存不足,仅剩余" + productInfo.getStock_num(), "");
- // }
- //判断是否已经存在product_id购物车商品
- CartVo cartInfo = cartService.queryObject(id);
- //只是更新number
- if (cartInfo.getProduct_id().equals(productId)) {
- cartInfo.setNumber(number);
- cartInfo.setStockNum(productInfo.getStock_num());
- cartService.update(cartInfo);
- return toResponsObject(0, msg, getCart(checkCart));
- }
- Map cartParam = new HashMap();
- cartParam.put("goodsId", goodsId);
- cartParam.put("productId", productId);
- cartParam.put("store_id", storeId);
- List<CartVo> cartInfoList = cartService.queryList(cartParam);
- CartVo newcartInfo = null != cartInfoList && cartInfoList.size() > 0 ? cartInfoList.get(0) : null;
- if (null == newcartInfo) {
- cartInfo.setProduct_id(productId);
- cartInfo.setGoods_sn(productInfo.getGoods_sn());
- cartInfo.setNumber(number);
- cartInfo.setRetail_price(productInfo.getRetail_price());
- cartInfo.setMarket_price(productInfo.getRetail_price());
- cartInfo.setGoods_specification_name_value(productInfo.getGoods_specification_name_value());
- cartInfo.setGoods_specification_ids(productInfo.getGoods_specification_ids());
- cartService.update(cartInfo);
- } else {
- //添加规格名和值
- cartInfo.setProduct_id(productId);
- cartInfo.setGoods_sn(productInfo.getGoods_sn());
- cartInfo.setNumber(number);
- cartInfo.setRetail_price(productInfo.getRetail_price());
- cartInfo.setMarket_price(productInfo.getRetail_price());
- cartInfo.setGoods_specification_name_value(productInfo.getGoods_specification_name_value());
- cartInfo.setGoods_specification_ids(productInfo.getGoods_specification_ids());
- cartService.update(cartInfo);
- }
- return toResponsObject(0, msg, getCart(checkCart));
- }
- /**
- * 是否选择商品,如果已经选择,则取消选择,批量操作
- */
- @PostMapping("checked")
- public Object checked(@LoginUser UserVo loginUser) {
- JSONObject jsonParam = getJsonRequest();
- String productIds = jsonParam.getString("productIds");
- Integer isChecked = jsonParam.getInteger("isChecked");
- String goodsBizType = jsonParam.getString("goodsBizType");
- String checkCart = jsonParam.getString("checkCart");
- String[] productIdArray = null;
- if(StringUtils.isNullOrEmpty(goodsBizType)) {
- if (StringUtils.isNullOrEmpty(productIds)) {//点击全选时
- return this.toResponsFail("删除出错");
- }
- productIdArray = productIds.split(",");
- }else{
- //根据业务类型查询购物车
- List<CartVo> cartVoList = cartService.queryCartByGoodsBizType(goodsBizType);
- productIdArray = new String[cartVoList.size()];
- for (int i = 0;i< cartVoList.size();i++){
- productIdArray[i] =cartVoList.get(i).getProduct_id()+"";
- }
- }
- cartService.updateCheck(productIdArray, isChecked, loginUser.getId(), getStoreId());
- return toResponsSuccess(getCart(checkCart));
- }
- //删除选中的购物车商品,批量删除
- @PostMapping("delete")
- public Object delete(@LoginUser UserVo loginUser) {
- JSONObject jsonObject = getJsonRequest();
- Long cartId = jsonObject.getLong("cartId");
- String checkCart = jsonObject.getString("checkCart");
- if (null == cartId) {
- return toResponsFail("删除出错");
- }
- cartService.delete(cartId);
- return toResponsSuccess(getCart(checkCart));
- }
- // 获取购物车商品的总件件数
- @GetMapping("goodscount")
- public Object goodscount(@LoginUser UserVo loginUser) {
- if (null == loginUser || null == loginUser.getId()) {
- return toResponsFail("未登录");
- }
- Map<String, Object> resultObj = new HashMap();
- //查询列表数据
- Map param = new HashMap();
- param.put("user_id", loginUser.getId());
- param.put("store_id", getStoreId());
- List<CartVo> cartList = cartService.queryList(param);
- //获取购物车统计信息
- Integer goodsCount = 0;
- for (CartVo cartItem : cartList) {
- goodsCount += cartItem.getNumber();
- }
- resultObj.put("cartList", cartList);
- //
- Map<String, Object> cartTotal = new HashMap();
- cartTotal.put("goodsCount", goodsCount);
- //
- resultObj.put("cartTotal", cartTotal);
- return toResponsSuccess(resultObj);
- }
- /**
- * 订单提交前的检验和填写相关订单信息
- */
- @GetMapping("checkout")
- public Object checkout(@LoginUser UserVo loginUser, Long userCouponId, String merchSn, String checkCart) {
- Map<String, Object> resultObj = new HashMap();
- //选择的收货地址
- Map param = new HashMap();
- param.put("is_default", 1);
- param.put("user_id", loginUser.getId());
- List<AddressVo> addressEntityList = addressService.queryList(param);
- //获取要购买的商品
- Map<String, Object> cartData = (Map<String, Object>) this.getCartMoney(loginUser,checkCart);
- List<CartVo> checkedGoodsList = new ArrayList();
- for (CartVo cartEntity : (List<CartVo>) cartData.get("cartList")) {
- if (cartEntity.getChecked() == 1) {
- checkedGoodsList.add(cartEntity);
- }
- }
- //计算订单的费用
- //商品总价
- BigDecimal goodsTotalPrice = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount");
- //获取可用的优惠券信息 抵用券
- List<UserCouponVo> enableCouponList = apiUserCouponService.matchUserCouponList(loginUser.getId(),merchSn,goodsTotalPrice);
- UserCouponVo checkedCoupon = null;
- BigDecimal couponPrice = new BigDecimal(0.00); //使用优惠券减免的金额
- if (null != enableCouponList && enableCouponList.size() > 0) {
- if (null != userCouponId) {
- for (UserCouponVo couponVo : enableCouponList) {
- if (null != userCouponId && userCouponId.equals(couponVo.getId())) {
- couponPrice = couponVo.getType_money();
- checkedCoupon = couponVo;
- }
- }
- }
- }
- // 根据收货地址计算运费
- // BigDecimal freightPrice = apiCouponService.matchShipping(loginUser.getId(), goodsTotalPrice);
- BigDecimal freightPrice00 = new BigDecimal("00");
- BigDecimal freightPrice02 = new BigDecimal("00");
- BigDecimal freightPrice10 = new BigDecimal("00");
- BigDecimal freightPrice11 = new BigDecimal("00");
- //查询运费模板
- for (CartVo vo: checkedGoodsList) {
- if(org.apache.commons.lang.StringUtils.isEmpty(checkCart)){
- if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
- FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
- freightPrice00 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice00;
- }
- if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
- FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
- freightPrice02 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice02;
- }
- if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
- FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
- freightPrice10 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice10;
- }
- if (vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
- FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
- freightPrice11 = freightEntity != null ? freightEntity.getDefaultFreight() : freightPrice11;
- }
- }else{
- if(Dict.orderBizType.item_11.getItem().equalsIgnoreCase(checkCart)) {
- if (vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
- FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
- freightPrice11 = freightEntity != null ? freightEntity.getDefaultFreight() : freightPrice11;
- }
- }else{
- if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
- FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
- freightPrice00 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice00;
- }
- if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
- FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
- freightPrice02 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice02;
- }
- if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
- FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(vo.getGoods_id());
- freightPrice10 = freightEntity!=null ? freightEntity.getDefaultFreight() : freightPrice10;
- }
- }
- }
- }
- // 计算优惠券 todo 暂时不计算全场金额满减
- BigDecimal fullCutCouponDec = Constant.ZERO;
- Long fullCutCouponId = 0L;
- /*CouponVo fullSubCoupon = apiCouponService.matchFullSub(loginUser.getId(), goodsTotalPrice);
- if (null != fullSubCoupon && null != fullSubCoupon.getId()) {
- // 满减
- fullCutCouponDec = fullSubCoupon.getType_money();
- fullCutCouponId = fullSubCoupon.getId();
- }*/
- resultObj.put("fullCutCouponDec", fullCutCouponDec);
- resultObj.put("fullCutCouponId", fullCutCouponId);
- //订单的总价
- BigDecimal orderTotalPrice = goodsTotalPrice.add(freightPrice00).add(freightPrice02).add(freightPrice10).add(freightPrice11);
- BigDecimal actualPrice = orderTotalPrice.subtract(fullCutCouponDec).subtract(couponPrice); //减去其它支付的金额后,要实际支付的金额
- resultObj.put("freightPrice00", freightPrice00);
- resultObj.put("freightPrice02", freightPrice02);
- resultObj.put("freightPrice10", freightPrice10);
- resultObj.put("freightPrice11", freightPrice11);
- resultObj.put("checkedCoupon", checkedCoupon);
- resultObj.put("couponList", enableCouponList);
- resultObj.put("couponPrice", couponPrice);
- resultObj.put("checkedGoodsList", checkedGoodsList);
- resultObj.put("goodsTotalPrice", goodsTotalPrice);
- resultObj.put("orderTotalPrice", orderTotalPrice);
- resultObj.put("actualPrice", actualPrice);
- if (null != addressEntityList && addressEntityList.size() > 0) {
- resultObj.put("addressVo", addressEntityList.get(0));
- } else { // 没有默认地址,选择一个
- param = new HashMap();
- param.put("user_id", loginUser.getId());
- addressEntityList = addressService.queryList(param);
- if (null != addressEntityList && addressEntityList.size() > 0) {
- resultObj.put("addressVo", addressEntityList.get(0));
- }
- }
- UserVo userVo = apiUserService.queryObject(loginUser.getId());
- if(userVo != null){
- resultObj.put("idNo", userVo.getIdNo());
- resultObj.put("userName", userVo.getUsername());
- }
- return toResponsSuccess(resultObj);
- }
- /**
- * 选择优惠券列表
- */
- @GetMapping("checkedCouponList")
- public Object checkedCouponList(@LoginUser UserVo loginUser) {
- //查询列表数据
- Map param = new HashMap();
- param.put("user_id", loginUser.getId());
- Long storeId = getStoreId();
- param.put("store_id", storeId);
- List<CartVo> cartList = cartService.queryList(param);
- //获取购物车统计信息
- BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
- for (CartVo cartItem : cartList) {
- if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
- checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
- }
- }
- // 匹配优惠券
- List<UserCouponVo> couponVos = apiUserCouponService.signUserCouponList(loginUser.getId(),getMerchSn(),checkedGoodsAmount);
- return toResponsSuccess(couponVos);
- }
- // select checked from mall_cart where goods_biz_type ='00'
- @PostMapping("getCheckedDataByType")
- public Object getCheckedDataByType() {
- JSONObject jsonParam = getJsonRequest();
- String goodsBizType = jsonParam.getString("goodsBizType");
- List<CartVo> cartVoList = cartService.queryCartByGoodsBizType(goodsBizType);
- for (int i=0;i<cartVoList.size();i++){
- if(cartVoList.get(i).getChecked()==0){//未选中
- Map map=new HashMap();
- map.put("isChecked",false);
- return toResponsSuccess(map);
- }
- }
- Map map=new HashMap();
- map.put("isChecked",true);
- return toResponsSuccess(map);
- }
- /**
- * 清空购物车失效商品
- * @return
- */
- @GetMapping("deleteValidCart")
- public Object deleteValidCart(String checkCart) {
- try{
- Map param = new HashMap();
- param.put("user_id", getUserId());
- Long storeId = getStoreId();
- param.put("store_id", storeId);
- List<CartVo> list = cartService.queryValidCartList(param);
- for (CartVo cart: list) {
- cartService.delete(cart.getId());
- }
- return toResponsSuccess(getCart(checkCart));
- }catch (Exception e){
- return toResponsFail("清空失败");
- }
- }
- }
|