PointServiceImpl.java 10.0 KB

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