package vip.xiaonuo.biz.modular.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.biz.modular.bizappointmentrecord.entity.BizAppointmentRecord;
import vip.xiaonuo.biz.modular.bizappointmentrecord.service.BizAppointmentRecordService;
import vip.xiaonuo.biz.modular.bizappointmenttime.entity.BizAppointmentTime;
import vip.xiaonuo.biz.modular.bizappointmenttime.service.BizAppointmentTimeService;
import vip.xiaonuo.common.timer.CommonTimerTaskRunner;

import java.util.List;

@Slf4j
@Component
public class AppointmentTimeOutTask implements CommonTimerTaskRunner {

    @Resource
    private BizAppointmentTimeService bizAppointmentTimeService;
    @Resource
    private BizAppointmentRecordService bizAppointmentRecordService;

    @Override
    public void action(String extJson) {
        String format = DateUtil.format(DateUtil.date(), "HH:mm");
        //查询当前时间点之前的预约时段
        List<BizAppointmentTime> timeList = bizAppointmentTimeService.list(new QueryWrapper<BizAppointmentTime>().lambda().
                lt(BizAppointmentTime::getFinishTime, format));
        for(BizAppointmentTime bizAppointmentTime : timeList){
            //根据时间段查询还未入场预约记录
            List<BizAppointmentRecord> recordList = bizAppointmentRecordService.list(new QueryWrapper<BizAppointmentRecord>().lambda().
                    eq(BizAppointmentRecord::getTimeId, bizAppointmentTime.getId()).
                    in(BizAppointmentRecord::getStatus,"1","2","3","4"));
            for(BizAppointmentRecord bizAppointmentRecord : recordList){
                //超期作废
                bizAppointmentRecord.setStatus("15");
                bizAppointmentRecordService.updateById(bizAppointmentRecord);
                log.info("车牌号："+bizAppointmentRecord.getLicenseNumber()+"的预约记录超期作废成功！");
            }
        }
    }
}
