CouponSendService.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.dk.oauth.service.integral;
  2. import com.dk.common.infrastructure.annotaiton.Pagination;
  3. import com.dk.common.mapper.BaseMapper;
  4. import com.dk.common.model.pojo.PageList;
  5. import com.dk.common.response.ResponseResultUtil;
  6. import com.dk.common.response.ResponseResultVO;
  7. import com.dk.common.service.BaseService;
  8. import com.dk.oauth.infrastructure.convert.integral.CouponSendConvert;
  9. import com.dk.oauth.mapper.integral.CouponMapper;
  10. import com.dk.oauth.mapper.integral.CouponReceiveMapper;
  11. import com.dk.oauth.mapper.integral.CouponSendMapper;
  12. import com.dk.oauth.model.VO.integral.CouponSendVO;
  13. import com.dk.oauth.model.pojo.integral.CouponSend;
  14. import com.dk.oauth.model.query.integral.CouponQuery;
  15. import com.dk.oauth.model.query.integral.CouponReceiveQuery;
  16. import com.dk.oauth.model.query.integral.CouponSendQuery;
  17. import com.dk.oauth.model.response.integral.CouponReceiveResponse;
  18. import com.dk.oauth.model.response.integral.CouponResponse;
  19. import com.dk.oauth.model.response.integral.CouponSendResponse;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import java.text.SimpleDateFormat;
  24. import java.time.LocalDateTime;
  25. import java.time.ZoneOffset;
  26. import java.time.format.DateTimeFormatter;
  27. import java.util.Date;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.UUID;
  31. @Service
  32. @Transactional
  33. public class CouponSendService extends BaseService<CouponSend> {
  34. @Override
  35. public BaseMapper<CouponSend> getRepository() {
  36. return couponSendMapper;
  37. }
  38. @Autowired
  39. private CouponSendMapper couponSendMapper;
  40. @Autowired
  41. //优惠券发出实体转换
  42. private CouponSendConvert couponSendConvert;
  43. @Autowired
  44. //优惠券获取
  45. private CouponReceiveMapper couponReceiveMapper;
  46. @Autowired
  47. //优惠券
  48. private CouponMapper couponMapper;
  49. /**
  50. * @desc : 条件查询
  51. * @date : 2024/7/25 10:22
  52. * @author : 寇珊珊
  53. */
  54. @Pagination
  55. public ResponseResultVO<PageList<CouponSendResponse>> selectByCond(CouponSendQuery couponSendQuery) {
  56. return super.mergeListWithCount(couponSendQuery, couponSendMapper.selectByCond(couponSendQuery),
  57. couponSendMapper.countByCond(couponSendQuery));
  58. }
  59. /**
  60. * @desc : 条件查询-历史查询
  61. * @date : 2024/7/25 10:22
  62. * @author : 寇珊珊
  63. */
  64. @Pagination
  65. public ResponseResultVO<PageList<CouponSendResponse>> selectByCondHistory(CouponSendQuery couponSendQuery) {
  66. return super.mergeListWithCount(couponSendQuery, couponSendMapper.selectByCondHistory(couponSendQuery),
  67. couponSendMapper.countByCond(couponSendQuery));
  68. }
  69. /**
  70. * @desc : 新建
  71. * @date : 2024/7/25 13:07
  72. * @author : 寇珊珊
  73. */
  74. @Transactional(rollbackFor = {Exception.class})
  75. public ResponseResultVO<?> insert(CouponSendVO couponSendVO) {
  76. // 20240801 判断是否当前用户 有未过期的 发出id
  77. Map<String, Object> map = couponSendMapper.selectCouponSendItem(couponSendVO);
  78. if (map != null) {
  79. return ResponseResultUtil.success(map);
  80. } else {
  81. //region 新建
  82. CouponSend couponSend = this.insertCouponSend(couponSendVO);
  83. //endregion
  84. // 返回请求数据时系统的当前时间
  85. LocalDateTime now = LocalDateTime.now();
  86. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  87. couponSend.setNowTime(now.format(formatter));
  88. return ResponseResultUtil.success(couponSend);
  89. }
  90. // 20240801
  91. // //region 根据id查询数据
  92. // CouponSendResponse couponSendResponse = couponSendMapper.selectById(couponSendVO.getCouponId());
  93. // if (couponSendResponse != null) {
  94. // //region 查询已助力人数
  95. // List<CouponReceiveResponse> couponReceiveResponses = couponReceiveMapper.selectByCond(new CouponReceiveQuery().setCouponId(couponSendResponse.getCouponId()));
  96. // int userNum = couponReceiveResponses.size();
  97. //// if(couponReceiveResponses.size()>couponSendResponse.getUserNum()){
  98. //// //所需助力人数已满
  99. //// throw new BaseBusinessException(ErrorCodeEnum.THE_REQUIRED_NUMBER_OF_HELPERS_IS_FULL.getCode(),ErrorCodeEnum.THE_REQUIRED_NUMBER_OF_HELPERS_IS_FULL.getMessage());
  100. //// }
  101. // //endregion
  102. //
  103. // //region 校验单据时间
  104. // LocalDateTime endTime = couponSendMapper.selectEndTime(new CouponSendQuery()
  105. // .setCouponId(couponSendVO.getCouponId()))
  106. // .getEndDate();
  107. // //原时间戳
  108. // long primaryTime = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  109. // //本次时间戳
  110. // long thisTime = couponSendVO.getEndDate().toInstant(ZoneOffset.of("+8")).toEpochMilli();
  111. // //本次发出时间大于截止日期
  112. // if ((thisTime / 1000) > (primaryTime / 1000)) {
  113. //
  114. // //region 新建
  115. // this.insertCouponSend(couponSendVO);
  116. // //endregion
  117. //
  118. // }
  119. // //本次发出时间小于等于截止日期 并且助力人数未够 直接返回
  120. // if ((thisTime / 1000) <= (primaryTime / 1000) && userNum < couponSendResponse.getUserNum()) {
  121. // return ResponseResultUtil.success();
  122. // }
  123. // //本次发出时间小于等于截止日期 并且助力已满足 新建
  124. // if ((thisTime / 1000) <= (primaryTime / 1000) && userNum == couponSendResponse.getUserNum()) {
  125. //
  126. // //region 新建
  127. // this.insertCouponSend(couponSendVO);
  128. // //endregion
  129. // // 返回请求数据时系统的当前时间
  130. // LocalDateTime now = LocalDateTime.now();
  131. // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  132. // couponSendVO.setNowTime(now.format(formatter));
  133. // return ResponseResultUtil.success(couponSendVO);
  134. // }
  135. // //endregion
  136. // }
  137. // //endregion
  138. //
  139. // //region 不存在 新建
  140. // else {
  141. // //region 新建
  142. // this.insertCouponSend(couponSendVO);
  143. // //endregion
  144. // }
  145. // //endregion
  146. // // 返回请求数据时系统的当前时间
  147. // LocalDateTime now = LocalDateTime.now();
  148. // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  149. // couponSendVO.setNowTime(now.format(formatter));
  150. // return ResponseResultUtil.success(couponSendVO);
  151. }
  152. /**
  153. * @desc : 新建优惠券获取
  154. * @date : 2024/7/26 10:58
  155. * @author : 寇珊珊
  156. */
  157. @Transactional(rollbackFor = {Exception.class})
  158. public CouponSend insertCouponSend(CouponSendVO couponSendVO) {
  159. //查询优惠券
  160. List<CouponResponse> couponResponses = couponMapper.selectByCond(new CouponQuery());
  161. //发出时间
  162. couponSendVO.setSendDate(LocalDateTime.now());
  163. //截止时间
  164. Integer linkValidTimes = couponResponses.get(0).getLinkValidTimes();
  165. couponSendVO.setEndDate(couponSendVO.getSendDate().plusHours(linkValidTimes));
  166. CouponSend couponSend = couponSendConvert.convertToPo(couponSendVO);
  167. UUID uuid = UUID.randomUUID();
  168. couponSend.setSendId(uuid.toString());
  169. couponSendMapper.insert(couponSend);
  170. return couponSend;
  171. }
  172. /**
  173. * @desc : 获取助力明细
  174. * @date : 2024/7/25 13:06
  175. * @author : 刘尧
  176. */
  177. public ResponseResultVO<?> getCouponSendItem(CouponSendVO couponSendVO) {
  178. return ResponseResultUtil.success(couponSendMapper.selectCouponSendItem(couponSendVO));
  179. }
  180. }