package com.casic.PgInterface.rs;

import com.casic.PgInterface.devData.domain.PgCh4;
import com.casic.PgInterface.devData.dto.PgCh4Dto;
import com.casic.PgInterface.devData.dto.PgHistoryDataDto;
import com.casic.PgInterface.devData.manager.PgCh4Manager;
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("pgCh4")
public class PgCh4Rs {

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

    @Resource
    private PgCh4Manager pgCh4Manager;

    @Resource
    private PgDeviceManager pgDeviceManager;

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

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

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

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

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

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

}
