package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgDevice;
import com.casic.PgInterface.devTable.dto.PgDeviceDto;
import com.casic.PgInterface.devTable.manager.PgDeviceManager;
import com.casic.PgInterface.devTable.manager.PgPartitionManager;
import com.casic.PgInterface.devTable.manager.PipeGalleryManager;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yxw on 2017/11/2.
 */
@Component
@Path("pgDevice")
public class PgDeviceRs {

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

    @Resource
    private PgDeviceManager pgDeviceManager;
    @Resource
    private PgPartitionManager pgPartitionManager;
    @Resource
    private PipeGalleryManager pipeGalleryManager;

    /**
     * 根据设备类型获取设备信息
     */
    @POST
    @Path("getPgDeviceByDevType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPgDeviceByDevType(@FormParam("devType") String devType) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgDeviceDto> pgDeviceDtoList=new ArrayList<PgDeviceDto>();
        try {
            pgDeviceDtoList = pgDeviceManager.getDeviceByDevTypee(devType);
            msg = "获取成功";
        } catch (Exception e) {
            msg = "获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgDeviceDtoList);
        return resultMap;
    }

    /**
     * 获取设备信息
     */
    @POST
    @Path("getPgDeviceByAssetCode")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPgDeviceByAssetCode(@FormParam("assetCode") String assetCode) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        PgDeviceDto pgDeviceDto=null;
        try {
            PgDevice pgDevice = pgDeviceManager.getDeviceByAssetCode(assetCode);
            pgDeviceDto=new PgDeviceDto(pgDevice);
            msg = "获取成功";
        } catch (Exception e) {
            msg = "获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgDeviceDto);
        return resultMap;
    }

    /**
     * 显示设备列表
     */
    @POST
    @Path("getPgDevice")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getDeviceList(@FormParam("road") String road,
                                             @FormParam("partition") String partition,
                                             @FormParam("devType") String devType)throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String msg="";
        List<PgDeviceDto> pgDeviceDtoList=new ArrayList<>();
        try {
            pgDeviceDtoList = pgDeviceManager.getAllDevice(road,partition,devType);
            if(pgDeviceDtoList.size()==0)
                msg="没有找到设备信息";
            else
                msg="设备信息获取成功";
        }catch(Exception e){
            e.printStackTrace();
            msg="设备信息获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgDeviceDtoList);
        return resultMap;
    }

    /**
     * 新增设备
     */
    @POST
    @Path("addDevice")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addDevice(@FormParam("partition") String partition,
                                         @FormParam("pipeGallery") String pipeGallery,
                                         @FormParam("road") String road,
                                         @FormParam("position") String position,
                                         @FormParam("devType") String devType,
                                         @FormParam("devCode") String devCode,
                                         @FormParam("assetName") String assetName,
                                         @FormParam("assetCode") String assetCode,
                                         @FormParam("assetBarCode") String assetBarCode,
                                         @FormParam("ip") String ip,
                                         @FormParam("modBus") String modBus,
                                         @FormParam("mome") String mome)
            throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try {
            if (pgDeviceManager.getDeviceByAssetCode(assetCode)!=null) {
                result = "false";
                msg = "已存在相同资产编号的设备："+assetCode;
            } else {
                PgDevice pgDevice = new PgDevice();

                pgDevice.setPipeGallery(pipeGallery);
                pgDevice.setPartition(partition);
                pgDevice.setRoad(road);
                pgDevice.setPosition(position);
                pgDevice.setDevType(devType);
                pgDevice.setDevCode(devCode);
                pgDevice.setAssetName(assetName);
                pgDevice.setAssetCode(assetCode);
                pgDevice.setAssetBarCode(assetBarCode);
                pgDevice.setIp(ip);
                pgDevice.setModBus(modBus);
                pgDevice.setMome(mome);
                pgDevice.setIsOpen(0);
                pgDevice.setActive(1);

                pgDeviceManager.save(pgDevice);
                result = "true";
                msg = "保存成功";
            }

        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }

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

        return resultMap;
    }

    /**
     * 编辑设备信息
     */
    @POST
    @Path("editDevice")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editPartition(@FormParam("id") String id,
                                             @FormParam("partition") String partition,
                                             @FormParam("pipeGallery") String pipeGallery,
                                             @FormParam("road") String road,
                                             @FormParam("position") String position,
                                             @FormParam("devType") String devType,
                                             @FormParam("devCode") String devCode,
                                             @FormParam("assetName") String assetName,
                                             @FormParam("assetCode") String assetCode,
                                             @FormParam("assetBarCode") String assetBarCode,
                                             @FormParam("ip") String ip,
                                             @FormParam("modBus") String modBus,
                                             @FormParam("mome") String mome,
                                             @FormParam("isOpen") String isOpen) throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try {
            /*
            if (pgDeviceManager.getDeviceByAssetCode(assetCode)!=null) {
                result = "false";
                msg = "已存在同名设备";
            } else {
            */
                PgDevice pgDevice = pgDeviceManager.get(Long.valueOf(id));
                pgDevice.setPipeGallery(pipeGallery);
                pgDevice.setPartition(partition);
                pgDevice.setRoad(road);
                pgDevice.setPosition(position);
                pgDevice.setDevType(devType);
                pgDevice.setDevCode(devCode);
                pgDevice.setAssetName(assetName);
                pgDevice.setAssetCode(assetCode);
                pgDevice.setAssetBarCode(assetBarCode);
                pgDevice.setIp(ip);
                pgDevice.setModBus(modBus);
                pgDevice.setMome(mome);
                pgDevice.setActive(1);
                pgDevice.setIsOpen(Integer.valueOf(isOpen));

                pgDeviceManager.save(pgDevice);
                result = "true";
                msg = "编辑成功";
            //}
        } catch (Exception e) {
            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }

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

    /**
     * 删除设备信息
     */
    @POST
    @Path("deleteDevice")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deleteDevice(@FormParam("id") String id)throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        String result="false";

        try {
            PgDevice pgDevice=pgDeviceManager.get(Long.valueOf(id));
            if(pgDevice==null) {
                msg = "不存在该设备信息";
                result="false";
            }
            else
            {
                pgDevice.setActive(0);
                pgDeviceManager.save(pgDevice);
                msg="设备删除成功";
                result="true";
            }
        }catch(Exception e){
            e.printStackTrace();
            msg="设备删除失败";
            result="false";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);
        return resultMap;
    }

    /**
     * 根据设备编号获取设备的资产信息
     */
    @POST
    @Path("getDeviceByDevCode")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getDeviceByDevCode(@FormParam("devCode") String devCode) throws Exception
    {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        PgDeviceDto pgDeviceDto=null;

        try{
            PgDevice pgDevice=pgDeviceManager.getDeviceBydevCode(devCode);
            pgDeviceDto=new PgDeviceDto(pgDevice);
            if(pgDeviceDto==null)
                msg="设备信息获取失败";
            else
                msg="设备信息获取成功";
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            msg="设备状态获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgDeviceDto);
        return resultMap;
    }

    /*
    *根据分区获取设备列表
     */
    @POST
    @Path("getDeviceByPartition")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getDeviceByPartition(@FormParam("partition") String partition) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        List<PgDeviceDto> pgDeviceDtoList=new ArrayList<PgDeviceDto>();

        try{
            pgDeviceDtoList=pgDeviceManager.getPgDeviceDtoListByPartition(partition);
            if(pgDeviceDtoList==null)
                msg="设备信息获取失败";
            else
                msg="设备信息获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="设备信息获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgDeviceDtoList);

        return resultMap;
    }


    @POST
    @Path("getDeviceByOldDAssetCodeCode")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getDeviceByOldDAssetCodeCode(@FormParam("assetCode") String assetCode) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        List<PgDeviceDto> pgDeviceDtoList=new ArrayList<PgDeviceDto>();

        try{
            pgDeviceDtoList=pgDeviceManager.getDeviceByOldDAssetCodeCode(assetCode);
            if(pgDeviceDtoList==null)
                msg="设备信息获取失败";
            else
                msg="设备信息获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="设备信息获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgDeviceDtoList);

        return resultMap;
    }



}
