|  | @@ -1,31 +1,32 @@
 | 
	
		
			
				|  |  |  package com.yonge.cooleshow.biz.dal.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import cn.hutool.core.thread.ThreadUtil;
 | 
	
		
			
				|  |  | +import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  |  import com.microsvc.toolkit.common.webportal.exception.BizException;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.entity.DiscountCardRecord;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.entity.Student;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.enums.EVipRecordStatus;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.enums.EVipType;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.enums.SourceTypeEnum;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.entity.SysUser;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.enums.*;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.mapper.DiscountCardRecordMapper;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.service.DiscountCardRecordService;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.service.MemberPriceSettingsService;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.service.StudentService;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.service.VipCardRecordService;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.service.*;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.vo.MemberPriceSettingsVo;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.vo.UserOrderDetailVo;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.wrapper.VipCardRecordWrapper;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.wrapper.DiscountCardRecordWrapper;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.thirdparty.message.MessageSenderPluginContext;
 | 
	
		
			
				|  |  |  import com.yonge.toolset.utils.date.DateUtil;
 | 
	
		
			
				|  |  |  import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  | +import org.apache.commons.collections.CollectionUtils;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import java.time.LocalDate;
 | 
	
		
			
				|  |  |  import java.time.LocalDateTime;
 | 
	
		
			
				|  |  |  import java.time.LocalTime;
 | 
	
		
			
				|  |  |  import java.time.ZoneId;
 | 
	
		
			
				|  |  | -import java.util.Date;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  | +import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * 购买会员卡记录表
 | 
	
	
		
			
				|  | @@ -41,6 +42,12 @@ public class DiscountCardRecordServiceImpl extends ServiceImpl<DiscountCardRecor
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private StudentService studentService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SysUserService sysUserService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SysMessageService sysMessageService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |      public void orderSuccess(UserOrderDetailVo orderDetailVo) {
 | 
	
	
		
			
				|  | @@ -53,64 +60,277 @@ public class DiscountCardRecordServiceImpl extends ServiceImpl<DiscountCardRecor
 | 
	
		
			
				|  |  |              return;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        DiscountCardRecord discountCardRecord = new DiscountCardRecord();
 | 
	
		
			
				|  |  | +        DiscountCardRecordWrapper.DiscountCardRecordChange discountCardRecord = new DiscountCardRecordWrapper.DiscountCardRecordChange();
 | 
	
		
			
				|  |  |          discountCardRecord.setUserId(orderDetailVo.getUserId());
 | 
	
		
			
				|  |  |          discountCardRecord.setVipCardId(orderDetailVo.getBizId());
 | 
	
		
			
				|  |  |          discountCardRecord.setOrderNo(orderDetailVo.getOrderNo());
 | 
	
		
			
				|  |  |          discountCardRecord.setSourceType(SourceTypeEnum.ORDER);
 | 
	
		
			
				|  |  |          discountCardRecord.setClientType(orderDetailVo.getOrderClient());
 | 
	
		
			
				|  |  |          discountCardRecord.setSubOrderNo(orderDetailVo.getSubOrderNo());
 | 
	
		
			
				|  |  | -        discountCardRecord.setCreateBy(orderDetailVo.getUserId());
 | 
	
		
			
				|  |  | +//        discountCardRecord.setCreateBy(orderDetailVo.getUserId());
 | 
	
		
			
				|  |  |          discountCardRecord.setType(detail.getPeriod());
 | 
	
		
			
				|  |  |          discountCardRecord.setTimes(orderDetailVo.getGoodNum());
 | 
	
		
			
				|  |  | -        discountCardRecord.setVipType(detail.getVipType());
 | 
	
		
			
				|  |  | +        discountCardRecord.setStatus(EVipRecordStatus.ADD);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        addStudentDiscount(discountCardRecord);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private List<DiscountCardRecord> usedDiscountCardRecordList(Long userId) {
 | 
	
		
			
				|  |  | +        return this.lambdaQuery()
 | 
	
		
			
				|  |  | +                .eq(DiscountCardRecord::getUserId, userId)
 | 
	
		
			
				|  |  | +                .eq(DiscountCardRecord::getEfficientFlag, 1)
 | 
	
		
			
				|  |  | +                .gt(DiscountCardRecord::getEndTime, new Date())
 | 
	
		
			
				|  |  | +                .list();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void addStudentVip(DiscountCardRecordWrapper.DiscountCardRecord result) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 扣减
 | 
	
		
			
				|  |  | +        if (result.getStatus() == EVipRecordStatus.DEDUCTION) {
 | 
	
		
			
				|  |  | +            result.setSourceType(SourceTypeEnum.PLATFORM_DEDUCT);
 | 
	
		
			
				|  |  | +            deductionStudentDiscount(result);
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            result.setSourceType(SourceTypeEnum.BACKEND_GIVE);
 | 
	
		
			
				|  |  | +            addStudentDiscount(JSON.parseObject(JSON.toJSONString(result), DiscountCardRecordWrapper.DiscountCardRecordChange.class));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (Boolean.TRUE.equals(result.getSendMsg())) {
 | 
	
		
			
				|  |  | +            ThreadUtil.execute(() -> sendVipDeductionMessage(result));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void sendVipDeductionMessage(DiscountCardRecordWrapper.DiscountCardRecord result) {
 | 
	
		
			
				|  |  | +        // 添加VIP时长短信
 | 
	
		
			
				|  |  | +        com.yonge.cooleshow.auth.api.entity.SysUser sysUser = sysUserService.getByUserId(result.getUserId());
 | 
	
		
			
				|  |  | +        if (sysUser == null) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<Long, String> receivers = new HashMap<>();
 | 
	
		
			
				|  |  | +        receivers.put(sysUser.getId(), sysUser.getPhone());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 短信
 | 
	
		
			
				|  |  | +        MessageTypeEnum messageTypeEnum = MessageTypeEnum.ADD_DISCOUNT_SMS;
 | 
	
		
			
				|  |  | +        if (result.getStatus() == EVipRecordStatus.DEDUCTION) {
 | 
	
		
			
				|  |  | +            messageTypeEnum = MessageTypeEnum.DEDUCTION_DISCOUNT_SMS;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.AWSMS, messageTypeEnum,receivers,
 | 
	
		
			
				|  |  | +                    null,0,null,ClientEnum.STUDENT.name()
 | 
	
		
			
				|  |  | +                    ,result.getTimes() + result.getType().name(),result.getReason());
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            log.error("消息发送失败 ", e);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public long deductionStudentDiscount(DiscountCardRecordWrapper.DiscountCardRecord result) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<DiscountCardRecord> discountCardRecords = usedDiscountCardRecordList(result.getUserId());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 最后匹配的激活记录
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 查询最大有效时间
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isEmpty(discountCardRecords)) {
 | 
	
		
			
				|  |  | +            throw new BizException("会员时长不足");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 根据结束时间倒序
 | 
	
		
			
				|  |  | +        discountCardRecords.sort(Comparator.comparing(DiscountCardRecord::getEndTime).reversed());
 | 
	
		
			
				|  |  | +        DiscountCardRecord record = discountCardRecords.stream().max(Comparator.comparing(DiscountCardRecord::getEndTime)).get();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Date membershipEndTime =  record.getEndTime();
 | 
	
		
			
				|  |  | +        membershipEndTime = addTime(membershipEndTime, result.getType(), result.getTimes(), 1);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        DiscountCardRecord startVipPurchaseRecord = null;
 | 
	
		
			
				|  |  | +        for (DiscountCardRecord vipPurchaseRecord : discountCardRecords) {
 | 
	
		
			
				|  |  | +            vipPurchaseRecord.setEfficientFlag(false);
 | 
	
		
			
				|  |  | +            if (!DateUtil.addSeconds(membershipEndTime,1).before(vipPurchaseRecord.getStartTime())) {
 | 
	
		
			
				|  |  | +                if (DateUtil.addDays(membershipEndTime,1).after(new Date())) {
 | 
	
		
			
				|  |  | +                    // 会员匹配完
 | 
	
		
			
				|  |  | +                    startVipPurchaseRecord = JSON.parseObject(JSON.toJSONString(vipPurchaseRecord), DiscountCardRecord.class);
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (startVipPurchaseRecord == null) {
 | 
	
		
			
				|  |  | +            throw new BizException("会员时长不足");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        List<DiscountCardRecord> saveList = new ArrayList<>();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setEfficientFlag(true);
 | 
	
		
			
				|  |  | +        long id = IdWorker.getId();
 | 
	
		
			
				|  |  | +        if (startVipPurchaseRecord.getStartTime().compareTo(membershipEndTime)>0) {
 | 
	
		
			
				|  |  | +            membershipEndTime = startVipPurchaseRecord.getStartTime();
 | 
	
		
			
				|  |  | +            startVipPurchaseRecord.setEfficientFlag(false);
 | 
	
		
			
				|  |  | +            startVipPurchaseRecord.setRefId(id);
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            startVipPurchaseRecord.setRefId(null);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setVipType(EVipType.DISCOUNT);
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setId(id);
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setEndTime(membershipEndTime);
 | 
	
		
			
				|  |  | +//        startVipPurchaseRecord.setVipCardId(-1L);
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setOrderNo("");
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setTimes(result.getTimes());
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setMsgStatus(0);
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setType(result.getType());
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setUpdateTime(null);
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setStatus(EVipRecordStatus.DEDUCTION);
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setCreateTime(null);
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setCreateBy(result.getCreateBy());
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setDisplayFlag(true);
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setReason(result.getReason());
 | 
	
		
			
				|  |  | +        startVipPurchaseRecord.setSendMsg(result.getSendMsg());
 | 
	
		
			
				|  |  | +        saveList.add(startVipPurchaseRecord);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        for (DiscountCardRecord cardRecord : discountCardRecords) {
 | 
	
		
			
				|  |  | +            if (Boolean.FALSE.equals(cardRecord.getEfficientFlag())) {
 | 
	
		
			
				|  |  | +                DiscountCardRecord discountCardRecord = new DiscountCardRecord();
 | 
	
		
			
				|  |  | +                discountCardRecord.setId(cardRecord.getId());
 | 
	
		
			
				|  |  | +                discountCardRecord.setRefId(startVipPurchaseRecord.getId());
 | 
	
		
			
				|  |  | +                discountCardRecord.setEfficientFlag(false);
 | 
	
		
			
				|  |  | +                saveList.add(discountCardRecord);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        saveOrUpdateBatch(saveList);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return id;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Long addStudentDiscount(DiscountCardRecordWrapper.DiscountCardRecordChange result) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        DiscountCardRecord discountCardRecord = new DiscountCardRecord();
 | 
	
		
			
				|  |  | +        discountCardRecord.setUserId(result.getUserId());
 | 
	
		
			
				|  |  | +        discountCardRecord.setVipCardId(result.getVipCardId());
 | 
	
		
			
				|  |  | +        discountCardRecord.setOrderNo(result.getOrderNo());
 | 
	
		
			
				|  |  | +        discountCardRecord.setSourceType(result.getSourceType());
 | 
	
		
			
				|  |  | +        discountCardRecord.setClientType(ClientEnum.STUDENT);
 | 
	
		
			
				|  |  | +        discountCardRecord.setSubOrderNo(result.getSubOrderNo());
 | 
	
		
			
				|  |  | +        discountCardRecord.setCreateBy(result.getCreateBy());
 | 
	
		
			
				|  |  | +        discountCardRecord.setType(result.getType());
 | 
	
		
			
				|  |  | +        discountCardRecord.setTimes(result.getTimes());
 | 
	
		
			
				|  |  | +        discountCardRecord.setReason(result.getReason());
 | 
	
		
			
				|  |  | +        discountCardRecord.setVipType(EVipType.DISCOUNT);
 | 
	
		
			
				|  |  |          discountCardRecord.setStatus(EVipRecordStatus.ADD);
 | 
	
		
			
				|  |  |          discountCardRecord.setDisplayFlag(true);
 | 
	
		
			
				|  |  |          discountCardRecord.setEfficientFlag(true);
 | 
	
		
			
				|  |  | -        discountCardRecord.setSendMsg(false);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        // 设置时间
 | 
	
		
			
				|  |  | -        Student student = studentService.getById(orderDetailVo.getUserId());
 | 
	
		
			
				|  |  | -        LocalDate startTime = LocalDate.now();
 | 
	
		
			
				|  |  | -        if (student.getDiscountEndTime() != null && student.getDiscountEndTime().after(new Date())) {
 | 
	
		
			
				|  |  | -            startTime = student.getDiscountEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        LocalDate endTime = LocalDate.now();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        switch (discountCardRecord.getType()) {
 | 
	
		
			
				|  |  | -            case DAY:
 | 
	
		
			
				|  |  | -                endTime = startTime.plusDays(discountCardRecord.getTimes());
 | 
	
		
			
				|  |  | -                break;
 | 
	
		
			
				|  |  | -            case MONTH:
 | 
	
		
			
				|  |  | -                endTime = startTime.plusMonths(discountCardRecord.getTimes());
 | 
	
		
			
				|  |  | -                break;
 | 
	
		
			
				|  |  | -            case QUARTERLY:
 | 
	
		
			
				|  |  | -                endTime = startTime.plusMonths(discountCardRecord.getTimes() * 3L);
 | 
	
		
			
				|  |  | -                break;
 | 
	
		
			
				|  |  | -            case YEAR_HALF:
 | 
	
		
			
				|  |  | -                endTime = startTime.plusMonths(discountCardRecord.getTimes() * 6L);
 | 
	
		
			
				|  |  | -                break;
 | 
	
		
			
				|  |  | -            case YEAR:
 | 
	
		
			
				|  |  | -                endTime = startTime.plusYears(discountCardRecord.getTimes());
 | 
	
		
			
				|  |  | -                break;
 | 
	
		
			
				|  |  | -            default:
 | 
	
		
			
				|  |  | -                log.error("时间类型错误{}", discountCardRecord.getType());
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if (startTime.isAfter(LocalDate.now())) {
 | 
	
		
			
				|  |  | -            startTime = startTime.plusDays(1);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 设置开始时间
 | 
	
		
			
				|  |  | +        List<DiscountCardRecord> discountCardRecords = usedDiscountCardRecordList(result.getUserId());
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isNotEmpty(discountCardRecords)) {
 | 
	
		
			
				|  |  | +            discountCardRecord.setStartTime(discountCardRecords.stream().max(Comparator.comparing(DiscountCardRecord::getEndTime)).get().getEndTime());
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            discountCardRecord.setStartTime(new Date());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        discountCardRecord.setStartTime(DateUtil.localDateTimeToDate(startTime.atTime(LocalTime.MIN)));
 | 
	
		
			
				|  |  | -        discountCardRecord.setEndTime(DateUtil.localDateTimeToDate(endTime.atTime(23,59,59)));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        this.save(discountCardRecord);
 | 
	
		
			
				|  |  | +        discountCardRecord.setEndTime(addTime(discountCardRecord.getStartTime(), discountCardRecord.getType(), discountCardRecord.getTimes(),1));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        this.save(formatVipRecordTime(discountCardRecord));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // 更新学生时长
 | 
	
		
			
				|  |  | -        studentService.lambdaUpdate()
 | 
	
		
			
				|  |  | -                .set(Student::getDiscountEndTime, discountCardRecord.getEndTime())
 | 
	
		
			
				|  |  | -                .set(student.getDiscountStartTime() == null,Student::getDiscountStartTime, discountCardRecord.getStartTime())
 | 
	
		
			
				|  |  | -                .eq(Student::getUserId, orderDetailVo.getUserId())
 | 
	
		
			
				|  |  | -                .update();
 | 
	
		
			
				|  |  | +        baseMapper.updateUserDiscountTime(result.getUserId(), discountCardRecord.getStartTime(), discountCardRecord.getEndTime());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return discountCardRecord.getId();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public IPage<DiscountCardRecordWrapper.DiscountCardRecord> selectPage(IPage<DiscountCardRecordWrapper.DiscountCardRecord> page, DiscountCardRecordWrapper.DiscountCardRecordQuery query) {
 | 
	
		
			
				|  |  | +        IPage<DiscountCardRecordWrapper.DiscountCardRecord> discountCardRecordIPage = baseMapper.selectPage(page, query);
 | 
	
		
			
				|  |  | +        List<DiscountCardRecordWrapper.DiscountCardRecord> records = discountCardRecordIPage.getRecords();
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isEmpty(records)) {
 | 
	
		
			
				|  |  | +            return discountCardRecordIPage;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // 创建人ID集合
 | 
	
		
			
				|  |  | +        List<Long> createByList = records.stream()
 | 
	
		
			
				|  |  | +                .map(DiscountCardRecordWrapper.DiscountCardRecord::getCreateBy).filter(Objects::nonNull).distinct().collect(Collectors.toList());
 | 
	
		
			
				|  |  | +        Map<Long, SysUser> userMap = sysUserService.getMapByIds(createByList);
 | 
	
		
			
				|  |  | +        records.forEach(discountCardRecord -> {
 | 
	
		
			
				|  |  | +            SysUser sysUser = userMap.get(discountCardRecord.getCreateBy());
 | 
	
		
			
				|  |  | +            if (sysUser != null) {
 | 
	
		
			
				|  |  | +                discountCardRecord.setOperatorName(sysUser.getRealName());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        return discountCardRecordIPage;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 获取剩余时长
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public DiscountCardRecordWrapper.Info info(DiscountCardRecordWrapper.InfoQuery infoQuery) {
 | 
	
		
			
				|  |  | +        DiscountCardRecordWrapper.Info info = new DiscountCardRecordWrapper.Info();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<DiscountCardRecord> discountCardRecords = usedDiscountCardRecordList(infoQuery.getUserId());
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isEmpty(discountCardRecords)) {
 | 
	
		
			
				|  |  | +            return info;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 最大时间
 | 
	
		
			
				|  |  | +        DiscountCardRecord discountCardRecord = discountCardRecords.stream().max(Comparator.comparing(DiscountCardRecord::getEndTime)).get();
 | 
	
		
			
				|  |  | +        info.setSurplusDay(DateUtil.daysBetweenUp(new Date(), discountCardRecord.getEndTime()));
 | 
	
		
			
				|  |  | +        return info;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Date addTime(Date time, PeriodEnum period, Integer times, int type) {
 | 
	
		
			
				|  |  | +        Calendar cal = Calendar.getInstance();
 | 
	
		
			
				|  |  | +        cal.setTime(time);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (PeriodEnum.DAY.equals(period)) {
 | 
	
		
			
				|  |  | +            cal.add(Calendar.DAY_OF_MONTH, times);
 | 
	
		
			
				|  |  | +        } else if (PeriodEnum.MONTH.equals(period)) {
 | 
	
		
			
				|  |  | +            cal.add(Calendar.MONTH, times);
 | 
	
		
			
				|  |  | +        } else if (PeriodEnum.YEAR.equals(period)) {
 | 
	
		
			
				|  |  | +            cal.add(Calendar.YEAR, times);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (type ==0) {
 | 
	
		
			
				|  |  | +            cal.set(Calendar.HOUR_OF_DAY, 0);
 | 
	
		
			
				|  |  | +            cal.set(Calendar.MINUTE, 0);
 | 
	
		
			
				|  |  | +            cal.set(Calendar.SECOND, 0);
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            cal.set(Calendar.HOUR_OF_DAY, 23);
 | 
	
		
			
				|  |  | +            cal.set(Calendar.MINUTE, 59);
 | 
	
		
			
				|  |  | +            cal.set(Calendar.SECOND, 59);
 | 
	
		
			
				|  |  | +            cal.set(Calendar.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        cal.set(Calendar.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        return cal.getTime();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public DiscountCardRecord formatVipRecordTime(DiscountCardRecord discountCardRecord) {
 | 
	
		
			
				|  |  | +        // 如果开始时间是23:59:59,开始时间改为第二天的00:00:00
 | 
	
		
			
				|  |  | +        LocalDateTime startTime = discountCardRecord.getStartTime().toInstant()
 | 
	
		
			
				|  |  | +                .atZone(ZoneId.systemDefault()).toLocalDateTime();
 | 
	
		
			
				|  |  | +        if (startTime.toLocalTime().equals(LocalTime.of(23, 59, 59))) {
 | 
	
		
			
				|  |  | +            startTime = startTime.plusDays(1);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        startTime = startTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
 | 
	
		
			
				|  |  | +        // 如果结束时间是00:00:00,开始时间改为前一天的23:59:59
 | 
	
		
			
				|  |  | +        LocalDateTime endTime = discountCardRecord.getEndTime().toInstant()
 | 
	
		
			
				|  |  | +                .atZone(ZoneId.systemDefault()).toLocalDateTime();
 | 
	
		
			
				|  |  | +        if (endTime.toLocalTime().equals(LocalTime.of(0, 0, 0))) {
 | 
	
		
			
				|  |  | +            endTime = endTime.plusDays(-1);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        endTime = endTime.withHour(23).withMinute(59).withSecond(59).withNano(0);
 | 
	
		
			
				|  |  | +        discountCardRecord.setStartTime(Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()));
 | 
	
		
			
				|  |  | +        discountCardRecord.setEndTime(Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant()));
 | 
	
		
			
				|  |  | +        if (discountCardRecord.getStartTime().after(discountCardRecord.getEndTime())) {
 | 
	
		
			
				|  |  | +            discountCardRecord.setEndTime(discountCardRecord.getStartTime());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return discountCardRecord;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |