package io.hmit.modules.job.task;

import io.hmit.common.constant.Constant;
import io.hmit.common.utils.DateUtils;
import io.hmit.modules.appointment.service.AppointmentOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 检查是否超出预约时间还未取号
 * 是 则将订单状态status改成0 爽约/失约
 *
 */
@Component("checkReservationTime")
public class CheckReservationTimeTask implements ITask {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AppointmentOrderService appointmentOrderService;

    @Override
    public void run(String params) {

        logger.debug("TestTask定时任务正在执行，参数为：{}", params);
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("status", Constant.ReservationStatus.RESERVATION.getValue());
        appointmentOrderService.list(queryParams).stream().forEach(item->{
            System.out.println("订单状态："+item.getStatus()+" "+item.getServiceName());
            try {
                if(DateUtils.compareTime(item.getAppointmentTime())){
                    item.setStatus(Constant.ReservationStatus.BREAK_APPOINTMENT.getValue());
                    item.setStatusName(Constant.ReservationStatus.BREAK_APPOINTMENT.getMsg());
                    item.setUpdateDate(new Date());
                    appointmentOrderService.update(item);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });

    }
}
