Newer
Older
PgInterface / src / main / java / com / casic / PgInterface / rs / PgDeviceRs.java
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);

    private PgDeviceManager pgDeviceManager;

    private PgPartitionManager pgPartitionManager;

    private PipeGalleryManager pipeGalleryManager;

    @Resource
    public void setPgDeviceManager(PgDeviceManager pgDeviceManager) {
        this.pgDeviceManager = pgDeviceManager;
    }

    @Resource
    public void setPgPartitionManager(PgPartitionManager pgPartitionManager) {
        this.pgPartitionManager = pgPartitionManager;
    }

    @Resource
    public void setPipeGalleryManager(PipeGalleryManager pipeGalleryManager) {
        this.pipeGalleryManager = pipeGalleryManager;
    }

    /**
     * 获取设备信息
     */
    @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()throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        List<PgDeviceDto> pgDeviceDtoList=new ArrayList<PgDeviceDto>();
        try {
            pgDeviceDtoList = pgDeviceManager.getAllDevice();
            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 = "已存在同名设备";
            } 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("success", 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;
    }


}