You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
6.4 KiB
165 lines
6.4 KiB
package com.hai.service.impl;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.hai.common.exception.ErrorCode;
|
|
import com.hai.common.exception.ErrorHelp;
|
|
import com.hai.common.exception.SysCode;
|
|
import com.hai.dao.HighUserCouponMapper;
|
|
import com.hai.entity.HighCoupon;
|
|
import com.hai.entity.HighCouponCode;
|
|
import com.hai.entity.HighUserCoupon;
|
|
import com.hai.entity.HighUserCouponExample;
|
|
import com.hai.model.HighUserCouponModel;
|
|
import com.hai.service.HighCouponCodeService;
|
|
import com.hai.service.HighCouponService;
|
|
import com.hai.service.HighUserCouponService;
|
|
import org.apache.commons.collections4.MapUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* @Auther: 胡锐
|
|
* @Description:
|
|
* @Date: 2021/3/27 16:10
|
|
*/
|
|
@Service("highUserCouponService")
|
|
public class HighUserCouponServiceImpl implements HighUserCouponService {
|
|
|
|
@Resource
|
|
private HighUserCouponMapper highUserCouponMapper;
|
|
|
|
@Resource
|
|
private HighCouponService highCouponService;
|
|
|
|
@Resource
|
|
private HighCouponCodeService highCouponCodeService;
|
|
|
|
@Override
|
|
public void insertUserCoupon(HighUserCoupon highUserCoupon) {
|
|
highUserCouponMapper.insert(highUserCoupon);
|
|
}
|
|
|
|
@Override
|
|
public void updateUserCoupon(HighUserCoupon highUserCoupon) {
|
|
highUserCouponMapper.updateByPrimaryKey(highUserCoupon);
|
|
}
|
|
|
|
@Override
|
|
public List<HighUserCoupon> getOverdueCoupon() {
|
|
HighUserCouponExample example = new HighUserCouponExample();
|
|
example.createCriteria().andStatusEqualTo(1).andUseEndTimeGreaterThanOrEqualTo(new Date());
|
|
return highUserCouponMapper.selectByExample(example);
|
|
}
|
|
|
|
@Override
|
|
public HighUserCoupon getDetailByCodeId(Long couponCodeId) {
|
|
|
|
HighUserCouponExample example = new HighUserCouponExample();
|
|
example.createCriteria().andStatusEqualTo(1).andCouponCodeIdEqualTo(couponCodeId);
|
|
List<HighUserCoupon> highUserCoupons = highUserCouponMapper.selectByExample(example);
|
|
if (highUserCoupons.size() > 0) {
|
|
return highUserCoupons.get(0);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public List<HighUserCoupon> getUserCouponList(Map<String, Object> map) {
|
|
HighUserCouponExample example = new HighUserCouponExample();
|
|
HighUserCouponExample.Criteria criteria = example.createCriteria();
|
|
|
|
if (MapUtils.getLong(map, "userId") != null) {
|
|
criteria.andUserIdEqualTo(MapUtils.getLong(map, "userId"));
|
|
}
|
|
|
|
if (MapUtils.getInteger(map, "status") != null) {
|
|
criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
|
|
}
|
|
|
|
criteria.andStatusNotEqualTo(100);
|
|
example.setOrderByClause("create_time desc");
|
|
List<HighUserCoupon> highUserCoupons = highUserCouponMapper.selectByExample(example);
|
|
if (highUserCoupons.size() > 0) {
|
|
for (HighUserCoupon highUserCoupon : highUserCoupons) {
|
|
highUserCoupon.setHighCouponModel(highCouponService.getCouponById(highUserCoupon.getCouponId()));
|
|
}
|
|
}
|
|
return highUserCoupons;
|
|
}
|
|
|
|
@Override
|
|
public HighUserCoupon getUserCoupon(Long userId, Long couponId) {
|
|
HighUserCouponExample example = new HighUserCouponExample();
|
|
example.createCriteria().andUserIdEqualTo(userId).andCouponIdEqualTo(couponId);
|
|
List<HighUserCoupon> coupons = highUserCouponMapper.selectByExample(example);
|
|
if (coupons != null && coupons.size() > 0) {
|
|
return coupons.get(0);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public List<HighUserCoupon> getCouponList(Long userId, Integer status) {
|
|
HighUserCouponExample example = new HighUserCouponExample();
|
|
HighUserCouponExample.Criteria criteria = example.createCriteria().andUserIdEqualTo(userId);
|
|
if (status != null) {
|
|
criteria.andStatusEqualTo(status);
|
|
}
|
|
example.setOrderByClause("create_time desc");
|
|
return highUserCouponMapper.selectByExample(example);
|
|
}
|
|
|
|
@Override
|
|
@Transactional(propagation= Propagation.REQUIRES_NEW)
|
|
public HighUserCoupon againReceiveCoupon(Long userId, Long couponId) {
|
|
// 用户的卡卷
|
|
HighUserCoupon userCoupon = getUserCoupon(userId, couponId);
|
|
if (userCoupon == null) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到卡卷");
|
|
}
|
|
if (userCoupon.getStatus() != 0) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法重新领取");
|
|
}
|
|
userCoupon.setStatus(100); // 删除
|
|
|
|
HighCoupon coupon = highCouponService.getCouponById(userCoupon.getCouponId());
|
|
if (coupon == null) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到卡卷");
|
|
}
|
|
|
|
// 查询待销售
|
|
List<HighCouponCode> list = highCouponCodeService.getNoSaleCode(coupon.getId());
|
|
if (list == null || list.size() == 0) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, coupon.getCouponName() + "暂无库存,请联系客服");
|
|
}
|
|
HighCouponCode highCouponCode = list.get(0);
|
|
highCouponCode.setChildOrderId(userCoupon.getId());
|
|
highCouponCode.setStatus(2); // 状态:1.待销售 2.未使用 3.已使用 99.预支付
|
|
highCouponCodeService.updateCouponCode(highCouponCode);
|
|
|
|
// 卡卷关联用户
|
|
HighUserCoupon highUserCoupon = new HighUserCoupon();
|
|
highUserCoupon.setMerchantId(coupon.getMerchantId());
|
|
highUserCoupon.setCouponId(coupon.getId());
|
|
highUserCoupon.setUserId(userId);
|
|
highUserCoupon.setCouponCodeId(highCouponCode.getId());
|
|
highUserCoupon.setCreateTime(new Date());
|
|
|
|
// 计算使用有效期
|
|
Calendar userEndTime = Calendar.getInstance();
|
|
userEndTime.add(Calendar.DATE, coupon.getRecycleDay());
|
|
if (userEndTime.getTime().compareTo(highCouponCode.getUseEndTime()) == 1) {
|
|
highUserCoupon.setUseEndTime(highCouponCode.getUseEndTime());
|
|
} else {
|
|
highUserCoupon.setUseEndTime(userEndTime.getTime());
|
|
}
|
|
highUserCoupon.setStatus(1); // 状态 0:已过期 1:未使用 2:已使用
|
|
highUserCouponMapper.insert(highUserCoupon);
|
|
|
|
return highUserCoupon;
|
|
}
|
|
}
|
|
|