package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgDeviceType;
import com.casic.PgInterface.devTable.dto.PgDeviceTypeDto;
import com.casic.PgInterface.devTable.manager.PgDeviceTypeManager;
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 2018/1/8.
 */
@Component
@Path("pgDeviceType")
public class PgDeviceTypeRs {

    private static Logger logger = LoggerFactory.getLogger(PgDeviceTypeRs.class);
    @Resource
    private PgDeviceTypeManager pgDeviceTypeManager;

    /**
     * 显示设备列表
     */
    @POST
    @Path("getDeviceType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getDeviceTpeyList()throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        List<PgDeviceTypeDto> pgDeviceTypeDtoList=new ArrayList<PgDeviceTypeDto>();
        try {
            pgDeviceTypeDtoList = pgDeviceTypeManager.getAllDeviceType();
            if(pgDeviceTypeDtoList.size()==0)
                msg="没有找到设备类型信息";
            else
                msg="设备类型信息获取成功";
        }catch(Exception e){
            e.printStackTrace();
            msg="设备类型信息获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgDeviceTypeDtoList);
        return resultMap;
    }

    /**
     * 新增设备
     */
    @POST
    @Path("addDeviceType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addDevice(@FormParam("devType") String devType)
            throws Exception {

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

        try {
            if (pgDeviceTypeManager.getDeviceTypeByDeType(devType)!=null) {
                result = "false";
                msg = "已存在设备类型:"+devType;
            } else {
                PgDeviceType pgDeviceType = new PgDeviceType();

                pgDeviceType.setDevType(devType);
                pgDeviceType.setActive(1);

                pgDeviceTypeManager.save(pgDeviceType);
                result = "true";
                msg = "保存成功";
            }

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

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

        return resultMap;
    }


}
