package com.xydl.cac.old;

import com.xydl.cac.entity.NSensor;
import com.xydl.cac.entity.Zsb;
import com.xydl.cac.repository.NSensorRepository;
import com.xydl.cac.repository.ZsbRepository;
import com.xydl.cac.service.ZsbService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class OldService {

    @Resource
    ZsbService zsbService;
    @Resource
    ZsbRepository zsbRepository;
    @Resource
    NSensorRepository sensorRepository;
    @Resource
    private JdbcTemplate jdbcTemplate;

    public List<BaseLxDevice> getLxDevice(Integer jgid, List<String> mtid) throws Exception {
        List<BaseLxDevice> result = new ArrayList<>();
        HashMap<Integer, String> doneMap = new HashMap<>();
        List<Zsb> zsbList = zsbService.listAll(jgid);
        for (Zsb zsb : zsbList) {
            List<NSensor> sensorList = sensorRepository.findByZsbId(zsb.getId());
            for (NSensor sensor : sensorList) {
                String typeid = String.valueOf(sensor.getTypeId());
                if (mtid.contains(typeid)) {
                    if (!doneMap.containsKey(zsb.getLxid())) {
                        BaseLxDevice item = new BaseLxDevice();
                        item.setLxid(String.valueOf(zsb.getLxid()));
                        item.setLmc(zsb.getLxName());
                        result.add(item);
                        doneMap.put(zsb.getLxid(), "done");
                    }

                }
            }
        }
        return result;
    }

    public List<BaseLxDevice> getLxDevice(Integer jgid) throws Exception {
        List<BaseLxDevice> result = new ArrayList<>();
        HashMap<Integer, String> doneMap = new HashMap<>();
        List<Zsb> zsbList = zsbService.listAll(jgid);
        for (Zsb zsb : zsbList) {
            if (!doneMap.containsKey(zsb.getLxid())) {
                BaseLxDevice item = new BaseLxDevice();
                item.setLxid(String.valueOf(zsb.getLxid()));
                item.setLmc(zsb.getLxName());
                result.add(item);
                doneMap.put(zsb.getLxid(), "done");
            }
        }
        return result;
    }

    public List<BaseEqmDevice> getEqmDevice(Integer jgid, Integer lxid, List<String> mtids) throws Exception {
        List<BaseEqmDevice> result = new ArrayList<>();
        HashMap<Integer, String> doneMap = new HashMap<>();
        List<Zsb> zsbList = zsbRepository.findByJgidAndLxid(jgid, lxid);
        for (Zsb zsb : zsbList) {
            List<NSensor> sensorList = sensorRepository.findByZsbId(zsb.getId());
            for (NSensor sensor : sensorList) {
                String typeid = String.valueOf(sensor.getTypeId());
                if (mtids.contains(typeid)) {
                    if (!doneMap.containsKey(sensor.getDevId())) {
                        BaseEqmDevice item = new BaseEqmDevice();
                        item.setId(String.valueOf(sensor.getDevId()));
                        item.setName(sensor.getName());
                        result.add(item);
                        doneMap.put(sensor.getDevId(), "done");
                    }
                }
            }
        }
        return result;
    }

    public Equipment queryEquipNum() {
        Equipment equipment = new Equipment();
        long totalNum = sensorRepository.count();
        List<Zsb> zsbList = zsbRepository.findByJgid(1);
        List<Integer> idList = new ArrayList<>();
        for (Zsb zsb : zsbList) {
            idList.add(zsb.getId());
        }
        long byqNum = sensorRepository.countByZsbIdIn(idList);
        zsbList = zsbRepository.findByJgid(2);
        idList.clear();
        for (Zsb zsb : zsbList) {
            idList.add(zsb.getId());
        }
        long gisNum = sensorRepository.countByZsbIdIn(idList);
        zsbList = zsbRepository.findByJgid(3);
        idList.clear();
        for (Zsb zsb : zsbList) {
            idList.add(zsb.getId());
        }
        long blqNum = sensorRepository.countByZsbIdIn(idList);
        zsbList = zsbRepository.findByJgid(5);
        idList.clear();
        for (Zsb zsb : zsbList) {
            idList.add(zsb.getId());
        }
        long dlNum = sensorRepository.countByZsbIdIn(idList);
        zsbList = zsbRepository.findByJgid(4);
        idList.clear();
        for (Zsb zsb : zsbList) {
            idList.add(zsb.getId());
        }
        long cwNum = sensorRepository.countByZsbIdIn(idList);
        long otherNum = totalNum - byqNum - gisNum - blqNum - dlNum - cwNum;
        equipment.setTotalNum(totalNum);
        equipment.setByqNum(byqNum);
        equipment.setGisNum(gisNum);
        equipment.setBlqNum(blqNum);
        equipment.setDlNum(dlNum);
        equipment.setCwNum(cwNum);
        equipment.setOtherNum(otherNum);
        return equipment;
    }

    List<OilChromaVo> queryTableDataList(Integer eqmid) {
        String sql = "SELECT * FROM data_epa_h WHERE eqmid=" + eqmid + " ORDER BY d_time DESC LIMIT 1";
        List<OilChromaVo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(OilChromaVo.class));
        return list;
    }

    List<OilVo> queryLineDataList(Integer eqmid) {
        String sql = "SELECT * FROM data_epa_h WHERE eqmid=" + eqmid + " ORDER BY d_time DESC LIMIT 15";
        List<OilChromaVo> oilDataList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(OilChromaVo.class));
        OilVo ch4 = new OilVo();
        List<String> ch4Values = new ArrayList<>();
        ch4.setCurrentVals(ch4Values);

        OilVo c2h4 = new OilVo();
        List<String> c2h4Values = new ArrayList<>();
        c2h4.setCurrentVals(c2h4Values);

        OilVo c2h6 = new OilVo();
        List<String> c2h6Values = new ArrayList<>();
        c2h6.setCurrentVals(c2h6Values);

        OilVo c2h2 = new OilVo();
        List<String> c2h2Values = new ArrayList<>();
        c2h2.setCurrentVals(c2h2Values);

        OilVo h2 = new OilVo();
        List<String> h2Values = new ArrayList<>();
        h2.setCurrentVals(h2Values);

        OilVo co = new OilVo();
        List<String> coValues = new ArrayList<>();
        co.setCurrentVals(coValues);

        OilVo co2 = new OilVo();
        List<String> co2Values = new ArrayList<>();
        co2.setCurrentVals(co2Values);

        OilVo totalhydrocarbon = new OilVo();
        List<String> totalhydrocarbonValues = new ArrayList<>();
        totalhydrocarbon.setCurrentVals(totalhydrocarbonValues);

        List<String> times = new ArrayList<>();
        for (OilChromaVo vo : oilDataList) {
            ch4.getCurrentVals().add(vo.getCh4ppm());
            c2h4.getCurrentVals().add(vo.getC2h4ppm());
            c2h6.getCurrentVals().add(vo.getC2h6ppm());
            c2h2.getCurrentVals().add(vo.getC2h2ppm());
            h2.getCurrentVals().add(vo.getH2ppm());
            co.getCurrentVals().add(vo.getCoppm());
            co2.getCurrentVals().add(vo.getCo2ppm());
            totalhydrocarbon.getCurrentVals().add(vo.getTotalhydrocarbon());
            times.add(vo.getDTime());
        }

        ch4.setDTimes(times);
        c2h4.setDTimes(times);
        c2h6.setDTimes(times);
        c2h2.setDTimes(times);
        h2.setDTimes(times);
        co.setDTimes(times);
        co2.setDTimes(times);
        totalhydrocarbon.setDTimes(times);

        ch4.setGas("甲烷(μL/L)");
        c2h4.setGas("乙烯(μL/L)");
        c2h6.setGas("乙烷(μL/L)");
        c2h2.setGas("乙炔(μL/L)");
        h2.setGas("氢气(μL/L)");
        co.setGas("一氧化碳(μL/L)");
        co2.setGas("二氧化碳(μL/L)");
        totalhydrocarbon.setGas("总烃(μL/L)");

        List<OilVo> lineDataList = new ArrayList<>();
        lineDataList.add(ch4);
        lineDataList.add(c2h4);
        lineDataList.add(c2h6);
        lineDataList.add(c2h2);
        lineDataList.add(h2);
        lineDataList.add(co);
        lineDataList.add(co2);
        lineDataList.add(totalhydrocarbon);
        return lineDataList;
    }

    List<IronCoreJoinClampDto> queryTableDataList(IronCoreJoinClampDto dto) {
        String sql = "select t1.d_time, t1.d_ct_1 iron_core_value, t2.d_ct_1 clamp_value" +
                "        from (select eqmid, d_time, d_ct_1 from data_eia_h where eqmid = " + dto.getIronCoreId() + " order by d_time desc limit 5) t1" +
                "                 left join" +
                "             (select eqmid, d_time, d_ct_1 from data_eia_h where eqmid = " + dto.getClampId() + " order by d_time desc limit 5) t2" +
                "             on t1.d_time = t2.d_time" +
                "        order by d_time desc";
        List<IronCoreJoinClampDto> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(IronCoreJoinClampDto.class));
        return list;
    }

    List<IronVo> queryLineDataList(IronCoreJoinClampDto dto) {
        String sql = "select t1.d_time, t1.d_ct_1 iron_core_value, t2.d_ct_1 clamp_value" +
                "        from (select eqmid, d_time, d_ct_1 from data_eia_h where eqmid = " + dto.getIronCoreId() + " order by d_time desc limit 15) t1" +
                "                 left join" +
                "             (select eqmid, d_time, d_ct_1 from data_eia_h where eqmid = " + dto.getClampId() + " order by d_time desc limit 15) t2" +
                "             on t1.d_time = t2.d_time" +
                "        order by d_time desc";
        List<IronCoreJoinClampDto> IronDataList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(IronCoreJoinClampDto.class));
        IronVo ironCore = new IronVo();
        List<String> ironCoreValues = new ArrayList<>();
        ironCore.setCurrentVals(ironCoreValues);

        IronVo clamp = new IronVo();
        List<String> clampValues = new ArrayList<>();
        clamp.setCurrentVals(clampValues);

        List<String> times = new ArrayList<>();
        for (IronCoreJoinClampDto vo : IronDataList) {
            ironCore.getCurrentVals().add(vo.getIronCoreValue());
            clamp.getCurrentVals().add(vo.getClampValue());
            times.add(vo.getDTime());
        }

        ironCore.setDTimes(times);
        clamp.setDTimes(times);

        ironCore.setEquipment("铁芯泄露电流(mA)");
        clamp.setEquipment("夹件泄露电流(mA)");

        List<IronVo> lineDataList = new ArrayList<>();
        lineDataList.add(ironCore);
        lineDataList.add(clamp);
        return lineDataList;
    }

    List<ArresterVo> queryTableDataList2(Integer eqmid) {
        String sql = "SELECT * FROM data_moa_h WHERE eqmid=" + eqmid + " ORDER BY d_time DESC LIMIT 2";
        List<ArresterVo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ArresterVo.class));
        return list;
    }

    List<ArrVo> queryLineDataList2(Integer eqmid) {
        String sql = "SELECT * FROM data_moa_h WHERE eqmid=" + eqmid + " ORDER BY d_time DESC LIMIT 15";
        List<ArresterVo> arresterDataList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ArresterVo.class));
        // A相系统电压
        ArrVo a1 = new ArrVo();
        List<String> a1Values = new ArrayList<>();
        a1.setCurrentVals(a1Values);
        // A相全电流
        ArrVo a2 = new ArrVo();
        List<String> a2Values = new ArrayList<>();
        a2.setCurrentVals(a2Values);
        // A相阻性电流
        ArrVo a3 = new ArrVo();
        List<String> a3Values = new ArrayList<>();
        a3.setCurrentVals(a3Values);

        ArrVo b1 = new ArrVo();
        List<String> b1Values = new ArrayList<>();
        b1.setCurrentVals(b1Values);

        ArrVo b2 = new ArrVo();
        List<String> b2Values = new ArrayList<>();
        b2.setCurrentVals(b2Values);

        ArrVo b3 = new ArrVo();
        List<String> b3Values = new ArrayList<>();
        b3.setCurrentVals(b3Values);

        ArrVo c1 = new ArrVo();
        List<String> c1Values = new ArrayList<>();
        c1.setCurrentVals(c1Values);

        ArrVo c2 = new ArrVo();
        List<String> c2Values = new ArrayList<>();
        c2.setCurrentVals(c2Values);

        ArrVo c3 = new ArrVo();
        List<String> c3Values = new ArrayList<>();
        c3.setCurrentVals(c3Values);

        List<String> times = new ArrayList<>();
        for (ArresterVo vo : arresterDataList) {
            a1.getCurrentVals().add(vo.getPtA());
            a2.getCurrentVals().add(vo.getLcA());
            a3.getCurrentVals().add(vo.getRcA());
            b1.getCurrentVals().add(vo.getPtB());
            b2.getCurrentVals().add(vo.getLcB());
            b3.getCurrentVals().add(vo.getRcB());
            c1.getCurrentVals().add(vo.getPtC());
            c2.getCurrentVals().add(vo.getLcC());
            c3.getCurrentVals().add(vo.getRcC());
            times.add(vo.getDTime());
        }

        a1.setDTimes(times);
        a2.setDTimes(times);
        a3.setDTimes(times);
        b1.setDTimes(times);
        b2.setDTimes(times);
        b3.setDTimes(times);
        c1.setDTimes(times);
        c2.setDTimes(times);
        c3.setDTimes(times);

        a1.setEquipment("A相系统电压(V)");
        a2.setEquipment("A相全电流(mA)");
        a3.setEquipment("A相阻性电流(mA)");
        b1.setEquipment("B相系统电压(V)");
        b2.setEquipment("B相全电流(mA)");
        b3.setEquipment("B相阻性电流(mA)");
        c1.setEquipment("C相系统电压(V)");
        c2.setEquipment("C相全电流(mA)");
        c3.setEquipment("C相阻性电流(mA)");

        List<ArrVo> lineDataList = new ArrayList<>();
        lineDataList.add(a1);
        lineDataList.add(a2);
        lineDataList.add(a3);
        lineDataList.add(b1);
        lineDataList.add(b2);
        lineDataList.add(b3);
        lineDataList.add(c1);
        lineDataList.add(c2);
        lineDataList.add(c3);
        return lineDataList;
    }

    List<SF6Vo> queryTableDataList3(Integer eqmid) {
        String sql = "SELECT * FROM data_sf6_h WHERE eqmid=" + eqmid + " ORDER BY d_time DESC LIMIT 2";
        List<SF6Vo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SF6Vo.class));
        return list;
    }

    List<SF6LineVo> queryLineDataList3(Integer eqmid) {
        String sql = "SELECT * FROM data_sf6_h WHERE eqmid=" + eqmid + " ORDER BY d_time DESC LIMIT 15";
        List<SF6Vo> sf6DataList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SF6Vo.class));

        // A相温度
        SF6LineVo aTemp = new SF6LineVo();
        List<String> aTempValues = new ArrayList<>();
        aTemp.setCurrentVals(aTempValues);

        // A相压力
        SF6LineVo aPressure = new SF6LineVo();
        List<String> aPressureValues = new ArrayList<>();
        aPressure.setCurrentVals(aPressureValues);

        // A相微水
        SF6LineVo aWater = new SF6LineVo();
        List<String> aWaterValues = new ArrayList<>();
        aWater.setCurrentVals(aWaterValues);

        // B相温度
        SF6LineVo bTemp = new SF6LineVo();
        List<String> bTempValues = new ArrayList<>();
        bTemp.setCurrentVals(bTempValues);

        // B相压力
        SF6LineVo bPressure = new SF6LineVo();
        List<String> bPressureValues = new ArrayList<>();
        bPressure.setCurrentVals(bPressureValues);

        // B相微水
        SF6LineVo bWater = new SF6LineVo();
        List<String> bWaterValues = new ArrayList<>();
        bWater.setCurrentVals(bWaterValues);

        // C相温度
        SF6LineVo cTemp = new SF6LineVo();
        List<String> cTempValues = new ArrayList<>();
        cTemp.setCurrentVals(cTempValues);

        // C相压力
        SF6LineVo cPressure = new SF6LineVo();
        List<String> cPressureValues = new ArrayList<>();
        cPressure.setCurrentVals(cPressureValues);

        // C相微水
        SF6LineVo cWater = new SF6LineVo();
        List<String> cWaterValues = new ArrayList<>();
        cWater.setCurrentVals(cWaterValues);

        List<String> times = new ArrayList<>();
        for (SF6Vo vo : sf6DataList) {
            aTemp.getCurrentVals().add(vo.getTempA());
            aPressure.getCurrentVals().add(vo.getPressureA());
            aWater.getCurrentVals().add(vo.getPmA());
            bTemp.getCurrentVals().add(vo.getTempB());
            bPressure.getCurrentVals().add(vo.getPressureB());
            bWater.getCurrentVals().add(vo.getPmB());
            cTemp.getCurrentVals().add(vo.getTempC());
            cPressure.getCurrentVals().add(vo.getPressureC());
            cWater.getCurrentVals().add(vo.getPmC());
            times.add(vo.getDTime());
        }

        aTemp.setDTimes(times);
        aPressure.setDTimes(times);
        aWater.setDTimes(times);
        bTemp.setDTimes(times);
        bPressure.setDTimes(times);
        bWater.setDTimes(times);
        cTemp.setDTimes(times);
        cPressure.setDTimes(times);
        cWater.setDTimes(times);

        aTemp.setEquipment("A相温度(℃)");
        aPressure.setEquipment("A相压力(kPa)");
        aWater.setEquipment("A相微水(μL/L)");
        bTemp.setEquipment("B相温度(℃)");
        bPressure.setEquipment("B相压力(kPa)");
        bWater.setEquipment("B相微水(μL/L)");
        cTemp.setEquipment("C相温度(℃)");
        cPressure.setEquipment("C相压力(kPa)");
        cWater.setEquipment("C相微水(μL/L)");

        List<SF6LineVo> lineDataList = new ArrayList<>();
        lineDataList.add(aTemp);
        lineDataList.add(aPressure);
        lineDataList.add(aWater);
        lineDataList.add(bTemp);
        lineDataList.add(bPressure);
        lineDataList.add(bWater);
        lineDataList.add(cTemp);
        lineDataList.add(cPressure);
        lineDataList.add(cWater);
        return lineDataList;
    }

    List<CableWarningVo> selectPages(CableWarningDto dto) {
        String sql = "SELECT w.id,w.eqmid,w.warn_time,w.warning_value,w.threadval,w.warn_desc,w.warn_level," +
                "     w.state,w.process,w.process_user,w.process_time,COUNT(w.id) AS warningCount,v.NAME" +
                "        FROM warning w" +
                "    LEFT JOIN vw_sb v ON v.id = w.eqmid" +
                "    LEFT JOIN vw_jg j ON j.id = v.jgid" +
                "    LEFT JOIN i2relation i ON v.id = i.eqmid WHERE 1=1 ";
        if (StringUtils.isNotBlank(dto.getState())) {
            sql = sql + " AND w.state=" + dto.getState();
        }
        if (StringUtils.isNotBlank(dto.getStartTime())) {
            sql = sql + " AND w.warn_time>='" + dto.getStartTime() + " 00:00:00'";
        }
        if (StringUtils.isNotBlank(dto.getEndTime())) {
            sql = sql + " AND w.warn_time<='" + dto.getEndTime() + " 23:59:59'";
        }
        sql = sql + " GROUP BY w.eqmid";
        List<CableWarningVo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CableWarningVo.class));
        return list;
    }

    List<CableWarningVo> getWarningPage(CableWarningDto dto) {
        String sql = "SELECT w.id,w.eqmid,w.warn_time,w.warning_value,w.threadval,w.warn_desc,w.warn_level," +
                "     w.state,w.process,w.process_user,w.process_time,COUNT(w.id) AS warningCount,v.NAME" +
                "        FROM warning w" +
                "    LEFT JOIN vw_sb v ON v.id = w.eqmid" +
                "    LEFT JOIN vw_jg j ON j.id = v.jgid" +
                "    LEFT JOIN i2relation i ON v.id = i.eqmid WHERE 1=1 ";
        if (StringUtils.isNotBlank(dto.getState())) {
            sql = sql + " AND w.state=" + dto.getState();
        }
        if (StringUtils.isNotBlank(dto.getStartTime())) {
            sql = sql + " AND w.warn_time>='" + dto.getStartTime() + " 00:00:00'";
        }
        if (StringUtils.isNotBlank(dto.getEndTime())) {
            sql = sql + " AND w.warn_time<='" + dto.getEndTime() + " 23:59:59'";
        }
        sql = sql + " GROUP BY w.eqmid";
        List<CableWarningVo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CableWarningVo.class));
        return list;
    }
}
