package com.casic.PgInterface.rs;

import com.casic.PgInterface.devData.domain.PgSh;
import com.casic.PgInterface.devData.dto.PgHistoryDataDto;
import com.casic.PgInterface.devData.dto.PgShDto;
import com.casic.PgInterface.devData.manager.PgShManager;
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("pgSh")
public class PgShRs {

    private static Logger logger = LoggerFactory.getLogger(PgO2Rs.class);

    @Resource
    private PgDeviceManager pgDeviceManager;

    @Resource
    private PgShManager pgShManager;

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

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

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

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

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

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

}