package com.casic.PgInterface.rs;

import com.casic.PgInterface.patroler.domain.PgAuthority;
import com.casic.PgInterface.patroler.domain.PgRole;
import com.casic.PgInterface.patroler.dto.PgRoleDto;
import com.casic.PgInterface.patroler.manager.PgAuthorityManager;
import com.casic.PgInterface.patroler.manager.PgRoleManager;
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("pgRole")
public class PgRoleRs {

    private static Logger logger = LoggerFactory.getLogger(PgRole.class);
    @Resource
    private PgRoleManager pgRoleManager;
    @Resource
    private PgAuthorityManager pgAuthorityManager;

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

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

        try{
            PgAuthority pgAuthority=pgAuthorityManager.getAuthorityByAuthority(authority);
            PgRole pgRole=new PgRole();
            pgRole.setRole(role);
            pgRole.setPgAuthorityId(pgAuthority);
            pgRole.setActive(1);

            pgRoleManager.save(pgRole);
            msg="用户角色保存成功";
            result="true";
        }
        catch(Exception e) {
            e.printStackTrace();
            msg = "新增角色失败";
            result = "false";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);

        return resultMap;
    }

    /*
    *编辑角色信息
     */
    @POST
    @Path("editRole")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> editRole(@FormParam("id") String id,
                                       @FormParam("role") String role,
                                       @FormParam("authority") String authority) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        try {
            PgRole pgRole = pgRoleManager.get(Long.valueOf(id));

            PgAuthority pgAuthority = pgAuthorityManager.getAuthorityByAuthority(authority);
            pgRole.setRole(role);
            pgRole.setPgAuthorityId(pgAuthority);
            pgRoleManager.save(pgRole);

            msg="角色保存成功";
            result="true";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="角色编辑失败";
            result="false";
        }

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

        return resultMap;
    }

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

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

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

}
