package com.zcloud.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zcloud.common.utils.DateUtil;
import com.zcloud.modules.alarmInfo.entity.AlarmInfoEntity;
import com.zcloud.modules.alarmInfo.service.AlarmInfoService;
import com.zcloud.modules.anemometerMachine.entity.AnemometerMachineEntity;
import com.zcloud.modules.anemometerMachine.service.AnemometerMachineService;
import com.zcloud.modules.anemometerRecord.entity.AnemometerRecordEntity;
import com.zcloud.modules.anemometerRecord.mapper.AnemometerRecordMapper;
import com.zcloud.modules.job.task.ITask;
import com.zcloud.modules.sys.entity.PageData;
import com.zcloud.util.HttpClientUtil;
import com.zcloud.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("aemometerTask")
public class AnemometerTask implements ITask {
    @Autowired
    private AnemometerMachineService anemometerMachineService;
    @Autowired
    private AnemometerRecordMapper anemometerRecordMapper;
    @Autowired
    private AlarmInfoService alarmInfoService;
    static String anemometerUrl = "http://192.168.210.58:8076/zk-ht/third/outNet/getUnloadShipWindList";

    @Override
    public void run(String params) {
        Map map = HttpClientUtil.doPost(anemometerUrl, new HashMap<>());
        ArrayList<Map<String, String>> list = (ArrayList<Map<String, String>>) map.get("list");
        LambdaQueryWrapper<AnemometerMachineEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AnemometerMachineEntity::getCorpinfoId,"8854edee3aa94be496cee676b6d4845a");
        List<AnemometerMachineEntity> dataList = anemometerMachineService.list(queryWrapper);
        List<AnemometerMachineEntity> addList = new ArrayList<>();
        List<AnemometerMachineEntity> updateList = new ArrayList<>();
        List<AnemometerRecordEntity> recordList = new ArrayList<>();
        for (Map<String, String> data : list) {
            boolean flag = false;
            if (dataList.size() > 0) {
                for (AnemometerMachineEntity machine : dataList) {
                    if (machine.getAnemometerName().equals(data.get("machine"))) {
                        AnemometerRecordEntity anemometerRecordEntity = new AnemometerRecordEntity();
                        anemometerRecordEntity.setAnemometerId(machine.getAnemometerId());
                        anemometerRecordEntity.setAnemometerRecordId(UuidUtil.get32UUID());
                        anemometerRecordEntity.setCREATOR("5c61afed41bc4c25acf77aed1391e666");
                        anemometerRecordEntity.setCREATTIME(DateUtil.date2Str(new Date()));
                        anemometerRecordEntity.setOPERATOR("5c61afed41bc4c25acf77aed1391e666");
                        anemometerRecordEntity.setOPERATTIME(DateUtil.date2Str(new Date()));
                        anemometerRecordEntity.setVALUE(String.valueOf(data.get("data")));
                        anemometerRecordEntity.setDATE(data.get("fdate").toString());
                        anemometerRecordEntity.setAnemometerName(machine.getAnemometerName());
                        anemometerRecordEntity.setCorpinfoId("8854edee3aa94be496cee676b6d4845a");

                        machine.setOPERATOR("5c61afed41bc4c25acf77aed1391e666");
                        machine.setCorpinfoId("8854edee3aa94be496cee676b6d4845a");
                        machine.setOPERATTIME(DateUtil.date2Str(new Date()));
                        updateList.add(machine);
                        recordList.add(anemometerRecordEntity);

                        flag = true;
                    }
                }
            }
            if (flag) {
                continue;
            }
            AnemometerMachineEntity machine = new AnemometerMachineEntity();
            machine.setAnemometerId(UuidUtil.get32UUID());
            machine.setAnemometerName(data.get("machine").toString());
            machine.setCREATOR("5c61afed41bc4c25acf77aed1391e666");
            machine.setCREATTIME(DateUtil.date2Str(new Date()));
            machine.setCorpinfoId("8854edee3aa94be496cee676b6d4845a");
            machine.setOPERATOR("5c61afed41bc4c25acf77aed1391e666");
            machine.setOPERATTIME(DateUtil.date2Str(new Date()));

            AnemometerRecordEntity anemometerRecordEntity = new AnemometerRecordEntity();
            anemometerRecordEntity.setAnemometerId(machine.getAnemometerId());
            anemometerRecordEntity.setAnemometerRecordId(UuidUtil.get32UUID());
            anemometerRecordEntity.setCREATOR("5c61afed41bc4c25acf77aed1391e666");
            anemometerRecordEntity.setCREATTIME(DateUtil.date2Str(new Date()));
            anemometerRecordEntity.setOPERATOR("5c61afed41bc4c25acf77aed1391e666");
            anemometerRecordEntity.setOPERATTIME(DateUtil.date2Str(new Date()));
            anemometerRecordEntity.setCorpinfoId("8854edee3aa94be496cee676b6d4845a");
            anemometerRecordEntity.setVALUE(String.valueOf(data.get("data")));
            anemometerRecordEntity.setDATE(data.get("fdate").toString());
            anemometerRecordEntity.setAnemometerName(machine.getAnemometerName());

            addList.add(machine);
            recordList.add(anemometerRecordEntity);
        }
        if (addList.size() > 0) {
            anemometerMachineService.saveBatch(addList);
        }
        if (recordList.size() > 0) {
            anemometerRecordMapper.insert(recordList);
        }
        List<AlarmInfoEntity> alarmList = new ArrayList<>();
        //查询风速仪报警阈值
        PageData tPd = new PageData();
        tPd.put("DICT_NAME","风速仪");
        List<PageData> levelList = alarmInfoService.findThreshold(tPd);

        for (AnemometerRecordEntity anemometerRecord : recordList) {
            //若风速大于阈值则存储报警信息
            for (PageData data : levelList) {
                if (Double.parseDouble(anemometerRecord.getVALUE()) > Double.parseDouble(data.getString("LEVELVALUE"))) {
                    AlarmInfoEntity alarmInfoEntity = new AlarmInfoEntity();
                    alarmInfoEntity.setAlarmInfoId(UuidUtil.get32UUID());
                    alarmInfoEntity.setAlarmValue(anemometerRecord.getVALUE());
                    alarmInfoEntity.setAlarmTime(DateUtil.date2Str(new Date()));
                    alarmInfoEntity.setLEVEL(data.getString("LEVELNAME"));
                    alarmInfoEntity.setCREATOR("5c61afed41bc4c25acf77aed1391e666");
                    alarmInfoEntity.setCREATTIME(DateUtil.date2Str(new Date()));
                    alarmInfoEntity.setOPERATOR("5c61afed41bc4c25acf77aed1391e666");
                    alarmInfoEntity.setOPERATTIME(DateUtil.date2Str(new Date()));
                    alarmInfoEntity.setISDELETE("0");
                    alarmInfoEntity.setCorpinfoId("8854edee3aa94be496cee676b6d4845a");
                    alarmInfoEntity.setLOG(anemometerRecord.getAnemometerName()+"风速达到"+anemometerRecord.getVALUE()+",米/秒,已经达到"+data.getString("LEVELNAME")+"阈值");
                    alarmInfoEntity.setAnemometerId(anemometerRecord.getAnemometerId());
                    alarmList.add(alarmInfoEntity);
                    break;
                }
            }
        }
        if (alarmList.size() > 0) {
            alarmInfoService.saveBatch(alarmList);
        }
        if (updateList.size() > 0) {
            for (AnemometerMachineEntity updatePd : updateList) {
                LambdaUpdateWrapper<AnemometerMachineEntity> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(AnemometerMachineEntity::getAnemometerName,updatePd.getAnemometerName());
                anemometerMachineService.update(updatePd, updateWrapper);
            }
        }
    }
}
