ApiCartService.java 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. package com.kmall.api.service;
  2. import com.google.common.collect.Maps;
  3. import com.kmall.api.annotation.LoginUser;
  4. import com.kmall.api.dao.*;
  5. import com.kmall.api.dto.CampMinusDto;
  6. import com.kmall.api.dto.CheckOutDto;
  7. import com.kmall.api.dto.UserCouponDto;
  8. import com.kmall.api.entity.*;
  9. import com.kmall.api.entity.mk.MkStoreCampMinusVo;
  10. import com.kmall.api.service.mk.ApiMkStoreCampMinusService;
  11. import com.kmall.common.constant.Dict;
  12. import com.kmall.common.utils.Constant;
  13. import com.kmall.common.utils.MapUtils;
  14. import com.kmall.common.utils.RRException;
  15. import com.kmall.common.utils.StringUtils;
  16. import org.apache.commons.logging.Log;
  17. import org.apache.commons.logging.LogFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.math.BigDecimal;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. @Service
  26. public class ApiCartService {
  27. private Log logger = LogFactory.getLog(ApiCartService.class);
  28. @Autowired
  29. private ApiCartMapper cartDao;
  30. @Autowired
  31. private ApiGoodsCrashMapper apiGoodsCrashMapper;
  32. @Autowired
  33. private ApiProductMapper apiProductMapper;
  34. @Autowired
  35. private ApiGoodsMapper apiGoodsMapper;
  36. @Autowired
  37. private ApiGoodsSpecificationService apiGoodsSpecificationService;
  38. @Autowired
  39. private ApiStoreMapper apiStoreMapper;
  40. @Autowired
  41. private ApiThirdMerchantBizMapper apiThirdMerchantBizMapper;
  42. @Autowired
  43. private ApiFreightMapper apiFreightMapper;
  44. @Autowired
  45. private ApiUserCouponService apiUserCouponService;
  46. @Autowired
  47. private ApiMkStoreCampMinusService apiMkStoreCampMinusService;
  48. public CartVo queryObject(Integer id) {
  49. return cartDao.queryObject(id);
  50. }
  51. public CartVo queryObjectByGoodsIdAndUserId(Long goods_id,Long user_id,Long storeId) {
  52. return cartDao.queryObjectByGoodsIdAndUserId(goods_id,user_id,storeId);
  53. }
  54. public List<CartVo> queryList(Map<String, Object> map) {
  55. return cartDao.queryList(map);
  56. }
  57. public List<CartVo> queryValidCartList(Map<String, Object> map) {
  58. return cartDao.queryValidCartList(map);
  59. }
  60. public int queryTotal(Map<String, Object> map) {
  61. return cartDao.queryTotal(map);
  62. }
  63. public void save(CartVo cart) {
  64. // 更新购物车搭配减价
  65. cartDao.save(cart);
  66. // 判断购物车中是否存在此规格商品
  67. Map cartParam = Maps.newHashMap();
  68. cartParam.put("user_id", cart.getUser_id());
  69. List<CartVo> cartInfoList = cartDao.queryList(cartParam);
  70. // 是否有搭配减价
  71. Map crashParam = Maps.newHashMap();
  72. List<Long> goods_ids = new ArrayList();
  73. List<CartVo> cartUpdateList = new ArrayList();
  74. if(cartInfoList != null && cartInfoList.size() > 0) {
  75. for (CartVo cartItem : cartInfoList) {
  76. if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
  77. goods_ids.add(cartItem.getGoods_id());
  78. }
  79. if (!cartItem.getRetail_price().equals(cartItem.getRetail_product_price())) {
  80. cartItem.setRetail_price(cartItem.getRetail_product_price());
  81. cartUpdateList.add(cartItem);
  82. }
  83. }
  84. }else{
  85. goods_ids.add(cart.getGoods_id());
  86. }
  87. crashParam.put("store_id", cart.getStore_id());
  88. crashParam.put("goods_ids", goods_ids);
  89. List<GoodsCrashVo> goodsCrashList = apiGoodsCrashMapper.queryList(crashParam);
  90. if (null != goodsCrashList) {
  91. for (GoodsCrashVo crashVo : goodsCrashList) {
  92. for (CartVo cartItem : cartInfoList) {
  93. // 存在原始的
  94. if (null != cartItem.getChecked() && 1 == cartItem.getChecked()
  95. && cartItem.getGoods_id().equals(crashVo.getGoods_id())) {
  96. for (CartVo cartCrash : cartInfoList) { // 搭配上了商品
  97. if (!cartCrash.getId().equals(cartItem.getId())
  98. && cartCrash.getGoods_id().equals(crashVo.getGoods_crash_id())
  99. && cartCrash.getProduct_id().equals(crashVo.getProduct_crash_id())) {
  100. cartItem.setRetail_price(crashVo.getRetail_crash_price());
  101. cartUpdateList.add(cartItem);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. if (null != cartUpdateList && cartUpdateList.size() > 0) {
  109. for (CartVo cartItem : cartUpdateList) {
  110. cartDao.update(cartItem);
  111. }
  112. }
  113. }
  114. public void update(CartVo cart) {
  115. cartDao.update(cart);
  116. }
  117. public void delete(Long id) {
  118. cartDao.delete(id);
  119. }
  120. public void deleteBatch(Integer[] ids) {
  121. cartDao.deleteBatch(ids);
  122. }
  123. public void updateCheck(String[] goodsIds, Integer isChecked, Long userId, Long storeId) {
  124. cartDao.updateCheck(goodsIds, isChecked, userId, storeId);
  125. }
  126. public void deleteByProductIds(String[] productIds, Long storeId) {
  127. cartDao.deleteByProductIds(productIds, storeId);
  128. }
  129. /**
  130. * 只根据商品Id添加到购物车
  131. *
  132. * @param user_id
  133. * @param goods_id
  134. */
  135. public Map addByGoodsId(Long user_id, Long goods_id, Long store_id) {
  136. Map<String, Object> resultObj = Maps.newHashMap();
  137. resultObj.put("errno", 0);
  138. resultObj.put("errmsg", "添加成功");
  139. // 判断购物车中是否存在此规格商品
  140. Map cartParam = Maps.newHashMap();
  141. cartParam.put("user_id", user_id);
  142. cartParam.put("goods_id", goods_id);
  143. List<CartVo> cartInfoList = cartDao.queryList(cartParam);
  144. if (null != cartInfoList && cartInfoList.size() > 0) {
  145. CartVo cartVo = cartInfoList.get(0);
  146. cartVo.setNumber(cartVo.getNumber() + 1);
  147. update(cartVo);
  148. return resultObj;
  149. }
  150. ProductVo productVo = apiProductMapper.queryOneByGoodsId(goods_id, store_id);
  151. if (null == productVo || productVo.getStock_num() < 1) {
  152. // resultObj.put("errno", 1);
  153. // resultObj.put("errmsg", "库存不足,仅剩余" + productVo.getStock_num());
  154. // return resultObj;
  155. logger.error("库存不足,仅剩余" + productVo.getStock_num());
  156. throw new RRException("库存不足,仅剩余" + productVo.getStock_num());
  157. }
  158. GoodsVo goodsVo = apiGoodsMapper.queryObjectByStoreId(goods_id, store_id);
  159. CartVo cartInfo = new CartVo();
  160. cartInfo.setGoods_id(productVo.getGoods_id());
  161. cartInfo.setProduct_id(productVo.getId());
  162. cartInfo.setGoods_sn(productVo.getGoods_sn());
  163. cartInfo.setGoods_name(goodsVo.getName());
  164. cartInfo.setList_pic_url(goodsVo.getList_pic_url());
  165. cartInfo.setNumber(1);
  166. cartInfo.setStore_id(store_id);
  167. cartInfo.setUser_id(user_id);
  168. cartInfo.setRetail_price(productVo.getRetail_price());
  169. cartInfo.setMarket_price(productVo.getMarket_price());
  170. //添加规格名和值
  171. cartInfo.setGoods_specification_name_value(productVo.getGoods_specification_name_value());
  172. cartInfo.setGoods_specification_ids(productVo.getGoods_specification_ids());
  173. cartInfo.setChecked(1);
  174. cartInfo.setGoodsBizType(goodsVo.getGoodsBizType());
  175. cartInfo.setMerchSn(goodsVo.getMerchSn());
  176. save(cartInfo);
  177. return resultObj;
  178. }
  179. public List<CartVo> queryCartByGoodsBizType(String goodsBizType) {
  180. return cartDao.queryCartByGoodsBizType(goodsBizType);
  181. }
  182. public Map<String, Object> getCartMoney(UserVo loginUser, CheckOutDto checkOutDto, Long storeId){
  183. Map<String, Object> resultObj = Maps.newHashMap();
  184. Long goodsId = checkOutDto.getGoodsId();
  185. Integer number = checkOutDto.getNumber();
  186. String checkCart = checkOutDto.getCheckCart();
  187. Integer goodsCount = 0;
  188. BigDecimal goodsAmount = new BigDecimal(0.00);
  189. Integer checkedGoodsCount = 0;
  190. BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
  191. BigDecimal checkedGoodsAmount00 = new BigDecimal(0.00);
  192. BigDecimal checkedGoodsAmount02 = new BigDecimal(0.00);
  193. BigDecimal checkedGoodsAmount10 = new BigDecimal(0.00);
  194. BigDecimal checkedGoodsAmount11 = new BigDecimal(0.00);
  195. List<CartVo> cartList = new ArrayList<>();
  196. //商品id不为空,则提交订单是从商品详情页立即购买过来的
  197. if(goodsId != null){
  198. GoodsVo goodsVo = apiGoodsMapper.queryObjectByStoreId(goodsId, storeId);
  199. if(goodsVo != null){
  200. if(goodsVo.getGoods_number() == 0){
  201. logger.error("商品库存不足" );
  202. throw new RRException("商品库存不足");
  203. }
  204. CartVo vo = new CartVo();
  205. vo.setGoodsBizType(goodsVo.getGoodsBizType());
  206. vo.setGoods_id(goodsVo.getId());
  207. vo.setNumber(number);
  208. vo.setUser_id(checkOutDto.getLoginUser().getId());
  209. vo.setSku(goodsVo.getSku());
  210. vo.setGoods_sn(goodsVo.getGoods_sn());
  211. vo.setGoods_name(goodsVo.getName());
  212. vo.setRetail_price(goodsVo.getRetail_price());
  213. vo.setStore_id(Long.valueOf(goodsVo.getStoreId()));
  214. vo.setChecked(1);
  215. vo.setList_pic_url(goodsVo.getList_pic_url());
  216. vo.setProduct_id(Long.valueOf(goodsVo.getProduct_id()));
  217. vo.setMarket_price(goodsVo.getMarket_price());
  218. cartList.add(vo);
  219. }else {
  220. logger.error("商品信息不存在" );
  221. throw new RRException("商品信息不存在");
  222. }
  223. }else{
  224. //查询列表数据
  225. Map param = Maps.newHashMap();
  226. param.put("user_id", loginUser.getId());
  227. param.put("store_id", storeId);
  228. param.put("checkCart",checkCart);
  229. param.put("checked", "1");
  230. param.putAll(setIsStockShare(storeId));
  231. cartList = cartDao.queryList(param);
  232. }
  233. //获取购物车统计信息
  234. for (CartVo cartItem : cartList) {
  235. goodsCount += cartItem.getNumber();
  236. goodsAmount = goodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  237. if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
  238. if(cartItem.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
  239. checkedGoodsAmount00 = checkedGoodsAmount00.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  240. }
  241. if(cartItem.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
  242. checkedGoodsAmount02 = checkedGoodsAmount02.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  243. }
  244. if(cartItem.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
  245. checkedGoodsAmount10 = checkedGoodsAmount10.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  246. }
  247. if (cartItem.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
  248. checkedGoodsAmount11 = checkedGoodsAmount11.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  249. }
  250. checkedGoodsCount += cartItem.getNumber();
  251. checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
  252. }
  253. }
  254. resultObj.put("cartList", cartList);
  255. Map<String, Object> cartTotal = Maps.newHashMap();
  256. cartTotal.put("goodsCount", goodsCount);
  257. cartTotal.put("goodsAmount", goodsAmount);
  258. cartTotal.put("checkedGoodsCount", checkedGoodsCount);
  259. cartTotal.put("checkedGoodsAmount", checkedGoodsAmount);
  260. cartTotal.put("checkedGoodsAmount00", checkedGoodsAmount00);
  261. cartTotal.put("checkedGoodsAmount02", checkedGoodsAmount02);
  262. cartTotal.put("checkedGoodsAmount10", checkedGoodsAmount10);
  263. cartTotal.put("checkedGoodsAmount11", checkedGoodsAmount11);
  264. resultObj.put("cartTotal", cartTotal);
  265. return resultObj;
  266. }
  267. private Map setIsStockShare(Long storeId){
  268. Map param = Maps.newHashMap();
  269. StoreVo storeVo = apiStoreMapper.queryObject(storeId);
  270. if(storeVo != null) {
  271. ThirdMerchantBizVo thirdMerchantBiz = apiThirdMerchantBizMapper.getThirdMerchangByCode(storeVo.getThirdPartyMerchCode());
  272. if (null == thirdMerchantBiz) {
  273. logger.error("第三方商户为空" );
  274. throw new RRException("第三方商户为空");
  275. }
  276. param.put("isStockShare", thirdMerchantBiz.getIsStockShare());
  277. }
  278. return param;
  279. }
  280. public Integer queryCartNumByStoreId(Map<String, Object> map){
  281. return cartDao.queryCartNumByStoreId(map);
  282. }
  283. /**
  284. * 优惠券活动计算实付抵扣
  285. * @param checkedCoupon
  286. * @param actualPrice
  287. * @return
  288. */
  289. public BigDecimal mathActualPrice(UserCouponVo checkedCoupon, BigDecimal actualPrice,List<CartVo> checkedGoodsList){
  290. BigDecimal actualPrice2 = Constant.ZERO;
  291. BigDecimal actualPrice3 = Constant.ZERO;
  292. if(checkedCoupon != null){
  293. if (checkedCoupon.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_00.getItem())) {
  294. actualPrice = actualPrice.subtract(checkedCoupon.getTypeMoney());
  295. }
  296. if (checkedCoupon.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_01.getItem())) {
  297. for(int i = 0; i < checkedGoodsList.size(); i++){
  298. CartVo cartVo = checkedGoodsList.get(i);
  299. List<Integer> goodsIds = checkedCoupon.getGoodsList();
  300. for(int j=0;j<goodsIds.size();j++){
  301. if(cartVo.getGoods_id().compareTo(Long.valueOf(goodsIds.get(j)))==0){
  302. actualPrice2 = actualPrice2.add(cartVo.getRetail_price().multiply(BigDecimal.valueOf(cartVo.getNumber())));
  303. }else{
  304. actualPrice3 = actualPrice3.add(cartVo.getRetail_price().multiply(BigDecimal.valueOf(cartVo.getNumber())));
  305. }
  306. }
  307. }
  308. //实付金额*(优惠券折扣*0.1) = 折扣后的实付金额
  309. actualPrice = actualPrice3.add(actualPrice2.multiply(checkedCoupon.getTypeMoney().multiply(new BigDecimal(0.1))).setScale(2, BigDecimal.ROUND_HALF_UP));//满多少打几折
  310. }
  311. }
  312. return actualPrice;
  313. }
  314. /**
  315. * 满减满折活动计算实付满减
  316. * @param checkedCampMinus
  317. * @param actualPrice
  318. * @return
  319. */
  320. public BigDecimal mathActualPriceByCamp(CampMinusDto checkedCampMinus, BigDecimal actualPrice,List<CartVo> checkedGoodsList){
  321. BigDecimal actualPrice2 = Constant.ZERO;
  322. BigDecimal actualPrice3 = Constant.ZERO;
  323. if(checkedCampMinus != null){
  324. if (checkedCampMinus.getCampMinusType().equalsIgnoreCase(Dict.campMinusType.item_00.getItem()) || checkedCampMinus.getCampMinusType().equalsIgnoreCase(Dict.campMinusType.item_01.getItem())) {
  325. actualPrice = actualPrice.subtract(checkedCampMinus.getMoney());
  326. }
  327. if (checkedCampMinus.getCampMinusType().equalsIgnoreCase(Dict.campMinusType.item_10.getItem()) || checkedCampMinus.getCampMinusType().equalsIgnoreCase(Dict.campMinusType.item_11.getItem())) {
  328. for(int i = 0; i < checkedGoodsList.size(); i++){
  329. CartVo cartVo = checkedGoodsList.get(i);
  330. List<Integer> goodsIds = checkedCampMinus.getGoodsIds();
  331. for(int j=0;j<goodsIds.size();j++){
  332. if(cartVo.getGoods_id().compareTo(Long.valueOf(goodsIds.get(j)))==0){
  333. actualPrice2 = actualPrice2.add(cartVo.getRetail_price().multiply(BigDecimal.valueOf(cartVo.getNumber())));
  334. }else{
  335. actualPrice3 = actualPrice3.add(cartVo.getRetail_price().multiply(BigDecimal.valueOf(cartVo.getNumber())));
  336. }
  337. }
  338. }
  339. //实付金额*(满减满折折扣*0.1) = 折扣后的实付金额
  340. actualPrice = actualPrice3.add(actualPrice2.multiply(checkedCampMinus.getMoney().multiply(new BigDecimal(0.1))).setScale(2, BigDecimal.ROUND_HALF_UP));//满多少打几折
  341. }
  342. }
  343. return actualPrice;
  344. }
  345. /**
  346. * 确认订单页
  347. * @param checkOutDto
  348. * @return
  349. */
  350. public Map<String, Object> getCheckOut(CheckOutDto checkOutDto){
  351. Map<String, Object> resultObj = Maps.newHashMap();
  352. UserVo loginUser = checkOutDto.getLoginUser();
  353. String checkCart = checkOutDto.getCheckCart();
  354. Long storeId = checkOutDto.getStoreId();
  355. Long tickDiscId00 = checkOutDto.getTickDiscId00();
  356. Long tickDiscId02 = checkOutDto.getTickDiscId02();
  357. Long tickDiscId10 = checkOutDto.getTickDiscId10();
  358. Long tickDiscId11 = checkOutDto.getTickDiscId11();
  359. Integer campId00 = checkOutDto.getCampId00();
  360. Integer campId02 = checkOutDto.getCampId02();
  361. Integer campId10 = checkOutDto.getCampId10();
  362. Integer campId11 = checkOutDto.getCampId11();
  363. Map<String, Object> cartData = checkOutDto.getCartData();
  364. List<CartVo> checkedGoodsList = new ArrayList();
  365. List<CartVo> cartGoodsList = (List<CartVo>) cartData.get("cartList");
  366. for (CartVo cartEntity : cartGoodsList) {
  367. if (cartEntity.getChecked() == 1) {
  368. checkedGoodsList.add(cartEntity);
  369. }
  370. }
  371. //计算订单的费用 商品总价
  372. BigDecimal goodsTotalPrice = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount");
  373. //商品销售价*数量
  374. BigDecimal goodsTotalPrice00 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount00");
  375. BigDecimal goodsTotalPrice02 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount02");
  376. BigDecimal goodsTotalPrice10 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount10");
  377. BigDecimal goodsTotalPrice11 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount11");
  378. // 根据收货地址计算运费
  379. // BigDecimal freightPrice = apiCouponService.matchShipping(loginUser.getId(), goodsTotalPrice);
  380. BigDecimal freightPrice00 = new BigDecimal("00");
  381. BigDecimal freightPrice02 = new BigDecimal("00");
  382. BigDecimal freightPrice10 = new BigDecimal("00");
  383. BigDecimal freightPrice11 = new BigDecimal("00");
  384. Boolean isBizType00 = false;
  385. Boolean isBizType02 = false;
  386. Boolean isBizType10 = false;
  387. Boolean isBizType11 = false;
  388. //查询运费模板
  389. for (CartVo vo: checkedGoodsList) {
  390. if(org.apache.commons.lang.StringUtils.isEmpty(checkCart)){
  391. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
  392. FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
  393. freightPrice00 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice00) : freightPrice00;
  394. isBizType00 =true;
  395. }
  396. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
  397. FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
  398. freightPrice02 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice02) : freightPrice02;
  399. isBizType02 =true;
  400. }
  401. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
  402. FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
  403. freightPrice10 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice10) : freightPrice10;
  404. isBizType10 =true;
  405. }
  406. if (vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
  407. FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
  408. freightPrice11 = freightEntity != null ? freightEntity.getDefaultFreight().add(freightPrice11) : freightPrice11;
  409. isBizType11 =true;
  410. }
  411. }else{
  412. if(Dict.orderBizType.item_11.getItem().equalsIgnoreCase(checkCart)) {
  413. if (vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
  414. FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
  415. freightPrice11 = freightEntity != null ? freightEntity.getDefaultFreight().add(freightPrice11) : freightPrice11;
  416. isBizType11 =true;
  417. }
  418. }else{
  419. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
  420. FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
  421. freightPrice00 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice00) : freightPrice00;
  422. isBizType00 =true;
  423. }
  424. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
  425. FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
  426. freightPrice02 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice02) : freightPrice02;
  427. isBizType02 =true;
  428. }
  429. if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
  430. FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
  431. freightPrice10 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice10) : freightPrice10;
  432. isBizType10 =true;
  433. }
  434. }
  435. }
  436. }
  437. //整个订单包含运费的总价,商品总价+运费
  438. BigDecimal orderTotalPrice = goodsTotalPrice.add(freightPrice00).add(freightPrice02).add(freightPrice10).add(freightPrice11);
  439. // 计算满减 todo 暂时不计算全场金额满减
  440. // BigDecimal fullCutCouponDec = Constant.ZERO;
  441. // Long fullCutCouponId = 0L;
  442. /*CouponVo fullSubCoupon = apiCouponService.matchFullSub(loginUser.getId(), goodsTotalPrice);
  443. if (null != fullSubCoupon && null != fullSubCoupon.getId()) {
  444. // 满减
  445. fullCutCouponDec = fullSubCoupon.getType_money();
  446. fullCutCouponId = fullSubCoupon.getId();
  447. }*/
  448. // resultObj.put("fullCutCouponDec", fullCutCouponDec);
  449. // resultObj.put("fullCutCouponId", fullCutCouponId);
  450. List<UserCouponVo> couponList00 = null;
  451. List<UserCouponVo> couponList02 = null;
  452. List<UserCouponVo> couponList10 = null;
  453. List<UserCouponVo> couponList11 = null;
  454. List<CampMinusDto> campList00 = null;
  455. List<CampMinusDto> campList02 = null;
  456. List<CampMinusDto> campList10 = null;
  457. List<CampMinusDto> campList11 = null;
  458. //获取每个订单的可用的优惠券信息 抵用券
  459. if(goodsTotalPrice00.compareTo(new BigDecimal(0)) > 0){
  460. UserCouponDto userCouponDto00 = new UserCouponDto();
  461. userCouponDto00.setUserId(loginUser.getId());
  462. userCouponDto00.setStoreId(storeId);
  463. userCouponDto00.setGoodsTotalPrice(goodsTotalPrice00);
  464. userCouponDto00.setCartGoodsList(cartGoodsList);
  465. userCouponDto00.setBizType(Dict.orderBizType.item_00.getItem());
  466. if(campId00 != null && campId00 != 0){
  467. userCouponDto00.setCampId(campId00);
  468. }
  469. if(tickDiscId00 != null && tickDiscId00 != 0){
  470. userCouponDto00.setTickDiscId(Integer.parseInt(String.valueOf(tickDiscId00)));
  471. }
  472. couponList00 = apiUserCouponService.matchUserCouponList(userCouponDto00);
  473. campList00 = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto00);
  474. }
  475. if(goodsTotalPrice02.compareTo(new BigDecimal(0)) > 0) {
  476. UserCouponDto userCouponDto02 = new UserCouponDto();
  477. userCouponDto02.setUserId(loginUser.getId());
  478. userCouponDto02.setStoreId(storeId);
  479. userCouponDto02.setGoodsTotalPrice(goodsTotalPrice02);
  480. userCouponDto02.setCartGoodsList(cartGoodsList);
  481. userCouponDto02.setBizType(Dict.orderBizType.item_02.getItem());
  482. if(campId02 != null && campId02 != 0){
  483. userCouponDto02.setCampId(campId02);
  484. }
  485. if(tickDiscId02 != null && tickDiscId02 != 0){
  486. userCouponDto02.setTickDiscId(Integer.parseInt(String.valueOf(tickDiscId02)));
  487. }
  488. couponList02 = apiUserCouponService.matchUserCouponList(userCouponDto02);
  489. campList02 = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto02);
  490. }
  491. if(goodsTotalPrice10.compareTo(new BigDecimal(0)) > 0) {
  492. UserCouponDto userCouponDto10 = new UserCouponDto();
  493. userCouponDto10.setUserId(loginUser.getId());
  494. userCouponDto10.setStoreId(storeId);
  495. userCouponDto10.setGoodsTotalPrice(goodsTotalPrice10);
  496. userCouponDto10.setCartGoodsList(cartGoodsList);
  497. userCouponDto10.setBizType(Dict.orderBizType.item_10.getItem());
  498. if(campId10 != null && campId10 != 0){
  499. userCouponDto10.setCampId(campId10);
  500. }
  501. if(tickDiscId10 != null && tickDiscId10 != 0){
  502. userCouponDto10.setTickDiscId(Integer.parseInt(String.valueOf(tickDiscId10)));
  503. }
  504. couponList10 = apiUserCouponService.matchUserCouponList(userCouponDto10);
  505. campList10 = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto10);
  506. }
  507. if(goodsTotalPrice11.compareTo(new BigDecimal(0)) > 0) {
  508. UserCouponDto userCouponDto11 = new UserCouponDto();
  509. userCouponDto11.setUserId(loginUser.getId());
  510. userCouponDto11.setStoreId(storeId);
  511. userCouponDto11.setGoodsTotalPrice(goodsTotalPrice11);
  512. userCouponDto11.setCartGoodsList(cartGoodsList);
  513. userCouponDto11.setBizType(Dict.orderBizType.item_11.getItem());
  514. if(campId11 != null && campId11 != 0){
  515. userCouponDto11.setCampId(campId11);
  516. }
  517. if(tickDiscId11 != null && tickDiscId11 != 0){
  518. userCouponDto11.setTickDiscId(Integer.parseInt(String.valueOf(tickDiscId11)));
  519. }
  520. couponList11 = apiUserCouponService.matchUserCouponList(userCouponDto11);
  521. campList11 = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto11);
  522. }
  523. CampMinusDto campMinusDto00 = null;
  524. CampMinusDto campMinusDto02 = null;
  525. CampMinusDto campMinusDto10 = null;
  526. CampMinusDto campMinusDto11 = null;
  527. UserCouponVo checkedCoupon00 = null;
  528. UserCouponVo checkedCoupon02 = null;
  529. UserCouponVo checkedCoupon10 = null;
  530. UserCouponVo checkedCoupon11 = null;
  531. Integer isLoadStatus = checkOutDto.getIsLoadStatus();
  532. //商品销售价*数量
  533. BigDecimal actualPrice00 = goodsTotalPrice00;
  534. BigDecimal actualPrice02 = goodsTotalPrice02;
  535. BigDecimal actualPrice10 = goodsTotalPrice10;
  536. BigDecimal actualPrice11 = goodsTotalPrice11;
  537. //根据购物车的商品list、实付金额、满减满折id、购物车商品满足可用的满减满折券list、加载状态,获取当前页面该选中的满减满折券信息
  538. Map campMinusMap00 = checkedCampMinus(campList00, checkedGoodsList, actualPrice00, campId00, isLoadStatus);
  539. Map campMinusMap02 = checkedCampMinus(campList02, checkedGoodsList, actualPrice02, campId02, isLoadStatus);
  540. Map campMinusMap10 = checkedCampMinus(campList10, checkedGoodsList, actualPrice10, campId10, isLoadStatus);
  541. Map campMinusMap11 = checkedCampMinus(campList11, checkedGoodsList, actualPrice11, campId11, isLoadStatus);
  542. //根据购物车的商品list、实付金额、优惠券id、购物车商品满足可用的优惠券list、加载状态,获取当前页面该选中的优惠券信息
  543. Map couponMap00 = checkedCoupon(couponList00, checkedGoodsList, tickDiscId00, campMinusMap00, isLoadStatus);
  544. Map couponMap02 = checkedCoupon(couponList02, checkedGoodsList, tickDiscId02, campMinusMap02, isLoadStatus);
  545. Map couponMap10 = checkedCoupon(couponList10, checkedGoodsList, tickDiscId10, campMinusMap10, isLoadStatus);
  546. Map couponMap11 = checkedCoupon(couponList11, checkedGoodsList, tickDiscId11, campMinusMap11, isLoadStatus);
  547. campMinusDto00 = getCampMinusByMap(campMinusMap00);
  548. campMinusDto02 = getCampMinusByMap(campMinusMap02);
  549. campMinusDto10 = getCampMinusByMap(campMinusMap10);
  550. campMinusDto11 = getCampMinusByMap(campMinusMap11);
  551. checkedCoupon00 = getUserCouponByMap(couponMap00);
  552. checkedCoupon02 = getUserCouponByMap(couponMap02);
  553. checkedCoupon10 = getUserCouponByMap(couponMap10);
  554. checkedCoupon11 = getUserCouponByMap(couponMap11);
  555. actualPrice00 = getActualPriceByMap(couponMap00);
  556. actualPrice02 = getActualPriceByMap(couponMap02);
  557. actualPrice10 = getActualPriceByMap(couponMap10);
  558. actualPrice11 = getActualPriceByMap(couponMap11);
  559. //商品销售价*数量-优惠金额+运费
  560. actualPrice00 = actualPrice00.add(freightPrice00);
  561. actualPrice02 = actualPrice02.add(freightPrice02);
  562. actualPrice10 = actualPrice10.add(freightPrice10);
  563. actualPrice11 = actualPrice11.add(freightPrice11);
  564. BigDecimal actualPrice = actualPrice00.add(actualPrice02).add(actualPrice10).add(actualPrice11); //实付金额:商品销售价*数量-优惠金额+运费的总和
  565. for(CartVo cartVo: checkedGoodsList){
  566. List<Integer> goodsList = null;
  567. List<Integer> goodsDiscountList = null;
  568. if(cartVo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
  569. if(campMinusDto00 != null) {
  570. goodsList = campMinusDto00.getGoodsIds();
  571. }
  572. if(checkedCoupon00 != null) {
  573. goodsDiscountList = checkedCoupon00.getGoodsList();
  574. }
  575. }
  576. if(cartVo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
  577. if(campMinusDto02 != null) {
  578. goodsList = campMinusDto02.getGoodsIds();
  579. }
  580. if(checkedCoupon02 != null) {
  581. goodsDiscountList = checkedCoupon02.getGoodsList();
  582. }
  583. }
  584. if(cartVo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
  585. if(campMinusDto10 != null) {
  586. goodsList = campMinusDto10.getGoodsIds();
  587. }
  588. if(checkedCoupon10 != null) {
  589. goodsDiscountList = checkedCoupon10.getGoodsList();
  590. }
  591. }
  592. if(cartVo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())){
  593. if(campMinusDto11 != null) {
  594. goodsList = campMinusDto11.getGoodsIds();
  595. }
  596. if(checkedCoupon11 != null) {
  597. goodsDiscountList = checkedCoupon11.getGoodsList();
  598. }
  599. }
  600. if(goodsList != null) {
  601. for (Integer goodsId : goodsList) {
  602. if (cartVo.getGoods_id().compareTo(Long.valueOf(goodsId)) == 0) {
  603. cartVo.setIsCamp("true");
  604. }
  605. }
  606. }
  607. if(goodsDiscountList != null) {
  608. for (Integer goodsId : goodsDiscountList) {
  609. if (cartVo.getGoods_id().compareTo(Long.valueOf(goodsId)) == 0) {
  610. cartVo.setIsDiscount("true");
  611. }
  612. }
  613. }
  614. }
  615. resultObj.put("freightPrice00", freightPrice00);
  616. resultObj.put("freightPrice02", freightPrice02);
  617. resultObj.put("freightPrice10", freightPrice10);
  618. resultObj.put("freightPrice11", freightPrice11);
  619. resultObj.put("checkedCoupon00", checkedCoupon00);
  620. resultObj.put("checkedCoupon02", checkedCoupon02);
  621. resultObj.put("checkedCoupon10", checkedCoupon10);
  622. resultObj.put("checkedCoupon11", checkedCoupon11);
  623. resultObj.put("campMinusDto00", campMinusDto00);
  624. resultObj.put("campMinusDto02", campMinusDto02);
  625. resultObj.put("campMinusDto10", campMinusDto10);
  626. resultObj.put("campMinusDto11", campMinusDto11);
  627. resultObj.put("couponList00", couponList00);
  628. resultObj.put("couponList02", couponList02);
  629. resultObj.put("couponList10", couponList10);
  630. resultObj.put("couponList11", couponList11);
  631. resultObj.put("campList00", campList00);
  632. resultObj.put("campList02", campList02);
  633. resultObj.put("campList10", campList10);
  634. resultObj.put("campList11", campList11);
  635. resultObj.put("campId00", campMinusDto00 != null ? campMinusDto00.getCampMinusId():0);
  636. resultObj.put("campId02", campMinusDto02 != null ? campMinusDto02.getCampMinusId():0);
  637. resultObj.put("campId10", campMinusDto10 != null ? campMinusDto10.getCampMinusId():0);
  638. resultObj.put("campId11", campMinusDto11 != null ? campMinusDto11.getCampMinusId():0);
  639. resultObj.put("tickDiscId00", checkedCoupon00 != null ? checkedCoupon00.getId():0);
  640. resultObj.put("tickDiscId02", checkedCoupon02 != null ? checkedCoupon02.getId():0);
  641. resultObj.put("tickDiscId10", checkedCoupon10 != null ? checkedCoupon10.getId():0);
  642. resultObj.put("tickDiscId11", checkedCoupon11 != null ? checkedCoupon11.getId():0);
  643. BigDecimal couponPrice = new BigDecimal(0.00); //使用优惠券减免的金额
  644. resultObj.put("couponPrice", couponPrice);
  645. resultObj.put("checkedGoodsList", checkedGoodsList);
  646. resultObj.put("goodsTotalPrice", goodsTotalPrice);//整个订单商品总价,不包含运费和优惠金额
  647. resultObj.put("orderTotalPrice", orderTotalPrice);//整个订单包含运费的总价
  648. resultObj.put("actualPrice", actualPrice);
  649. resultObj.put("actualPrice00", actualPrice00);
  650. resultObj.put("actualPrice02", actualPrice02);
  651. resultObj.put("actualPrice10", actualPrice10);
  652. resultObj.put("actualPrice11", actualPrice11);
  653. resultObj.put("goodsTotalPrice00", goodsTotalPrice00);
  654. resultObj.put("goodsTotalPrice02", goodsTotalPrice02);
  655. resultObj.put("goodsTotalPrice10", goodsTotalPrice10);
  656. resultObj.put("goodsTotalPrice11", goodsTotalPrice11);
  657. resultObj.put("isBizType00", isBizType00);
  658. resultObj.put("isBizType02", isBizType02);
  659. resultObj.put("isBizType10", isBizType10);
  660. resultObj.put("isBizType11", isBizType11);
  661. return resultObj;
  662. }
  663. /**
  664. * 选择满减满折
  665. * @param campList
  666. * @param checkedGoodsList
  667. * @param actualPrice
  668. * @param campId
  669. * @param isLoadStatus 0则为进入页面加载优惠信息,默认选择最高层级促销活动,1为选择促销活动后加载优惠信息,2为选择优惠券时触发,优惠券不能与促销活动同时使用
  670. */
  671. private Map checkedCampMinus(List<CampMinusDto> campList, List<CartVo> checkedGoodsList, BigDecimal actualPrice, Integer campId, Integer isLoadStatus){
  672. Map campMap = new HashMap();
  673. CampMinusDto campMinusDto = null;
  674. if(campList != null && campList.size() > 0 && isLoadStatus != null){
  675. if(isLoadStatus == 0){ //0则为进入页面加载优惠信息,默认选择最高层级促销活动
  676. campMinusDto = campList.get(0);
  677. if(campMinusDto.getCampMinusId().compareTo(0) > 0){
  678. //商品销售价*数量-优惠金额
  679. actualPrice = mathActualPriceByCamp(campMinusDto, actualPrice, checkedGoodsList);
  680. campMap.put("actualPrice", actualPrice);
  681. campMap.put("isCamp", true);//满减活动是否满足该次订单
  682. }else{
  683. campMap.put("actualPrice", actualPrice);
  684. campMap.put("isCamp", false);
  685. }
  686. campMap.put("campMinusDto", campMinusDto);
  687. return campMap;
  688. }else if(isLoadStatus == 1) { //1为选择促销活动后加载优惠信息
  689. if (null != campId) {
  690. if (campId == 0) {
  691. //不参与满减活动
  692. setNotCampMinus(campList, actualPrice, campMap);
  693. } else {
  694. for (CampMinusDto dto : campList) {
  695. if (campId.compareTo(dto.getCampMinusId()) == 0) {
  696. campMinusDto = dto;
  697. //商品销售价*数量-优惠金额
  698. actualPrice = mathActualPriceByCamp(campMinusDto, actualPrice, checkedGoodsList);
  699. campMap.put("actualPrice", actualPrice);
  700. campMap.put("isCamp", true);
  701. campMap.put("campMinusDto", campMinusDto);
  702. }
  703. }
  704. }
  705. }else{
  706. campMap.put("actualPrice", actualPrice);
  707. campMap.put("isCamp", false);
  708. }
  709. }else{//2为选择优惠券时触发,优惠券不能与促销活动同时使用
  710. setNotCampMinus(campList, actualPrice, campMap);
  711. }
  712. return campMap;
  713. }else{
  714. campMap.put("actualPrice", actualPrice);
  715. campMap.put("isCamp", false);
  716. return campMap;
  717. }
  718. }
  719. /**
  720. * 选择优惠券
  721. * @param couponList
  722. * @param checkedGoodsList
  723. * @param tickDiscId
  724. * @param campMinusMap
  725. * @param isLoadStatus 0则为进入页面加载优惠信息,默认选择最高层级促销活动,1为选择促销活动后加载优惠信息,2为选择优惠券时触发,优惠券不能与促销活动同时使用
  726. */
  727. private Map checkedCoupon(List<UserCouponVo> couponList, List<CartVo> checkedGoodsList, Long tickDiscId, Map campMinusMap, Integer isLoadStatus){
  728. BigDecimal actualPrice = MapUtils.getBigDecimal("actualPrice", campMinusMap);
  729. Boolean isCamp = (Boolean)campMinusMap.get("isCamp");
  730. Map couponMap = new HashMap();
  731. UserCouponVo checkedCoupon = null;
  732. //满减活动不与优惠券同时使用,当isCamp为false时可使用优惠券
  733. if(isCamp == false) {
  734. if (null != couponList && couponList.size() > 0) {
  735. if (isLoadStatus == 0) {
  736. checkedCoupon = couponList.get(0);
  737. actualPrice = mathActualPrice(checkedCoupon, actualPrice, checkedGoodsList);
  738. couponMap.put("actualPrice", actualPrice);
  739. couponMap.put("checkedCoupon", checkedCoupon);
  740. }else if(isLoadStatus == 1) {
  741. //不参与优惠活动
  742. setNotCoupon(couponList,actualPrice,couponMap);
  743. }else{
  744. if (null != tickDiscId && tickDiscId == 0) {//不参与优惠活动
  745. setNotCoupon(couponList,actualPrice,couponMap);
  746. } else {
  747. for (UserCouponVo userCouponVo : couponList) {
  748. if (null != tickDiscId && tickDiscId.equals(userCouponVo.getId())) {
  749. checkedCoupon = userCouponVo;
  750. //商品销售价*数量-优惠金额
  751. actualPrice = mathActualPrice(checkedCoupon, actualPrice, checkedGoodsList);
  752. couponMap.put("actualPrice", actualPrice);
  753. couponMap.put("checkedCoupon", checkedCoupon);
  754. }
  755. }
  756. }
  757. }
  758. }else {
  759. couponMap.put("actualPrice", actualPrice);
  760. }
  761. return couponMap;
  762. }else{
  763. setNotCoupon(couponList,actualPrice,couponMap);
  764. return couponMap;
  765. }
  766. }
  767. /**
  768. * 不参与满减活动
  769. * @return
  770. */
  771. private void setNotCampMinus(List<CampMinusDto> campList,BigDecimal actualPrice, Map campMap){
  772. for (CampMinusDto dto : campList) {
  773. if (dto.getCampMinusId().compareTo(0)==0) {
  774. CampMinusDto campMinusDto = dto;
  775. campMap.put("actualPrice", actualPrice);
  776. campMap.put("isCamp", false);
  777. campMap.put("campMinusDto", campMinusDto);
  778. return;
  779. }
  780. }
  781. }
  782. /**
  783. * 不参与优惠券活动
  784. * @return
  785. */
  786. private void setNotCoupon(List<UserCouponVo> couponList,BigDecimal actualPrice, Map couponMap){
  787. if(couponList != null && couponList.size() > 0){
  788. for (UserCouponVo userCouponVo : couponList) {
  789. if (userCouponVo.getId().compareTo(0L)==0) {
  790. UserCouponVo checkedCoupon = userCouponVo;
  791. couponMap.put("actualPrice", actualPrice);
  792. couponMap.put("checkedCoupon", checkedCoupon);
  793. return;
  794. }
  795. }
  796. }else{
  797. couponMap.put("actualPrice", actualPrice);
  798. return;
  799. }
  800. }
  801. private CampMinusDto getCampMinusByMap(Map campMinusMap){
  802. if(campMinusMap.get("campMinusDto") != null){
  803. return (CampMinusDto)campMinusMap.get("campMinusDto");
  804. }
  805. return null;
  806. }
  807. private UserCouponVo getUserCouponByMap(Map couponMap){
  808. if(couponMap.get("checkedCoupon") != null){
  809. return (UserCouponVo) couponMap.get("checkedCoupon");
  810. }
  811. return null;
  812. }
  813. private BigDecimal getActualPriceByMap(Map couponMap){
  814. if(couponMap.get("actualPrice") != null){
  815. return MapUtils.getBigDecimal("actualPrice", couponMap);
  816. }
  817. return null;
  818. }
  819. }