package com.casic.PgInterface.rs;

import com.casic.PgInterface.devData.domain.PgCo;
import com.casic.PgInterface.devData.dto.PgCoDto;
import com.casic.PgInterface.devData.dto.PgHistoryDataDto;
import com.casic.PgInterface.devData.manager.PgCoManager;
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("pgCo")
public class PgCoRs {

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

    /**
     * 获取设备监测数据
     */
    @POST
    @Path("getCoInfoByDevAndDate")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getCoInfoByDevAndDate(@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<PgHistoryDataDto> pgHistoryDataDtoList=new ArrayList<>();

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

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

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

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


    /**
     * 新增数据
     */
    @POST
    @Path("addCoData")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addCoData(@FormParam("devCode") String devCode,
                                          @FormParam("upTime") String upTime,
                                          @FormParam("co") String co)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 {
            PgCo pgCo=new PgCo();
            PgDevice pgDevice=pgDeviceManager.getDeviceBydevCode(devCode);
            if (pgDevice== null) {
                result = "false";
                msg = "不存在编号为"+devCode+"的设备";
            } else {
                pgCo.setCo(co);
                pgCo.setLogTime(new Date());
                pgCo.setUpTime(sdf.parse(upTime));
                pgCo.setPgDeviceId(pgDevice);

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

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

}
