ApiCouponService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.kmall.api.service;
  2. import com.google.common.collect.Maps;
  3. import com.kmall.api.dao.mk.ApiMkStoreTicketDiscountMapper;
  4. import com.kmall.api.entity.mk.MkStoreTicketDiscountVo;
  5. import com.kmall.common.constant.Dict;
  6. import com.kmall.api.dao.ApiCouponMapper;
  7. import com.kmall.api.dao.ApiUserCouponMapper;
  8. import com.kmall.api.entity.CouponVo;
  9. import com.kmall.api.entity.UserCouponVo;
  10. import com.kmall.common.utils.CharUtil;
  11. import com.kmall.common.utils.Constant;
  12. import com.qiniu.util.StringUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.math.BigDecimal;
  16. import java.util.Date;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. @Service
  21. public class ApiCouponService {
  22. @Autowired
  23. private ApiCouponMapper apiCouponMapper;
  24. @Autowired
  25. private ApiUserCouponMapper apiUserCouponMapper;
  26. @Autowired
  27. private ApiMkStoreTicketDiscountMapper mkStoreTicketDiscountMapper;
  28. public CouponVo queryObject(Long couponId) {
  29. return apiCouponMapper.queryObject(couponId);
  30. }
  31. public List<CouponVo> queryList(Map<String, Object> map) {
  32. return apiCouponMapper.queryList(map);
  33. }
  34. public int queryTotal(Map<String, Object> map) {
  35. return apiCouponMapper.queryTotal(map);
  36. }
  37. public void save(CouponVo userVo) {
  38. apiCouponMapper.save(userVo);
  39. }
  40. public void update(CouponVo user) {
  41. apiCouponMapper.update(user);
  42. }
  43. public void delete(Long userId) {
  44. apiCouponMapper.delete(userId);
  45. }
  46. public void deleteBatch(Long[] userIds) {
  47. apiCouponMapper.deleteBatch(userIds);
  48. }
  49. public List<CouponVo> queryUserCoupons(Map<String, Object> map) {
  50. return apiCouponMapper.queryUserCoupons(map);
  51. }
  52. public CouponVo queryMaxUserEnableCoupon(Map<String, Object> map) {
  53. return apiCouponMapper.queryMaxUserEnableCoupon(map);
  54. }
  55. public UserCouponVo takeCoupon(CouponVo couponVo, Long userId, String source_key, Long referrer, Integer show_state) {
  56. UserCouponVo userCouponVo = new UserCouponVo();
  57. userCouponVo.setAddTime(new Date());
  58. // userCouponVo.setCoupon_id(couponVo.getId());
  59. userCouponVo.setCouponNumber(CharUtil.getRandomString(12));
  60. userCouponVo.setUserId(userId);
  61. userCouponVo.setReferrer(referrer);
  62. userCouponVo.setSourceKey(source_key);
  63. userCouponVo.setShowState(show_state);
  64. userCouponVo.setSendType(couponVo.getSend_type());
  65. userCouponVo.setTypeMoney(couponVo.getType_money());
  66. userCouponVo.setCouponName(couponVo.getName());
  67. // userCouponVo.setMin_goods_amount(couponVo.getMin_goods_amount());
  68. userCouponVo.setEndTime(couponVo.getUse_end_date());
  69. userCouponVo.setIsUsed(Dict.isUsed.item_0.getItem());
  70. apiUserCouponMapper.save(userCouponVo);
  71. return userCouponVo;
  72. }
  73. /**
  74. * 获取优惠信息提示 邮费
  75. *
  76. * @param userId
  77. * @param goodsTotalPrice
  78. * @return
  79. */
  80. public CouponVo matchShippingSign(Long userId, BigDecimal goodsTotalPrice) {
  81. CouponVo result = new CouponVo();
  82. //
  83. Map couponParam = Maps.newHashMap();
  84. couponParam.put("enabled", true);
  85. Integer[] send_types = new Integer[]{7};
  86. couponParam.put("send_types", send_types);
  87. List<CouponVo> couponVos = apiCouponMapper.queryList(couponParam);
  88. if (null != couponVos && couponVos.size() > 0) {
  89. for (CouponVo couponVo : couponVos) {
  90. BigDecimal difDec = couponVo.getMin_goods_amount().subtract(goodsTotalPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
  91. // 是否最低优惠券
  92. boolean optimal = false;
  93. if (null != result && null != result.getId()) {
  94. // 都是凑单,取小的
  95. if (difDec.doubleValue() > 0.0 && result.getDifDec().doubleValue() > 0.0
  96. && difDec.compareTo(result.getDifDec()) < 0) {
  97. optimal = true;
  98. }
  99. // 一个凑单,一个满足,取凑单
  100. else if (difDec.doubleValue() >= 0.0 && result.getDifDec().doubleValue() <= 0.0) {
  101. optimal = true;
  102. } // 都满足,取type_money小的
  103. else if (difDec.doubleValue() < 0.0 && result.getDifDec().doubleValue() < 0.0
  104. && result.getMoney().compareTo(couponVo.getType_money()) > 0) {
  105. optimal = true;
  106. }
  107. } else {
  108. optimal = true;
  109. }
  110. if (!optimal) {
  111. continue;
  112. }
  113. if (difDec.doubleValue() > 0.0 && couponVo.getType_money().compareTo(Constant.ZERO) == 0) {
  114. couponVo.setMsg("满¥" + couponVo.getMin_goods_amount() + "元免配送费,还差" + difDec + "元");
  115. couponVo.setType(1);
  116. } else if (couponVo.getType_money().doubleValue() > 0.0) {
  117. couponVo.setMsg("满¥" + couponVo.getMin_goods_amount() + "元,配送费" + couponVo.getType_money() + "元,还差" + difDec + "元");
  118. couponVo.setType(1);
  119. } else {
  120. couponVo.setMsg("满¥" + couponVo.getMin_goods_amount() + "元免配送费");
  121. couponVo.setType(0);
  122. }
  123. couponVo.setDifDec(difDec);
  124. couponVo.setMoney(couponVo.getType_money());
  125. result = couponVo;
  126. }
  127. }
  128. return result;
  129. }
  130. /**
  131. * 获取可使用的优惠 邮费
  132. *
  133. * @param userId
  134. * @param goodsTotalPrice
  135. * @return
  136. */
  137. public BigDecimal matchShipping(Long userId, BigDecimal goodsTotalPrice) {
  138. BigDecimal result = new BigDecimal(10);
  139. //
  140. Map couponParam = Maps.newHashMap();
  141. couponParam.put("enabled", true);
  142. Integer[] send_types = new Integer[]{7};
  143. couponParam.put("send_types", send_types);
  144. List<CouponVo> couponVos = apiCouponMapper.queryList(couponParam);
  145. if (null != couponVos && couponVos.size() > 0) {
  146. for (CouponVo couponVo : couponVos) {
  147. // 是否免运费
  148. if (couponVo.getMin_goods_amount().compareTo(goodsTotalPrice) <= 0
  149. && result.compareTo(couponVo.getType_money()) > 0) {
  150. result = couponVo.getType_money();
  151. }
  152. }
  153. }
  154. return result;
  155. }
  156. /**
  157. * 获取可用的 满减券
  158. *
  159. * @param userId
  160. * @param goodsTotalPrice
  161. * @return
  162. */
  163. public CouponVo matchFullSub(Long userId, BigDecimal goodsTotalPrice) {
  164. CouponVo result = new CouponVo();
  165. //
  166. Map couponParam = Maps.newHashMap();
  167. couponParam.put("enabled", true);
  168. // 获取优惠信息提示 满减
  169. Integer[] send_types = new Integer[]{0};
  170. // param.put("unUsed", true);
  171. couponParam.put("send_types", send_types);
  172. List<CouponVo> couponVos = apiCouponMapper.queryList(couponParam);
  173. if (null != couponVos && couponVos.size() > 0) {
  174. CouponVo fullCutVo = new CouponVo();
  175. BigDecimal fullCutDec = Constant.ZERO;
  176. for (CouponVo couponVo : couponVos) {
  177. BigDecimal difDec = couponVo.getMin_goods_amount().subtract(goodsTotalPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
  178. if (difDec.doubleValue() < 0.0 && fullCutDec.compareTo(couponVo.getType_money()) < 0) {
  179. fullCutDec = couponVo.getType_money();
  180. fullCutVo = couponVo;
  181. fullCutVo.setType(0);
  182. fullCutVo.setMsg("可使用满减券" + couponVo.getName());
  183. }
  184. if (!StringUtils.isNullOrEmpty(fullCutVo.getMsg())) {
  185. result = fullCutVo;
  186. }
  187. }
  188. }
  189. return result;
  190. }
  191. /**
  192. * 获取优惠信息提示 满减券
  193. *
  194. * @param userId
  195. * @param goodsTotalPrice
  196. * @return
  197. *//*
  198. public CouponVo matchFullSubSign(Long userId, BigDecimal goodsTotalPrice, long storeId) {
  199. CouponVo result = new CouponVo();
  200. //
  201. Map couponParam = Maps.newHashMap();
  202. couponParam.put("enabled", true);
  203. // 获取优惠信息提示 满减
  204. couponParam.put("storeId", storeId);
  205. // List<CouponVo> couponVos = apiCouponMapper.queryList(couponParam);
  206. List<MkStoreTicketDiscountVo> discountVoList = mkStoreTicketDiscountMapper.queryList(couponParam);
  207. if (null != discountVoList && discountVoList.size() > 0) {
  208. CouponVo fullCutVo = new CouponVo();
  209. BigDecimal fullCutDec = Constant.ZERO;
  210. BigDecimal minAmount = new BigDecimal(100000);
  211. for (MkStoreTicketDiscountVo discountVo : discountVoList) {
  212. BigDecimal cond = Constant.ZERO;
  213. BigDecimal money = Constant.ZERO;
  214. if(discountVo.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_00.getItem())){
  215. cond = discountVo.getVoucherCond();
  216. money = discountVo.getVoucherMoney();
  217. }
  218. if(discountVo.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_01.getItem())){
  219. cond = discountVo.getDiscCond();
  220. money = discountVo.getDiscRatio();//满多少打几折
  221. }
  222. if(discountVo.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_02.getItem())){
  223. cond = discountVo.getExchCond();
  224. money = discountVo.getExchCond();
  225. }
  226. BigDecimal difDec = cond.subtract(goodsTotalPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
  227. if (difDec.doubleValue() > 0.0
  228. && minAmount.compareTo(discountVo.getVoucherCond()) > 0) {
  229. fullCutDec = money;
  230. fullCutVo.setType(1);
  231. fullCutVo.setMsg(discountVo.getName() + ",还差" + difDec + "元");
  232. } else if (difDec.doubleValue() < 0.0 && fullCutDec.compareTo(money) < 0) {
  233. fullCutVo.setType(0);
  234. fullCutVo.setMsg("可使用满减券" + discountVo.getName());
  235. }
  236. if (!StringUtils.isNullOrEmpty(fullCutVo.getMsg())) {
  237. result = fullCutVo;
  238. }
  239. }
  240. }
  241. return result;
  242. }*/
  243. }