package com.huimv.env.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.huimv.common.utils.DateUtil;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.env.dao.entity.*;
import com.huimv.env.dao.repo.*;
import com.huimv.env.service.IEnvironmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Service
public class EnvironmentServiceImpl implements IEnvironmentService {

    @Autowired
    private DaZsdaEntityRepository zsdaEntityRepo;
    @Autowired
    private DaLqdaEntityRepository lqdaEntityRepo;
    @Autowired
    private SjHkAqEntityRepository aqEntityRepo;
    @Autowired
    private SjHkSdEntityRepository sdEntityRepo;
    @Autowired
    private SjHkWdEntityRepository wdEntityRepo;
    @Autowired
    private EnvironmentDataEntityRepository environmentDataRepo;

    /**
     * @Method      : getLastEnvData
     * @Description : 
     * @Params      : [mcid]
     * @Return      : com.huimv.common.utils.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2020/12/29       
     * @Time        : 13:29
     */
    @Override
    @DS("slave")
    public Result getLastEnvData(int mcid) {
        //--读取所有猪舍
        List<DaZsdaEntity> zsdsList = zsdaEntityRepo.getAllZs(mcid);
//        System.out.println("zsdsList.size>>"+zsdsList.size());
        if (zsdsList.size() == 0) {
            //返回出错
            return new Result(10001, "牧场id(" + mcid + ")中的猪舍为0", false);
        }
        List zsidList = new ArrayList();
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            zsidList.add(zsdaEntity.getID());
        }
//        System.out.println("zsSb>>"+zsidList.toString());
        //--读取所有栏期
        List<DaLqdaEntity> lqdaEntityList = lqdaEntityRepo.getLqid(zsidList);
//        System.out.println("栏期length>>"+lqdaEntityList.size());
        if (lqdaEntityList.size() == 0) {
            //返回出错.
            return new Result(10002, "这个牧场(" + mcid + ")中的猪舍暂无相关任何栏期", false);
        }
        List lqidList = new ArrayList();
        for (DaLqdaEntity lqdaEntity : lqdaEntityList) {
            lqidList.add(lqdaEntity.getID());
        }
//        System.out.println("lqidList>>"+lqidList.toString());
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            int zsid = zsdaEntity.getID();
//            String zsmc = zsdaEntity.getZSMC();
            for (DaLqdaEntity lqdaEntity : lqdaEntityList) {
                if (lqdaEntity.getZSID() == zsid) {
                    zsdaEntity.setLqid(lqdaEntity.getID());
                    break;
                }
            }
        }
        String todayDate = new DateUtil().formatDate(new Date());
        System.out.println("" + lqidList.toString());
//        //--读取最新氨气
        List<SjHkAqEntity> aqList = aqEntityRepo.getLastAq(lqidList, todayDate);
//        System.out.println("aqList.size>>"+aqList.size());

        //--读取最新湿度
        List<SjHkSdEntity> sdList = sdEntityRepo.getLastSd(lqidList, todayDate);
//        System.out.println("sdList.size>>"+sdList.size());

        //--读取最新温度
        List<SjHkWdEntity> wdList = wdEntityRepo.getLastWd(lqidList, todayDate);
//        System.out.println("wdList.size>>"+wdList.size());
        for (DaZsdaEntity zsdaEntity : zsdsList) {
//            System.out.println(zsdaEntity.getZSMC()+","+zsdaEntity.getLqid());
            // 温度
            for (SjHkWdEntity wdEntity : wdList) {
                if (wdEntity.getLQID() == zsdaEntity.getLqid()) {
                    zsdaEntity.setDqwd(wdEntity.getDQWD());
                    break;
                }
            }
            // 湿度
            for (SjHkSdEntity sdEntity : sdList) {
                if (sdEntity.getLQID() == zsdaEntity.getLqid()) {
                    zsdaEntity.setDqsd(sdEntity.getDQSD());
                    break;
                }
            }
            // 氨气
            for (SjHkAqEntity aqEntity : aqList) {
                if (aqEntity.getLQID() == zsdaEntity.getLqid()) {
                    zsdaEntity.setDqaq(aqEntity.getDQAQ());
//                    System.out.println("dqaq>>"+aqEntity.getDQAQ());
                    break;
                }
            }
        }
        JSONArray envJa = new JSONArray();
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            JSONObject envJo = new JSONObject();
            envJa.add(envJo);
//            System.out.println(""+zsdaEntity.toString());
            envJo.put("id", zsdaEntity.getID());
            envJo.put("position", zsdaEntity.getZSMC());
            // 温度
            if (zsdaEntity.getDqwd() == null) {
                envJo.put("temp", "");
            } else {
                envJo.put("temp", zsdaEntity.getDqwd());
            }
            // 湿度
            if (zsdaEntity.getDqsd() == null) {
                envJo.put("humidity", "");
            } else {
                envJo.put("humidity", zsdaEntity.getDqsd());
            }
            // 氨气
            if (zsdaEntity.getDqaq() == null) {
                envJo.put("ammonia", "");
            } else {
                envJo.put("ammonia", zsdaEntity.getDqaq());
            }
        }
        return new Result(ResultCode.SUCCESS, envJa);
    }

    /**
     * @Method      : getLastEnvData2
     * @Description : 
     * @Params      : [mcid]
     * @Return      : com.huimv.common.utils.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2020/12/28       
     * @Time        : 21:01
     */
    @Override
    @DS("slave")
    public Result getLastEnvData2(int mcid) {
        //--读取所有猪舍
        List<DaZsdaEntity> zsdsList = zsdaEntityRepo.getAllZs(mcid);
//        System.out.println("zsdsList.size>>"+zsdsList.size());
        if (zsdsList.size() == 0) {
            //返回出错
            return new Result(10001, "牧场id(" + mcid + ")中的猪舍为0", false);
        }
        List zsidList = new ArrayList();
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            zsidList.add(zsdaEntity.getID());
        }
//        System.out.println("zsSb>>"+zsidList.toString());
        //--读取所有栏期
        List<DaLqdaEntity> lqdaEntityList = lqdaEntityRepo.getLqid(zsidList);
//        System.out.println("栏期length>>"+lqdaEntityList.size());
        if (lqdaEntityList.size() == 0) {
            //返回出错.
            return new Result(10002, "这个牧场(" + mcid + ")中的猪舍暂未设置相关栏期", false);
        }
        List lqidList = new ArrayList();
        for (DaLqdaEntity lqdaEntity : lqdaEntityList) {
            lqidList.add(lqdaEntity.getID());
        }
//        System.out.println("lqidList>>"+lqidList.toString());
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            int zsid = zsdaEntity.getID();
//            String zsmc = zsdaEntity.getZSMC();
            for (DaLqdaEntity lqdaEntity : lqdaEntityList) {
                if (lqdaEntity.getZSID() == zsid) {
                    zsdaEntity.setLqid(lqdaEntity.getID());
                    break;
                }
            }
        }
        String todayDate = new DateUtil().formatDate(new Date());
        System.out.println("" + lqidList.toString());

        JSONArray envJa = new JSONArray();
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            int lqid = zsdaEntity.getLqid();
            System.out.println("lqid>>" + lqid);
            JSONObject envJo = new JSONObject();
            envJa.add(envJo);
            envJo.put("id",zsdaEntity.getID());
            envJo.put("lqid",lqid);
            envJo.put("zsmc",zsdaEntity.getZSMC());
            //--读取最新氨气
            SjHkAqEntity aqEntity = aqEntityRepo.getLastAq(lqid, todayDate);
            System.out.println("aqEntity>>" + aqEntity);
            if (aqEntity != null) {
                envJo.put("ammonia", aqEntity.getDQAQ());
            } else {
                envJo.put("ammonia", "");
            }
            //--读取最新湿度
            SjHkSdEntity sdEntity = sdEntityRepo.getLastSd(lqid, todayDate);
            if (sdEntity != null) {
                envJo.put("humidity", sdEntity.getDQSD());
            } else {
                envJo.put("humidity", "");
            }
            //--读取最新温度
            SjHkWdEntity wdEntity = wdEntityRepo.getLastWd(lqid, todayDate);
            if (wdEntity != null) {
                envJo.put("temp", wdEntity.getDQWD());
            } else {
                envJo.put("temp", "");
            }
        }
        return new Result(ResultCode.SUCCESS, envJa);
    }

    /**
     * @Method      : getZsda
     * @Description : 
     * @Params      : [mcid]
     * @Return      : com.huimv.common.utils.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2020/12/28       
     * @Time        : 21:01
     */
    @Override
    @DS("slave")
    public Result getZsda(int mcid) {
        //--读取所有猪舍
        List<DaZsdaEntity> zsdsList = zsdaEntityRepo.getAllZs(mcid);
//        System.out.println("zsdsList.size>>"+zsdsList.size());
        if (zsdsList.size() == 0) {
            //返回出错
            return new Result(10001, "牧场id(" + mcid + ")中的猪舍为0", false);
        }
        List zsidList = new ArrayList();
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            zsidList.add(zsdaEntity.getID());
        }
//        System.out.println("zsSb>>"+zsidList.toString());
        //--读取所有栏期
        List<DaLqdaEntity> lqdaEntityList = lqdaEntityRepo.getLqid(zsidList);
//        System.out.println("栏期length>>"+lqdaEntityList.size());
        if (lqdaEntityList.size() == 0) {
            //返回出错.
            return new Result(10002, "这个牧场(" + mcid + ")中的猪舍暂无相关任何栏期", false);
        }
        List lqidList = new ArrayList();
//        for (DaLqdaEntity lqdaEntity : lqdaEntityList) {
//            lqidList.add(lqdaEntity.getID());
//        }
//        System.out.println("lqidList>>"+lqidList.toString());
        JSONArray zsJa = new JSONArray();
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            int zsid = zsdaEntity.getID();
            JSONObject zsJo = new JSONObject();
            zsJa.add(zsJo);
            zsJo.put("zsid", zsid);
            zsJo.put("zsmc", zsdaEntity.getZSMC());
            for (DaLqdaEntity lqdaEntity : lqdaEntityList) {
                if (lqdaEntity.getZSID() == zsid) {
                    zsJo.put("lqid", lqdaEntity.getID());
                    break;
                }
            }
        }
        return new Result(ResultCode.SUCCESS, zsJa);
    }

    /**
     * @Method      : getLastEnvByLq
     * @Description : 
     * @Params      : [lqid]
     * @Return      : com.huimv.common.utils.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2020/12/28       
     * @Time        : 21:01
     */
    @Override
    @DS("slave")
    public Result getLastEnvByLq(Integer lqid) {
        String todayDate = new DateUtil().formatDate(new Date());
        System.out.println(" todayDate>>"+todayDate);
        System.out.println(" lqid>>"+lqid);
        JSONObject envJo = new JSONObject();
        envJo.put("lqid",lqid);
        //--读取最新氨气
        SjHkAqEntity aqEntity = aqEntityRepo.getLastAq(lqid, todayDate);
        System.out.println("aqEntity>>" + aqEntity);
        if (aqEntity != null) {
            envJo.put("ammonia", aqEntity.getDQAQ());
        } else {
            envJo.put("ammonia", "");
        }
        //--读取最新湿度
        SjHkSdEntity sdEntity = sdEntityRepo.getLastSd(lqid, todayDate);
        if (sdEntity != null) {
            envJo.put("humidity", sdEntity.getDQSD());
        } else {
            envJo.put("humidity", "");
        }
        //--读取最新温度
        SjHkWdEntity wdEntity = wdEntityRepo.getLastWd(lqid, todayDate);
        if (wdEntity != null) {
            envJo.put("temp", wdEntity.getDQWD());
        } else {
            envJo.put("temp", "");
        }
        return new Result(ResultCode.SUCCESS, envJo);
    }

    /**
     * @Method      : getTemperatureOnToday
     * @Description : 获取今天温度
     * @Params      : [lqid]
     * @Return      : com.huimv.common.utils.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2020/12/29       
     * @Time        : 13:29
     */
    @Override
    @DS("slave")
    public Result getTemperatureOnToday(Integer lqid) {
        String beforeDateTime = new DateUtil().beforeTime(1);
        JSONObject dataJo = new JSONObject();
        //--读取最新温度
//        List<SjHkWdEntity> wdEntityList = wdEntityRepo.getTemperatureOnToday(lqid, beforeDateTime);
        List<SjHkWdEntity> wdEntityList = wdEntityRepo.getTemperature(lqid, beforeDateTime);
        List<SjHkWdEntity> newList = new ArrayList();
        for(int a=wdEntityList.size()-1;a>=0;a--){
            SjHkWdEntity wdEntity = wdEntityList.get(a);
            newList.add(wdEntity);
        }
        if (newList.size()>0) {
            JSONArray tempJa = new JSONArray();
            JSONArray timeJa = new JSONArray();
            for(SjHkWdEntity wdEntity: newList){
                System.out.println(""+wdEntity.getCJSJ()+","+wdEntity.getDQWD());
                String dateTime = wdEntity.getCJSJ().toString();
                String hourAndMin =  dateTime.substring(11,16);
                timeJa.add(hourAndMin);
                tempJa.add(wdEntity.getDQWD());
            }
            dataJo.put("timeData",timeJa);
            dataJo.put("wenduList",tempJa);
        }
        return new Result(ResultCode.SUCCESS, dataJo);
    }

    /**
     * @Method      : getAmmoniaAndHumidityOnToday
     * @Description : 获取今天氨气和湿度
     * @Params      : [lqid]
     * @Return      : com.huimv.common.utils.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2020/12/29       
     * @Time        : 13:28
     */
    @Override
    @DS("slave")
    public Result getAmmoniaAndHumidityOnToday(Integer lqid) {
        String beforeDateTime = new DateUtil().beforeTime(3);
        JSONArray timeJa = new JSONArray();
        JSONObject dataJo = new JSONObject();
        System.out.println("lqid>>"+lqid);
        System.out.println("beforeDateTime>>"+beforeDateTime);
        //--读取最新氨气
//        List<SjHkAqEntity> aqEntityList = aqEntityRepo.getAmmoniaOnToday(lqid, beforeDateTime);
        List<SjHkAqEntity> aqEntityList = aqEntityRepo.getAmmonia(lqid, beforeDateTime);
        System.out.println("读取最新氨气>>"+aqEntityList.size());
        List<SjHkAqEntity> newList = new ArrayList();
        for(int a=aqEntityList.size()-1;a>=0;a--){
            SjHkAqEntity aqEntity = aqEntityList.get(a);
            newList.add(aqEntity);
        }
        System.out.println("读取最新氨气 newList>>"+newList.size());

        if (newList.size()>0) {
            JSONArray ammoniaJa = new JSONArray();
            for(SjHkAqEntity aqEntity:newList){
                String dateTime = aqEntity.getCJSJ().toString();
                String hourAndMin =  dateTime.substring(11,16);
                timeJa.add(hourAndMin);
                ammoniaJa.add(aqEntity.getDQAQ());
            }
            dataJo.put("timeData",timeJa);
            dataJo.put("anqiList",ammoniaJa);
        }
        //--读取最新湿度
//        List<SjHkSdEntity> sdEntityList = sdEntityRepo.getHumidityOnToday(lqid, beforeDateTime);
        List<SjHkSdEntity> sdEntityList = sdEntityRepo.getHumidity(lqid, beforeDateTime);
        System.out.println("读取最新氨气>>"+sdEntityList.size());
        List<SjHkSdEntity> newList2 = new ArrayList();
        for(int a=sdEntityList.size()-1;a>=0;a--){
            SjHkSdEntity sdEntity = sdEntityList.get(a);
            newList2.add(sdEntity);
        }
        System.out.println("读取最新氨气 newList2>>"+newList2.size());
        if (newList2.size()>0) {
            JSONArray humidityJa = new JSONArray();
            for(SjHkSdEntity sdEntity:newList2){
                humidityJa.add(sdEntity.getDQSD());
            }
            dataJo.put("shiduList",humidityJa);
        }
        return new Result(ResultCode.SUCCESS, dataJo);
    }

    @Override
    @DS("third")
    public Result getEnvDetails() {
        /**  */
        EnvironmentDataEntity environmentData = environmentDataRepo.getOneLastEnvData();
        System.out.println("environmentData:" + environmentData);
        if (environmentData == null) {
            return new Result(10003,"没有环控数据",false);
        } else {
            return new Result(ResultCode.SUCCESS,environmentData);
        }
    }


}
