CouponServiceImpl.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.kmall.admin.service.impl;
  2. import com.google.common.collect.ImmutableBiMap;
  3. import com.kmall.admin.dao.CouponDao;
  4. import com.kmall.admin.dao.CouponGoodsDao;
  5. import com.kmall.admin.dao.UserCouponDao;
  6. import com.kmall.admin.dao.UserDao;
  7. import com.kmall.admin.entity.CouponEntity;
  8. import com.kmall.admin.entity.CouponGoodsEntity;
  9. import com.kmall.admin.entity.UserCouponEntity;
  10. import com.kmall.admin.entity.UserEntity;
  11. import com.kmall.admin.service.CouponService;
  12. import com.kmall.common.utils.MapBeanUtil;
  13. import com.kmall.common.utils.R;
  14. import com.kmall.common.utils.RRException;
  15. import com.kmall.common.utils.ValidatorUtil;
  16. import org.apache.commons.collections.MapUtils;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.util.Date;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * Service实现类
  25. *
  26. * @author Scott
  27. * @email
  28. * @date 2017-08-19 12:53:26
  29. */
  30. @Service("couponService")
  31. public class CouponServiceImpl implements CouponService {
  32. @Autowired
  33. private CouponDao couponDao;
  34. @Autowired
  35. private UserCouponDao userCouponDao;
  36. @Autowired
  37. private CouponGoodsDao couponGoodsDao;
  38. @Autowired
  39. private UserDao userDao;
  40. @Override
  41. public CouponEntity queryObject(Integer id) {
  42. return couponDao.queryObject(id);
  43. }
  44. @Override
  45. public List<CouponEntity> queryList(Map<String, Object> map) {
  46. return couponDao.queryList(map);
  47. }
  48. @Override
  49. public int queryTotal(Map<String, Object> map) {
  50. return couponDao.queryTotal(map);
  51. }
  52. @Override
  53. public int save(CouponEntity coupon) {
  54. Map<String, Object> valideDate = MapBeanUtil.fromObject(coupon);
  55. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  56. builder.put("merchSn", "商户");
  57. builder.put("storeId", "门店");
  58. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  59. if (Integer.valueOf(r.get("code").toString()) != 0) {
  60. throw new RRException(r.get("msg").toString());
  61. }
  62. return couponDao.save(coupon);
  63. }
  64. @Override
  65. public int update(CouponEntity coupon) {
  66. Map<String, Object> valideDate = MapBeanUtil.fromObject(coupon);
  67. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  68. builder.put("merchSn", "商户");
  69. builder.put("storeId", "门店");
  70. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  71. if (Integer.valueOf(r.get("code").toString()) != 0) {
  72. throw new RRException(r.get("msg").toString());
  73. }
  74. return couponDao.update(coupon);
  75. }
  76. @Override
  77. public int delete(Integer id) {
  78. return couponDao.delete(id);
  79. }
  80. @Override
  81. public int deleteBatch(Integer[] ids) {
  82. return couponDao.deleteBatch(ids);
  83. }
  84. @Override
  85. public R publish(Map<String, Object> params) {
  86. // 发放方式 0:按订单发放 1:按用户发放 2:商品转发送券 3:按商品发放 4:新用户注册 5:线下发放 6评价好评红包(固定或随机红包)
  87. Integer sendType = MapUtils.getInteger(params, "sendType");
  88. Integer couponId = MapUtils.getInteger(params, "couponId");
  89. if (null == sendType) {
  90. return R.error("发放方式不能为空");
  91. }
  92. if (null == couponId) {
  93. return R.error("优惠券不能为空");
  94. }
  95. if (1 == sendType) {
  96. String userIds = MapUtils.getString(params, "userIds"); // 下发用户逗号分割
  97. if (StringUtils.isEmpty(userIds)) {
  98. return R.error("用户不能为空");
  99. }
  100. //是否发送短信通知
  101. boolean sendSms = "true".equals(MapUtils.getString(params, "sendSms"));
  102. for (String strUserId : userIds.split(",")) {
  103. if (StringUtils.isEmpty(strUserId)) {
  104. continue;
  105. }
  106. Integer userId = Integer.valueOf(strUserId);
  107. UserCouponEntity userCouponVo = new UserCouponEntity();
  108. userCouponVo.setUserId(userId);
  109. userCouponVo.setCouponId(couponId);
  110. userCouponVo.setCouponNumber("1");
  111. userCouponVo.setAddTime(new Date());
  112. userCouponDao.save(userCouponVo);
  113. if (sendSms) {
  114. UserEntity userEntity = userDao.queryObject(userId);
  115. // todo 发送短信
  116. }
  117. }
  118. } else if (3 == sendType) {
  119. String goodsIds = MapUtils.getString(params, "goodsIds"); // 下发商品逗号分割
  120. if (StringUtils.isEmpty(goodsIds)) {
  121. return R.error("商品Id不能为空");
  122. }
  123. for (String goodsId : goodsIds.split(",")) {
  124. if (StringUtils.isEmpty(goodsId)) {
  125. continue;
  126. }
  127. CouponGoodsEntity couponGoodsVo = new CouponGoodsEntity();
  128. couponGoodsVo.setCouponId(couponId);
  129. couponGoodsVo.setGoodsId(Integer.valueOf(goodsId));
  130. couponGoodsDao.save(couponGoodsVo);
  131. }
  132. } else {
  133. return R.error("此类优惠券不支持手动发放");
  134. }
  135. return R.ok("发放成功");
  136. }
  137. }