UserCouponServiceImpl.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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.Constants;
  6. import com.fuint.common.config.Message;
  7. import com.fuint.common.dto.CouponDto;
  8. import com.fuint.common.dto.MyCouponDto;
  9. import com.fuint.common.enums.*;
  10. import com.fuint.common.param.CouponReceiveParam;
  11. import com.fuint.common.service.*;
  12. import com.fuint.common.util.DateUtil;
  13. import com.fuint.common.util.SeqUtil;
  14. import com.fuint.framework.exception.BusinessCheckException;
  15. import com.fuint.framework.pagination.PaginationRequest;
  16. import com.fuint.framework.pagination.PaginationResponse;
  17. import com.fuint.framework.web.ResponseObject;
  18. import com.fuint.repository.mapper.MtUserCouponMapper;
  19. import com.fuint.repository.model.*;
  20. import com.fuint.utils.StringUtil;
  21. import com.github.pagehelper.Page;
  22. import com.github.pagehelper.PageHelper;
  23. import lombok.AllArgsConstructor;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.springframework.context.annotation.Lazy;
  26. import org.springframework.data.domain.PageImpl;
  27. import org.springframework.data.domain.PageRequest;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import java.math.BigDecimal;
  31. import java.util.*;
  32. /**
  33. * 会员卡券业务实现类
  34. *
  35. * Created by FSQ
  36. * CopyRight https://www.fuint.cn
  37. */
  38. @Service
  39. @AllArgsConstructor(onConstructor_= {@Lazy})
  40. public class UserCouponServiceImpl extends ServiceImpl<MtUserCouponMapper, MtUserCoupon> implements UserCouponService {
  41. private MtUserCouponMapper mtUserCouponMapper;
  42. /**
  43. * 卡券服务接口
  44. * */
  45. private CouponService couponService;
  46. /**
  47. * 卡券分组服务接口
  48. * */
  49. private CouponGroupService couponGroupService;
  50. /**
  51. * 会员服务接口
  52. * */
  53. private MemberService memberService;
  54. /**
  55. * 积分服务接口
  56. * */
  57. private PointService pointService;
  58. /**
  59. * 卡券核销记录服务接口
  60. * */
  61. private ConfirmLogService confirmLogService;
  62. /**
  63. * 店铺服务接口
  64. * */
  65. private StoreService storeService;
  66. /**
  67. * 系统设置服务接口
  68. * */
  69. private SettingService settingService;
  70. /**
  71. * 订单服务接口
  72. * */
  73. private OrderService orderService;
  74. /**
  75. * 分页查询券列表
  76. *
  77. * @param paginationRequest
  78. * @return
  79. */
  80. @Override
  81. public PaginationResponse<MtUserCoupon> queryUserCouponListByPagination(PaginationRequest paginationRequest) {
  82. LambdaQueryWrapper<MtUserCoupon> lambdaQueryWrapper = Wrappers.lambdaQuery();
  83. lambdaQueryWrapper.ne(MtUserCoupon::getStatus, StatusEnum.DISABLE.getKey());
  84. String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
  85. if (StringUtils.isNotBlank(status)) {
  86. lambdaQueryWrapper.eq(MtUserCoupon::getStatus, status);
  87. }
  88. String userCouponId = paginationRequest.getSearchParams().get("userCouponId") == null ? "" : paginationRequest.getSearchParams().get("userCouponId").toString();
  89. if (StringUtils.isNotBlank(userCouponId)) {
  90. lambdaQueryWrapper.eq(MtUserCoupon::getId, userCouponId);
  91. }
  92. String userId = paginationRequest.getSearchParams().get("userId") == null ? "" : paginationRequest.getSearchParams().get("userId").toString();
  93. if (StringUtils.isNotBlank(userId)) {
  94. lambdaQueryWrapper.eq(MtUserCoupon::getUserId, userId);
  95. }
  96. String couponId = paginationRequest.getSearchParams().get("couponId") == null ? "" : paginationRequest.getSearchParams().get("couponId").toString();
  97. if (StringUtils.isNotBlank(couponId)) {
  98. lambdaQueryWrapper.eq(MtUserCoupon::getCouponId, couponId);
  99. }
  100. String code = paginationRequest.getSearchParams().get("code") == null ? "" : paginationRequest.getSearchParams().get("code").toString();
  101. if (StringUtils.isNotBlank(code)) {
  102. lambdaQueryWrapper.eq(MtUserCoupon::getCode, code);
  103. }
  104. String mobile = paginationRequest.getSearchParams().get("mobile") == null ? "" : paginationRequest.getSearchParams().get("mobile").toString();
  105. if (StringUtils.isNotBlank(mobile)) {
  106. lambdaQueryWrapper.eq(MtUserCoupon::getMobile, mobile);
  107. }
  108. lambdaQueryWrapper.orderByDesc(MtUserCoupon::getId);
  109. Page<MtUserCoupon> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  110. List<MtUserCoupon> dataList = mtUserCouponMapper.selectList(lambdaQueryWrapper);
  111. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  112. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  113. PaginationResponse<MtUserCoupon> paginationResponse = new PaginationResponse(pageImpl, MtUserCoupon.class);
  114. paginationResponse.setTotalPages(pageHelper.getPages());
  115. paginationResponse.setTotalElements(pageHelper.getTotal());
  116. paginationResponse.setContent(dataList);
  117. return paginationResponse;
  118. }
  119. /**
  120. * 领取卡券(优惠券、计次卡)
  121. *
  122. * @param receiveParam 领取参数
  123. * @return
  124. * */
  125. @Override
  126. @Transactional(rollbackFor = Exception.class)
  127. public boolean receiveCoupon(CouponReceiveParam receiveParam) throws BusinessCheckException {
  128. Integer couponId = receiveParam.getCouponId() == null ? 0 : receiveParam.getCouponId();
  129. Integer userId = receiveParam.getUserId() == null ? 0 : receiveParam.getUserId();
  130. Integer num = receiveParam.getNum() == null ? 1 : receiveParam.getNum();
  131. String receiveCode = receiveParam.getReceiveCode() == null ? "" : receiveParam.getReceiveCode();
  132. Integer userCouponId = 0;
  133. MtCoupon couponInfo = couponService.queryCouponById(couponId);
  134. if (couponInfo == null) {
  135. MtUserCoupon userCoupon = mtUserCouponMapper.findByCode(receiveCode);
  136. if (userCoupon != null) {
  137. if (userCoupon.getUserId() != null && userCoupon.getUserId() > 0) {
  138. if (userCoupon.getUserId().compareTo(userId) == 0) {
  139. throw new BusinessCheckException(Message.HAS_COUPON);
  140. } else {
  141. throw new BusinessCheckException(Message.CODE_ERROR);
  142. }
  143. }
  144. couponInfo = couponService.queryCouponById(userCoupon.getCouponId());
  145. userCouponId = userCoupon.getId();
  146. } else {
  147. throw new BusinessCheckException(Message.CODE_ERROR_1);
  148. }
  149. if (couponInfo == null) {
  150. throw new BusinessCheckException(Message.COUPON_NOT_EXIST);
  151. }
  152. }
  153. // 卡券类型检查
  154. if (couponInfo.getType().equals(CouponTypeEnum.PRESTORE.getKey())) {
  155. throw new BusinessCheckException(Message.COUPON_TYPE_ERROR);
  156. }
  157. MtCouponGroup groupInfo = couponGroupService.queryCouponGroupById(couponInfo.getGroupId());
  158. MtUser userInfo = memberService.queryMemberById(userId);
  159. if (null == userInfo) {
  160. throw new BusinessCheckException(Message.USER_NOT_EXIST);
  161. }
  162. // 会员等级限制
  163. if (couponInfo.getGradeIds() != null && StringUtil.isNotEmpty(couponInfo.getGradeIds())) {
  164. String gradeIds[] = couponInfo.getGradeIds().split(",");
  165. if (gradeIds.length > 0) {
  166. boolean isContains = Arrays.asList(gradeIds).contains(userInfo.getGradeId()+"");
  167. if (!isContains) {
  168. throw new BusinessCheckException(Message.GRADE_ERROR);
  169. }
  170. }
  171. }
  172. // 是否需要领取码
  173. if (couponInfo.getReceiveCode() != null && StringUtil.isNotEmpty(couponInfo.getReceiveCode())) {
  174. if (StringUtil.isEmpty(receiveCode)) {
  175. throw new BusinessCheckException(Message.NEED_CODE);
  176. }
  177. // 线下发放的领取码
  178. if (couponInfo.getSendWay().equals(SendWayEnum.OFFLINE.getKey())) {
  179. MtUserCoupon userCoupon = mtUserCouponMapper.findByCode(receiveCode);
  180. if (userCoupon == null || !userCoupon.getCode().equals(receiveCode)) {
  181. throw new BusinessCheckException(Message.CODE_ERROR_1);
  182. } else {
  183. userCouponId = userCoupon.getId();
  184. }
  185. }
  186. // 前台领取的领取码
  187. if (couponInfo.getSendWay().equals(SendWayEnum.FRONT.getKey()) && !receiveCode.equals(couponInfo.getReceiveCode())) {
  188. throw new BusinessCheckException(Message.CODE_ERROR);
  189. }
  190. }
  191. // 是否已经领取
  192. List<String> statusList = Arrays.asList(UserCouponStatusEnum.UNUSED.getKey(), UserCouponStatusEnum.USED.getKey(), UserCouponStatusEnum.EXPIRE.getKey());
  193. List<MtUserCoupon> userCouponData = mtUserCouponMapper.getUserCouponListByCouponId(userId, couponId, statusList);
  194. if ((userCouponData.size() >= couponInfo.getLimitNum()) && (couponInfo.getLimitNum() > 0)) {
  195. throw new BusinessCheckException(Message.MAX_COUPON_LIMIT);
  196. }
  197. // 积分不足以领取
  198. if (couponInfo.getPoint() != null && couponInfo.getPoint() > 0) {
  199. if (userInfo.getPoint() < couponInfo.getPoint()) {
  200. throw new BusinessCheckException(Message.POINT_LIMIT);
  201. }
  202. }
  203. // 可领取多张,领取序列号
  204. StringBuffer uuid = new StringBuffer();
  205. uuid.append(SeqUtil.getRandomNumber(4));
  206. uuid.append(SeqUtil.getRandomNumber(4));
  207. uuid.append(SeqUtil.getRandomNumber(4));
  208. uuid.append(SeqUtil.getRandomNumber(4));
  209. for (int i = 1; i <= num; i++) {
  210. MtUserCoupon userCoupon = new MtUserCoupon();
  211. if (userCouponId > 0) {
  212. userCoupon = mtUserCouponMapper.selectById(userCouponId);
  213. }
  214. userCoupon.setMerchantId(userInfo.getMerchantId());
  215. userCoupon.setStoreId(couponInfo.getStoreId());
  216. userCoupon.setCouponId(couponInfo.getId());
  217. userCoupon.setType(couponInfo.getType());
  218. userCoupon.setAmount(couponInfo.getAmount());
  219. userCoupon.setGroupId(groupInfo.getId());
  220. userCoupon.setMobile(userInfo.getMobile());
  221. userCoupon.setUserId(userInfo.getId());
  222. userCoupon.setStatus(UserCouponStatusEnum.UNUSED.getKey());
  223. userCoupon.setCreateTime(new Date());
  224. userCoupon.setUpdateTime(new Date());
  225. userCoupon.setExpireTime(couponInfo.getEndTime());
  226. if (couponInfo.getExpireType().equals(CouponExpireTypeEnum.FLEX.getKey())) {
  227. Date expireTime = new Date();
  228. Calendar c = Calendar.getInstance();
  229. c.setTime(expireTime);
  230. c.add(Calendar.DATE, couponInfo.getExpireTime());
  231. expireTime = c.getTime();
  232. userCoupon.setExpireTime(expireTime);
  233. }
  234. // 12位随机数
  235. StringBuffer code = new StringBuffer();
  236. code.append(SeqUtil.getRandomNumber(4));
  237. code.append(SeqUtil.getRandomNumber(4));
  238. code.append(SeqUtil.getRandomNumber(4));
  239. code.append(SeqUtil.getRandomNumber(4));
  240. userCoupon.setCode(code.toString());
  241. userCoupon.setUuid(uuid.toString());
  242. if (userCoupon.getId() != null) {
  243. mtUserCouponMapper.updateById(userCoupon);
  244. } else {
  245. mtUserCouponMapper.insert(userCoupon);
  246. }
  247. }
  248. // 是否需要扣除相应积分
  249. if (couponInfo.getPoint() != null && couponInfo.getPoint() > 0) {
  250. MtPoint reqPointDto = new MtPoint();
  251. reqPointDto.setUserId(userId);
  252. reqPointDto.setAmount(-couponInfo.getPoint());
  253. reqPointDto.setDescription("领取"+ couponInfo.getName() + "扣除" +couponInfo.getPoint() +"积分");
  254. reqPointDto.setOperator("");
  255. pointService.addPoint(reqPointDto);
  256. }
  257. return true;
  258. }
  259. /**
  260. * 储值卡券
  261. *
  262. * @param paramMap 储值参数
  263. * @return
  264. * */
  265. public boolean preStore(Map<String, Object> paramMap) throws BusinessCheckException {
  266. Integer couponId = paramMap.get("couponId") == null ? 0 : Integer.parseInt(paramMap.get("couponId").toString());
  267. Integer userId = paramMap.get("userId") == null ? 0 : Integer.parseInt(paramMap.get("userId").toString());
  268. String param = paramMap.get("param") == null ? "" : paramMap.get("param").toString();
  269. Integer orderId = paramMap.get("orderId") == null ? 0 : Integer.parseInt(paramMap.get("orderId").toString());
  270. if (StringUtil.isEmpty(param) || couponId <= 0 || userId <= 0) {
  271. throw new BusinessCheckException(Message.PARAM_ERROR);
  272. }
  273. MtCoupon couponInfo = couponService.queryCouponById(couponId);
  274. if (couponInfo == null) {
  275. throw new BusinessCheckException(Message.COUPON_NOT_EXIST);
  276. }
  277. MtUser userInfo = memberService.queryMemberById(userId);
  278. if (userInfo == null) {
  279. throw new BusinessCheckException(Message.USER_NOT_EXIST);
  280. }
  281. String[] paramArr = param.split(",");
  282. for (int i = 0; i < paramArr.length; i++) {
  283. String item = paramArr[i];
  284. if (StringUtil.isNotEmpty(item)) {
  285. String buyItem = paramArr[i]; // 100_200_1
  286. String[] buyItemArr = buyItem.split("_");
  287. if (StringUtil.isNotEmpty(buyItemArr[2])) {
  288. Integer numInt = Integer.parseInt(buyItemArr[2]);
  289. for (int j = 1; j <= numInt; j++) {
  290. if (StringUtil.isNotEmpty(buyItemArr[1])) {
  291. preStoreItem(couponInfo, userInfo, orderId, new BigDecimal(buyItemArr[1]));
  292. }
  293. }
  294. }
  295. }
  296. }
  297. return true;
  298. }
  299. /**
  300. * 获取会员卡券列表
  301. *
  302. * @param userId 会员ID
  303. * @param status 状态
  304. * @return
  305. * */
  306. @Override
  307. public List<MtUserCoupon> getUserCouponList(Integer userId, List<String> status) {
  308. return mtUserCouponMapper.getUserCouponList(userId, status);
  309. }
  310. /**
  311. * 获取会员卡券列表
  312. *
  313. * @param paramMap
  314. * @throws BusinessCheckException
  315. * @return
  316. * */
  317. @Override
  318. @Transactional(rollbackFor = Exception.class)
  319. public ResponseObject getUserCouponList(Map<String, Object> paramMap) throws BusinessCheckException {
  320. Integer pageNumber = paramMap.get("pageNumber") == null ? Constants.PAGE_NUMBER : Integer.parseInt(paramMap.get("pageNumber").toString());
  321. Integer pageSize = paramMap.get("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(paramMap.get("pageSize").toString());
  322. String userId = paramMap.get("userId") == null ? "" : paramMap.get("userId").toString();
  323. String userNo = paramMap.get("userNo") == null ? "" : paramMap.get("userNo").toString();
  324. String status = paramMap.get("status") == null ? "" : paramMap.get("status").toString();
  325. String type = paramMap.get("type") == null ? "": paramMap.get("type").toString();
  326. String mobile = paramMap.get("mobile") == null ? "" : paramMap.get("mobile").toString();
  327. String merchantId = paramMap.get("merchantId") == null ? "" : paramMap.get("merchantId").toString();
  328. String storeId = paramMap.get("storeId") == null ? "" : paramMap.get("storeId").toString();
  329. String couponId = paramMap.get("couponId") == null ? "" : paramMap.get("couponId").toString();
  330. String code = paramMap.get("code") == null ? "" : paramMap.get("code").toString();
  331. String id = paramMap.get("id") == null ? "" : paramMap.get("id").toString();
  332. // 处理已失效
  333. if (pageNumber <= 1 && StringUtil.isNotEmpty(userId)) {
  334. List<String> statusList = Arrays.asList(UserCouponStatusEnum.UNUSED.getKey());
  335. List<MtUserCoupon> data = mtUserCouponMapper.getUserCouponList(Integer.parseInt(userId), statusList);
  336. for (MtUserCoupon uc : data) {
  337. MtCoupon coupon = couponService.queryCouponById(uc.getCouponId());
  338. // 已过期
  339. if (coupon.getExpireType().equals(CouponExpireTypeEnum.FIX.getKey()) && coupon.getEndTime() != null && coupon.getEndTime().before(new Date())) {
  340. uc.setStatus(UserCouponStatusEnum.EXPIRE.getKey());
  341. uc.setUpdateTime(new Date());
  342. mtUserCouponMapper.updateById(uc);
  343. }
  344. // 已过期
  345. if (coupon.getExpireType().equals(CouponExpireTypeEnum.FLEX.getKey()) && uc.getExpireTime() != null && uc.getExpireTime().before(new Date())) {
  346. uc.setStatus(UserCouponStatusEnum.EXPIRE.getKey());
  347. uc.setUpdateTime(new Date());
  348. mtUserCouponMapper.updateById(uc);
  349. }
  350. // 已删除
  351. if (coupon.getStatus().equals(StatusEnum.DISABLE.getKey())) {
  352. uc.setStatus(UserCouponStatusEnum.DISABLE.getKey());
  353. uc.setUpdateTime(new Date());
  354. mtUserCouponMapper.updateById(uc);
  355. }
  356. }
  357. }
  358. LambdaQueryWrapper<MtUserCoupon> lambdaQueryWrapper = Wrappers.lambdaQuery();
  359. lambdaQueryWrapper.ne(MtUserCoupon::getStatus, StatusEnum.DISABLE.getKey());
  360. if (StringUtil.isNotEmpty(status)) {
  361. lambdaQueryWrapper.eq(MtUserCoupon::getStatus, status);
  362. }
  363. if (StringUtil.isNotEmpty(userId)) {
  364. lambdaQueryWrapper.eq(MtUserCoupon::getUserId, userId);
  365. }
  366. if (StringUtil.isNotEmpty(userNo)) {
  367. if (StringUtil.isEmpty(merchantId)) {
  368. merchantId = "0";
  369. }
  370. MtUser userInfo = memberService.queryMemberByUserNo(Integer.parseInt(merchantId), userNo);
  371. if (userInfo != null) {
  372. lambdaQueryWrapper.eq(MtUserCoupon::getUserId, userInfo.getId());
  373. }
  374. }
  375. if (StringUtil.isNotEmpty(mobile)) {
  376. lambdaQueryWrapper.eq(MtUserCoupon::getMobile, mobile);
  377. }
  378. if (StringUtil.isNotEmpty(type)) {
  379. lambdaQueryWrapper.eq(MtUserCoupon::getType, type);
  380. }
  381. if (StringUtil.isNotEmpty(merchantId)) {
  382. lambdaQueryWrapper.eq(MtUserCoupon::getMerchantId, merchantId);
  383. }
  384. if (StringUtil.isNotEmpty(storeId)) {
  385. lambdaQueryWrapper.eq(MtUserCoupon::getStoreId, storeId);
  386. }
  387. if (StringUtil.isNotEmpty(couponId)) {
  388. lambdaQueryWrapper.eq(MtUserCoupon::getCouponId, couponId);
  389. }
  390. if (StringUtil.isNotEmpty(code)) {
  391. lambdaQueryWrapper.eq(MtUserCoupon::getCode, code);
  392. }
  393. if (StringUtil.isNotEmpty(id)) {
  394. lambdaQueryWrapper.eq(MtUserCoupon::getId, id);
  395. }
  396. lambdaQueryWrapper.orderByDesc(MtUserCoupon::getId);
  397. Page<MtUserCoupon> pageHelper = PageHelper.startPage(pageNumber, pageSize);
  398. List<MtUserCoupon> userCouponList = mtUserCouponMapper.selectList(lambdaQueryWrapper);
  399. List<MyCouponDto> dataList = new ArrayList<>();
  400. if (userCouponList.size() > 0) {
  401. for (MtUserCoupon userCouponDto : userCouponList) {
  402. MtCoupon couponInfo = couponService.queryCouponById(userCouponDto.getCouponId());
  403. MtUser userInfo = memberService.queryMemberById(userCouponDto.getUserId());
  404. MtStore storeInfo = storeService.queryStoreById(userCouponDto.getStoreId());
  405. if (couponInfo == null) {
  406. continue;
  407. }
  408. MyCouponDto dto = new MyCouponDto();
  409. dto.setId(userCouponDto.getId());
  410. dto.setName(couponInfo.getName());
  411. dto.setCode(userCouponDto.getCode());
  412. dto.setCouponId(couponInfo.getId());
  413. dto.setUseRule(couponInfo.getDescription());
  414. String image = couponInfo.getImage();
  415. String baseImage = settingService.getUploadBasePath();
  416. dto.setImage(baseImage + image);
  417. dto.setStatus(userCouponDto.getStatus());
  418. dto.setAmount(userCouponDto.getAmount());
  419. dto.setBalance(userCouponDto.getBalance());
  420. dto.setType(couponInfo.getType());
  421. dto.setUsedTime(userCouponDto.getUsedTime());
  422. dto.setCreateTime(userCouponDto.getCreateTime());
  423. dto.setUserInfo(userInfo);
  424. dto.setStoreInfo(storeInfo);
  425. dto.setNum(0);
  426. boolean canUse = couponService.isCouponEffective(couponInfo, userCouponDto);
  427. if (!userCouponDto.getStatus().equals(UserCouponStatusEnum.UNUSED.getKey())) {
  428. canUse = false;
  429. }
  430. dto.setCanUse(canUse);
  431. if (couponInfo.getExpireType().equals(CouponExpireTypeEnum.FIX.getKey())) {
  432. String effectiveDate = DateUtil.formatDate(couponInfo.getBeginTime(), "yyyy.MM.dd HH:mm") + "-" + DateUtil.formatDate(couponInfo.getEndTime(), "yyyy.MM.dd HH:mm");
  433. dto.setEffectiveDate(effectiveDate);
  434. }
  435. if (couponInfo.getExpireType().equals(CouponExpireTypeEnum.FLEX.getKey())) {
  436. String effectiveDate = DateUtil.formatDate(userCouponDto.getCreateTime(), "yyyy.MM.dd HH:mm") + "-" + DateUtil.formatDate(userCouponDto.getExpireTime(), "yyyy.MM.dd HH:mm");
  437. dto.setEffectiveDate(effectiveDate);
  438. }
  439. String tips = "";
  440. // 优惠券tips
  441. if (couponInfo.getType().equals(CouponTypeEnum.COUPON.getKey())) {
  442. if (StringUtil.isNotEmpty(couponInfo.getOutRule()) && Float.parseFloat(couponInfo.getOutRule()) > 0) {
  443. tips = "满" + couponInfo.getOutRule() + "可用";
  444. } else {
  445. tips = "无门槛券";
  446. }
  447. }
  448. // 储值卡tips
  449. if (couponInfo.getType().equals(CouponTypeEnum.PRESTORE.getKey())) {
  450. tips = "¥" + userCouponDto .getAmount() + ",余额¥" + userCouponDto.getBalance();
  451. }
  452. // 计次卡tips
  453. if (couponInfo.getType().equals(CouponTypeEnum.TIMER.getKey())) {
  454. Long confirmNum = confirmLogService.getConfirmNum(userCouponDto.getId());
  455. tips = "已使用"+ confirmNum +"次,可使用" + couponInfo.getOutRule() + "次";
  456. dto.setNum(Integer.parseInt(couponInfo.getOutRule()) - confirmNum.intValue());
  457. }
  458. dto.setTips(tips);
  459. dataList.add(dto);
  460. }
  461. }
  462. PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);
  463. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  464. PaginationResponse<MyCouponDto> paginationResponse = new PaginationResponse(pageImpl, MyCouponDto.class);
  465. paginationResponse.setTotalPages(pageHelper.getPages());
  466. paginationResponse.setTotalElements(pageHelper.getTotal());
  467. if (dataList.size() == 0) {
  468. paginationResponse.setTotalPages(0);
  469. paginationResponse.setTotalElements(0);
  470. }
  471. paginationResponse.setContent(dataList);
  472. return new ResponseObject(200, "查询成功", paginationResponse);
  473. }
  474. /**
  475. * 获取会员可支付使用的卡券
  476. *
  477. * @param userId 会员ID
  478. * @param storeId 使用门店
  479. * @param useFor 用途
  480. * @return
  481. * */
  482. @Override
  483. public List<CouponDto> getPayAbleCouponList(Integer userId, Integer storeId, String useFor) throws BusinessCheckException {
  484. List<String> statusList = Arrays.asList(UserCouponStatusEnum.UNUSED.getKey());
  485. List<MtUserCoupon> userCouponList = mtUserCouponMapper.getUserCouponList(userId, statusList);
  486. List<CouponDto> dataList = new ArrayList<>();
  487. if (userCouponList.size() > 0) {
  488. for (MtUserCoupon userCoupon : userCouponList) {
  489. MtCoupon couponInfo = couponService.queryCouponById(userCoupon.getCouponId());
  490. // 适用门店
  491. if (storeId != null && storeId > 0 && StringUtil.isNotEmpty(couponInfo.getStoreIds())) {
  492. String[] storeIds = couponInfo.getStoreIds().split(",");
  493. if (!Arrays.asList(storeIds).contains(storeId.toString())) {
  494. continue;
  495. }
  496. }
  497. // 只取专用卡券
  498. if (StringUtil.isNotEmpty(useFor) && !couponInfo.getUseFor().equals(useFor)) {
  499. continue;
  500. }
  501. // 不取专用卡券
  502. if (StringUtil.isEmpty(useFor) && couponInfo.getUseFor() != null && StringUtil.isNotEmpty(couponInfo.getUseFor())) {
  503. continue;
  504. }
  505. CouponDto couponDto = new CouponDto();
  506. couponDto.setId(couponInfo.getId());
  507. couponDto.setUserCouponId(userCoupon.getId());
  508. couponDto.setName(couponInfo.getName());
  509. couponDto.setAmount(userCoupon.getAmount());
  510. couponDto.setStatus(UserCouponStatusEnum.UNUSED.getKey());
  511. boolean isEffective = couponService.isCouponEffective(couponInfo, userCoupon);
  512. // 1.储值卡可用
  513. if (isEffective && couponInfo.getType().equals(CouponTypeEnum.PRESTORE.getKey())) {
  514. if (userCoupon.getBalance().compareTo(new BigDecimal("0")) > 0) {
  515. couponDto.setType(CouponTypeEnum.PRESTORE.getValue());
  516. couponDto.setAmount(userCoupon.getBalance());
  517. dataList.add(couponDto);
  518. }
  519. } else if(isEffective && couponInfo.getType().equals(CouponTypeEnum.COUPON.getKey())) {
  520. // 2.无门槛的优惠券可用
  521. if (StringUtil.isEmpty(couponInfo.getOutRule()) || couponInfo.getOutRule().equals("0")) {
  522. couponDto.setType(CouponTypeEnum.COUPON.getValue());
  523. dataList.add(couponDto);
  524. }
  525. }
  526. }
  527. }
  528. return dataList;
  529. }
  530. /**
  531. * 获取会员卡券详情
  532. *
  533. * @param userId 会员ID
  534. * @param couponId 卡券ID
  535. * @return
  536. * */
  537. @Override
  538. public List<MtUserCoupon> getUserCouponDetail(Integer userId, Integer couponId) {
  539. return mtUserCouponMapper.findUserCouponDetail(couponId, userId);
  540. }
  541. /**
  542. * 获取会员卡券详情
  543. *
  544. * @param userCouponId 会员卡券ID
  545. * @return
  546. * */
  547. @Override
  548. public MtUserCoupon getUserCouponDetail(Integer userCouponId) {
  549. MtUserCoupon userCoupon = mtUserCouponMapper.selectById(userCouponId);
  550. return userCoupon;
  551. }
  552. /**
  553. * 根据过期时间查询会员卡券
  554. *
  555. * @param userId 会员ID
  556. * @param status 状态
  557. * @param startTime 开始时间
  558. * @param endTime 结束时间
  559. * @return
  560. * */
  561. @Override
  562. public List<MtUserCoupon> getUserCouponListByExpireTime(Integer userId, String status, String startTime, String endTime) {
  563. List<MtUserCoupon> result = mtUserCouponMapper.getUserCouponListByExpireTime(userId, status, startTime, endTime);
  564. return result;
  565. }
  566. /**
  567. * 会员发送卡券
  568. *
  569. * @param orderId 订单ID
  570. * @param couponId 卡券ID
  571. * @param userId 会员ID
  572. * @param mobile 手机号
  573. * @return
  574. * */
  575. public boolean buyCouponItem(Integer orderId, Integer couponId, Integer userId, String mobile) throws BusinessCheckException {
  576. MtCoupon couponInfo = couponService.queryCouponById(couponId);
  577. MtUserCoupon userCoupon = new MtUserCoupon();
  578. userCoupon.setCouponId(couponId);
  579. userCoupon.setMerchantId(couponInfo.getMerchantId());
  580. userCoupon.setStoreId(couponInfo.getStoreId());
  581. userCoupon.setType(couponInfo.getType());
  582. userCoupon.setGroupId(couponInfo.getGroupId());
  583. userCoupon.setMobile(mobile);
  584. userCoupon.setUserId(userId);
  585. userCoupon.setStatus(UserCouponStatusEnum.UNUSED.getKey());
  586. userCoupon.setCreateTime(new Date());
  587. userCoupon.setUpdateTime(new Date());
  588. userCoupon.setExpireTime(couponInfo.getEndTime());
  589. if (couponInfo.getExpireType().equals(CouponExpireTypeEnum.FLEX.getKey())) {
  590. Date expireTime = new Date();
  591. Calendar c = Calendar.getInstance();
  592. c.setTime(expireTime);
  593. c.add(Calendar.DATE, couponInfo.getExpireTime());
  594. expireTime = c.getTime();
  595. userCoupon.setExpireTime(expireTime);
  596. }
  597. userCoupon.setOrderId(orderId);
  598. // 如果购买的是储值卡
  599. if (couponInfo.getType().equals(CouponTypeEnum.PRESTORE.getKey()) && couponInfo.getInRule() != null) {
  600. String[] paramArr = couponInfo.getInRule().split(","); // 100_200,300_500
  601. MtOrder orderInfo = orderService.getOrderInfo(orderId);
  602. if (orderInfo != null) {
  603. BigDecimal payAmount = orderInfo.getPayAmount();
  604. BigDecimal totalAmount = new BigDecimal(0);
  605. if (paramArr.length > 0) {
  606. for (int i = 0; i < paramArr.length; i++) {
  607. String[] storeItem = paramArr[i].split("_");
  608. if (storeItem.length > 0) {
  609. BigDecimal amount = new BigDecimal(paramArr[i].split("_")[0]);
  610. if (payAmount.compareTo(amount) >= 0) {
  611. totalAmount = new BigDecimal(paramArr[i].split("_")[1]);
  612. payAmount = payAmount.subtract(amount);
  613. }
  614. }
  615. }
  616. }
  617. couponInfo.setAmount(totalAmount);
  618. }
  619. }
  620. userCoupon.setAmount(couponInfo.getAmount());
  621. userCoupon.setBalance(couponInfo.getAmount());
  622. // 12位随机数
  623. StringBuffer code = new StringBuffer();
  624. code.append(SeqUtil.getRandomNumber(4));
  625. code.append(SeqUtil.getRandomNumber(4));
  626. code.append(SeqUtil.getRandomNumber(4));
  627. code.append(SeqUtil.getRandomNumber(4));
  628. userCoupon.setCode(code.toString());
  629. userCoupon.setUuid(code.toString());
  630. mtUserCouponMapper.insert(userCoupon);
  631. return true;
  632. }
  633. /**
  634. * 预存单张
  635. *
  636. * @param couponInfo 卡券信息
  637. * @param userInfo 会员信息
  638. * @return
  639. * */
  640. private boolean preStoreItem(MtCoupon couponInfo, MtUser userInfo, Integer orderId, BigDecimal amount) {
  641. MtUserCoupon userCoupon = new MtUserCoupon();
  642. userCoupon.setCouponId(couponInfo.getId());
  643. userCoupon.setType(couponInfo.getType());
  644. userCoupon.setGroupId(couponInfo.getGroupId());
  645. userCoupon.setMobile(userInfo.getMobile());
  646. userCoupon.setMerchantId(couponInfo.getMerchantId());
  647. userCoupon.setStoreId(couponInfo.getStoreId());
  648. userCoupon.setUserId(userInfo.getId());
  649. userCoupon.setStatus(UserCouponStatusEnum.UNUSED.getKey());
  650. userCoupon.setCreateTime(new Date());
  651. userCoupon.setUpdateTime(new Date());
  652. userCoupon.setExpireTime(couponInfo.getEndTime());
  653. if (couponInfo.getExpireType().equals(CouponExpireTypeEnum.FLEX.getKey())) {
  654. Date expireTime = new Date();
  655. Calendar c = Calendar.getInstance();
  656. c.setTime(expireTime);
  657. c.add(Calendar.DATE, couponInfo.getExpireTime());
  658. expireTime = c.getTime();
  659. userCoupon.setExpireTime(expireTime);
  660. }
  661. userCoupon.setOrderId(orderId);
  662. userCoupon.setAmount(amount);
  663. userCoupon.setBalance(amount);
  664. // 12位随机数
  665. StringBuffer code = new StringBuffer();
  666. code.append(SeqUtil.getRandomNumber(4));
  667. code.append(SeqUtil.getRandomNumber(4));
  668. code.append(SeqUtil.getRandomNumber(4));
  669. code.append(SeqUtil.getRandomNumber(4));
  670. userCoupon.setCode(code.toString());
  671. userCoupon.setUuid(code.toString());
  672. mtUserCouponMapper.insert(userCoupon);
  673. return true;
  674. }
  675. }