package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
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.key.RedisKey;
import com.mortals.xhx.common.keys.RedisCacheKeys;
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 com.mortals.xhx.module.room.model.RoomDeviceQuery;
import com.mortals.xhx.module.room.model.RoomEntity;
import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
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;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 会议室切换状态
 */
@Slf4j
@Service("MeetSwitchTaskTask")
public class MeetSwitchTaskImpl implements ITaskExcuteService {

    @Autowired
    private MettingRecordService mettingRecordService;

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

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.debug("会议室切换状态");

        BasicSetEntity basicSetEntity = basicSetService.selectOne(new BasicSetEntity());
        if (!ObjectUtils.isEmpty(basicSetEntity)) {
            log.info("基础未配置");
            return;
        }

        swicthMeet(basicSetEntity);
        log.debug("会议室切换状态完成");

    }


    private void swicthMeet(BasicSetEntity basicSetEntity) {
        if (!ObjectUtils.isEmpty(basicSetEntity.getPreMeetStart()) && basicSetEntity.getPreMeetStart() > 0) {
            //会议开始前几分钟 通知设备进行数据更新
            MettingRecordQuery mettingRecordQuery = new MettingRecordQuery();
            //mettingRecordQuery.setFinishMethod(FinishMethodEnum.自动结束.getValue());
            mettingRecordQuery.setMeetStatus(MeetStatusEnum.等待中.getValue());
            List<MettingRecordEntity> recordEntities = mettingRecordService.find(mettingRecordQuery);
            for (MettingRecordEntity recordEntity : recordEntities) {
                long between = DateUtil.between(new Date(), recordEntity.getMeetTimeStart(), DateUnit.MINUTE);
                if (between <= basicSetEntity.getPreMeetStart()) {
                    //通知设备进行数据更新
                    boolean setnx = cacheService.setnx(RedisKey.KEY_METTING_START_CACHE, recordEntity.getId(), between * 60);
                    if (setnx) {
                        UploadDeviceReq uploadDeviceReq = new UploadDeviceReq();
                        uploadDeviceReq.setDeviceCodeList(Arrays.asList(recordEntity.getDeviceCode()));
                        uploadDeviceReq.setAction("preMeetStart");
                        uploadDeviceReq.setContent(JSON.toJSONString(recordEntity));
                        messageFeign.downMsg(uploadDeviceReq);
                    }
                }

            }

            if (!ObjectUtils.isEmpty(basicSetEntity.getPostMeetStart()) && basicSetEntity.getPostMeetStart() > 0) {
                mettingRecordQuery = new MettingRecordQuery();
                mettingRecordQuery.setMeetStatus(MeetStatusEnum.进行中.getValue());
                List<MettingRecordEntity> pendRecordEntities = mettingRecordService.find(mettingRecordQuery);
                for (MettingRecordEntity pendRecordEntity : pendRecordEntities) {
                    long between = DateUtil.between(pendRecordEntity.getMeetTimeStart(), new Date(), DateUnit.MINUTE);
                    if (between >= basicSetEntity.getPostMeetStart()) {
                        //结束通知 通过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 {

    }

    public static void main(String[] args) {


        long between = DateUtil.between(new Date(), DateUtil.offsetMinute(new Date(), 5), DateUnit.MINUTE);

        System.out.println(between);

    }

}
