package vip.xiaonuo.biz.modular.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
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 AppointmentCallTask implements CommonTimerTaskRunner {

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

    @Override
    public void action(String extJson) {
        String format = DateUtil.format(DateUtil.date(), "HH:mm");
        //查询当前时间点属于时间段
        BizAppointmentTime bizAppointmentTime = bizAppointmentTimeService.getOne(new QueryWrapper<BizAppointmentTime>().lambda().
                le(BizAppointmentTime::getBeginTime,format).
                gt(BizAppointmentTime::getFinishTime,format).
                last("limit 1"));
        if(ObjectUtil.isNotNull(bizAppointmentTime)){
            //根据时间段查询排队中的预约记录
            BizAppointmentRecord record = bizAppointmentRecordService.getOne(new QueryWrapper<BizAppointmentRecord>().lambda().
                    eq(BizAppointmentRecord::getTimeId, bizAppointmentTime.getId()).
                    eq(BizAppointmentRecord::getStatus,"3").
                    orderByAsc(BizAppointmentRecord::getCreateTime).
                    last("limit 1"));
            if(ObjectUtil.isNotNull(record)){
                record.setStatus("4");
                bizAppointmentRecordService.updateById(record);
                log.info("车牌号："+record.getLicenseNumber()+"在("+bizAppointmentTime.getBeginTime()+"~"+bizAppointmentTime.getEndTime()+")时间段内叫号成功！");
            }
        }
    }
}
