PointServiceImpl.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.fuint.common.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.fuint.common.dto.PointDto;
  6. import com.fuint.common.enums.StatusEnum;
  7. import com.fuint.common.enums.WxMessageEnum;
  8. import com.fuint.common.service.MemberService;
  9. import com.fuint.common.service.PointService;
  10. import com.fuint.common.service.WeixinService;
  11. import com.fuint.common.util.DateUtil;
  12. import com.fuint.framework.annoation.OperationServiceLog;
  13. import com.fuint.framework.exception.BusinessCheckException;
  14. import com.fuint.framework.pagination.PaginationRequest;
  15. import com.fuint.framework.pagination.PaginationResponse;
  16. import com.fuint.repository.mapper.MtPointMapper;
  17. import com.fuint.repository.mapper.MtUserMapper;
  18. import com.fuint.repository.model.MtPoint;
  19. import com.fuint.repository.model.MtUser;
  20. import com.fuint.utils.StringUtil;
  21. import com.github.pagehelper.PageHelper;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import com.github.pagehelper.Page;
  25. import org.springframework.data.domain.PageImpl;
  26. import org.springframework.data.domain.PageRequest;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import javax.annotation.Resource;
  30. import java.util.*;
  31. /**
  32. * 积分管理业务实现类
  33. *
  34. * Created by FSQ
  35. * CopyRight https://www.fuint.cn
  36. */
  37. @Service
  38. public class PointServiceImpl extends ServiceImpl<MtPointMapper, MtPoint> implements PointService {
  39. @Resource
  40. private MtPointMapper mtPointMapper;
  41. @Resource
  42. private MtUserMapper mtUserMapper;
  43. @Autowired
  44. private MemberService memberService;
  45. @Autowired
  46. private WeixinService weixinService;
  47. /**
  48. * 分页查询积分列表
  49. *
  50. * @param paginationRequest
  51. * @return
  52. */
  53. @Override
  54. public PaginationResponse<PointDto> queryPointListByPagination(PaginationRequest paginationRequest) throws BusinessCheckException {
  55. Page<MtPoint> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  56. LambdaQueryWrapper<MtPoint> lambdaQueryWrapper = Wrappers.lambdaQuery();
  57. lambdaQueryWrapper.ne(MtPoint::getStatus, StatusEnum.DISABLE.getKey());
  58. String description = paginationRequest.getSearchParams().get("description") == null ? "" : paginationRequest.getSearchParams().get("description").toString();
  59. if (StringUtils.isNotBlank(description)) {
  60. lambdaQueryWrapper.like(MtPoint::getDescription, description);
  61. }
  62. String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
  63. if (StringUtils.isNotBlank(status)) {
  64. lambdaQueryWrapper.eq(MtPoint::getStatus, status);
  65. }
  66. String userId = paginationRequest.getSearchParams().get("userId") == null ? "" : paginationRequest.getSearchParams().get("userId").toString();
  67. if (StringUtils.isNotBlank(userId)) {
  68. lambdaQueryWrapper.eq(MtPoint::getUserId, userId);
  69. }
  70. String merchantId = paginationRequest.getSearchParams().get("merchantId") == null ? "" : paginationRequest.getSearchParams().get("merchantId").toString();
  71. if (StringUtils.isNotBlank(merchantId)) {
  72. lambdaQueryWrapper.eq(MtPoint::getMerchantId, merchantId);
  73. }
  74. String storeId = paginationRequest.getSearchParams().get("storeId") == null ? "" : paginationRequest.getSearchParams().get("storeId").toString();
  75. if (StringUtils.isNotBlank(storeId)) {
  76. lambdaQueryWrapper.eq(MtPoint::getStoreId, storeId);
  77. }
  78. lambdaQueryWrapper.orderByDesc(MtPoint::getId);
  79. List<MtPoint> pointList = mtPointMapper.selectList(lambdaQueryWrapper);
  80. List<PointDto> dataList = new ArrayList<>();
  81. for (MtPoint point : pointList) {
  82. MtUser userInfo = memberService.queryMemberById(point.getUserId());
  83. PointDto item = new PointDto();
  84. item.setId(point.getId());
  85. item.setAmount(point.getAmount());
  86. item.setDescription(point.getDescription());
  87. item.setCreateTime(point.getCreateTime());
  88. item.setUpdateTime(point.getUpdateTime());
  89. item.setUserId(point.getUserId());
  90. item.setUserInfo(userInfo);
  91. item.setOrderSn(point.getOrderSn());
  92. item.setOperator(point.getOperator());
  93. item.setStatus(point.getStatus());
  94. dataList.add(item);
  95. }
  96. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  97. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  98. PaginationResponse<PointDto> paginationResponse = new PaginationResponse(pageImpl, PointDto.class);
  99. paginationResponse.setTotalPages(pageHelper.getPages());
  100. paginationResponse.setTotalElements(pageHelper.getTotal());
  101. paginationResponse.setContent(dataList);
  102. return paginationResponse;
  103. }
  104. /**
  105. * 添加积分记录
  106. *
  107. * @param mtPoint
  108. * @throws BusinessCheckException
  109. */
  110. @Override
  111. @Transactional(rollbackFor = Exception.class)
  112. @OperationServiceLog(description = "修改会员积分")
  113. public void addPoint(MtPoint mtPoint) throws BusinessCheckException {
  114. if (mtPoint.getUserId() < 0) {
  115. return;
  116. }
  117. mtPoint.setStatus(StatusEnum.ENABLED.getKey());
  118. mtPoint.setCreateTime(new Date());
  119. mtPoint.setUpdateTime(new Date());
  120. if (mtPoint.getOperator() != null) {
  121. mtPoint.setOperator(mtPoint.getOperator());
  122. }
  123. if (mtPoint.getOrderSn() != null) {
  124. mtPoint.setOrderSn(mtPoint.getOrderSn());
  125. }
  126. MtUser mtUser = mtUserMapper.selectById(mtPoint.getUserId());
  127. Integer newAmount = mtUser.getPoint() + mtPoint.getAmount();
  128. if (newAmount < 0) {
  129. return;
  130. }
  131. mtUser.setPoint(newAmount);
  132. if (mtUser.getStoreId() != null) {
  133. mtPoint.setStoreId(mtUser.getStoreId());
  134. }
  135. mtPoint.setMerchantId(mtUser.getMerchantId());
  136. mtUserMapper.updateById(mtUser);
  137. mtPointMapper.insert(mtPoint);
  138. // 发送小程序订阅消息
  139. Date nowTime = new Date();
  140. Date sendTime = new Date(nowTime.getTime() + 60000);
  141. Map<String, Object> params = new HashMap<>();
  142. String dateTime = DateUtil.formatDate(Calendar.getInstance().getTime(), "yyyy-MM-dd HH:mm");
  143. params.put("amount", mtPoint.getAmount());
  144. params.put("time", dateTime);
  145. params.put("remark", "您的积分发生了变动,请留意~");
  146. weixinService.sendSubscribeMessage(mtPoint.getMerchantId(), mtPoint.getUserId(), mtUser.getOpenId(), WxMessageEnum.POINT_CHANGE.getKey(), "pages/user/index", params, sendTime);
  147. return;
  148. }
  149. /**
  150. * 转赠积分
  151. *
  152. * @param userId
  153. * @param mobile
  154. * @param amount
  155. * @param remark
  156. * @return boolean
  157. */
  158. @Override
  159. @Transactional(rollbackFor = Exception.class)
  160. public boolean doGift(Integer userId, String mobile, Integer amount, String remark) throws BusinessCheckException {
  161. if (userId < 0 || StringUtil.isEmpty(mobile) || amount <= 0) {
  162. return false;
  163. }
  164. MtUser userInfo = memberService.queryMemberById(userId);
  165. MtUser fUserInfo = memberService.queryMemberByMobile(userInfo.getMerchantId(), mobile);
  166. // 自动注册会员
  167. if (fUserInfo == null) {
  168. fUserInfo = memberService.addMemberByMobile(userInfo.getMerchantId(), mobile);
  169. }
  170. if (fUserInfo == null) {
  171. throw new BusinessCheckException("转赠的好友信息不存在");
  172. }
  173. if (fUserInfo.getId().equals(userInfo.getId())) {
  174. throw new BusinessCheckException("积分不能转赠给自己");
  175. }
  176. Integer newAmount = fUserInfo.getPoint() + amount;
  177. if (newAmount < 0) {
  178. throw new BusinessCheckException("积分赠送失败");
  179. }
  180. fUserInfo.setPoint(newAmount);
  181. Integer myNewAmount = userInfo.getPoint() - amount;
  182. if (myNewAmount < 0) {
  183. throw new BusinessCheckException("您的积分不足");
  184. }
  185. userInfo.setPoint(myNewAmount);
  186. mtUserMapper.updateById(fUserInfo);
  187. mtUserMapper.updateById(userInfo);
  188. MtPoint fMtPoint = new MtPoint();
  189. fMtPoint.setStatus(StatusEnum.ENABLED.getKey());
  190. fMtPoint.setAmount(amount);
  191. fMtPoint.setCreateTime(new Date());
  192. fMtPoint.setUpdateTime(new Date());
  193. fMtPoint.setOperator(userInfo.getName());
  194. fMtPoint.setOrderSn("");
  195. fMtPoint.setDescription(remark);
  196. fMtPoint.setUserId(fUserInfo.getId());
  197. fMtPoint.setMerchantId(fUserInfo.getMerchantId());
  198. mtPointMapper.insert(fMtPoint);
  199. MtPoint mtPoint = new MtPoint();
  200. mtPoint.setUserId(userId);
  201. mtPoint.setAmount(-amount);
  202. mtPoint.setStatus(StatusEnum.ENABLED.getKey());
  203. mtPoint.setCreateTime(new Date());
  204. mtPoint.setUpdateTime(new Date());
  205. mtPoint.setOperator(userInfo.getName());
  206. mtPoint.setOrderSn("");
  207. mtPoint.setDescription("转赠好友");
  208. mtPoint.setMerchantId(userInfo.getMerchantId());
  209. mtPointMapper.insert(mtPoint);
  210. return true;
  211. }
  212. }