package com.casic.PgInterface.rs;

import com.casic.PgInterface.devData.domain.PgTempHum;
import com.casic.PgInterface.devData.dto.PgHistoryTempDto;
import com.casic.PgInterface.devData.dto.PgTempHumDto;
import com.casic.PgInterface.devData.manager.PgTempHumManager;
import com.casic.PgInterface.devTable.domain.PgDevice;
import com.casic.PgInterface.devTable.manager.PgDeviceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by yxw on 2017/12/10.
 */
@Component
@Path("pgTempHum")
public class PgTempHumRs {

    private static Logger logger = LoggerFactory.getLogger(PgCoRs.class);
    @Resource
    private PgDeviceManager pgDeviceManager;
    @Resource
    private PgTempHumManager pgTempHumManager;

    /**
     * 获取设备监测数据
     */
    @POST
    @Path("getTempHumInfoByDevAndDate")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getTempHumInfoByDevAndDate(@FormParam("assetCode") String assetCode,
                                                      @FormParam("startDate") String startDate,
                                                      @FormParam("endDate") String endDate)throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        List<PgHistoryTempDto> pgHistoryTempDtoList=new ArrayList<>();

        try {
            PgDevice pgDevice=pgDeviceManager.getDeviceByAssetCode(assetCode);
            if(pgDevice==null)
                msg="设备不存在";
            else{
                pgHistoryTempDtoList=pgTempHumManager.getAllTempHumData(pgDevice, startDate, endDate);
                if(pgHistoryTempDtoList==null)
                    msg="没有满足要求的数据";
                else
                    msg="数据获取成功";
            }
        }catch(Exception e){
            e.printStackTrace();
            msg="设备监测数据获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgHistoryTempDtoList);
        return resultMap;
    }

    /**
     * 获取设备监测数据
     */
    @POST
    @Path("getNewestTempHumInfoByDev")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getNewestTempHumInfoByDev(@FormParam("assetCode") String assetCode)throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        PgTempHumDto pgTempHumDto=new PgTempHumDto(null);

        try {
            PgDevice pgDevice=pgDeviceManager.getDeviceByAssetCode(assetCode);
            if(pgDevice==null)
                msg="设备不存在";
            else{
                pgTempHumDto=pgTempHumManager.getNewestTempHumData(pgDevice);
                if(pgTempHumDto==null)
                    msg="没有满足要求的数据";
                else
                    msg="数据获取成功";
            }
        }catch(Exception e){
            e.printStackTrace();
            msg="设备监测数据获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgTempHumDto);
        return resultMap;
    }

    /**
     * 新增数据
     */
    @POST
    @Path("addTempHumData")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addTempHumData(@FormParam("devCode") String devCode,
                                         @FormParam("upTime") String upTime,
                                         @FormParam("temp") String temp,
                                         @FormParam("hum") String hum)throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            PgTempHum pgTempHum=new PgTempHum();
            PgDevice pgDevice=pgDeviceManager.getDeviceBydevCode(devCode);
            if (pgDevice== null) {
                result = "false";
                msg = "不存在编号为"+devCode+"的设备";
            } else {
                pgTempHum.setTemp(temp);
                pgTempHum.setHum(hum);
                pgTempHum.setLogTime(new Date());
                pgTempHum.setUpTime(sdf.parse(upTime));
                pgTempHum.setPgDeviceId(pgDevice);

                pgTempHumManager.save(pgTempHum);
                result = "true";
                msg = "保存成功";
            }
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }

        resultMap.put("result", result);
        resultMap.put("msg", msg);
        return resultMap;
    }


}
