增加新增部门接口

This commit is contained in:
chixiao 2019-06-15 23:11:09 +08:00
parent e7c9464804
commit 8f4433bf79
13 changed files with 329 additions and 1 deletions

View File

@ -0,0 +1,39 @@
package cn.iocoder.mall.admin.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.DeptmentService;
import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO;
import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO;
import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:07
*/
@RestController
@RequestMapping("admins/dept")
@Api("部门模块")
public class DeptmentController {
@Autowired
private DeptmentService deptmentService;
@PostMapping("add")
@ApiOperation(value = "新增部门", notes = "选择部门名称,父级部门")
public CommonResult<DeptmentBO> add(@RequestBody DeptmentAddDTO deptmentAddDTO){
return success(deptmentService.addDeptment(
AdminSecurityContextHolder.getContext().getAdminId(), deptmentAddDTO));
}
}

View File

@ -0,0 +1,14 @@
package cn.iocoder.mall.admin.application.vo.deptment;
import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-15
* @time: 16:57
*/
public class DeptmentVO extends DeptmentBO {
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.mall.admin.api;
import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO;
import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:35
*/
public interface DeptmentService {
DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO);
}

View File

@ -0,0 +1,44 @@
package cn.iocoder.mall.admin.api.bo.deptment;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:49
*/
@ApiModel("部门 BO")
@Data
public class DeptmentBO implements Serializable {
private static final long serialVersionUID = 7656901281539594453L;
/**
* 部门编号
*/
private Integer id;
/**
* 部门名称
*/
private String name;
/**
* 部门排序字段
*/
private Integer sort;
/**
* 父级部门id
*/
private Integer pid;
private Date createTime;
private Date updateTime;
}

View File

@ -57,6 +57,11 @@ public enum AdminErrorCodeEnum {
SMS_TEMPLATE_NOT_EXISTENT(1002006020, "短信签名不存在"), SMS_TEMPLATE_NOT_EXISTENT(1002006020, "短信签名不存在"),
SMS_TEMPLATE_IS_EXISTENT(1002006021, "短信签名不存在"), SMS_TEMPLATE_IS_EXISTENT(1002006021, "短信签名不存在"),
SMS_NOT_SEND_CLIENT(1002006030, "短信没有发送的client"), SMS_NOT_SEND_CLIENT(1002006030, "短信没有发送的client"),
// ========== 部门模块 1002007000 ==========
DEPT_SAME_LEVEL_NAME_EXITS(1002007001,"当前级别部门名字已存在"),
DEPT_PARENT_NOT_EXITS(1002007002,"父级部门不存在")
; ;
private final int code; private final int code;

View File

@ -0,0 +1,33 @@
package cn.iocoder.mall.admin.api.dto.depetment;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:55
*/
@ApiModel("添加部门 DTO")
@Data
public class DeptmentAddDTO {
@ApiModelProperty(value = "名字", required = true, example = "销售一组")
@NotNull(message = "不能为空")
private String name;
@ApiModelProperty(value = "排序", required = true, example = "1")
@NotNull(message = "不能为空")
private Integer sort;
@ApiModelProperty(value = "父级id", required = true, example = "1")
@NotNull(message = "可以为空默认0顶层")
@Min(value = 0,message = "父id不能小于0")
private Integer pid = 0;
}

View File

@ -0,0 +1,28 @@
package cn.iocoder.mall.admin.convert;
import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO;
import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO;
import cn.iocoder.mall.admin.dataobject.DeptmentDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 20:06
*/
@Mapper
public interface DeptmentConvert {
DeptmentConvert INSTANCE = Mappers.getMapper(DeptmentConvert.class);
@Mappings({})
DeptmentDO convert(DeptmentAddDTO deptmentAddDTO);
@Mappings({})
DeptmentBO convert(DeptmentDO deptmentDO);
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.AdminDO;
import cn.iocoder.mall.admin.dataobject.DeptmentDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:26
*/
public interface DeptmentMapper extends BaseMapper<DeptmentDO> {
default DeptmentDO findDeptByNameAndPid(String name, Integer pid){
return selectOne(new QueryWrapper<DeptmentDO>()
.eq("name", name)
.eq("pid", pid)
.eq("deleted", false)
);
}
}

View File

@ -0,0 +1,15 @@
package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.DeptmentRoleDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:27
*/
public interface DeptmentRoleMapper extends BaseMapper<DeptmentRoleDO> {
}

View File

@ -0,0 +1,37 @@
package cn.iocoder.mall.admin.dataobject;
import cn.iocoder.common.framework.dataobject.DeletableDO;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* Description:部门实体
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:12
*/
@TableName("deptment")
@Data
@Accessors(chain = true)
public class DeptmentDO extends DeletableDO {
/**
* 部门编号
*/
private Integer id;
/**
* 部门名称
*/
private String name;
/**
* 部门排序字段
*/
private Integer sort;
/**
* 父级部门id
*/
private Integer pid;
}

View File

@ -0,0 +1,29 @@
package cn.iocoder.mall.admin.dataobject;
import cn.iocoder.common.framework.dataobject.DeletableDO;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* Description: 部门-角色映射表
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:19
*/
@TableName("deptment_role")
@Data
public class DeptmentRoleDO extends DeletableDO {
/**
* 主键id
*/
private Integer id;
/**
* 部门id
*/
private Integer deptmentId;
/**
* 角色id
*/
private Integer roleId;
}

View File

@ -56,7 +56,7 @@ public class AdminServiceImpl implements AdminService {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
} }
// 密码不正确 // 密码不正确
if (encodePassword(adminAuthenticationDTO.getPassword()).equals(admin.getPassword())) { if (!encodePassword(adminAuthenticationDTO.getPassword()).equals(admin.getPassword())) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_PASSWORD_ERROR.getCode()); throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_PASSWORD_ERROR.getCode());
} }
// 账号被禁用 // 账号被禁用

View File

@ -0,0 +1,41 @@
package cn.iocoder.mall.admin.service;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.mall.admin.api.DeptmentService;
import cn.iocoder.mall.admin.api.bo.deptment.DeptmentBO;
import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum;
import cn.iocoder.mall.admin.api.dto.depetment.DeptmentAddDTO;
import cn.iocoder.mall.admin.convert.DeptmentConvert;
import cn.iocoder.mall.admin.dao.DeptmentMapper;
import cn.iocoder.mall.admin.dataobject.DeptmentDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Description:
*
* @author: zhenxianyimeng
* @date: 2019-06-14
* @time: 19:30
*/
@Service
public class DeptmentServiceImpl implements DeptmentService {
@Autowired
private DeptmentMapper deptmentMapper;
@Override
public DeptmentBO addDeptment(Integer adminId, DeptmentAddDTO deptmentAddDTO) {
if(deptmentAddDTO.getPid() != 0 &&
deptmentMapper.selectById(deptmentAddDTO.getPid()) == null){
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DEPT_PARENT_NOT_EXITS.getCode());
}
//不同的大部门下好像可以小部门名字一样验证同级别部门名字
if (null != deptmentMapper.findDeptByNameAndPid(deptmentAddDTO.getName(), deptmentAddDTO.getPid())) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DEPT_SAME_LEVEL_NAME_EXITS.getCode());
}
DeptmentDO deptmentDO = DeptmentConvert.INSTANCE.convert(deptmentAddDTO);
deptmentMapper.insert(deptmentDO);
return DeptmentConvert.INSTANCE.convert(deptmentDO);
}
}