package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.common.code.FinishMethodEnum;
import com.mortals.xhx.common.code.MeetStatusEnum;
import com.mortals.xhx.module.metting.model.MettingRecordEntity;
import com.mortals.xhx.module.metting.model.MettingRecordQuery;
import com.mortals.xhx.module.metting.service.MettingRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 同步更新会议状态
 */
@Slf4j
@Service("SyncMeetStatusTask")
public class SyncMeetStatusTaskImpl implements ITaskExcuteService {

    @Autowired
    private MettingRecordService mettingRecordService;


    @Override
    public void excuteTask(ITask task) throws AppException {
        log.debug("同步更新会议状态");
        syncMeetStatus();
        log.debug("同步更新会议状态完成");

    }


    private void syncMeetStatus() {
        MettingRecordQuery mettingRecordQuery = new MettingRecordQuery();
        //mettingRecordQuery.setFinishMethod(FinishMethodEnum.自动结束.getValue());
        mettingRecordQuery.setMeetStatus(MeetStatusEnum.等待中.getValue());
        List<MettingRecordEntity> recordEntities = mettingRecordService.find(mettingRecordQuery);
        for (MettingRecordEntity recordEntity : recordEntities) {


            if(new Date().getTime()>recordEntity.getMeetTimeStart().getTime()){
                //更新状态
                recordEntity.setMeetStatus(MeetStatusEnum.进行中.getValue());
                recordEntity.setUpdateTime(new Date());
                recordEntity.setUpdateUserId(1L);
                mettingRecordService.update(recordEntity);
            }
/*
            //判断当前时间是否已经在区间范围内，如果在 更新状态
            boolean in = DateUtil.isIn(new Date(), recordEntity.getMeetTimeStart(), recordEntity.getMeetTimeEnd());
            if (in) {
                //更新状态
                recordEntity.setMeetStatus(MeetStatusEnum.进行中.getValue());
                recordEntity.setUpdateTime(new Date());
                recordEntity.setUpdateUserId(1L);
                mettingRecordService.update(recordEntity);
            }*/
        }

        mettingRecordQuery = new MettingRecordQuery();
        //mettingRecordQuery.setFinishMethod(FinishMethodEnum.自动结束.getValue());
        mettingRecordQuery.setMeetStatus(MeetStatusEnum.进行中.getValue());
        List<MettingRecordEntity> pendRecordEntities = mettingRecordService.find(mettingRecordQuery);
        for (MettingRecordEntity pendRecordEntity : pendRecordEntities) {
            boolean in = DateUtil.isIn(new Date(), pendRecordEntity.getMeetTimeStart(), pendRecordEntity.getMeetTimeEnd());
            if (!in) {
                //更新状态
                pendRecordEntity.setMeetStatus(MeetStatusEnum.已结束.getValue());
                pendRecordEntity.setFinishMethod(FinishMethodEnum.自动结束.getValue());
                pendRecordEntity.setUpdateTime(new Date());
                pendRecordEntity.setUpdateUserId(1L);
                mettingRecordService.update(pendRecordEntity);
            }
        }

    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }

}
