汎用バックグラウンド管理システム(6)-管理者インタフェース、実装、コントローラの作成

17503 ワード

管理者インタフェース
package com.sundablog.service.backend.system.upms.user;

import com.sundablog.pojo.AdminUser;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.result.LayUiResult;

/**
 *         
 * @ClassName:  UserService   
 * @Description:           
 * @author:    
 * @date:   2018 3 14    6:59:29   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
public interface UserService {

    /**
     *        
     * @Title: addAdminUser   
     * @Description: TODO(               )   
     * @param: @param adminUser
     * @param: @return      
     * @return: int      
     * @throws
     */
    int addAdminUser(AdminUser adminUser,int roleId);
    
    /**
     *        
     * @Title: updataAdminUser   
     * @Description: TODO(               )   
     * @param: @param adminUser
     * @param: @return      
     * @return: int      
     * @throws
     */
    int updataAdminUser(AdminUser adminUser,int roleId);
    
    /**
     *        
     * @Title: updataDisableUserId   
     * @Description:        
     * @param: @param userId
     * @param: @return      
     * @return: int      
     * @throws
     */
    int updataDisableUserId(int userId);
    
    /**
     *     
     * @Title: delectUser   
     * @Description: TODO(               )   
     * @param: @param userId
     * @param: @return      
     * @return: int      
     * @throws
     */
    int delectUser(int userId);
    
    /**
     *     ID       
     * @Title: selectAdminUserByUserId   
     * @Description: TODO(               )   
     * @param: @param userId
     * @param: @return      
     * @return: AdminUser      
     * @throws
     */
    AdminUser selectAdminUserByUserId(int userId);
    
    /**
     *            
     * @Title: selectAdminUserByUserName   
     * @Description: TODO(               )   
     * @param: @param UserName
     * @param: @return      
     * @return: AdminUser      
     * @throws
     */
    AdminUser selectAdminUserByUserName(String UserName);
    
    
    /**
     *     
     * @Title: findPagingQuery   
     * @Description: TODO(               )   
     * @param: @param page
     * @param: @param limit
     * @param: @return      
     * @return: LayUiResult      
     * @throws
     */
    LayUiResult findPagingQuery(int page,int limit);
    
    /**
     *     ID            
     * @Title: selectByUserId   
     * @Description: TODO(               )   
     * @param: @param userId
     * @param: @return      
     * @return: AdminUserRole      
     * @throws
     */
    AdminUserRole selectByUserId(int userId);
    
}

管理者実装
package com.sundablog.service.impl.backend.system.upms.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;
import com.sundablog.mapper.AdminUserMapper;
import com.sundablog.mapper.AdminUserRoleMapper;
import com.sundablog.pojo.AdminUser;
import com.sundablog.pojo.AdminUserExample;
import com.sundablog.pojo.AdminUserExample.Criteria;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.pojo.AdminUserRoleExample;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.backend.system.upms.filesystem.FilesyStemService;
import com.sundablog.service.backend.system.upms.user.UserService;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;

/**
 *         
 * @ClassName:  UserServiceImpl   
 * @Description:TODO(              )   
 * @author:    
 * @date:   2018 3 14    7:02:19   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private AdminUserMapper adminUserMapper;
    
    @Autowired
    private FilesyStemService filesyStemService;
    
    @Autowired
    private AdminUserRoleMapper adminUserRoleMapper;
    /**
     *      
     * Title: addAdminUser   
     * Description:    
     * @param adminUser
     * @return   -2(       )
     * @see com.sundablog.service.backend.system.upms.user.UserService#addAdminUser(com.sundablog.pojo.AdminUser)
     */
    @Override
    public int addAdminUser(AdminUser adminUser,int roleId) {
        //           
        AdminUserExample example = new AdminUserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(adminUser.getUsername());
        List list = adminUserMapper.selectByExample(example);
        if (list.size() > 0) {
            return -2;
        } 
        //       
        String salt = RandomUtil.simpleUUID();
        String md5PWD = DigestUtil.md5Hex(adminUser.getPassword() + salt);
        adminUser.setSalt(salt);
        adminUser.setPassword(md5PWD);
        adminUser.setCtime(new Date());
        adminUser.setLocked((byte) 0);
        int i = adminUserMapper.insertSelective(adminUser);
        if (i > 0) {
            //           
            AdminUserExample example1 = new AdminUserExample();
            Criteria criteria1 = example1.createCriteria();
            criteria1.andUsernameEqualTo(adminUser.getUsername());
            List list2 = adminUserMapper.selectByExample(example1);
            AdminUser user = list2.get(0);  
            //      
            AdminUserRole adminUserRole = new AdminUserRole();
            adminUserRole.setUserId(user.getUserId());
            adminUserRole.setRoleId(roleId);
            
            int j = adminUserRoleMapper.insert(adminUserRole);
            return j;
        } else {
            return i;
        }
        
        
    }

    /**
     *       
     * Title: updataAdminUser   
     * Description:    
     * @param adminUser
     * @return   
     * @see com.sundablog.service.backend.system.upms.user.UserService#updataAdminUser(com.sundablog.pojo.AdminUser)
     */
    @Override
    public int updataAdminUser(AdminUser adminUser,int roleId) {
        AdminUser user = adminUserMapper.selectByPrimaryKey(adminUser.getUserId());
        
        if (StrUtil.isEmpty(adminUser.getPassword())) {
            adminUser.setPassword(user.getPassword());
        } else {
            String md5PWD = DigestUtil.md5Hex(adminUser.getPassword() + user.getSalt());
            adminUser.setPassword(md5PWD);
        }
        if (StrUtil.isEmpty(adminUser.getAvatar())) {
            adminUser.setAvatar(user.getAvatar());
        }
        
        
        
        int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
        if (i > 0) {
            AdminUserRoleExample example = new AdminUserRoleExample();
            com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(adminUser.getUserId());
            List list = adminUserRoleMapper.selectByExample(example);
            AdminUserRole userRole = list.get(0);
            userRole.setRoleId(roleId);
            int j = adminUserRoleMapper.updateByPrimaryKeySelective(userRole);
            return j;
        }
        
        return i;
    }

    /**
     *        (         ,       )
     * Title: updataDisableUserId   
     * Description:    
     * @param userId
     * @return   
     * @see com.sundablog.service.backend.system.upms.user.UserService#updataDisableUserId(int)
     */
    @Override
    public int updataDisableUserId(int userId) {
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(userId);
        if (0 == adminUser.getLocked().intValue()) {
            adminUser.setLocked((byte) 1);
        } else {
            adminUser.setLocked((byte) 0);
        }
        int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
        
        return i;
    }

    /**
     *     ID      
     * Title: selectAdminUserByUserId   
     * Description:    
     * @param userId
     * @return   
     * @see com.sundablog.service.backend.system.upms.user.UserService#selectAdminUserByUserId(int)
     */
    @Override
    public AdminUser selectAdminUserByUserId(int userId) {
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(userId);
        
        String downloadUrl = filesyStemService.getDownloadUrl(adminUser.getAvatar());
        adminUser.setAvatar(downloadUrl);
        return adminUser;
    }

    /**
     *     
     * Title: findPagingQuery   
     * Description:    
     * @param page
     * @param limit
     * @return   
     * @see com.sundablog.service.backend.system.upms.user.UserService#findPagingQuery(int, int)
     */
    @Override
    public LayUiResult findPagingQuery(int page, int limit) {
        PageHelper.startPage(page, limit);
        AdminUserExample example = new AdminUserExample();
        List list = adminUserMapper.selectByExample(example);
        for (AdminUser adminUser : list) {
            String avatar = filesyStemService.getDownloadUrl(adminUser.getAvatar());
            adminUser.setAvatar(avatar);
        }
        PageInfo pageInfo = new PageInfo<>(list);
        LayUiResult layUiResult = new LayUiResult();
        layUiResult.setCode(0);
        layUiResult.setMsg("");
        layUiResult.setCount(pageInfo.getTotal());
        layUiResult.setData(list);
        return layUiResult;
    }

    /**
     *     ID            
     * Title: selectByUserId   
     * Description:    
     * @param userId
     * @return   
     * @see com.sundablog.service.backend.system.upms.user.UserService#selectByUserId(int)
     */
    @Override
    public AdminUserRole selectByUserId(int userId) {
        AdminUserRoleExample example = new AdminUserRoleExample();
        com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List list = adminUserRoleMapper.selectByExample(example);
        return list.get(0);
    }

    /**
     *     
     * Title: delectUser   
     * Description:    
     * @param userId
     * @return   
     * @see com.sundablog.service.backend.system.upms.user.UserService#delectUser(int)
     */
    @Override
    public int delectUser(int userId) {
        AdminUserRoleExample example = new AdminUserRoleExample();
        com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        int i = adminUserRoleMapper.deleteByExample(example);
        if (i > 0) {
            int j = adminUserMapper.deleteByPrimaryKey(userId);
            return j;
        } else {
            return 0;
        }
    }

    @Override
    public AdminUser selectAdminUserByUserName(String UserName) {
        AdminUserExample example = new AdminUserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(UserName);
        List list = adminUserMapper.selectByExample(example);
        return list.get(0);
    }

}

せいぎょそうち
package com.sundablog.controller.backend.system.upms.user;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sundablog.pojo.AdminUser;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.result.BaseResult;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.backend.system.upms.user.UserService;

/**
 *           
 * @ClassName:  UserController   
 * @Description:          
 * @author:    
 * @date:   2018 3 14    7:10:35   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("/backend/system/upms/user")
public class UserController {

    @Autowired
    public UserService userService;
    
    /**
     *          
     * @Title: index   
     * @Description:             
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_user_index")
    @RequestMapping("/index")
    public String index() {
        return "backend/system/upms/user/index";
    }
    
    /**
     *        
     * @Title: addIndex   
     * @Description:         
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_user_add")
    @RequestMapping("/add")
    public String addIndex() {
        return "backend/system/upms/user/add";
    }
    
    /**
     *        
     * @Title: delectUser   
     * @Description:        
     * @param: @param userId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_user_delectUser")
    @RequestMapping("/delectUser")
    @ResponseBody
    public BaseResult delectUser(int userId) {
        int i = userService.delectUser(userId);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.delectSuccess); 
        } else {
            return BaseResult.build(209, BaseResult.delectFail);
        }
    }
    
    /**
     *        
     * @Title: editIndex   
     * @Description:        
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_user_edit")
    @RequestMapping("/edit")
    public String editIndex() {
        return "backend/system/upms/user/edit";
    }
    
    /**
     *        
     * @Title: updataDisableUserId   
     * @Description: TODO(               )   
     * @param: @param userId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_user_disable")
    @RequestMapping("/updataDisableUserId")
    @ResponseBody
    public BaseResult updataDisableUserId(int userId) {
        int i = userService.updataDisableUserId(userId);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.updataSuccess);
        } else {
            return BaseResult.build(201, BaseResult.updataFail);
        }
    }
    
    
    /**
     *     
     * @Title: findPagingQuery   
     * @Description: TODO(               )   
     * @param: @param page
     * @param: @param limit
     * @param: @return      
     * @return: LayUiResult      
     * @throws
     */
    @RequestMapping("/findPagingQuery")
    @ResponseBody
    public LayUiResult findPagingQuery(@RequestParam(value="page", defaultValue="1") int page,@RequestParam(value="limit", defaultValue="10") int limit) {
        LayUiResult result = userService.findPagingQuery(page, limit);
        return result;
    }
    
    /**
     *      
     * @Title: addAdminUser   
     * @Description:       
     * @param: @param adminUser
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/addAdminUser")
    @ResponseBody
    public BaseResult addAdminUser(AdminUser adminUser,int roleId) {
        int i = userService.addAdminUser(adminUser,roleId);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.addSuccess);
        } else {
            return BaseResult.build(209, BaseResult.addFail);
        }
    }
    
    /**
     *     ID            
     * @Title: selectByUserId   
     * @Description:     ID            
     * @param: @param userId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/selectByUserId")
    @ResponseBody
    public BaseResult selectByUserId(int userId) {
        AdminUserRole userRole = userService.selectByUserId(userId);
        return BaseResult.build(200, "", userRole);
    }
    
    /**
     *     ID       
     * @Title: selectAdminUserByUserId   
     * @Description:     ID       
     * @param: @param userId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/selectAdminUserByUserId")
    @ResponseBody
    public BaseResult selectAdminUserByUserId(int userId) {
        AdminUser adminUser = userService.selectAdminUserByUserId(userId);
        return BaseResult.build(200, "", adminUser);
    }
    
    /**
     *       
     * @Title: updataAdminUser   
     * @Description:       
     * @param: @param adminUser
     * @param: @param roleId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/updataAdminUser")
    @ResponseBody
    public BaseResult updataAdminUser(AdminUser adminUser,int roleId) {
        int i = userService.updataAdminUser(adminUser,roleId);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.updataSuccess);
        } else {
            return BaseResult.build(209, BaseResult.updataFail);
        }
    }
}