package org.jeecg.modules.nettyServer.task;

import jtznyj.engineer.entity.AFocusWarnSection;
import jtznyj.engineer.mapper.AFoucusWarnSectionMapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.dataVue.entity.Ajh;
import org.jeecg.modules.dataVue.mapper.CustomMapper;
import org.jeecg.modules.dataVue.service.IAFocusWarnService;
import org.jeecg.modules.dataVue.service.ILtdSourceService;
import org.jeecg.modules.dataVue.service.IMasterService;
import org.jeecg.modules.dataVue.service.IZjyService;
import org.jeecg.modules.nettyServer.domain.SocketBody;
import org.jeecg.modules.nettyServer.service.PushMessageService;
import org.jeecg.modules.nettyServer.store.WellChannelStore;
import org.jeecg.modules.newWarning.service.RealTimeZjService;
import org.jeecg.modules.warning.entity.Ltd;
import org.jeecg.modules.warning.service.ILtdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.*;

/**
 * 共用一个库 不方便用框架自带定时任务
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Slf4j
public class SaticScheduleTask {
    @Resource
    CustomMapper customMapper;
    @Resource
    AFoucusWarnSectionMapper foucusWarnSectionMapper;
    @Resource
    PushMessageService pushMessageService;
    @Autowired
    ILtdSourceService iLtdSourceService;
    @Autowired
    IZjyService iZjyService;
    @Autowired
    ILtdService ltdService;
    @Autowired
    IMasterService iLogicService;
    @Autowired
    IAFocusWarnService iaFocusWarnService;
    @Autowired
    RealTimeZjService realTimeZjService;

    //单井数据任务
    @Scheduled(cron = "0/5 * * * * ?")
    private void SingleWellTasks() {


        for (String key : WellChannelStore.getWellMap().keySet()) {
            Ajh ajh = customMapper.loadAJh(key);
            if (ajh == null) {
                continue;
            }

            WellChannelStore.getWellMap().get(key);

            if (ajh.getLtd() == 1) {


                Map<String,Object> dateMap=new HashMap<>();
                String table = iLogicService.getTableCodeNew(ajh.getJH());
                dateMap.put("table",table);

                Ltd date = realTimeZjService.selectDate(dateMap);
                if (date==null){
                    continue;
                }
                List<Ltd> dataList = realTimeZjService.selectNewOne(table);
                if (dataList.size()>0){
                    Ltd data = dataList.get(0);
                    data.setTime(data.getTime().substring(0, 19));
                    SocketBody bodyNow = new SocketBody(SocketBody.type_nowWellData, data);
                    pushMessageService.pushMsgToOneWell(ajh.getJH(), bodyNow.toJson(), SocketBody.type_nowWellData);
                }

                date.setTime(date.getTime().substring(0, 19));

                SocketBody body = new SocketBody(SocketBody.type_oneWellData, date);


                pushMessageService.pushMsgToOneWell(ajh.getJH(), body.toJson(), SocketBody.type_oneWellData);

            } else if (ajh.getZjy() == 1) {
                String table = String.format(" ZJYCS_%s ", ajh.getJH().replace('-', '_'));
                String sql = " select *\n" +
                        "  from %s t\n" +
                        " where sj = (select max(sj) from %s)";
                sql = String.format(sql, table, table);
                List<Map<String, Object>> list = iZjyService.querySql(sql, null);
                if (list.size() < 1) {
                    continue;
                }
                SocketBody body = new SocketBody(SocketBody.type_oneWellData, list.get(0));
                SocketBody bodyNow = new SocketBody(SocketBody.type_nowWellData, list.get(0));
                pushMessageService.pushMsgToOneWell(ajh.getJH(), body.toJson(), SocketBody.type_oneWellData);
                pushMessageService.pushMsgToOneWell(ajh.getJH(), bodyNow.toJson(), SocketBody.type_nowWellData);
            }
        }
    }

    //预警数据任务  30秒一次
    @Scheduled(cron = "0/30 * * * * ?")
    private void warningTask() {
        int minutesBefore = 2;
        String newWarnSql = "select * from a_focus_warn where create_time > to_date(%s, 'yyyy-MM-dd hh24:mi:ss') ";
        newWarnSql = String.format(newWarnSql,
                DateUtils.date2Str(org.apache.commons.lang.time.DateUtils.addMinutes(new Date(), minutesBefore), DateUtils.yyyymmddhhmmss.get()));
        List<Map<String, Object>> list = iaFocusWarnService.querySql(newWarnSql, null);

        SocketBody allWarnBody = new SocketBody(SocketBody.type_moreWellWarningData, list);
        pushMessageService.pushMsgToAll(allWarnBody.toJson());
    }

    //单井数据任务  3分钟一次
    @Scheduled(cron = "0 0/3 * * * ?")
    private void wellWarnTask() {
        int minutesBefore = 2;
        String newWarnSql = "select * from A_FOCUS_WARN_SECTION where created > to_date(%s, 'yyyy-MM-dd hh24:mi:ss') " +
                " order by catogary, start_time ";
        newWarnSql = String.format(newWarnSql,
                DateUtils.date2Str(org.apache.commons.lang.time.DateUtils.addMinutes(new Date(), minutesBefore), DateUtils.yyyymmddhhmmss.get()));
        List<AFocusWarnSection> list = foucusWarnSectionMapper.getListBySql(newWarnSql);

        for (String key : WellChannelStore.getWellMap().keySet()) {
            Ajh ajh = customMapper.loadAJh(key);
            if (ajh==null || ajh.getLtd() != 1){
                continue;
            }
            List<AFocusWarnSection> wellWarnList = new ArrayList<>();
            for(AFocusWarnSection item: list){
                if(item.getJh().equals(key)){
                    wellWarnList.add(item);
                }
            }
            SocketBody body = new SocketBody(SocketBody.type_oneWellWarningData, wellWarnList);
            pushMessageService.pushMsgToOneWell(key,body.toJson(),SocketBody.type_oneWellWarningData);
        }
    }
}
