xiaoyong931011
2022-05-26 ad2c2d53d756c8c9caa6e043c1868cfdf9736b9d
20220526
40 files added
4 files modified
1500 ■■■■■ changed files
src/main/java/com/xcong/farmer/cms/FarmerCmsApplication.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/common/response/Result.java 9 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/common/system/base/BaseEntity.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminMenuController.java 60 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminRoleController.java 60 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminUserController.java 82 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminAddMenuDto.java 26 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminAddRoleDto.java 20 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminAddUserDto.java 28 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminMenuDto.java 17 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminRoleDto.java 19 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminUpdateMenuDto.java 29 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminUpdateRoleDto.java 23 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminUpdateUserDto.java 32 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminUserDto.java 20 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/entity/MenuEntity.java 35 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/entity/RoleEntity.java 17 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/entity/RoleMenuEntity.java 16 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/entity/UserEntity.java 43 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/entity/UserRoleEntity.java 16 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/mapper/MenuMapper.java 14 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/mapper/RoleMapper.java 14 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/mapper/RoleMenuMapper.java 14 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/mapper/UserMapper.java 14 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/mapper/UserRoleMapper.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/service/IMenuService.java 21 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/service/IRoleService.java 21 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/service/IUserService.java 27 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/MenuServiceImpl.java 171 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/RoleServiceImpl.java 169 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/UserServiceImpl.java 187 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminMenuInfoVo.java 17 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminMenuVo.java 40 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminRoleVo.java 26 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminSeeMenuInfoVo.java 28 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminSeeRoleInfoVo.java 25 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminSeeUserInfoVo.java 32 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminUserVo.java 38 ●●●●● patch | view | raw | blame | history
src/main/resources/application.yml 2 ●●● patch | view | raw | blame | history
src/main/resources/mapper/MenuMapper.xml 20 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/RoleMapper.xml 20 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/RoleMenuMapper.xml 13 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/UserMapper.xml 20 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/UserRoleMapper.xml 5 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/FarmerCmsApplication.java
@@ -13,6 +13,7 @@
 */
@EnableSwagger2
@SpringBootApplication
@MapperScan("com.xcong.farmer.cms.modules.*.mapper")
@MapperScan("com.xcong.farmer.cms.modules.*.dao")
public class FarmerCmsApplication implements CommandLineRunner {
src/main/java/com/xcong/farmer/cms/common/response/Result.java
@@ -16,7 +16,7 @@
    private static final long serialVersionUID = 1L;
    private static final int SUCCESS = 0;
    private static final int SUCCESS = 200;
    private static final int FAIL = -1;
@@ -38,6 +38,13 @@
        return result;
    }
    public static Result ok(Object data) {
        Result result = new Result();
        result.code = SUCCESS;
        result.data = data;
        return result;
    }
    public static Result ok(String msg, Object data) {
        Result result = new Result();
        result.code = SUCCESS;
src/main/java/com/xcong/farmer/cms/common/system/base/BaseEntity.java
@@ -30,4 +30,6 @@
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;
    private Integer version;
}
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminMenuController.java
New file
@@ -0,0 +1,60 @@
package com.xcong.farmer.cms.modules.system.Controller;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.*;
import com.xcong.farmer.cms.modules.system.service.IMenuService;
import com.xcong.farmer.cms.modules.system.service.IRoleService;
import com.xcong.farmer.cms.modules.system.vo.AdminMenuVo;
import com.xcong.farmer.cms.modules.system.vo.AdminRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
@RestController
@RequestMapping(value = "/menu")
@Slf4j
@Api(value = "系统菜单", tags = "系统菜单")
public class AdminMenuController {
    @Resource
    private IMenuService iMenuService;
    @ApiOperation(value = "系统菜单分页列表", notes = "系统菜单分页列表")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = AdminMenuVo.class)})
    @PostMapping(value = "/menuInPage")
    public Result getMenuInPage(@RequestBody @Valid AdminMenuDto adminMenuDto) {
        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
        return iMenuService.getMenuInPage(adminMenuDto);
    }
    @ApiOperation(value = "添加系统菜单", notes = "添加系统菜单")
    @PostMapping(value = "/addMenu")
    public Result addMenu(@RequestBody @Valid AdminAddMenuDto adminAddMenuDto) {
        return iMenuService.addMenu(adminAddMenuDto);
    }
    @ApiOperation(value = "删除系统菜单", notes = "删除系统菜单")
    @GetMapping(value = "/deleteMenu/{id}")
    public Result deleteMenu(@PathVariable(value = "id") Long id) {
        return iMenuService.deleteMenu(id);
    }
    @ApiOperation(value = "查看系统菜单详情", notes = "查看系统菜单详情")
    @GetMapping(value = "/seeMenuInfo/{id}")
    public Result seeMenuInfo(@PathVariable(value = "id") Long id) {
        return iMenuService.seeMenuInfo(id);
    }
    @ApiOperation(value = "更新系统菜单", notes = "更新系统菜单")
    @PostMapping(value = "/updateMenu")
    public Result updateMenu(@RequestBody @Valid AdminUpdateMenuDto adminUpdateMenuDto) {
        return iMenuService.updateMenu(adminUpdateMenuDto);
    }
}
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminRoleController.java
New file
@@ -0,0 +1,60 @@
package com.xcong.farmer.cms.modules.system.Controller;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.*;
import com.xcong.farmer.cms.modules.system.service.IRoleService;
import com.xcong.farmer.cms.modules.system.service.IUserService;
import com.xcong.farmer.cms.modules.system.vo.AdminRoleVo;
import com.xcong.farmer.cms.modules.system.vo.AdminUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
@RestController
@RequestMapping(value = "/role")
@Slf4j
@Api(value = "系统角色", tags = "系统角色")
public class AdminRoleController {
    @Resource
    private IRoleService iRoleService;
    @ApiOperation(value = "系统角色分页列表", notes = "系统角色分页列表")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = AdminRoleVo.class)})
    @PostMapping(value = "/userInPage")
    public Result getUserInPage(@RequestBody @Valid AdminRoleDto adminRoleDto) {
        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
        return iRoleService.getRoleInPage(adminRoleDto);
    }
    @ApiOperation(value = "添加系统角色", notes = "添加系统角色")
    @PostMapping(value = "/addRole")
    public Result addRole(@RequestBody @Valid AdminAddRoleDto adminAddRoleDto) {
        return iRoleService.addRole(adminAddRoleDto);
    }
    @ApiOperation(value = "删除系统角色", notes = "删除系统角色")
    @GetMapping(value = "/deleteRole/{id}")
    public Result deleteRole(@PathVariable(value = "id") Long id) {
        return iRoleService.deleteRole(id);
    }
    @ApiOperation(value = "查看系统角色详情", notes = "查看系统角色详情")
    @GetMapping(value = "/seeRoleInfo/{id}")
    public Result seeRoleInfo(@PathVariable(value = "id") Long id) {
        return iRoleService.seeRoleInfo(id);
    }
    @ApiOperation(value = "更新系统角色", notes = "更新系统角色")
    @PostMapping(value = "/updateRole")
    public Result updateRole(@RequestBody @Valid AdminUpdateRoleDto adminUpdateRoleDto) {
        return iRoleService.updateRole(adminUpdateRoleDto);
    }
}
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminUserController.java
New file
@@ -0,0 +1,82 @@
package com.xcong.farmer.cms.modules.system.Controller;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddUserDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateUserDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUserDto;
import com.xcong.farmer.cms.modules.system.service.IUserService;
import com.xcong.farmer.cms.modules.system.vo.AdminUserVo;
import com.xcong.farmer.cms.modules.test.dto.TestUserDto;
import com.xcong.farmer.cms.modules.test.entity.TestUserEntity;
import com.xcong.farmer.cms.modules.test.mapper.TestUserEntityMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
@RestController
@RequestMapping(value = "/user")
@Slf4j
@Api(value = "系统用户", tags = "系统用户")
public class AdminUserController {
    @Resource
    private IUserService iUserService;
    @ApiOperation(value = "系统用户分页列表", notes = "系统用户分页列表")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = AdminUserVo.class)})
    @PostMapping(value = "/userInPage")
    public Result getUserInPage(@RequestBody @Valid AdminUserDto adminUserDto) {
        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
        return iUserService.getUserInPage(adminUserDto);
    }
    @ApiOperation(value = "添加系统用户", notes = "添加系统用户")
    @PostMapping(value = "/addUser")
    public Result addUser(@RequestBody @Valid AdminAddUserDto adminAddUserDto) {
        return iUserService.addUser(adminAddUserDto);
    }
    @ApiOperation(value = "激活用户", notes = "激活用户")
    @GetMapping(value = "/activeUser/{id}")
    public Result activeUser(@PathVariable(value = "id") Long id) {
        return iUserService.activeUser(id);
    }
    @ApiOperation(value = "禁用用户", notes = "禁用用户")
    @GetMapping(value = "/forbiddenUser/{id}")
    public Result forbiddenUser(@PathVariable(value = "id") Long id) {
        return iUserService.forbiddenUser(id);
    }
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @GetMapping(value = "/deleteUser/{id}")
    public Result deleteUser(@PathVariable(value = "id") Long id) {
        return iUserService.deleteUser(id);
    }
    @ApiOperation(value = "查看用户详情", notes = "查看用户详情")
    @GetMapping(value = "/seeUserInfo/{id}")
    public Result seeUserInfo(@PathVariable(value = "id") Long id) {
        return iUserService.seeUserInfo(id);
    }
    @ApiOperation(value = "更新系统用户", notes = "更新系统用户")
    @PostMapping(value = "/updateUser")
    public Result updateUser(@RequestBody @Valid AdminUpdateUserDto adminUpdateUserDto) {
        return iUserService.updateUser(adminUpdateUserDto);
    }
    @ApiOperation(value = "重置密码", notes = "重置密码")
    @GetMapping(value = "/resetPassword/{id}")
    public Result resetPassword(@PathVariable(value = "id") Long id) {
        return iUserService.resetPassword(id);
    }
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminAddMenuDto.java
New file
@@ -0,0 +1,26 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminAddMenuDto", description = "参数接收类")
public class AdminAddMenuDto {
    @ApiModelProperty(value = "菜单按钮标题")
    private String menuName;
    @ApiModelProperty(value = "菜单类型 1:菜单 2:按钮")
    private Integer menuType;
    @ApiModelProperty(value = "路由地址")
    private String routeUrl;
    @ApiModelProperty(value = "排序")
    private Integer orderNum;
    @ApiModelProperty(value = "父级ID")
    private Long parentId;
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminAddRoleDto.java
New file
@@ -0,0 +1,20 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminAddRoleDto", description = "参数接收类")
public class AdminAddRoleDto {
    @ApiModelProperty(value = "角色名称")
    private String roleName;
    @ApiModelProperty(value = "备注")
    private String remark;
    @ApiModelProperty(value = "菜单按钮ID")
    private String menuIds;
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminAddUserDto.java
New file
@@ -0,0 +1,28 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
@ApiModel(value = "AdminAddUserDto", description = "参数接收类")
public class AdminAddUserDto {
    @ApiModelProperty(value = "用户名")
    private String username;
    @ApiModelProperty(value = "昵称")
    private String nickname;
    @ApiModelProperty(value = "角色ID")
    private Long roleId;
    @ApiModelProperty(value = "联系电话")
    private String phone;
    @ApiModelProperty(value = "邮箱")
    private String email;
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminMenuDto.java
New file
@@ -0,0 +1,17 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminMenuDto", description = "参数接收类")
public class AdminMenuDto {
    @ApiModelProperty(value = "每页条数", example = "10")
    private Integer pageSize;
    @ApiModelProperty(value = "第几页", example = "1")
    private Integer pageNum;
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminRoleDto.java
New file
@@ -0,0 +1,19 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminRoleDto", description = "参数接收类")
public class AdminRoleDto {
    @ApiModelProperty(value = "角色名称", example = "管理员")
    private String roleName;
    @ApiModelProperty(value = "每页条数", example = "10")
    private Integer pageSize;
    @ApiModelProperty(value = "第几页", example = "1")
    private Integer pageNum;
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminUpdateMenuDto.java
New file
@@ -0,0 +1,29 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminUpdateMenuDto", description = "参数接收类")
public class AdminUpdateMenuDto {
    @ApiModelProperty(value = "菜单ID")
    private Long id;
    @ApiModelProperty(value = "菜单按钮标题")
    private String menuName;
    @ApiModelProperty(value = "菜单类型 1:菜单 2:按钮")
    private Integer menuType;
    @ApiModelProperty(value = "路由地址")
    private String routeUrl;
    @ApiModelProperty(value = "排序")
    private Integer orderNum;
    @ApiModelProperty(value = "父级ID")
    private Long parentId;
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminUpdateRoleDto.java
New file
@@ -0,0 +1,23 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminUpdateRoleDto", description = "参数接收类")
public class AdminUpdateRoleDto {
    @ApiModelProperty(value = "角色ID")
    private Long id;
    @ApiModelProperty(value = "角色名称")
    private String roleName;
    @ApiModelProperty(value = "拓展备注")
    private String remark;
    @ApiModelProperty(value = "菜单按钮ID")
    private String menuIds;
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminUpdateUserDto.java
New file
@@ -0,0 +1,32 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminUpdateUserDto", description = "参数接收类")
public class AdminUpdateUserDto {
    @ApiModelProperty(value = "用户ID")
    private Long id;
    @ApiModelProperty(value = "用户名")
    private String username;
    @ApiModelProperty(value = "昵称")
    private String nickname;
    @ApiModelProperty(value = "联系电话")
    private String phone;
    @ApiModelProperty(value = "邮箱")
    private String email;
    @ApiModelProperty(value = "角色ID")
    private Long roleId;
    @ApiModelProperty(value = "角色名称")
    private String roleName;
}
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminUserDto.java
New file
@@ -0,0 +1,20 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminUserDto", description = "参数接收类")
public class AdminUserDto {
    @ApiModelProperty(value = "用户名", example = "张三")
    private String username;
    @ApiModelProperty(value = "每页条数", example = "10")
    private Integer pageSize;
    @ApiModelProperty(value = "第几页", example = "1")
    private Integer pageNum;
}
src/main/java/com/xcong/farmer/cms/modules/system/entity/MenuEntity.java
New file
@@ -0,0 +1,35 @@
package com.xcong.farmer.cms.modules.system.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xcong.farmer.cms.common.system.base.BaseEntity;
import lombok.Data;
/**
 * 菜单按钮表
 */
@Data
@TableName("t_menu")
public class MenuEntity extends BaseEntity {
    /**
     * 菜单类型 - 菜单
     */
    public static final Long PARENTID_DEFAULT = 0L;
    /**
     * 菜单类型 - 菜单
     */
    public static final Integer MENU_TYPE_MENU = 1;
    /**
     * 菜单类型 - 按钮
     */
    public static final Integer MENU_TYPE_BUTTON = 2;
    //菜单按钮标题
    private String menuName;
    //菜单类型 1:菜单 2:按钮
    private Integer menuType;
    //路由地址
    private String routeUrl;
    //排序
    private Integer orderNum;
    //父级ID
    private Long parentId;
}
src/main/java/com/xcong/farmer/cms/modules/system/entity/RoleEntity.java
New file
@@ -0,0 +1,17 @@
package com.xcong.farmer.cms.modules.system.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xcong.farmer.cms.common.system.base.BaseEntity;
import lombok.Data;
/**
 * 角色表
 */
@Data
@TableName("t_role")
public class RoleEntity extends BaseEntity {
    //角色名称
    private String roleName;
    //拓展备注
    private String remark;
}
src/main/java/com/xcong/farmer/cms/modules/system/entity/RoleMenuEntity.java
New file
@@ -0,0 +1,16 @@
package com.xcong.farmer.cms.modules.system.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
 * 角色菜单关联表
 */
@Data
@TableName("t_role_menu")
public class RoleMenuEntity {
    //角色ID
    private Long roleId;
    //菜单/按钮ID
    private Long menuId;
}
src/main/java/com/xcong/farmer/cms/modules/system/entity/UserEntity.java
New file
@@ -0,0 +1,43 @@
package com.xcong.farmer.cms.modules.system.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xcong.farmer.cms.common.system.base.BaseEntity;
import lombok.Data;
/**
 * 用户表
 */
@Data
@TableName("t_user")
public class UserEntity extends BaseEntity {
    /**
     * 默认密码
     */
    public static final String PASSWORD_DEFAULT = "123456";
    /**
     * 账号状态 - 禁用
     */
    public static final Integer STATUS_DISABLED = 0;
    /**
     * 账号状态 - 激活
     */
    public static final Integer STATUS_ENABLE = 1;
    //用户名
    private String username;
    //昵称
    private String nickname;
    //登录密码
    private String password;
    //角色ID
    private Long roleId;
    //角色名称
    private String roleName;
    //联系电话
    private String phone;
    //邮箱
    private String email;
    //账号状态 0:禁用 1:激活
    private Integer status;
    //拓展备注
    private String remark;
}
src/main/java/com/xcong/farmer/cms/modules/system/entity/UserRoleEntity.java
New file
@@ -0,0 +1,16 @@
package com.xcong.farmer.cms.modules.system.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
 * 用户角色关联表
 */
@Data
@TableName("t_user_role")
public class UserRoleEntity {
    //用户ID
    private Long userId;
    //角色ID
    private Long roleId;
}
src/main/java/com/xcong/farmer/cms/modules/system/mapper/MenuMapper.java
New file
@@ -0,0 +1,14 @@
package com.xcong.farmer.cms.modules.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcong.farmer.cms.modules.system.entity.MenuEntity;
import com.xcong.farmer.cms.modules.system.vo.AdminMenuVo;
import org.apache.ibatis.annotations.Param;
public interface MenuMapper extends BaseMapper<MenuEntity> {
    IPage<AdminMenuVo> selectAdminMenuVoInPage(Page<AdminMenuVo> page, @Param("record")MenuEntity menuEntity);
}
src/main/java/com/xcong/farmer/cms/modules/system/mapper/RoleMapper.java
New file
@@ -0,0 +1,14 @@
package com.xcong.farmer.cms.modules.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcong.farmer.cms.modules.system.entity.RoleEntity;
import com.xcong.farmer.cms.modules.system.vo.AdminRoleVo;
import org.apache.ibatis.annotations.Param;
public interface RoleMapper extends BaseMapper<RoleEntity> {
    IPage<AdminRoleVo> selectAdminRoleVoInPage(Page<AdminRoleVo> page, @Param("record") RoleEntity roleEntity);
}
src/main/java/com/xcong/farmer/cms/modules/system/mapper/RoleMenuMapper.java
New file
@@ -0,0 +1,14 @@
package com.xcong.farmer.cms.modules.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xcong.farmer.cms.modules.system.entity.RoleMenuEntity;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface RoleMenuMapper extends BaseMapper<RoleMenuEntity> {
    List<RoleMenuEntity> selectListByRoleId(@Param("roleId") Long id);
    void deleteByMenuId(@Param("menuId")Long id);
}
src/main/java/com/xcong/farmer/cms/modules/system/mapper/UserMapper.java
New file
@@ -0,0 +1,14 @@
package com.xcong.farmer.cms.modules.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
import com.xcong.farmer.cms.modules.system.vo.AdminUserVo;
import org.apache.ibatis.annotations.Param;
public interface UserMapper extends BaseMapper<UserEntity> {
    IPage<AdminUserVo> selectAdminUserVoInPage(Page<AdminUserVo> page, @Param("record") UserEntity userEntity);
}
src/main/java/com/xcong/farmer/cms/modules/system/mapper/UserRoleMapper.java
New file
@@ -0,0 +1,7 @@
package com.xcong.farmer.cms.modules.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xcong.farmer.cms.modules.system.entity.UserRoleEntity;
public interface UserRoleMapper extends BaseMapper<UserRoleEntity> {
}
src/main/java/com/xcong/farmer/cms/modules/system/service/IMenuService.java
New file
@@ -0,0 +1,21 @@
package com.xcong.farmer.cms.modules.system.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddMenuDto;
import com.xcong.farmer.cms.modules.system.dto.AdminMenuDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateMenuDto;
import com.xcong.farmer.cms.modules.system.entity.MenuEntity;
public interface IMenuService extends IService<MenuEntity> {
    Result getMenuInPage(AdminMenuDto adminMenuDto);
    Result addMenu(AdminAddMenuDto adminAddMenuDto);
    Result deleteMenu(Long id);
    Result seeMenuInfo(Long id);
    Result updateMenu(AdminUpdateMenuDto adminUpdateMenuDto);
}
src/main/java/com/xcong/farmer/cms/modules/system/service/IRoleService.java
New file
@@ -0,0 +1,21 @@
package com.xcong.farmer.cms.modules.system.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddRoleDto;
import com.xcong.farmer.cms.modules.system.dto.AdminRoleDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateRoleDto;
import com.xcong.farmer.cms.modules.system.entity.RoleEntity;
public interface IRoleService extends IService<RoleEntity> {
    Result getRoleInPage(AdminRoleDto adminRoleDto);
    Result addRole(AdminAddRoleDto adminAddRoleDto);
    Result deleteRole(Long id);
    Result seeRoleInfo(Long id);
    Result updateRole(AdminUpdateRoleDto adminUpdateRoleDto);
}
src/main/java/com/xcong/farmer/cms/modules/system/service/IUserService.java
New file
@@ -0,0 +1,27 @@
package com.xcong.farmer.cms.modules.system.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddUserDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateUserDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUserDto;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
public interface IUserService extends IService<UserEntity> {
    Result getUserInPage(AdminUserDto adminUserDto);
    Result addUser(AdminAddUserDto adminAddUserDto);
    Result activeUser(Long id);
    Result forbiddenUser(Long id);
    Result deleteUser(Long id);
    Result seeUserInfo(Long id);
    Result updateUser(AdminUpdateUserDto adminUpdateUserDto);
    Result resetPassword(Long id);
}
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/MenuServiceImpl.java
New file
@@ -0,0 +1,171 @@
package com.xcong.farmer.cms.modules.system.service.Impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddMenuDto;
import com.xcong.farmer.cms.modules.system.dto.AdminMenuDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateMenuDto;
import com.xcong.farmer.cms.modules.system.entity.MenuEntity;
import com.xcong.farmer.cms.modules.system.mapper.MenuMapper;
import com.xcong.farmer.cms.modules.system.mapper.RoleMenuMapper;
import com.xcong.farmer.cms.modules.system.service.IMenuService;
import com.xcong.farmer.cms.modules.system.vo.AdminMenuVo;
import com.xcong.farmer.cms.modules.system.vo.AdminSeeMenuInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.collection.CollUtil;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuEntity> implements IMenuService {
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Override
    public Result getMenuInPage(AdminMenuDto adminMenuDto) {
        Page<AdminMenuVo> page = new Page<>(adminMenuDto.getPageNum(), adminMenuDto.getPageSize());
        MenuEntity menuEntity = new MenuEntity();
        menuEntity.setParentId(MenuEntity.PARENTID_DEFAULT);
        IPage<AdminMenuVo> list = this.baseMapper.selectAdminMenuVoInPage(page,menuEntity);
        List<AdminMenuVo> records = list.getRecords();
        list.getTotal();
        if(CollUtil.isNotEmpty(records)){
            for(AdminMenuVo adminMenuVo : records){
                Long id = adminMenuVo.getId();
                QueryWrapper<MenuEntity> objectQueryWrapper = new QueryWrapper<>();
                objectQueryWrapper.eq("parent_id",id);
                List<MenuEntity> menuEntities = this.baseMapper.selectList(objectQueryWrapper);
                List<AdminMenuVo> adminMenuVoChilds = new ArrayList<>();
                if(CollUtil.isNotEmpty(menuEntities)){
                    for(MenuEntity menuEntityChild : menuEntities){
                        AdminMenuVo child = new AdminMenuVo();
                        child.setId(menuEntityChild.getId());
                        child.setMenuName(menuEntityChild.getMenuName());
                        child.setMenuType(menuEntityChild.getMenuType());
                        child.setRouteUrl(menuEntityChild.getRouteUrl());
                        child.setOrderNum(menuEntityChild.getOrderNum());
                        child.setCreateTime(menuEntityChild.getCreateTime());
                        adminMenuVoChilds.add(child);
                    }
                }
                adminMenuVo.setChild(adminMenuVoChilds);
            }
        }
        return Result.ok(list);
    }
    @Override
    @Transactional
    public Result addMenu(AdminAddMenuDto adminAddMenuDto) {
        String menuName = adminAddMenuDto.getMenuName();
        if(StrUtil.isEmpty(menuName)){
            return Result.fail("请输入菜单名称");
        }
        Integer menuType = adminAddMenuDto.getMenuType();
        if(ObjectUtil.isEmpty(menuType)){
            return Result.fail("请输入菜单类型");
        }
        MenuEntity menuEntity = new MenuEntity();
        menuEntity.setMenuName(menuName);
        menuEntity.setMenuType(menuType);
        Long parentId = adminAddMenuDto.getParentId();
        if(ObjectUtil.isEmpty(parentId)){
            menuEntity.setParentId(MenuEntity.PARENTID_DEFAULT);
        }else{
            menuEntity.setParentId(parentId);
        }
        Integer orderNum = adminAddMenuDto.getOrderNum();
        if(ObjectUtil.isNotEmpty(orderNum)){
            menuEntity.setOrderNum(orderNum);
        }
        String routeUrl = adminAddMenuDto.getRouteUrl();
        if(StrUtil.isNotEmpty(routeUrl)){
            menuEntity.setRouteUrl(routeUrl);
        }
        this.baseMapper.insert(menuEntity);
        return Result.ok("新增成功");
    }
    @Override
    @Transactional
    public Result deleteMenu(Long id) {
        MenuEntity menuEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(menuEntity)){
            return Result.fail("菜单不存在");
        }
        QueryWrapper<MenuEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("parent_id",id);
        List<MenuEntity> menuEntities = this.baseMapper.selectList(objectQueryWrapper);
        if(CollUtil.isNotEmpty(menuEntities)){
            return Result.fail("请先清空它的二级菜单");
        }
        this.baseMapper.deleteById(id);
        roleMenuMapper.deleteByMenuId(id);
        return Result.ok("删除成功");
    }
    @Override
    public Result seeMenuInfo(Long id) {
        MenuEntity menuEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(menuEntity)){
            return Result.fail("菜单不存在");
        }
        AdminSeeMenuInfoVo adminSeeMenuInfoVo = new AdminSeeMenuInfoVo();
        adminSeeMenuInfoVo.setId(id);
        adminSeeMenuInfoVo.setMenuName(menuEntity.getMenuName());
        adminSeeMenuInfoVo.setMenuType(menuEntity.getMenuType());
        adminSeeMenuInfoVo.setRouteUrl(menuEntity.getRouteUrl());
        adminSeeMenuInfoVo.setOrderNum(menuEntity.getOrderNum());
        adminSeeMenuInfoVo.setParentId(menuEntity.getParentId());
        return Result.ok(adminSeeMenuInfoVo);
    }
    @Override
    @Transactional
    public Result updateMenu(AdminUpdateMenuDto adminUpdateMenuDto) {
        Long id = adminUpdateMenuDto.getId();
        MenuEntity menuEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(menuEntity)){
            return Result.fail("菜单不存在");
        }
        String menuName = adminUpdateMenuDto.getMenuName();
        if(StrUtil.isEmpty(menuName)){
            return Result.fail("请输入菜单名称");
        }
        menuEntity.setMenuName(menuName);
        Integer menuType = adminUpdateMenuDto.getMenuType();
        if(ObjectUtil.isEmpty(menuType)){
            return Result.fail("请输入菜单类型");
        }
        menuEntity.setMenuType(menuType);
        String routeUrl = adminUpdateMenuDto.getRouteUrl();
        if(StrUtil.isNotEmpty(routeUrl)){
            menuEntity.setRouteUrl(routeUrl);
        }
        Integer orderNum = adminUpdateMenuDto.getOrderNum();
        if(ObjectUtil.isNotEmpty(orderNum)){
            menuEntity.setOrderNum(orderNum);
        }
        Long parentId = adminUpdateMenuDto.getParentId();
        if(ObjectUtil.isEmpty(parentId)){
            menuEntity.setParentId(MenuEntity.PARENTID_DEFAULT);
        }else{
            menuEntity.setParentId(parentId);
        }
        this.baseMapper.updateById(menuEntity);
        return Result.ok("更新成功");
    }
}
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/RoleServiceImpl.java
New file
@@ -0,0 +1,169 @@
package com.xcong.farmer.cms.modules.system.service.Impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddRoleDto;
import com.xcong.farmer.cms.modules.system.dto.AdminRoleDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateRoleDto;
import com.xcong.farmer.cms.modules.system.entity.MenuEntity;
import com.xcong.farmer.cms.modules.system.entity.RoleEntity;
import com.xcong.farmer.cms.modules.system.entity.RoleMenuEntity;
import com.xcong.farmer.cms.modules.system.entity.UserRoleEntity;
import com.xcong.farmer.cms.modules.system.mapper.MenuMapper;
import com.xcong.farmer.cms.modules.system.mapper.RoleMapper;
import com.xcong.farmer.cms.modules.system.mapper.RoleMenuMapper;
import com.xcong.farmer.cms.modules.system.mapper.UserRoleMapper;
import com.xcong.farmer.cms.modules.system.service.IRoleService;
import com.xcong.farmer.cms.modules.system.vo.AdminMenuInfoVo;
import com.xcong.farmer.cms.modules.system.vo.AdminRoleVo;
import com.xcong.farmer.cms.modules.system.vo.AdminSeeRoleInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.collection.CollUtil;
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements IRoleService {
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private MenuMapper menuMapper;
    @Override
    public Result getRoleInPage(AdminRoleDto adminRoleDto) {
        Page<AdminRoleVo> page = new Page<>(adminRoleDto.getPageNum(), adminRoleDto.getPageSize());
        RoleEntity roleEntity = new RoleEntity();
        if(StrUtil.isNotEmpty(adminRoleDto.getRoleName())){
            roleEntity.setRoleName(adminRoleDto.getRoleName());
        }
        IPage<AdminRoleVo> list = this.baseMapper.selectAdminRoleVoInPage(page,roleEntity);
        return Result.ok(list);
    }
    @Override
    @Transactional
    public Result addRole(AdminAddRoleDto adminAddRoleDto) {
        String roleName = adminAddRoleDto.getRoleName();
        if(StrUtil.isEmpty(roleName)){
            return Result.fail("请输入角色名称");
        }
        String menuIds = adminAddRoleDto.getMenuIds();
        if(StrUtil.isEmpty(menuIds)){
            return Result.fail("请选择角色权限");
        }
        RoleEntity roleEntity = new RoleEntity();
        String remark = adminAddRoleDto.getRemark();
        if(StrUtil.isNotEmpty(remark)){
            roleEntity.setRemark(remark);
        }
        roleEntity.setRoleName(roleName);
        int insert = this.baseMapper.insert(roleEntity);
        if(insert > 0){
            saveRoleMenus(menuIds,roleEntity.getId());
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }
    @Override
    @Transactional
    public Result deleteRole(Long id) {
        RoleEntity roleEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(roleEntity)){
            return Result.fail("角色不存在");
        }
        //获取角色下对应的用户信息
        QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("role_id",id);
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(objectQueryWrapper);
        if(CollUtil.isNotEmpty(userRoleEntities)){
            return Result.fail("当前角色下还有用户,请重新分配角色");
        }
        this.baseMapper.deleteById(id);
        return Result.ok("删除成功");
    }
    @Override
    public Result seeRoleInfo(Long id) {
        RoleEntity roleEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(roleEntity)){
            return Result.fail("角色不存在");
        }
        AdminSeeRoleInfoVo adminSeeRoleInfoVo = new AdminSeeRoleInfoVo();
        adminSeeRoleInfoVo.setId(roleEntity.getId());
        adminSeeRoleInfoVo.setRoleName(roleEntity.getRoleName());
        adminSeeRoleInfoVo.setRemark(roleEntity.getRemark());
        List<RoleMenuEntity> roleMenuEntities = roleMenuMapper.selectListByRoleId(id);
        if(CollUtil.isNotEmpty(roleMenuEntities)){
            List<AdminMenuInfoVo> adminMenuInfoVos = new ArrayList<>();
            for(RoleMenuEntity roleMenuEntity : roleMenuEntities){
                MenuEntity menuEntity = menuMapper.selectById(roleMenuEntity.getMenuId());
                AdminMenuInfoVo adminMenuInfoVo = new AdminMenuInfoVo();
                adminMenuInfoVo.setId(menuEntity.getId());
                adminMenuInfoVo.setMenuName(menuEntity.getMenuName());
                adminMenuInfoVos.add(adminMenuInfoVo);
            }
            adminSeeRoleInfoVo.setMenuInfoVos(adminMenuInfoVos);
        }
        return Result.ok(adminSeeRoleInfoVo);
    }
    @Override
    @Transactional
    public Result updateRole(AdminUpdateRoleDto adminUpdateRoleDto) {
        Long id = adminUpdateRoleDto.getId();
        RoleEntity roleEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(roleEntity)){
            return Result.fail("角色不存在");
        }
        String roleName = adminUpdateRoleDto.getRoleName();
        if(StrUtil.isEmpty(roleName)){
            return Result.fail("请输入角色名称");
        }
        roleEntity.setRoleName(roleName);
        String remark = adminUpdateRoleDto.getRemark();
        if(StrUtil.isNotEmpty(remark)){
            roleEntity.setRemark(remark);
        }
        this.baseMapper.updateById(roleEntity);
        QueryWrapper<RoleMenuEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("role_id",id);
        roleMenuMapper.delete(objectQueryWrapper);
        String menuIds = adminUpdateRoleDto.getMenuIds();
        if(StrUtil.isNotEmpty(menuIds)){
            saveRoleMenus(menuIds,id);
        }
        return Result.ok("更新成功");
    }
    private void saveRoleMenus(String menuIds,Long roleId) {
        if (StrUtil.isNotEmpty(menuIds)) {
            String[] menuIdList = menuIds.split(StringPool.COMMA);
            Arrays.stream(menuIdList).forEach(menuId -> {
                RoleMenuEntity roleMenu = new RoleMenuEntity();
                roleMenu.setMenuId(Long.valueOf(menuId));
                roleMenu.setRoleId(roleId);
                roleMenuMapper.insert(roleMenu);
            });
        }
    }
}
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/UserServiceImpl.java
New file
@@ -0,0 +1,187 @@
package com.xcong.farmer.cms.modules.system.service.Impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddUserDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateUserDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUserDto;
import com.xcong.farmer.cms.modules.system.entity.RoleEntity;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
import com.xcong.farmer.cms.modules.system.mapper.RoleMapper;
import com.xcong.farmer.cms.modules.system.mapper.UserMapper;
import com.xcong.farmer.cms.modules.system.service.IUserService;
import com.xcong.farmer.cms.modules.system.vo.AdminSeeUserInfoVo;
import com.xcong.farmer.cms.modules.system.vo.AdminUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.codec.Rot;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {
    @Resource
    private RoleMapper roleMapper;
    @Override
    public Result getUserInPage(AdminUserDto adminUserDto) {
        Page<AdminUserVo> page = new Page<>(adminUserDto.getPageNum(), adminUserDto.getPageSize());
        UserEntity userEntity = new UserEntity();
        if(StrUtil.isNotEmpty(adminUserDto.getUsername())){
            userEntity.setUsername(adminUserDto.getUsername());
        }
        IPage<AdminUserVo> list = this.baseMapper.selectAdminUserVoInPage(page,userEntity);
        return Result.ok(list);
    }
    @Override
    @Transactional
    public Result addUser(AdminAddUserDto adminAddUserDto) {
        String username = adminAddUserDto.getUsername();
        if(StrUtil.isEmpty(username)){
            return Result.fail("请输入用户名");
        }
        Long roleId = adminAddUserDto.getRoleId();
        if(ObjectUtil.isEmpty(roleId)){
            return Result.fail("请选择用户角色");
        }
        RoleEntity roleEntity = roleMapper.selectById(roleId);
        if(ObjectUtil.isEmpty(roleEntity)){
            return Result.fail("请选择用户角色");
        }
        String phone = adminAddUserDto.getPhone();
        if(StrUtil.isEmpty(phone)){
            return Result.fail("请输入联系电话");
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPhone(phone);
        userEntity.setRoleId(roleId);
        userEntity.setRoleName(roleEntity.getRoleName());
        if(StrUtil.isNotEmpty(adminAddUserDto.getNickname())){
            userEntity.setNickname(adminAddUserDto.getNickname());
        }
        if(StrUtil.isNotEmpty(adminAddUserDto.getEmail())){
            userEntity.setEmail(adminAddUserDto.getEmail());
        }
        ;
        userEntity.setPassword(Rot.encode13(UserEntity.PASSWORD_DEFAULT));
        userEntity.setStatus(UserEntity.STATUS_ENABLE);
        int insert = this.baseMapper.insert(userEntity);
        if(insert > 0){
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }
    @Override
    @Transactional
    public Result activeUser(Long id) {
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setStatus(UserEntity.STATUS_ENABLE);
        this.baseMapper.updateById(userEntity);
        return Result.ok("激活成功");
    }
    @Override
    @Transactional
    public Result forbiddenUser(Long id) {
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setStatus(UserEntity.STATUS_DISABLED);
        this.baseMapper.updateById(userEntity);
        return Result.ok("禁用成功");
    }
    @Override
    @Transactional
    public Result deleteUser(Long id) {
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        this.baseMapper.deleteById(id);
        return Result.ok("删除成功");
    }
    @Override
    public Result seeUserInfo(Long id) {
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        AdminSeeUserInfoVo adminSeeUserInfoVo = new AdminSeeUserInfoVo();
        adminSeeUserInfoVo.setId(userEntity.getId());
        adminSeeUserInfoVo.setUsername(userEntity.getUsername());
        adminSeeUserInfoVo.setNickname(userEntity.getNickname());
        adminSeeUserInfoVo.setPhone(userEntity.getPhone());
        adminSeeUserInfoVo.setEmail(userEntity.getEmail());
        adminSeeUserInfoVo.setRoleId(userEntity.getRoleId());
        adminSeeUserInfoVo.setRoleName(userEntity.getRoleName());
        return Result.ok(adminSeeUserInfoVo);
    }
    @Override
    @Transactional
    public Result updateUser(AdminUpdateUserDto adminUpdateUserDto) {
        String username = adminUpdateUserDto.getUsername();
        if(StrUtil.isEmpty(username)){
            return Result.fail("请输入用户名");
        }
        Long roleId = adminUpdateUserDto.getRoleId();
        if(ObjectUtil.isEmpty(roleId)){
            return Result.fail("请选择用户角色");
        }
        RoleEntity roleEntity = roleMapper.selectById(roleId);
        if(ObjectUtil.isEmpty(roleEntity)){
            return Result.fail("请选择用户角色");
        }
        String phone = adminUpdateUserDto.getPhone();
        if(StrUtil.isEmpty(phone)){
            return Result.fail("请输入联系电话");
        }
        UserEntity userEntity =  this.baseMapper.selectById(adminUpdateUserDto.getId());
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setUsername(username);
        userEntity.setPhone(phone);
        userEntity.setRoleId(roleId);
        userEntity.setRoleName(roleEntity.getRoleName());
        if(StrUtil.isNotEmpty(adminUpdateUserDto.getNickname())){
            userEntity.setNickname(adminUpdateUserDto.getNickname());
        }
        if(StrUtil.isNotEmpty(adminUpdateUserDto.getEmail())){
            userEntity.setEmail(adminUpdateUserDto.getEmail());
        }
        int insert = this.baseMapper.updateById(userEntity);
        if(insert > 0){
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }
    @Override
    @Transactional
    public Result resetPassword(Long id) {
        UserEntity userEntity =  this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setPassword(Rot.encode13(UserEntity.PASSWORD_DEFAULT));
        this.baseMapper.updateById(userEntity);
        return Result.ok("重置成功");
    }
}
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminMenuInfoVo.java
New file
@@ -0,0 +1,17 @@
package com.xcong.farmer.cms.modules.system.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminMenuInfoVo", description = "返回")
public class AdminMenuInfoVo {
    @ApiModelProperty(value = "菜单ID")
    private Long id;
    @ApiModelProperty(value = "菜单名称")
    private String menuName;
}
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminMenuVo.java
New file
@@ -0,0 +1,40 @@
package com.xcong.farmer.cms.modules.system.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
@ApiModel(value = "AdminMenuVo", description = "系统菜单列表返回")
public class AdminMenuVo {
    @ApiModelProperty(value = "菜单ID")
    private Long id;
    //菜单按钮标题
    @ApiModelProperty(value = "菜单按钮标题")
    private String menuName;
    //菜单类型 1:菜单 2:按钮
    @ApiModelProperty(value = "菜单类型 1:菜单 2:按钮")
    private Integer menuType;
    //路由地址
    @ApiModelProperty(value = "路由地址")
    private String routeUrl;
    //排序
    @ApiModelProperty(value = "排序")
    private Integer orderNum;
    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
    @ApiModelProperty(value = "子菜单")
    private List<AdminMenuVo> child;
}
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminRoleVo.java
New file
@@ -0,0 +1,26 @@
package com.xcong.farmer.cms.modules.system.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminRoleVo", description = "系统角色列表返回")
public class AdminRoleVo {
    @ApiModelProperty(value = "角色ID")
    private Long id;
    @ApiModelProperty(value = "角色名称")
    private String roleName;
    @ApiModelProperty(value = "拓展备注")
    private String remark;
    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
}
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminSeeMenuInfoVo.java
New file
@@ -0,0 +1,28 @@
package com.xcong.farmer.cms.modules.system.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminSeeMenuInfoVo", description = "系统菜单详情返回")
public class AdminSeeMenuInfoVo {
    @ApiModelProperty(value = "菜单ID")
    private Long id;
    @ApiModelProperty(value = "菜单按钮标题")
    private String menuName;
    @ApiModelProperty(value = "菜单类型 1:菜单 2:按钮")
    private Integer menuType;
    @ApiModelProperty(value = "路由地址")
    private String routeUrl;
    @ApiModelProperty(value = "排序")
    private Integer orderNum;
    @ApiModelProperty(value = "父级ID")
    private Long parentId;
}
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminSeeRoleInfoVo.java
New file
@@ -0,0 +1,25 @@
package com.xcong.farmer.cms.modules.system.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
@ApiModel(value = "AdminSeeRoleInfoVo", description = "系统角色详情返回")
public class AdminSeeRoleInfoVo {
    @ApiModelProperty(value = "角色ID")
    private Long id;
    @ApiModelProperty(value = "角色名称")
    private String roleName;
    @ApiModelProperty(value = "拓展备注")
    private String remark;
    @ApiModelProperty(value = "菜单按钮")
    private List<AdminMenuInfoVo> menuInfoVos;
}
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminSeeUserInfoVo.java
New file
@@ -0,0 +1,32 @@
package com.xcong.farmer.cms.modules.system.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminSeeUserInfoVo", description = "系统用户详情返回")
public class AdminSeeUserInfoVo {
    @ApiModelProperty(value = "用户ID")
    private Long id;
    @ApiModelProperty(value = "用户名")
    private String username;
    @ApiModelProperty(value = "昵称")
    private String nickname;
    @ApiModelProperty(value = "联系电话")
    private String phone;
    @ApiModelProperty(value = "邮箱")
    private String email;
    @ApiModelProperty(value = "角色ID")
    private Long roleId;
    @ApiModelProperty(value = "角色名称")
    private String roleName;
}
src/main/java/com/xcong/farmer/cms/modules/system/vo/AdminUserVo.java
New file
@@ -0,0 +1,38 @@
package com.xcong.farmer.cms.modules.system.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "AdminUserVo", description = "系统用户列表返回")
public class AdminUserVo {
    @ApiModelProperty(value = "用户ID")
    private Long id;
    @ApiModelProperty(value = "用户名")
    private String username;
    @ApiModelProperty(value = "昵称")
    private String nickname;
    @ApiModelProperty(value = "联系电话")
    private String phone;
    @ApiModelProperty(value = "邮箱")
    private String email;
    @ApiModelProperty(value = "角色名称")
    private String roleName;
    @ApiModelProperty(value = "账号状态 0:禁用 1:激活")
    private Integer status;
    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
}
src/main/resources/application.yml
@@ -5,7 +5,7 @@
spring:
  datasource:
    url: jdbc:mysql://120.27.238.55:3306/kss_framework?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
    url: jdbc:mysql://120.27.238.55:3306/farmer-cms?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
    username: ct_test
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
src/main/resources/mapper/MenuMapper.xml
New file
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xcong.farmer.cms.modules.system.mapper.MenuMapper">
    <select id="selectAdminMenuVoInPage" resultType="com.xcong.farmer.cms.modules.system.vo.AdminMenuVo">
        SELECT
        a.*
        FROM
        t_menu a
        <where>
            <if test="record != null" >
                <if test="record.parentId != null">
                    and a.parent_id = #{record.parentId}
                </if>
            </if>
        </where>
        order by a.create_time desc
    </select>
</mapper>
src/main/resources/mapper/RoleMapper.xml
New file
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xcong.farmer.cms.modules.system.mapper.RoleMapper">
    <select id="selectAdminRoleVoInPage" resultType="com.xcong.farmer.cms.modules.system.vo.AdminRoleVo">
        SELECT
        a.*
        FROM
        t_role a
        <where>
            <if test="record != null" >
                <if test="record.roleName!=null">
                    and a.role_name like concat ('%',#{record.roleName},'%')
                </if>
            </if>
        </where>
        order by a.create_time desc
    </select>
</mapper>
src/main/resources/mapper/RoleMenuMapper.xml
New file
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xcong.farmer.cms.modules.system.mapper.RoleMenuMapper">
    <select id="selectListByRoleId" resultType="com.xcong.farmer.cms.modules.system.entity.RoleMenuEntity">
        SELECT a.* FROM t_role_menu a where a.role_id = #{roleId}
    </select>
    <delete id="deleteByMenuId">
        DELETE FROM t_role_menu where menu_id=#{menuId}
    </delete>
</mapper>
src/main/resources/mapper/UserMapper.xml
New file
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xcong.farmer.cms.modules.system.mapper.UserMapper">
    <select id="selectAdminUserVoInPage" resultType="com.xcong.farmer.cms.modules.system.vo.AdminUserVo">
        SELECT
        a.*
        FROM
        t_user a
        <where>
            <if test="record != null" >
                <if test="record.username!=null">
                    and a.username like concat ('%',#{record.username},'%')
                </if>
            </if>
        </where>
        order by a.create_time desc
    </select>
</mapper>
src/main/resources/mapper/UserRoleMapper.xml
New file
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xcong.farmer.cms.modules.system.mapper.UserRoleMapper">
</mapper>