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.HighDiscount; import com.hai.entity.HighDiscountAgentRel; import com.hai.entity.HighDiscountUserRel; import com.hai.entity.HighDiscountUserRelExample; import com.hai.service.HighAgentService; import com.hai.service.HighDiscountAgentRelService; import com.hai.service.HighDiscountService; import com.hai.service.HighDiscountUserRelService; 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.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 HighDiscountService highDiscountService; @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void receiveDiscount(Long userId, Long discountAgentId) { // 查询优惠券信息 HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentId); if (rel == null || rel.getHighDiscount() == null || rel.getAgentId() == null){ throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_DISCOUNT, ""); } // 校验卡卷状态 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.setDiscountId(rel.getDiscountId()); userRel.setAgentId(rel.getAgentId()); userRel.setUserId(userId); userRel.setStatus(1); // 状态 0:已过期 1:未使用 2:已使用 userRel.setCreateTime(new Date()); // 计算使用有效期 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); } @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 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 getDiscountList(Map map) { HighDiscountUserRelExample example = new HighDiscountUserRelExample(); HighDiscountUserRelExample.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")); } example.setOrderByClause("create_time desc"); List list = highDiscountUserRelMapper.selectByExample(example); for (HighDiscountUserRel rel : list) { rel.setHighDiscount(highDiscountService.getDiscountById(rel.getDiscountId())); rel.setHighAgent(highAgentService.findByAgentMsgId(rel.getAgentId())); } return list; } @Override public List getExpiredDiscount() { HighDiscountUserRelExample example = new HighDiscountUserRelExample(); example.createCriteria().andStatusEqualTo(1).andUseEndTimeGreaterThanOrEqualTo(new Date()); return highDiscountUserRelMapper.selectByExample(example); } }