package com.casic.PgInterface.rs;

import com.casic.PgInterface.devData.domain.PgO2;
import com.casic.PgInterface.devData.dto.PgHistoryDataDto;
import com.casic.PgInterface.devData.dto.PgO2Dto;
import com.casic.PgInterface.devData.manager.PgO2Manager;
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("pgO2")
public class PgO2Rs {

    private static Logger logger = LoggerFactory.getLogger(PgO2Rs.class);
    @Resource
    private PgDeviceManager pgDeviceManager;
    @Resource
    private PgO2Manager pgO2Manager;

    /**
     * 获取设备监测数据
     */
    @POST
    @Path("getO2InfoByDevAndDate")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getO2InfoByDevAndDate(@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=pgO2Manager.getAllO2Data(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("getNewestO2InfoByDev")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getNewestO2InfoByDev(@FormParam("assetCode") String assetCode)throws Exception {

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

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

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

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

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


}
