Newer
Older
PgInterface / src / main / java / com / casic / PgInterface / rs / PgUserRs.java
xiaowei on 30 Mar 2018 6 KB 完成角色和权限管理功能
package com.casic.PgInterface.rs;

import com.casic.PgInterface.patroler.domain.PgRole;
import com.casic.PgInterface.patroler.domain.PgUser;
import com.casic.PgInterface.patroler.dto.PgUserDto;
import com.casic.PgInterface.patroler.manager.PgRoleManager;
import com.casic.PgInterface.patroler.manager.PgUserManager;
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/4.
 */
@Component
@Path("pgUser")
public class PgUserRs {

    private static Logger logger = LoggerFactory.getLogger(PgUserRs.class);

    private PgUserManager pgUserManager;

    private PgRoleManager pgRoleManager;

    @Resource
    public void setPgUserManager(PgUserManager pgUserManager) {
        this.pgUserManager = pgUserManager;
    }

    @Resource
    public void setPgRoleManager(PgRoleManager pgRoleManager) {
        this.pgRoleManager = pgRoleManager;
    }

    /**
     * 显示人员列表
     */
    @POST
    @Path("getUserList")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getUserList()throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        List<PgUserDto> pgUserDtoList=new ArrayList<PgUserDto>();
        try {
            pgUserDtoList = pgUserManager.getAllUser();
            if(pgUserDtoList.size()==0)
                msg="没有找到人员信息";
            else
                msg="获取成功";
        }catch(Exception e){
            e.printStackTrace();
            msg="获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgUserDtoList);
        return resultMap;
    }

    /**
     * 新增人员
     */
    @POST
    @Path("addUser")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addUser(@FormParam("userName") String userName,
                                           @FormParam("password") String password,
                                           @FormParam("department") String department,
                                           @FormParam("post") String post,
                                           @FormParam("phoneNumber") String phoneNumber,
                                           @FormParam("role") String role)throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try {
            PgRole pgRole=pgRoleManager.getPgRoleByRole(role);

            if (pgUserManager.userNameIsExits(userName)) {
                result = "false";
                msg = "用户名已经存在!";
            } else {
                PgUser pgUser = new PgUser();

                pgUser.setUserName(userName);
                //String pass = StringUtils.getMD5(password);
                pgUser.setPassword(password);
                pgUser.setDepartment(department);
                pgUser.setPost(post);
                pgUser.setPhoneNumber(phoneNumber);
                pgUser.setPgRoleId(pgRole);
                pgUser.setActive(1);
                logger.info("成功保存");

                pgUserManager.save(pgUser);
                result = "true";
                msg = "保存成功";
            }
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }

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

    /**
     * 编辑人员
     */
    @POST
    @Path("editUser")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editUser(@FormParam("id") String id,
                                             @FormParam("userName") String userName,
                                             @FormParam("password") String password,
                                             @FormParam("department") String department,
                                             @FormParam("post") String post,
                                             @FormParam("phoneNumber") String phoneNumber,
                                             @FormParam("role") String role) throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        try {
            /*
            if (pgUserManager.userNameIsExits(userName)) {
                result = "false";
                msg = "用户名已经存在!";
            } else {
            */
            PgRole pgRole=pgRoleManager.getPgRoleByRole(role);

            PgUser pgUser = pgUserManager.get(Long.valueOf(id));
            pgUser.setUserName(userName);
            pgUser.setPassword(password);
            pgUser.setDepartment(department);
            pgUser.setPost(post);
            pgUser.setPhoneNumber(phoneNumber);
            pgUser.setPgRoleId(pgRole);
            pgUserManager.save(pgUser);
            result = "true";
            msg = "编辑成功";
            //}
        } catch (Exception e) {
            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }

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

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

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

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


}