package com.yonge.cooleshow.biz.dal.service.impl;

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.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.vo.MemberPriceSettingsVo;
import com.yonge.cooleshow.biz.dal.vo.UserOrderDetailVo;
import com.yonge.cooleshow.biz.dal.wrapper.VipCardRecordWrapper;
import com.yonge.toolset.utils.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * 购买会员卡记录表
 * 2024-11-15 10:57:20
 */
@Slf4j
@Service
public class DiscountCardRecordServiceImpl extends ServiceImpl<DiscountCardRecordMapper, DiscountCardRecord> implements DiscountCardRecordService {

    @Autowired
    private MemberPriceSettingsService memberPriceSettingsService;

    @Autowired
    private StudentService studentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void orderSuccess(UserOrderDetailVo orderDetailVo) {

        if (null == orderDetailVo) {
            return;
        }
        MemberPriceSettingsVo detail = memberPriceSettingsService.detail(orderDetailVo.getBizId());
        if (!detail.getVipType().equals(EVipType.DISCOUNT)) {
            return;
        }


        DiscountCardRecord discountCardRecord = new DiscountCardRecord();
        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.setType(detail.getPeriod());
        discountCardRecord.setTimes(orderDetailVo.getGoodNum());
        discountCardRecord.setVipType(detail.getVipType());
        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);
        }
        discountCardRecord.setStartTime(DateUtil.localDateTimeToDate(startTime.atTime(LocalTime.MIN)));
        discountCardRecord.setEndTime(DateUtil.localDateTimeToDate(endTime.atTime(23,59,59)));

        this.save(discountCardRecord);

        // 更新学生时长
        studentService.lambdaUpdate()
                .set(Student::getDiscountEndTime, discountCardRecord.getEndTime())
                .set(student.getDiscountStartTime() == null,Student::getDiscountStartTime, discountCardRecord.getStartTime())
                .eq(Student::getUserId, orderDetailVo.getUserId())
                .update();

    }
}