Newer
Older
PgInterface / src / main / java / com / casic / PgInterface / rs / PgDeviceRs.java
xiaowei on 23 Nov 2017 9 KB 新增设备类型表
package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgDevice;
import com.casic.PgInterface.devTable.domain.PgPartition;
import com.casic.PgInterface.devTable.domain.PipeGallery;
import com.casic.PgInterface.devTable.dto.PgDeviceDto;
import com.casic.PgInterface.devTable.manager.PgDeviceManager;
import com.casic.PgInterface.devTable.manager.PgDeviceTypeManager;
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.text.SimpleDateFormat;
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 PgDeviceTypeManager pgDeviceTypeManager;

    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;
    }

    @Resource
    public void setPgDeviceTypeManager(PgDeviceTypeManager pgDeviceTypeManager) {
        this.pgDeviceTypeManager = pgDeviceTypeManager;
    }

    /**
     * 获取设备信息
     */
    @POST
    @Path("getPgDeviceByName")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPgDeviceByName(@FormParam("devName") String devName) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        PgDeviceDto pgDeviceDto=null;
        try {
            PgDevice pgDevice = pgDeviceManager.getDeviceBydevName(devName);
            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> addLog(@FormParam("devType") String devType,
                                      @FormParam("devCode") String devCode,
                                      @FormParam("devName") String devName,
                                      @FormParam("brand") String brand,
                                      @FormParam("ownerUnit") String ownerUnit,
                                      @FormParam("installDate") String installDate,
                                      @FormParam("pgName") String pgName,
                                      @FormParam("parName") String parName)
            throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

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

        PipeGallery pipeGallery = pipeGalleryManager.getPipeGalleryByPgName(pgName);
        PgPartition pgPartition = pgPartitionManager.getPartitionByParName(parName, pipeGallery);
        if (pgPartition == null) {
            result = "false";
            msg = "不存在管廊仓和分区信息";
        } else {
            try {
                if (pgDeviceManager.getDeviceBydevName(devName)!=null) {
                    result = "false";
                    msg = "已存在同名设备";
                } else {
                    PgDevice pgDevice = new PgDevice();

                    pgDevice.setDevTypeId(pgDeviceTypeManager.getPgDeviceTypeByDevType(devType));
                    pgDevice.setDevCode(devCode);
                    pgDevice.setDevName(devName);
                    pgDevice.setBrand(brand);
                    pgDevice.setOwnerUnit(ownerUnit);
                    pgDevice.setInstallDate(sdf.parse(installDate));
                    pgDevice.setPgPartitionId(pgPartition);
                    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("devType") String devType,
                                             @FormParam("devCode") String devCode,
                                             @FormParam("devName") String devName,
                                             @FormParam("brand") String brand,
                                             @FormParam("ownerUnit") String ownerUnit,
                                             @FormParam("installDate") String installDate,
                                             @FormParam("pgName") String pgName,
                                             @FormParam("parName") String parName,
                                             @FormParam("isOpen") String isOpen) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

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

        PipeGallery pipeGallery = pipeGalleryManager.getPipeGalleryByPgName(pgName);
        PgPartition pgPartition = pgPartitionManager.getPartitionByParName(parName, pipeGallery);
        if (pgPartition == null) {
            result = "false";
            msg = "不存在管廊仓和分区信息";
        } else {
            try {
                if (pgDeviceManager.getDeviceBydevName(devName)!=null) {
                    result = "false";
                    msg = "已存在同名设备";
                } else {
                    PgDevice pgDevice = pgDeviceManager.get(Long.valueOf(id));
                    pgDevice.setDevTypeId(pgDeviceTypeManager.getPgDeviceTypeByDevType(devType));
                    pgDevice.setDevCode(devCode);
                    pgDevice.setDevName(devName);
                    pgDevice.setBrand(brand);
                    pgDevice.setOwnerUnit(ownerUnit);
                    pgDevice.setInstallDate(sdf.parse(installDate));
                    pgDevice.setPgPartitionId(pgPartition);
                    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;
    }


}