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.
367 lines
17 KiB
367 lines
17 KiB
package com.hai.service.impl;
|
|
|
|
import com.hai.common.exception.ErrorCode;
|
|
import com.hai.common.exception.ErrorHelp;
|
|
import com.hai.common.exception.SysCode;
|
|
import com.hai.dao.HighDiscountUserRelMapper;
|
|
import com.hai.entity.*;
|
|
import com.hai.enum_type.DiscountPlatform;
|
|
import com.hai.enum_type.DiscountUseScope;
|
|
import com.hai.service.*;
|
|
import org.apache.commons.collections4.MapUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.TransactionDefinition;
|
|
import org.springframework.transaction.annotation.Isolation;
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @Auther: 胡锐
|
|
* @Description:
|
|
* @Date: 2021/4/4 15:12
|
|
*/
|
|
@Service("highDiscountUserRelService")
|
|
public class HighDiscountUserRelServiceImpl implements HighDiscountUserRelService {
|
|
|
|
@Resource
|
|
private HighDiscountUserRelMapper highDiscountUserRelMapper;
|
|
|
|
@Resource
|
|
private HighDiscountAgentRelService highDiscountAgentRelService;
|
|
|
|
@Resource
|
|
private HighAgentService highAgentService;
|
|
|
|
@Resource
|
|
private HighUserService userService;
|
|
|
|
@Resource
|
|
private HighDiscountService highDiscountService;
|
|
|
|
@Resource
|
|
private HighDiscountAgentCodeService highDiscountAgentCodeService;
|
|
@Resource
|
|
private SecConfigService secConfigService;
|
|
|
|
@Override
|
|
@Transactional(propagation= Propagation.REQUIRES_NEW,isolation= Isolation.SERIALIZABLE)
|
|
public void receiveDiscount(Long userId, Long codeId) {
|
|
|
|
// 查询优惠券二维码
|
|
HighDiscountAgentCode code = highDiscountAgentCodeService.getCodeById(codeId);
|
|
if(code == null) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "二维码不存在");
|
|
}
|
|
// 状态 0:删除 1:待领取 2:待使用 3:已使用 4:已过期 5: 已分配
|
|
if(code.getStatus() != 1 && code.getStatus() != 5) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "二维码不存在或已被领取");
|
|
}
|
|
code.setStatus(2);
|
|
highDiscountAgentCodeService.updateCode(code);
|
|
|
|
// 查询优惠券信息
|
|
HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(code.getDiscountAgentId());
|
|
if (rel == null || rel.getHighDiscount() == null || rel.getAgentId() == null){
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_DISCOUNT, "");
|
|
}
|
|
rel.setStockCount(rel.getStockCount() - 1);
|
|
highDiscountAgentRelService.updateDiscountAgentRel(rel);
|
|
|
|
// 查询优惠券信息
|
|
HighDiscount discount = highDiscountService.getDiscountById(rel.getDiscountId());
|
|
if (discount == null) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券信息");
|
|
}
|
|
if (!discount.getStatus().equals(2)) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券活动已过期");
|
|
}
|
|
if (new Date().compareTo(discount.getSalesEndTime()) >= 1) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券活动已过期");
|
|
}
|
|
if (discount.getReceiveNumber() > 0 && this.receiveDiscountCount(userId, discount.getId()).intValue() >= discount.getReceiveNumber().intValue()) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券已达到上线");
|
|
}
|
|
if (discount.getUseScope().equals(DiscountUseScope.type8.getType())) {
|
|
SecConfig cqCnpcQuota = secConfigService.findByCodeType("CQ_CNPC_QUOTA");
|
|
if (cqCnpcQuota != null && highDiscountUserRelMapper.userReceiveLimitNumber(userId,DiscountUseScope.type8.getType()).intValue() >= Integer.parseInt(cqCnpcQuota.getCodeValue())) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当日领取优惠券已达上限");
|
|
}
|
|
}
|
|
// 校验卡卷状态
|
|
if (rel.getHighDiscount().getStatus() != 2) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券已过期");
|
|
}
|
|
/* if (rel.getStockCount() <= 0) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.DISCOUNT_STOCK_COUNT_ERROR, "");
|
|
}
|
|
|
|
// 校验是否重复领取
|
|
HighDiscountUserRelExample example = new HighDiscountUserRelExample();
|
|
example.createCriteria().andUserIdEqualTo(userId).andDiscountIdEqualTo(rel.getDiscountId()).andStatusEqualTo(1);
|
|
if (highDiscountUserRelMapper.selectByExample(example).size() > 0) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "重复领取卡券,请使用后再进行领取");
|
|
}*/
|
|
|
|
HighDiscountUserRel userRel = new HighDiscountUserRel();
|
|
userRel.setDiscountPlatform(DiscountPlatform.getPlatformByCode(discount.getPlatform()).getNumber());
|
|
userRel.setDiscountCompanyId(discount.getCompanyId());
|
|
userRel.setDiscountId(rel.getDiscountId());
|
|
userRel.setDiscountName(discount.getDiscountName());
|
|
userRel.setDiscountImg(discount.getDiscountImg());
|
|
userRel.setDiscountUseScope(discount.getUseScope());
|
|
userRel.setDiscountUsingRange(discount.getUsingRange());
|
|
userRel.setDiscountType(discount.getDiscountType());
|
|
userRel.setDiscountCondition(discount.getDiscountCondition());
|
|
userRel.setDiscountPrice(discount.getDiscountPrice());
|
|
userRel.setAgentId(rel.getAgentId());
|
|
userRel.setUserId(userId);
|
|
userRel.setStatus(1); // 状态 0:已过期 1:未使用 2:已使用
|
|
userRel.setCreateTime(new Date());
|
|
userRel.setDiscountAgentCodeId(code.getId());
|
|
// 计算使用有效期
|
|
Calendar userEndTime = Calendar.getInstance();
|
|
userEndTime.setTime(new Date());
|
|
userEndTime.set(Calendar.HOUR_OF_DAY, 23);
|
|
userEndTime.set(Calendar.MINUTE, 59);
|
|
userEndTime.set(Calendar.SECOND, 59);
|
|
userEndTime.add(Calendar.DATE, rel.getHighDiscount().getEffectiveDay());
|
|
|
|
if (userEndTime.getTime().compareTo(rel.getHighDiscount().getSalesEndTime()) == 1) {
|
|
userRel.setUseEndTime(rel.getHighDiscount().getSalesEndTime());
|
|
} else {
|
|
userRel.setUseEndTime(userEndTime.getTime());
|
|
}
|
|
highDiscountUserRelMapper.insert(userRel);
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println(new Date().compareTo(new Date(1677600000000L)));
|
|
if (new Date().compareTo(new Date(1678464000000L)) >= 1) {
|
|
System.out.println(true);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
@Transactional(propagation= Propagation.REQUIRES_NEW,isolation= Isolation.SERIALIZABLE)
|
|
public void receive(Long userId, Long discountAgentId) {
|
|
// 查询优惠券与代理商关系
|
|
HighDiscountAgentRel discountAgentRel = highDiscountAgentRelService.getRelById(discountAgentId);
|
|
if (discountAgentRel == null) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券");
|
|
}
|
|
// 查询优惠券信息
|
|
HighDiscount discount = discountAgentRel.getHighDiscount();
|
|
if (discount == null) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券");
|
|
}
|
|
if (!discount.getStatus().equals(2)) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券活动已过期");
|
|
}
|
|
if (new Date().compareTo(discount.getSalesEndTime()) >= 1) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券活动已过期");
|
|
}
|
|
if (discount.getReceiveNumber() > 0 && this.receiveDiscountCount(userId, discount.getId()) >= discount.getReceiveNumber()) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "领取数量已达到上线");
|
|
}
|
|
// 查询可分配的优惠券code
|
|
List<HighDiscountAgentCode> discountAgentCodeList = highDiscountAgentCodeService.getDiscountCodeByStatus(discountAgentRel.getId(), 1);
|
|
if (discountAgentCodeList.size() == 0) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券已无库存");
|
|
}
|
|
HighDiscountAgentCode code = discountAgentCodeList.get(0);
|
|
code.setStatus(2);
|
|
highDiscountAgentCodeService.updateCode(code);
|
|
|
|
// 查询优惠券信息
|
|
discountAgentRel.setStockCount(discountAgentRel.getStockCount() - 1);
|
|
highDiscountAgentRelService.updateDiscountAgentRel(discountAgentRel);
|
|
|
|
HighDiscountUserRel userRel = new HighDiscountUserRel();
|
|
userRel.setDiscountPlatform(DiscountPlatform.getPlatformByCode(discount.getPlatform()).getNumber());
|
|
userRel.setDiscountCompanyId(discount.getCompanyId());
|
|
userRel.setDiscountId(discountAgentRel.getDiscountId());
|
|
userRel.setDiscountName(discount.getDiscountName());
|
|
userRel.setDiscountImg(discount.getDiscountImg());
|
|
userRel.setDiscountUseScope(discount.getUseScope());
|
|
userRel.setDiscountUsingRange(discount.getUsingRange());
|
|
userRel.setDiscountType(discount.getDiscountType());
|
|
userRel.setDiscountCondition(discount.getDiscountCondition());
|
|
userRel.setDiscountPrice(discount.getDiscountPrice());
|
|
userRel.setAgentId(discountAgentRel.getAgentId());
|
|
userRel.setUserId(userId);
|
|
userRel.setStatus(1); // 状态 0:已过期 1:未使用 2:已使用
|
|
userRel.setCreateTime(new Date());
|
|
userRel.setDiscountAgentCodeId(code.getId());
|
|
// 计算使用有效期
|
|
Calendar userEndTime = Calendar.getInstance();
|
|
userEndTime.setTime(new Date());
|
|
userEndTime.set(Calendar.HOUR_OF_DAY, 23);
|
|
userEndTime.set(Calendar.MINUTE, 59);
|
|
userEndTime.set(Calendar.SECOND, 59);
|
|
userEndTime.add(Calendar.DATE, discountAgentRel.getHighDiscount().getEffectiveDay());
|
|
|
|
if (userEndTime.getTime().compareTo(discountAgentRel.getHighDiscount().getSalesEndTime()) == 1) {
|
|
userRel.setUseEndTime(discountAgentRel.getHighDiscount().getSalesEndTime());
|
|
} else {
|
|
userRel.setUseEndTime(userEndTime.getTime());
|
|
}
|
|
highDiscountUserRelMapper.insert(userRel);
|
|
}
|
|
|
|
@Override
|
|
public void updateDiscountUserRel(HighDiscountUserRel highDiscountUserRel) {
|
|
highDiscountUserRelMapper.updateByPrimaryKey(highDiscountUserRel);
|
|
}
|
|
|
|
@Override
|
|
public HighDiscountUserRel getRelByUserDiscount(Long userId, Long discountId) {
|
|
HighDiscountUserRelExample example = new HighDiscountUserRelExample();
|
|
example.createCriteria().andUserIdEqualTo(userId).andDiscountIdEqualTo(discountId);
|
|
List<HighDiscountUserRel> list = highDiscountUserRelMapper.selectByExample(example);
|
|
if (list.size() > 0) {
|
|
return list.get(0);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public HighDiscountUserRel getRelByDiscount(Long discountId) {
|
|
HighDiscountUserRelExample example = new HighDiscountUserRelExample();
|
|
example.createCriteria().andDiscountAgentCodeIdEqualTo(discountId);
|
|
List<HighDiscountUserRel> list = highDiscountUserRelMapper.selectByExample(example);
|
|
if (list.size() > 0) {
|
|
return list.get(0);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public HighDiscountUserRel getRelByAgentCodeId(Long agentCodeId) {
|
|
HighDiscountUserRelExample example = new HighDiscountUserRelExample();
|
|
example.createCriteria().andDiscountAgentCodeIdEqualTo(agentCodeId);
|
|
List<HighDiscountUserRel> list = highDiscountUserRelMapper.selectByExample(example);
|
|
if (list.size() > 0) {
|
|
return list.get(0);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public HighDiscountUserRel getRelById(Long id) {
|
|
HighDiscountUserRel rel = highDiscountUserRelMapper.selectByPrimaryKey(id);
|
|
if(rel != null) {
|
|
rel.setHighDiscount(highDiscountService.getDiscountById(rel.getDiscountId()));
|
|
rel.setHighAgent(highAgentService.findByAgentMsgId(rel.getAgentId()));
|
|
}
|
|
return rel;
|
|
}
|
|
|
|
@Override
|
|
public List<HighDiscountUserRel> getDiscountList(Map<String, Object> map) {
|
|
HighDiscountUserRelExample example = new HighDiscountUserRelExample();
|
|
HighDiscountUserRelExample.Criteria criteria = example.createCriteria();
|
|
|
|
if (MapUtils.getInteger(map, "platform") != null) {
|
|
criteria.andDiscountPlatformEqualTo(MapUtils.getInteger(map, "platform"));
|
|
}
|
|
|
|
if (MapUtils.getLong(map, "userId") != null) {
|
|
criteria.andUserIdEqualTo(MapUtils.getLong(map, "userId"));
|
|
}
|
|
if (MapUtils.getLong(map, "agentId") != null) {
|
|
criteria.andAgentIdEqualTo(MapUtils.getLong(map, "agentId"));
|
|
}
|
|
if (MapUtils.getString(map, "discountName") != null) {
|
|
criteria.andDiscountNameLike("%" + MapUtils.getString(map, "discountName") + "%");
|
|
}
|
|
|
|
if (MapUtils.getInteger(map, "discountPlatform") != null) {
|
|
criteria.andDiscountPlatformEqualTo(MapUtils.getInteger(map, "discountPlatform"));
|
|
}
|
|
|
|
if (MapUtils.getInteger(map, "status") != null) {
|
|
criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
|
|
}
|
|
|
|
if (MapUtils.getString(map, "ext1") != null) {
|
|
criteria.andExt1EqualTo(MapUtils.getString(map, "ext1"));
|
|
}
|
|
|
|
if (MapUtils.getInteger(map, "discountUseScope") != null) {
|
|
criteria.andDiscountUseScopeEqualTo(MapUtils.getInteger(map, "discountUseScope"));
|
|
}
|
|
|
|
example.setOrderByClause("create_time desc");
|
|
return highDiscountUserRelMapper.selectByExample(example);
|
|
}
|
|
|
|
@Override
|
|
public List<HighDiscountUserRel> getExpiredDiscount() {
|
|
HighDiscountUserRelExample example = new HighDiscountUserRelExample();
|
|
example.createCriteria().andStatusEqualTo(1).andUseEndTimeLessThanOrEqualTo(new Date());
|
|
return highDiscountUserRelMapper.selectByExample(example);
|
|
}
|
|
|
|
@Override
|
|
public Integer receiveDiscountCount(Long userId, Long discountId) {
|
|
return highDiscountUserRelMapper.receiveDiscountCount(userId, discountId);
|
|
}
|
|
|
|
@Override
|
|
@Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
|
|
public void hzfUserDiscountUse(Long userDiscountId, String phone) {
|
|
HighDiscountUserRel discountUserRel = getRelById(userDiscountId);
|
|
if (discountUserRel == null) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券");
|
|
}
|
|
|
|
HighUser user = userService.findByUserId(discountUserRel.getUserId());
|
|
if (user == null){
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的用户");
|
|
}
|
|
if (!user.getPhone().equals(phone)) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "拥有人和使用人不一致");
|
|
}
|
|
|
|
discountUserRel.setUseTime(new Date()); // 使用时间
|
|
discountUserRel.setStatus(2); //状态 0:已过期 1:未使用 2:已使用
|
|
updateDiscountUserRel(discountUserRel);
|
|
|
|
HighDiscountAgentCode code = highDiscountAgentCodeService.getCodeById(discountUserRel.getDiscountAgentCodeId());
|
|
code.setStatus(3);
|
|
highDiscountAgentCodeService.updateCode(code);
|
|
|
|
}
|
|
|
|
@Override
|
|
@Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
|
|
public void hzfUserDiscountReturn(Long userDiscountId, String phone) {
|
|
HighDiscountUserRel discountUserRel = getRelById(userDiscountId);
|
|
if (discountUserRel == null) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券");
|
|
}
|
|
HighUser user = userService.findByUserId(discountUserRel.getUserId());
|
|
if (user == null){
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的用户");
|
|
}
|
|
if (!user.getPhone().equals(phone)) {
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "拥有人和使用人不一致");
|
|
}
|
|
|
|
discountUserRel.setUseTime(null); // 使用时间
|
|
discountUserRel.setStatus(1); //状态 0:已过期 1:未使用 2:已使用
|
|
updateDiscountUserRel(discountUserRel);
|
|
|
|
HighDiscountAgentCode code = highDiscountAgentCodeService.getCodeById(discountUserRel.getDiscountAgentCodeId());
|
|
code.setStatus(1);
|
|
highDiscountAgentCodeService.updateCode(code);
|
|
}
|
|
}
|
|
|