package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ICacheService;
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.common.code.YesNoEnum;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.common.pdu.UploadDeviceReq;
import com.mortals.xhx.feign.device.IDeviceMessageFeign;
import com.mortals.xhx.module.basic.model.BasicSetEntity;
import com.mortals.xhx.module.basic.service.BasicSetService;
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 org.springframework.util.ObjectUtils;

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

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

    @Autowired
    private MettingRecordService mettingRecordService;

    @Autowired
    private IDeviceMessageFeign messageFeign;

    @Autowired
    private BasicSetService basicSetService;
    @Autowired
    private ICacheService cacheService;


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

    }


    private void syncMeetStatus() {
        BasicSetEntity basicSetEntity = basicSetService.selectOne(new BasicSetEntity());
        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);
            }
        }

        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);
                //判断基础是否设置关闭切换通知，有则推送
                if (!ObjectUtils.isEmpty(basicSetEntity.getMeetEnd()) && basicSetEntity.getMeetEnd() == YesNoEnum.YES.getValue()) {
                    //结束通知 通过redis 去重复
                    long sub = DateUtil.between(pendRecordEntity.getMeetTimeStart(), pendRecordEntity.getMeetTimeEnd(), DateUnit.SECOND, true);
                    boolean setnx = cacheService.setnx(RedisKey.KEY_METTING_END_CACHE, pendRecordEntity.getId(), sub);
                    if (setnx) {
                        UploadDeviceReq uploadDeviceReq = new UploadDeviceReq();
                        uploadDeviceReq.setDeviceCodeList(Arrays.asList(pendRecordEntity.getDeviceCode()));
                        uploadDeviceReq.setAction("postMeet");
                        messageFeign.downMsg(uploadDeviceReq);
                    }
                }

            }
        }

    }


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

    }

}
