CartServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.fuint.common.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.fuint.common.enums.StatusEnum;
  5. import com.fuint.common.service.CartService;
  6. import com.fuint.framework.annoation.OperationServiceLog;
  7. import com.fuint.framework.exception.BusinessCheckException;
  8. import com.fuint.repository.mapper.MtCartMapper;
  9. import com.fuint.repository.mapper.MtGoodsMapper;
  10. import com.fuint.repository.mapper.MtGoodsSkuMapper;
  11. import com.fuint.repository.model.MtCart;
  12. import com.fuint.repository.model.MtGoods;
  13. import com.fuint.repository.model.MtGoodsSku;
  14. import com.fuint.utils.StringUtil;
  15. import lombok.AllArgsConstructor;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.util.Date;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * 购物车业务实现类
  25. *
  26. * Created by FSQ
  27. * CopyRight https://www.fuint.cn
  28. */
  29. @Service
  30. @AllArgsConstructor
  31. public class CartServiceImpl extends ServiceImpl<MtCartMapper, MtCart> implements CartService {
  32. private MtCartMapper mtCartMapper;
  33. private MtGoodsMapper mtGoodsMapper;
  34. private MtGoodsSkuMapper mtGoodsSkuMapper;
  35. /**
  36. * 切换购物车给会员
  37. *
  38. * @param userId
  39. * @param cartIds
  40. * @return
  41. * */
  42. @Override
  43. @Transactional(rollbackFor = Exception.class)
  44. public Boolean switchCartIds(Integer userId, String cartIds) {
  45. if (userId == null || userId < 1 || StringUtil.isEmpty(cartIds)) {
  46. return false;
  47. }
  48. List<String> cartIdList = Arrays.asList(cartIds.split(","));
  49. if (cartIdList != null && cartIdList.size() > 0) {
  50. for (String cartId : cartIdList) {
  51. if (StringUtil.isNotEmpty(cartId)) {
  52. MtCart mtCart = mtCartMapper.selectById(Integer.parseInt(cartId));
  53. if (mtCart != null) {
  54. mtCart.setUserId(userId);
  55. this.updateById(mtCart);
  56. }
  57. }
  58. }
  59. }
  60. return true;
  61. }
  62. /**
  63. * 保存购物车
  64. *
  65. * @param reqDto
  66. * @throws BusinessCheckException
  67. * @return
  68. */
  69. @Override
  70. @Transactional(rollbackFor = Exception.class)
  71. public Integer saveCart(MtCart reqDto, String action) throws BusinessCheckException {
  72. if (reqDto.getId() == null && (reqDto.getMerchantId() == null || reqDto.getMerchantId() < 1)) {
  73. throw new BusinessCheckException("商户不能为空");
  74. }
  75. if (reqDto.getId() == null && (reqDto.getStoreId() == null || reqDto.getStoreId() < 1)) {
  76. throw new BusinessCheckException("店铺不能为空");
  77. }
  78. MtCart mtCart = new MtCart();
  79. Integer cartId = 1;
  80. // 检查库存是否充足
  81. if (action.equals("+") || action.equals("=") && reqDto.getNum() > 0) {
  82. MtGoods mtGoods = mtGoodsMapper.selectById(reqDto.getGoodsId());
  83. Map<String, Object> param = new HashMap<>();
  84. param.put("status", StatusEnum.ENABLED.getKey());
  85. param.put("USER_ID", reqDto.getUserId());
  86. param.put("GOODS_ID", reqDto.getGoodsId());
  87. if (reqDto.getSkuId() != null && reqDto.getSkuId() > 0) {
  88. param.put("SKU_ID", reqDto.getSkuId());
  89. }
  90. List<MtCart> cartList = mtCartMapper.selectByMap(param);
  91. Integer cartNum = 0;
  92. if (cartList != null && cartList.size() > 0) {
  93. cartNum = cartList.get(0).getNum();
  94. }
  95. // 剩余库存数量
  96. Integer totalStock = 0;
  97. if (reqDto.getSkuId() != null && reqDto.getSkuId() > 0) {
  98. MtGoodsSku mtGoodsSku = mtGoodsSkuMapper.selectById(reqDto.getSkuId());
  99. if (mtGoodsSku != null && mtGoodsSku.getStock() != null) {
  100. totalStock = mtGoodsSku.getStock();
  101. }
  102. } else {
  103. totalStock = mtGoods.getStock();
  104. }
  105. // 判断库存,库存小于要添加的购物车数量、已添加的购物车数量大于库存
  106. if (totalStock < reqDto.getNum() || totalStock <= cartNum) {
  107. if (action.equals("=") && reqDto.getNum() < cartNum) {
  108. // empty
  109. } else {
  110. throw new BusinessCheckException(mtGoods.getName() + "库存不足了");
  111. }
  112. }
  113. }
  114. if (reqDto.getGoodsId() > 0) {
  115. mtCart.setGoodsId(reqDto.getGoodsId());
  116. }
  117. if (reqDto.getUserId() > 0) {
  118. mtCart.setUserId(reqDto.getUserId());
  119. }
  120. // 数量为0,删除购物车
  121. if (reqDto.getNum() == 0 && reqDto.getId() > 0) {
  122. this.removeCart(reqDto.getId()+"");
  123. } else if (reqDto.getNum() == 0 && action.equals("-")) {
  124. mtCartMapper.deleteCartItem(reqDto.getUserId(), reqDto.getGoodsId(), reqDto.getSkuId());
  125. }
  126. // 校验skuId是否正确
  127. if (reqDto.getSkuId() != null) {
  128. if (reqDto.getSkuId() > 0) {
  129. Map<String, Object> param = new HashMap<>();
  130. param.put("goods_id", reqDto.getGoodsId().toString());
  131. param.put("id", reqDto.getSkuId().toString());
  132. List<MtGoodsSku> skuList = mtGoodsSkuMapper.selectByMap(param);
  133. // 该skuId不正常
  134. if (skuList.size() < 1) {
  135. reqDto.setSkuId(0);
  136. }
  137. }
  138. }
  139. mtCart.setMerchantId(reqDto.getMerchantId());
  140. mtCart.setStoreId(reqDto.getStoreId() == null ? 0 : reqDto.getStoreId());
  141. mtCart.setStatus(StatusEnum.ENABLED.getKey());
  142. mtCart.setUpdateTime(new Date());
  143. mtCart.setSkuId(reqDto.getSkuId());
  144. mtCart.setNum(reqDto.getNum());
  145. mtCart.setHangNo(reqDto.getHangNo());
  146. mtCart.setIsVisitor(reqDto.getIsVisitor());
  147. Map<String, Object> params = new HashMap<>();
  148. params.put("userId", mtCart.getUserId());
  149. params.put("storeId", mtCart.getStoreId());
  150. params.put("goodsId", mtCart.getGoodsId());
  151. params.put("skuId", mtCart.getSkuId());
  152. params.put("hangNo", reqDto.getHangNo() == null ? "" : reqDto.getHangNo());
  153. List<MtCart> cartList = queryCartListByParams(params);
  154. if (action.equals("-") && cartList.size() == 0) {
  155. return cartId;
  156. }
  157. // 已存在,仅操作数量增加或减少
  158. if (cartList.size() > 0 && (mtCart.getId() == null || mtCart.getId() < 1)) {
  159. mtCart = cartList.get(0);
  160. if (action.equals("+")) {
  161. mtCart.setNum(mtCart.getNum() + reqDto.getNum());
  162. } else if (action.equals("=")) {
  163. mtCart.setNum(reqDto.getNum());
  164. } else {
  165. Integer num = mtCart.getNum() - 1;
  166. if (num <= 0) {
  167. this.removeCart(mtCart.getId()+"");
  168. return mtCart.getId();
  169. } else {
  170. mtCart.setNum(mtCart.getNum() - 1);
  171. }
  172. }
  173. this.updateById(mtCart);
  174. } else {
  175. mtCart.setCreateTime(new Date());
  176. this.save(mtCart);
  177. }
  178. return mtCart.getId();
  179. }
  180. /**
  181. * 删除购物车
  182. *
  183. * @param cartIds
  184. * @throws BusinessCheckException
  185. * @return
  186. */
  187. @Override
  188. @Transactional(rollbackFor = Exception.class)
  189. public void removeCart(String cartIds) {
  190. String[] ids = cartIds.split(",");
  191. if (ids.length < 1) {
  192. return;
  193. }
  194. for (int i = 0; i < ids.length; i++) {
  195. MtCart mtCart = mtCartMapper.selectById(Integer.parseInt(ids[i].trim()));
  196. if (mtCart != null) {
  197. mtCartMapper.deleteById(mtCart.getId());
  198. }
  199. }
  200. }
  201. /**
  202. * 删除挂单购物车
  203. *
  204. * @param hangNo 挂单序号
  205. * @throws BusinessCheckException
  206. * @return
  207. */
  208. @Override
  209. @OperationServiceLog(description = "删除挂单")
  210. @Transactional(rollbackFor = Exception.class)
  211. public void removeCartByHangNo(String hangNo) {
  212. if (hangNo != null && StringUtil.isNotEmpty(hangNo)) {
  213. mtCartMapper.deleteCartByHangNo(hangNo);
  214. }
  215. }
  216. /**
  217. * 清空会员购物车
  218. *
  219. * @param userId
  220. * @throws BusinessCheckException
  221. * @return
  222. */
  223. @Override
  224. @Transactional(rollbackFor = Exception.class)
  225. public void clearCart(Integer userId) {
  226. mtCartMapper.clearCart(userId);
  227. }
  228. /**
  229. * 根据条件查找
  230. *
  231. * @param params 查询参数
  232. * @return
  233. * */
  234. @Override
  235. public List<MtCart> queryCartListByParams(Map<String, Object> params) {
  236. String status = params.get("status") == null ? StatusEnum.ENABLED.getKey() : params.get("status").toString();
  237. String userId = params.get("userId") == null ? "" : params.get("userId").toString();
  238. String ids = params.get("ids") == null ? "" : params.get("ids").toString();
  239. String hangNo = params.get("hangNo") == null ? "" : params.get("hangNo").toString();
  240. String goodsId = params.get("goodsId") == null ? "" : params.get("goodsId").toString();
  241. String skuId = params.get("skuId") == null ? "" : params.get("skuId").toString();
  242. String storeId = params.get("storeId") == null ? "" : params.get("storeId").toString();
  243. String merchantId = params.get("merchantId") == null ? "" : params.get("merchantId").toString();
  244. LambdaQueryWrapper<MtCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  245. lambdaQueryWrapper.eq(MtCart::getStatus, status);
  246. if (StringUtil.isNotEmpty(userId)) {
  247. lambdaQueryWrapper.eq(MtCart::getUserId, userId);
  248. }
  249. if (StringUtil.isNotEmpty(ids)) {
  250. List<String> idList = Arrays.asList(ids.split(","));
  251. lambdaQueryWrapper.in(MtCart::getId, idList);
  252. if (StringUtil.isNotEmpty(hangNo)) {
  253. lambdaQueryWrapper.eq(MtCart::getHangNo, hangNo);
  254. }
  255. } else {
  256. lambdaQueryWrapper.eq(MtCart::getHangNo, hangNo);
  257. }
  258. if (StringUtil.isNotEmpty(goodsId)) {
  259. lambdaQueryWrapper.eq(MtCart::getGoodsId, goodsId);
  260. }
  261. if (StringUtil.isNotEmpty(merchantId) && Integer.parseInt(merchantId) > 0) {
  262. lambdaQueryWrapper.eq(MtCart::getMerchantId, merchantId);
  263. }
  264. if (StringUtil.isNotEmpty(storeId) && Integer.parseInt(storeId) > 0) {
  265. lambdaQueryWrapper.eq(MtCart::getStoreId, storeId);
  266. }
  267. if (StringUtil.isNotEmpty(skuId)) {
  268. lambdaQueryWrapper.eq(MtCart::getSkuId, skuId);
  269. }
  270. List<MtCart> result = mtCartMapper.selectList(lambdaQueryWrapper);
  271. return result;
  272. }
  273. /**
  274. * 更新购物车
  275. *
  276. * @param cartId ID
  277. * @param hangNo 挂单序号
  278. * @param isVisitor 是否游客
  279. * @return
  280. */
  281. @Override
  282. @OperationServiceLog(description = "执行挂单")
  283. @Transactional(rollbackFor = Exception.class)
  284. public MtCart setHangNo(Integer cartId, String hangNo, String isVisitor) {
  285. MtCart mtCart = mtCartMapper.selectById(cartId);
  286. if (mtCart != null) {
  287. mtCart.setHangNo(hangNo);
  288. mtCart.setUpdateTime(new Date());
  289. mtCart.setIsVisitor(isVisitor);
  290. this.updateById(mtCart);
  291. }
  292. return mtCart;
  293. }
  294. }