package com.huimv.environ.env.environ.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.environ.env.environ.dao.entity.EnvDayEntity;
import com.huimv.environ.env.environ.dao.entity.EnvHourEntity;
import com.huimv.environ.env.environ.dao.repo.EnvDayRepo;
import com.huimv.environ.env.environ.dao.repo.EnvHourRepo;
import com.huimv.environ.env.environ.service.IEnviron;
import com.huimv.environ.env.utils.Const;
import com.huimv.environ.env.utils.DateUtil;
import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.Optional;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Service
public class EnvironImpl implements IEnviron {
    @Autowired
    private EnvHourRepo envHourRepo;
    @Autowired
    private EnvDayRepo envDayRepo;
    @Autowired
    private DateUtil dateUtil;
    /**
     * @Method      : getLastEnviron
     * @Description : 读取最后一条记录
     * @Params      : []
     * @Return      : com.huimv.common.result.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/12/2       
     * @Time        : 23:28
     */
    @Override
    public Result getLastEnviron(Integer farmId){
        Optional<EnvHourEntity> optional = envHourRepo.findLastOne(farmId);
        System.out.println("optional.isPresent()>>"+optional.isPresent());
        if(optional.isPresent()){
            EnvHourEntity envHourEntity = optional.get();
            return new Result(ResultCode.SUCCESS,envHourEntity);
//            return new Result(ResultCode.SUCCESS,JSON.toJSON(envHourEntity).toString());
        }else{
            return new Result(Const.ERROR_CODE_NOTEXIST,Const.ERROR_MSG_NOTEXIST,false);
        }
    }

//    @Override
    public Result getLastEnviron2(Integer farmId){
        EnvHourEntity envHourEntity = envHourRepo.findLastOne2(farmId);
        System.out.println("envHourEntity>>"+envHourEntity);
        if(envHourEntity != null){
            System.out.println("envHourEntity>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+envHourEntity.toString());
            return new Result(ResultCode.SUCCESS,envHourEntity);
//            return new Result(ResultCode.SUCCESS,JSON.toJSON(envHourEntity).toString());
        }else{
            return new Result(Const.ERROR_CODE_NOTEXIST,Const.ERROR_MSG_NOTEXIST,false);
        }
    }

    /**
     * @Method      : getLastSevenDayEnviron
     * @Description : 
     * @Params      : [farmId, days]
     * @Return      : com.huimv.common.result.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/12/3       
     * @Time        : 21:18
     */
    @Override
    public Result getLastSevenDayEnviron(Integer farmId,Integer days) throws ParseException {
        System.out.println(">>>>> dateUtil.getTodayDate()>>"+dateUtil.getTodayDate());
        System.out.println(">>>>> days>>"+days);
        //查询最近N天记录
        List<EnvDayEntity> envDayEntityList = envDayRepo.findByFarmIdAndAddDate(farmId,days);
        System.out.println(">>>>> envDayEntityList.size>>"+envDayEntityList.size());
        if(envDayEntityList.size()>0){
            JSONArray dateJa = new JSONArray();
            JSONArray codJa = new JSONArray();
            JSONArray adJa = new JSONArray();
            JSONArray phJa = new JSONArray();
            JSONArray wsJa = new JSONArray();
            for(int a=envDayEntityList.size()-1;a>=0;a--){
                EnvDayEntity envDayEntity = envDayEntityList.get(a);
                dateJa.add(dateUtil.formatDate(envDayEntity.getAddDate()));
                codJa.add(envDayEntity.getCod());
                adJa.add(envDayEntity.getAd());
                phJa.add(envDayEntity.getPh());
                wsJa.add(envDayEntity.getWs());
            }
            JSONObject outJo = new JSONObject();
            outJo.put("date",dateJa);
            outJo.put("cod",codJa);
            outJo.put("ad",adJa);
            outJo.put("ph",phJa);
            outJo.put("ws",wsJa);
            return new Result(ResultCode.SUCCESS,outJo);
        }else{
            return new Result(Const.ERROR_CODE_NOTEXIST,Const.ERROR_MSG_NOTEXIST,false);
        }
    }
}
