package com.casic.PgInterface.rs;

import com.casic.PgInterface.patroler.domain.PgAuthority;
import com.casic.PgInterface.patroler.dto.PgAuthorityDto;
import com.casic.PgInterface.patroler.manager.PgAuthorityManager;
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/3/30.
 */
@Component
@Path("pgAuthority")
public class PgAuthorityRs {
    private static Logger logger = LoggerFactory.getLogger(PgAuthority.class);

    @Resource
    private PgAuthorityManager pgAuthorityManager;

    /**
     * 新增权限
     */
    @POST
    @Path("addAuthority")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> addAuthority(@FormParam("authority") String authority,
                                            @FormParam("concent") String concent) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try{
            PgAuthority pgAuthority=new PgAuthority();

            pgAuthority.setAuthority(authority);
            pgAuthority.setConcent(concent);
            pgAuthority.setActive(1);

            pgAuthorityManager.save(pgAuthority);
            msg="用户权限保存成功";
            result="true";
        }
        catch(Exception e) {
            e.printStackTrace();
            msg = "新增权限失败";
            result = "false";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);

        return resultMap;
    }

    /*
       *编辑角权限信息
        */
    @POST
    @Path("editAuthority")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> editAuthority(@FormParam("id") String id,
                                       @FormParam("authority") String authority,
                                       @FormParam("concent") String concent) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        try {
            PgAuthority pgAuthority=pgAuthorityManager.get(Long.valueOf(id));
            pgAuthority.setAuthority(authority);
            pgAuthority.setConcent(concent);

            pgAuthorityManager.save(pgAuthority);
            msg="权限保存成功";
            result="true";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="权限编辑失败";
            result="false";
        }

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

        return resultMap;
    }

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

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

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

    /*
* 获取权限信息
 */
    @POST
    @Path("getAuthorityList")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getAuthorityList() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgAuthorityDto> pgAuthorityDtoList = new ArrayList<PgAuthorityDto>();
        try {
            pgAuthorityDtoList = pgAuthorityManager.getAuthorityList();
            if (pgAuthorityDtoList == null)
                msg = "不存在权限信息";
            else
                msg = "权限信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "权限信息获取是失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgAuthorityDtoList);
        return resultMap;
    }




}
