1
0

ApiCouponService.java 9.7 KB

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