【dict】更新字典模块

pull/3/head
fengshuonan 2020-12-26 22:58:02 +08:00
parent d63a43b80e
commit 79bf2e4418
34 changed files with 1358 additions and 258 deletions

View File

@ -48,5 +48,9 @@ public interface RuleConstants {
*/
String RULE_EXCEPTION_STEP_CODE = "01";
/**
* id
*/
Long TREE_ROOT_ID = -1L;
}

View File

@ -0,0 +1,44 @@
package cn.stylefeng.roses.kernel.db.api.exception.enums;
import cn.stylefeng.roses.kernel.db.api.constants.DbConstants;
import cn.stylefeng.roses.kernel.rule.abstracts.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import lombok.Getter;
/**
*
* <p>
* mapping.xml
*
* @author fengshuonan
* @date 2020/6/20 21:08
*/
@Getter
public enum DbInitEnum implements AbstractExceptionEnum {
/**
*
*/
INIT_TABLE_EMPTY_PARAMS(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "01", "初始化数据库,存在为空的字段"),
/**
*
*/
FIELD_VALIDATE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "02", "数据库字段与实体字段不一致");
/**
*
*/
private final String errorCode;
/**
*
*/
private final String userTip;
DbInitEnum(String errorCode, String userTip) {
this.errorCode = errorCode;
this.userTip = userTip;
}
}

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-d-db</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>db-sdk-init</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--db操作的api-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>db-api</artifactId>
<version>1.0.0</version>
</dependency>
<!--auth鉴权模块的api-->
<!--需要用auth模块的LoginContext获取当前登录用户然后填充到create_user这类字段里-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>auth-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,202 @@
package cn.stylefeng.roses.kernel.db.init.actuator;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.exception.enums.DbInitEnum;
import cn.stylefeng.roses.kernel.db.init.util.SqlExe;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
*
* @author fengshuonan
* @date 2018-07-29 22:05
*/
@Slf4j
@Getter
@Setter
public abstract class DbInitializer {
/**
* true
*/
private Boolean fieldValidatorExceptionFlag = true;
public DbInitializer() {
}
public DbInitializer(Boolean fieldValidatorExceptionFlag) {
this.fieldValidatorExceptionFlag = fieldValidatorExceptionFlag;
}
/**
*
*
* @author fengshuonan
* @date 2018/7/30 10:30
*/
public void dbInit() {
/**
*
*/
initTable();
/**
*
*/
fieldsValidate();
}
/**
*
*
* @author fengshuonan
* @date 2018/7/30 10:24
*/
private void initTable() {
//校验参数
String tableName = this.getTableName();
String tableInitSql = this.getTableInitSql();
if (ObjectUtil.isEmpty(tableName) || ObjectUtil.isEmpty(tableInitSql)) {
if (fieldValidatorExceptionFlag) {
throw new ServiceException(DbInitEnum.INIT_TABLE_EMPTY_PARAMS);
}
}
//列出数据库中所有的表
List<Map<String, Object>> tables = SqlExe.selectList("SHOW TABLES");
boolean haveSmsTableFlag = false;
for (Map<String, Object> tableInfo : tables) {
if (tableInfo.containsValue(tableName.toUpperCase()) || tableInfo.containsValue(tableName.toLowerCase())) {
haveSmsTableFlag = true;
}
}
//判断数据库中是否有这张表,如果没有就初始化
if (!haveSmsTableFlag) {
SqlExe.update(tableInitSql);
log.info("初始化" + getTableName() + "成功!");
}
}
/**
*
*
* @author fengshuonan
* @date 2018/7/30 10:24
*/
private void fieldsValidate() {
//校验参数
String sql = this.showColumnsSql();
if (ObjectUtil.isEmpty(sql)) {
if (fieldValidatorExceptionFlag) {
throw new ServiceException(DbInitEnum.INIT_TABLE_EMPTY_PARAMS);
}
}
//检查数据库中的字段,是否和实体字段一致
List<Map<String, Object>> tableFields = SqlExe.selectList(sql);
if (tableFields != null && !tableFields.isEmpty()) {
//用于保存实体中不存在的字段的名称集合
List<String> fieldsNotInClass = new ArrayList<>();
//反射获取字段的所有字段名称
List<String> classFields = this.getClassFields();
for (Map<String, Object> tableField : tableFields) {
String fieldName = (String) tableField.get("COLUMN_NAME");
if (fieldName == null) {
fieldName = (String) tableField.get("Field");
}
if (!classFields.contains(fieldName.toLowerCase())) {
fieldsNotInClass.add(fieldName);
}
}
//如果集合不为空,代表有实体和数据库不一致的数据
if (!fieldsNotInClass.isEmpty()) {
log.error("实体中和数据库字段不一致的字段如下:" + JSON.toJSONString(fieldsNotInClass));
if (fieldValidatorExceptionFlag) {
throw new ServiceException(DbInitEnum.FIELD_VALIDATE_ERROR);
}
}
}
}
/**
*
*
* @author fengshuonan
* @date 2018/7/30 10:06
*/
private List<String> getClassFields() {
Class<?> entityClass = this.getEntityClass();
Field[] declaredFields = ClassUtil.getDeclaredFields(entityClass);
ArrayList<String> filedNamesUnderlineCase = new ArrayList<>();
for (Field declaredField : declaredFields) {
String fieldName = StrUtil.toUnderlineCase(declaredField.getName());
filedNamesUnderlineCase.add(fieldName);
}
// 获取父类的所有字段名称
Field[] superfields = ReflectUtil.getFields(entityClass.getSuperclass());
for (Field superfield : superfields) {
String fieldName = StrUtil.toUnderlineCase(superfield.getName());
filedNamesUnderlineCase.add(fieldName);
}
return filedNamesUnderlineCase;
}
/**
*
*
* @author stylefeng
* @date 2018/7/29 22:49
*/
private String showColumnsSql() {
return "SHOW COLUMNS FROM " + this.getTableName();
}
/**
*
*
* @author stylefeng
* @date 2018/7/29 22:10
*/
protected abstract String getTableInitSql();
/**
*
*
* @author stylefeng
* @date 2018/7/29 22:10
*/
protected abstract String getTableName();
/**
*
*
* @author stylefeng
* @date 2018/7/29 22:49
*/
protected abstract Class<?> getEntityClass();
}

View File

@ -0,0 +1,37 @@
package cn.stylefeng.roses.kernel.db.init.listener;
import cn.stylefeng.roses.kernel.db.init.actuator.DbInitializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import java.util.Map;
/**
*
*
* @author wangzhongqiang
* @date 2018/4/23 9:57
*/
@Slf4j
public class InitTableListener implements ApplicationListener<ApplicationReadyEvent>, Ordered {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
Map<String, DbInitializer> beansOfType = event.getApplicationContext().getBeansOfType(DbInitializer.class);
for (Map.Entry<String, DbInitializer> entry : beansOfType.entrySet()) {
DbInitializer value = entry.getValue();
value.dbInit();
}
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}

View File

@ -0,0 +1,188 @@
package cn.stylefeng.roses.kernel.db.init.util;
import cn.hutool.db.DbUtil;
import cn.hutool.db.handler.RsHandler;
import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* sql
*
* @author fengshuonan
* @date 2019/12/29 16:37
*/
@Slf4j
public class SqlExe {
/**
*
*
* @param dataSource
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static Map<String, Object> selectOne(DataSource dataSource, String sql, Object... params) {
RsHandler<Map<String, Object>> rsHandler = SqlUtil::resultSet2Map;
Connection conn = null;
try {
conn = dataSource.getConnection();
return SqlExecutor.query(conn, sql, rsHandler, params);
} catch (SQLException e) {
log.error("sql执行错误!", e);
return new HashMap<>();
} finally {
DbUtil.close(conn);
}
}
/**
*
*
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static Map<String, Object> selectOne(String sql, Object... params) {
DataSource dataSource = SpringUtil.getBean(DataSource.class);
return selectOne(dataSource, sql, params);
}
/**
*
*
* @param dataSource
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static List<Map<String, Object>> selectList(DataSource dataSource, String sql, Object... params) {
RsHandler<List<Map<String, Object>>> rsHandler = SqlUtil::resultSet2ListMap;
Connection conn = null;
try {
conn = dataSource.getConnection();
return SqlExecutor.query(conn, sql, rsHandler, params);
} catch (SQLException e) {
log.error("sql执行错误!", e);
return new ArrayList<>();
} finally {
DbUtil.close(conn);
}
}
/**
*
*
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static List<Map<String, Object>> selectList(String sql, Object... params) {
DataSource dataSource = SpringUtil.getBean(DataSource.class);
return selectList(dataSource, sql, params);
}
/**
*
*
* @param dataSource
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static int update(DataSource dataSource, String sql, Object... params) {
Connection conn = null;
try {
conn = dataSource.getConnection();
return SqlExecutor.execute(conn, sql, params);
} catch (SQLException e) {
log.error("sql执行错误!", e);
return 0;
} finally {
DbUtil.close(conn);
}
}
/**
*
*
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static int update(String sql, Object... params) {
DataSource dataSource = SpringUtil.getBean(DataSource.class);
return update(dataSource, sql, params);
}
/**
*
*
* @param dataSource
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static int insert(DataSource dataSource, String sql, Object... params) {
return update(dataSource, sql, params);
}
/**
*
*
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static int insert(String sql, Object... params) {
DataSource dataSource = SpringUtil.getBean(DataSource.class);
return insert(dataSource, sql, params);
}
/**
*
*
* @param dataSource
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static int delete(DataSource dataSource, String sql, Object... params) {
return update(dataSource, sql, params);
}
/**
*
*
* @param sql sql(sql?)
* @param params sql
* @author fengshuonan
* @date 2019/12/29 16:37
*/
public static int delete(String sql, Object... params) {
DataSource dataSource = SpringUtil.getBean(DataSource.class);
return delete(dataSource, sql, params);
}
}

View File

@ -0,0 +1,88 @@
package cn.stylefeng.roses.kernel.db.init.util;
import lombok.extern.slf4j.Slf4j;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* sql
*
* @author fengshuonan
* @date 2016126 1:01:54
*/
@Slf4j
public class SqlUtil {
/**
* "?"
*
* @author fengshuonan
*/
public static String parse(List<?> list) {
String str = "";
if (list != null && list.size() > 0) {
str = str + "?";
for (int i = 1; i < list.size(); i++) {
str = str + ",?";
}
}
return str;
}
/**
* map
*
* @author fengshuonan
* @date 2020/1/29 6:12
*/
public static Map<String, Object> resultSet2Map(ResultSet resultSet) {
try {
HashMap<String, Object> result = new HashMap<>();
ResultSetMetaData metaData = resultSet.getMetaData();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnName(i);
Object columnValue = resultSet.getObject(i);
result.put(columnName, columnValue);
}
return result;
} catch (SQLException e) {
log.error("转化结果集错误!", e);
//返回空map
return new HashMap<>();
}
}
/**
* map
*
* @author fengshuonan
* @date 2020/1/29 6:12
*/
public static List<Map<String, Object>> resultSet2ListMap(ResultSet resultSet) {
ArrayList<Map<String, Object>> result = new ArrayList<>();
try {
while (resultSet.next()) {
Map<String, Object> map = resultSet2Map(resultSet);
result.add(map);
}
return result;
} catch (SQLException e) {
log.error("转化结果集错误!", e);
//返回空map
return result;
}
}
}

View File

@ -24,6 +24,13 @@
<version>1.0.0</version>
</dependency>
<!--数据库初始化模块-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>db-sdk-init</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,21 @@
package cn.stylefeng.roses.kernel.db.starter;
import cn.stylefeng.roses.kernel.db.init.listener.InitTableListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author fengshuonan
* @date 2020/12/26 22:47
*/
@Configuration
public class GunsDbInitListenerAutoConfiguration {
@Bean
public InitTableListener initTableListener() {
return new InitTableListener();
}
}

View File

@ -1,4 +1,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.stylefeng.roses.kernel.db.starter.GunsDataSourceAutoConfiguration,\
cn.stylefeng.roses.kernel.db.starter.GunsDataSourceAutoConfiguration,\
cn.stylefeng.roses.kernel.db.starter.GunsMyBatisPlusAutoConfiguration
cn.stylefeng.roses.kernel.db.starter.GunsMyBatisPlusAutoConfiguration,\
cn.stylefeng.roses.kernel.db.starter.GunsDbInitListenerAutoConfiguration

View File

@ -17,6 +17,7 @@
<modules>
<module>db-api</module>
<module>db-sdk-init</module>
<module>db-sdk-mp</module>
<module>db-spring-boot-starter</module>
</modules>

View File

@ -8,4 +8,12 @@ package cn.stylefeng.roses.kernel.dict.api;
*/
public interface DictApi {
/**
* id
*
* @author fengshuonan
* @date 2020/12/25 14:14
*/
String getDictNameByDictCode(String dictCode);
}

View File

@ -21,6 +21,6 @@ public interface DictConstants {
/**
* id
*/
Long DEFAULT_DICT_PARENT_ID = 0L;
Long DEFAULT_DICT_PARENT_ID = -1L;
}

View File

@ -12,12 +12,12 @@ import lombok.Getter;
public enum DictTypeClassEnum {
/**
*
*
*/
BUSINESS_TYPE(1),
/**
*
*
*/
SYSTEM_TYPE(2);

View File

@ -1,5 +1,6 @@
package cn.stylefeng.roses.kernel.dict.api.exception;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.dict.api.constants.DictConstants;
import cn.stylefeng.roses.kernel.rule.abstracts.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
@ -12,8 +13,8 @@ import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
*/
public class DictException extends ServiceException {
public DictException(AbstractExceptionEnum exception, String userTip) {
super(DictConstants.DICT_MODULE_NAME, exception.getErrorCode(), userTip);
public DictException(AbstractExceptionEnum exception, Object... params) {
super(DictConstants.DICT_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
}
public DictException(AbstractExceptionEnum exception) {

View File

@ -27,7 +27,7 @@ public enum DictExceptionEnum implements AbstractExceptionEnum {
/**
* idid
*/
PARENT_DICT_NOT_EXISTED(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + DictConstants.DICT_EXCEPTION_STEP_CODE + "03", "父级字典id不存在输入的父级id不合理父级id{}"),
PARENT_DICT_NOT_EXISTED(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + DictConstants.DICT_EXCEPTION_STEP_CODE + "03", "父级id不存在输入的父级id不合理父级id{}"),
/**
*
@ -44,10 +44,15 @@ public enum DictExceptionEnum implements AbstractExceptionEnum {
*/
DICT_TYPE_CODE_REPEAT(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + DictConstants.DICT_EXCEPTION_STEP_CODE + "06", "字典类型编码重复,字典类型编码:{}"),
/**
*
*/
SYSTEM_DICT_NOT_ALLOW_OPERATION(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + DictConstants.DICT_EXCEPTION_STEP_CODE + "07", "系统字典不允许操作,如需操作请联系超级管理员!"),
/**
*
*/
DICT_TYPE_NOT_EXISTED(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + DictConstants.DICT_EXCEPTION_STEP_CODE + "07", "字典类型不存在字典类型id{}");
DICT_TYPE_NOT_EXISTED(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + DictConstants.DICT_EXCEPTION_STEP_CODE + "08", "字典类型不存在字典类型id{}");
/**
*

View File

@ -0,0 +1,27 @@
package cn.stylefeng.roses.kernel.dict.api.pojo.dict.request;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* ids
*
* @author fengshuonan
* @date 2020/12/11 18:12
*/
@Data
public class ParentIdsUpdateRequest implements Serializable {
private static final long serialVersionUID = 1L;
private String oldParentIds;
private String newParentIds;
private Date updateTime;
private Long updateUser;
}

View File

@ -40,6 +40,14 @@
<version>1.0.0</version>
</dependency>
<!--数据库sdk-->
<!--数据库初始化-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>db-sdk-init</artifactId>
<version>1.0.0</version>
</dependency>
<!--数据库sdk-->
<!--数据库dao框架-->
<dependency>
@ -48,6 +56,14 @@
<version>1.0.0</version>
</dependency>
<!--中文转首字母转化-->
<!--用户名称的首字母转换等-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>pinyin-sdk-pinyin4j</artifactId>
<version>1.0.0</version>
</dependency>
<!--web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -14,6 +14,7 @@ import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@ -38,7 +39,7 @@ public class DictController {
* @date 2020/10/29 16:35
*/
@PostResource(name = "添加字典", path = "/dict/addDict", requiredPermission = false)
public ResponseData addDictType(@RequestBody @Validated(DictRequest.add.class) DictRequest dictRequest) {
public ResponseData addDict(@RequestBody @Validated(DictRequest.add.class) DictRequest dictRequest) {
this.dictService.addDict(dictRequest);
return new SuccessResponseData();
}
@ -86,8 +87,8 @@ public class DictController {
* @date 2020/10/29 16:35
*/
@GetResource(name = "获取字典详情", path = "/dict/getDictDetail", requiredPermission = false)
public ResponseData getDictDetail(@RequestBody @Validated(DictRequest.detail.class) DictRequest dictRequest) {
SysDict detail = this.dictService.findDetail(dictRequest);
public ResponseData getDictDetail(@RequestParam("dictId") Long dictId) {
SysDict detail = this.dictService.findDetail(dictId);
return new SuccessResponseData(detail);
}
@ -97,10 +98,24 @@ public class DictController {
* @author fengshuonan
* @date 2020/10/29 16:35
*/
@PostResource(name = "获取字典列表", path = "/dict/getDictList", requiredPermission = false)
public ResponseData getDictList(@RequestBody DictRequest dictRequest) {
List<SysDict> dictList = this.dictService.findList(dictRequest);
return new SuccessResponseData(dictList);
@GetResource(name = "获取字典列表", path = "/dict/getDictList", requiredPermission = false)
public ResponseData getDictList(DictRequest dictRequest) {
List<SysDict> sysDictList = this.dictService.findList(dictRequest);
return new SuccessResponseData(sysDictList);
}
/**
*
* <p>
* dictIddictIddictId
*
* @author fengshuonan
* @date 2020/12/11 16:35
*/
@GetResource(name = "获取字典列表(排除下级)", path = "/dict/getDictListExcludeSub", requiredPermission = false)
public ResponseData getDictListExcludeSub(@RequestParam(value = "dictId", required = false) Long dictId) {
List<SysDict> sysDictList = this.dictService.getDictListExcludeSub(dictId);
return new SuccessResponseData(sysDictList);
}
/**
@ -109,8 +124,8 @@ public class DictController {
* @author fengshuonan
* @date 2020/10/29 16:35
*/
@PostResource(name = "获取字典列表", path = "/dict/getDictListPage", requiredPermission = false)
public ResponseData getDictListPage(@RequestBody DictRequest dictRequest) {
@GetResource(name = "获取字典列表", path = "/dict/getDictListPage", requiredPermission = false)
public ResponseData getDictListPage(DictRequest dictRequest) {
PageResult<SysDict> page = this.dictService.findPageList(dictRequest);
return new SuccessResponseData(page);
}
@ -121,8 +136,8 @@ public class DictController {
* @author fengshuonan
* @date 2020/10/29 16:36
*/
@PostResource(name = "获取树形字典列表", path = "/dict/getDictTreeList", requiredPermission = false)
public ResponseData getDictTreeList(@RequestBody @Validated(DictRequest.treeList.class) DictRequest dictRequest) {
@GetResource(name = "获取树形字典列表", path = "/dict/getDictTreeList", requiredPermission = false)
public ResponseData getDictTreeList(@Validated(DictRequest.treeList.class) DictRequest dictRequest) {
List<TreeDictInfo> treeDictList = this.dictService.getTreeDictList(dictRequest);
return new SuccessResponseData(treeDictList);
}
@ -133,8 +148,8 @@ public class DictController {
* @author fengshuonan
* @date 2020/10/29 16:36
*/
@PostResource(name = "code校验", path = "/dict/validateCodeAvailable", requiredPermission = false)
public ResponseData validateCodeAvailable(@RequestBody @Validated(DictRequest.treeList.class) DictRequest dictRequest) {
@GetResource(name = "code校验", path = "/dict/validateCodeAvailable", requiredPermission = false)
public ResponseData validateCodeAvailable(@Validated(DictRequest.validateAvailable.class) DictRequest dictRequest) {
boolean flag = this.dictService.validateCodeAvailable(dictRequest);
return new SuccessResponseData(flag);
}

View File

@ -1,16 +1,15 @@
package cn.stylefeng.roses.kernel.dict.modular.controller;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictTypeRequest;
import cn.stylefeng.roses.kernel.dict.modular.service.DictTypeService;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
@ -36,7 +35,7 @@ public class DictTypeController {
*
*
* @author fengshuonan
* @Date 2018/7/25 12:36
* @date 2018/7/25 12:36
*/
@PostResource(name = "添加字典类型", path = "/dictType/addDictType", requiredPermission = false)
public ResponseData addDictType(@RequestBody @Validated(DictTypeRequest.add.class) DictTypeRequest dictTypeRequest) {
@ -48,7 +47,7 @@ public class DictTypeController {
*
*
* @author fengshuonan
* @Date 2018/7/25 12:36
* @date 2018/7/25 12:36
*/
@PostResource(name = "修改字典类型", path = "/dictType/updateDictType", requiredPermission = false)
public ResponseData updateDictType(@RequestBody @Validated(DictTypeRequest.edit.class) DictTypeRequest dictTypeRequest) {
@ -60,7 +59,7 @@ public class DictTypeController {
*
*
* @author fengshuonan
* @Date 2018/7/25 12:36
* @date 2018/7/25 12:36
*/
@PostResource(name = "修改字典类型状态", path = "/dictType/updateStatus", requiredPermission = false)
public ResponseData updateStatus(@RequestBody @Validated(BaseRequest.updateStatus.class) DictTypeRequest dictTypeRequest) {
@ -72,7 +71,7 @@ public class DictTypeController {
*
*
* @author fengshuonan
* @Date 2018/7/25 12:36
* @date 2018/7/25 12:36
*/
@PostResource(name = "删除字典类型", path = "/dictType/deleteDictType", requiredPermission = false)
public ResponseData deleteDictType(@RequestBody @Validated(DictTypeRequest.delete.class) DictTypeRequest dictTypeRequest) {
@ -86,8 +85,8 @@ public class DictTypeController {
* @author fengshuonan
* @date 2020/10/30 21:46
*/
@PostResource(name = "获取字典类型列表", path = "/dictType/getDictTypeList", requiredPermission = false)
public ResponseData getDictTypeList(@RequestBody DictTypeRequest dictTypeRequest) {
@GetResource(name = "获取字典类型列表", path = "/dictType/getDictTypeList", requiredPermission = false)
public ResponseData getDictTypeList(DictTypeRequest dictTypeRequest) {
List<SysDictType> sysDictTypeList = dictTypeService.getDictTypeList(dictTypeRequest);
return new SuccessResponseData(sysDictTypeList);
}
@ -98,10 +97,9 @@ public class DictTypeController {
* @author fengshuonan
* @date 2020/10/30 21:46
*/
@PostResource(name = "获取字典类型列表(分页)", path = "/dictType/getDictTypePageList", requiredPermission = false)
public ResponseData getDictTypePageList(@RequestBody DictTypeRequest dictTypeRequest) {
Page<SysDictType> page = PageFactory.defaultPage();
PageResult<SysDictType> dictTypePageList = dictTypeService.getDictTypePageList(page, dictTypeRequest);
@GetResource(name = "获取字典类型列表(分页)", path = "/dictType/getDictTypePageList", requiredPermission = false)
public ResponseData getDictTypePageList(DictTypeRequest dictTypeRequest) {
PageResult<SysDictType> dictTypePageList = dictTypeService.getDictTypePageList(dictTypeRequest);
return new SuccessResponseData(dictTypePageList);
}
@ -111,8 +109,8 @@ public class DictTypeController {
* @author fengshuonan
* @date 2020/10/30 21:53
*/
@PostResource(name = "code校验", path = "/dictType/validateCodeAvailable", requiredPermission = false)
public ResponseData validateCodeAvailable(@RequestBody DictTypeRequest dictTypeRequest) {
@GetResource(name = "code校验", path = "/dictType/validateCodeAvailable", requiredPermission = false)
public ResponseData validateCodeAvailable(@Validated(DictTypeRequest.validateCode.class) DictTypeRequest dictTypeRequest) {
boolean flag = this.dictTypeService.validateCodeAvailable(dictTypeRequest);
return new SuccessResponseData(flag);
}

View File

@ -0,0 +1,49 @@
package cn.stylefeng.roses.kernel.dict.modular.db.init;
import cn.stylefeng.roses.kernel.db.init.actuator.DbInitializer;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import org.springframework.stereotype.Component;
/**
*
*
* @author majianguo
* @date 2020/12/9 11:02
*/
@Component
public class DictInitializer extends DbInitializer {
@Override
protected String getTableInitSql() {
return "CREATE TABLE `sys_dict` (\n" +
" `dict_id` bigint(20) NOT NULL COMMENT '字典id',\n" +
" `dict_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '字典编码',\n" +
" `dict_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '字典名称',\n" +
" `dict_name_pinyin` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典名称首字母',\n" +
" `dict_encode` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典编码',\n" +
" `dict_type_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '字典类型的编码',\n" +
" `dict_short_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典简称',\n" +
" `dict_short_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典简称的编码',\n" +
" `dict_parent_id` bigint(20) NOT NULL COMMENT '上级字典的id(如果没有上级字典id则为-1)',\n" +
" `status_flag` tinyint(4) NOT NULL COMMENT '状态:(1-启用,2-禁用),参考 StatusEnum',\n" +
" `dict_sort` decimal(10, 2) NULL DEFAULT NULL COMMENT '排序,带小数点',\n" +
" `dict_pids` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '父id集合',\n" +
" `del_flag` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'N' COMMENT '是否删除Y-被删除N-未删除',\n" +
" `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',\n" +
" `create_user` bigint(20) NULL DEFAULT NULL COMMENT '创建用户id',\n" +
" `update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',\n" +
" `update_user` bigint(20) NULL DEFAULT NULL COMMENT '修改用户id',\n" +
" PRIMARY KEY (`dict_id`) USING BTREE\n" +
") ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '字典表' ROW_FORMAT = Dynamic;";
}
@Override
protected String getTableName() {
return "sys_dict";
}
@Override
protected Class<?> getEntityClass() {
return SysDict.class;
}
}

View File

@ -0,0 +1,46 @@
package cn.stylefeng.roses.kernel.dict.modular.db.init;
import cn.stylefeng.roses.kernel.db.init.actuator.DbInitializer;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import org.springframework.stereotype.Component;
/**
*
*
* @author majianguo
* @date 2020/12/9 11:02
*/
@Component
public class DictTypeInitializer extends DbInitializer {
@Override
protected String getTableInitSql() {
return "CREATE TABLE `sys_dict_type` (\n" +
" `dict_type_id` bigint(20) NOT NULL COMMENT '字典类型id',\n" +
" `dict_type_class` int(2) NULL DEFAULT NULL COMMENT '字典类型: 1-业务类型2-系统类型,参考 DictTypeClassEnum',\n" +
" `dict_type_bus_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典类型业务编码',\n" +
" `dict_type_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典类型编码',\n" +
" `dict_type_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典类型名称',\n" +
" `dict_type_name_pinyin` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典类型名称首字母拼音',\n" +
" `dict_type_desc` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '字典类型描述',\n" +
" `status_flag` tinyint(4) NULL DEFAULT NULL COMMENT '字典类型的状态1-启用2-禁用,参考 StatusEnum',\n" +
" `dict_type_sort` decimal(10, 2) NULL DEFAULT NULL COMMENT '排序,带小数点',\n" +
" `del_flag` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'N' COMMENT '是否删除Y-被删除N-未删除',\n" +
" `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',\n" +
" `create_user` bigint(20) NULL DEFAULT NULL COMMENT '创建用户id',\n" +
" `update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',\n" +
" `update_user` bigint(20) NULL DEFAULT NULL COMMENT '修改用户id',\n" +
" PRIMARY KEY (`dict_type_id`) USING BTREE\n" +
") ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '字典类型表,一个字典类型下有多个字典' ROW_FORMAT = Dynamic;";
}
@Override
protected String getTableName() {
return "sys_dict_type";
}
@Override
protected Class<?> getEntityClass() {
return SysDictType.class;
}
}

View File

@ -14,13 +14,15 @@ import java.math.BigDecimal;
*
*
* @author fengshuonan
* @date 2020/10/30 9:54
* @date 2020/12/26 22:37
*/
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("sys_dict")
@Data
public class SysDict extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* id
*/
@ -28,10 +30,10 @@ public class SysDict extends BaseEntity {
private Long dictId;
/**
*
*
*/
@TableField("dict_type_code")
private String dictTypeCode;
@TableField("dict_code")
private String dictCode;
/**
*
@ -40,10 +42,22 @@ public class SysDict extends BaseEntity {
private String dictName;
/**
* +线
*
*/
@TableField("dict_code")
private String dictCode;
@TableField("dict_name_pinyin")
private String dictNamePinyin;
/**
*
*/
@TableField("dict_encode")
private String dictEncode;
/**
*
*/
@TableField("dict_type_code")
private String dictTypeCode;
/**
*
@ -58,25 +72,31 @@ public class SysDict extends BaseEntity {
private String dictShortCode;
/**
* id(id0)
* id(id-1)
*/
@TableField("parent_dict_id")
private Long parentDictId;
@TableField("dict_parent_id")
private Long dictParentId;
/**
*
*/
@TableField(value = "dict_sort")
private BigDecimal dictSort;
/**
* 1-2- StatusEnum
* (1-,2-), StatusEnum
*/
@TableField("status_flag")
private Integer statusFlag;
/**
* Y-N-
*
*/
@TableField("dict_sort")
private BigDecimal dictSort;
/**
* id
*/
@TableField("dict_pids")
private String dictPids;
/**
* Y-N-
*/
@TableField("del_flag")
private String delFlag;
@ -86,4 +106,9 @@ public class SysDict extends BaseEntity {
*/
private transient String dictTypeName;
/**
*
*/
private transient String parentName;
}

View File

@ -25,7 +25,7 @@ public class SysDictType extends BaseEntity {
* id
*/
@TableId(value = "dict_type_id", type = IdType.ASSIGN_ID)
private String dictTypeId;
private Long dictTypeId;
/**
* 1-2- DictTypeClassEnum
@ -39,24 +39,30 @@ public class SysDictType extends BaseEntity {
@TableField("dict_type_code")
private String dictTypeCode;
/**
*
*/
@TableField("dict_type_bus_code")
private String dictTypeBusCode;
/**
*
*/
@TableField("dict_type_name")
private String dictTypeName;
/**
*
*/
@TableField("dict_type_name_pinyin")
private String dictTypeNamePinyin;
/**
*
*/
@TableField("dict_type_desc")
private String dictTypeDesc;
/**
*
*/
@TableField(value = "dict_type_sort")
private BigDecimal dictTypeSort;
/**
* 1-2- StatusEnum
*/
@ -64,9 +70,15 @@ public class SysDictType extends BaseEntity {
private Integer statusFlag;
/**
* Y-N-
* Y-N- YesOrNotEnum
*/
@TableField("del_flag")
private String delFlag;
/**
*
*/
@TableField(value = "dict_type_sort")
private BigDecimal dictTypeSort;
}

View File

@ -1,5 +1,6 @@
package cn.stylefeng.roses.kernel.dict.modular.mapper;
import cn.stylefeng.roses.kernel.dict.api.pojo.dict.request.ParentIdsUpdateRequest;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictRequest;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@ -19,12 +20,12 @@ public interface DictMapper extends BaseMapper<SysDict> {
/**
*
*
* @param dictRequest
* @param dictId id
* @return
* @author fengshuonan
* @date 2020/10/29 17:21
*/
SysDict findDetail(@Param("dictRequest") DictRequest dictRequest);
SysDict findDetail(@Param("dictId") Long dictId);
/**
*
@ -35,6 +36,35 @@ public interface DictMapper extends BaseMapper<SysDict> {
* @author fengshuonan
* @date 2020/10/29 17:21
*/
List<SysDict> findList(Page<SysDict> page, @Param("dictRequest") DictRequest dictRequest);
List<SysDict> findList(@Param("page") Page<SysDict> page, @Param("dictRequest") DictRequest dictRequest);
/**
*
* <p>
* dictIddictIddictId
*
* @param dictId id
* @author fengshuonan
* @date 2020/12/11 16:35
*/
List<SysDict> getDictListExcludeSub(@Param("dictId") Long dictId);
/**
* pids
*
* @param parentIdsUpdateRequest
* @author fengshuonan
* @date 2020/12/11 17:21
*/
void updateSubPids(@Param("paramCondition") ParentIdsUpdateRequest parentIdsUpdateRequest);
/**
*
*
* @param dictId id
* @author fengshuonan
* @date 2020/12/11 16:35
*/
void deleteSub(@Param("dictId") Long dictId);
}

View File

@ -2,49 +2,46 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.stylefeng.roses.kernel.dict.modular.mapper.DictMapper">
<select id="findDetail" resultType="cn.stylefeng.roses.kernel.dict.modular.entity.SysDict">
SELECT
dict.dict_id AS dictId,
dict.dict_type_code AS dictTypeCode,
dict.dict_name as dictName,
dict.dict_code AS dictCode,
dict.dict_short_name AS dictShortName,
dict.dict_short_code AS dictShortCode,
dict.parent_dict_id AS parentDictId,
dict.dict_sort as dictSort,
dict.status_flag AS statusFlag,
dict.create_time AS createTime,
type.dict_type_name as dictTypeName
FROM
sys_dict dict
INNER JOIN sys_dict_type type ON dict.dict_type_code = type.dict_type_code
<select id="findDetail" resultType="cn.stylefeng.roses.kernel.dict.modular.entity.SysDict" parameterType="long">
SELECT dict.dict_id AS dictId,
dict.dict_code AS dictCode,
dict.dict_name as dictName,
dict.dict_encode as dictEncode,
dict.dict_sort as dictSort,
dict.dict_type_code AS dictTypeCode,
dict.dict_short_name AS dictShortName,
dict.dict_short_code AS dictShortCode,
dict.dict_parent_id AS dictParentId,
dict.status_flag AS statusFlag,
dict.create_time AS createTime,
type.dict_type_name AS dictTypeName
FROM sys_dict dict
INNER JOIN sys_dict_type type ON dict.dict_type_code = type.dict_type_code
WHERE dict.del_flag = 'N'
<if test="dictRequest.dictId != null and dictRequest.dictId != ''">
AND dict.dict_id = #{dictRequest.dictId}
</if>
<if test="dictRequest.dictCode != null and dictRequest.dictCode != ''">
AND dict.dict_code = #{dictRequest.dictCode}
</if>
ORDER BY dict_sort
AND dict.dict_id = #{dictId}
</select>
<select id="findList" resultType="cn.stylefeng.roses.kernel.dict.modular.entity.SysDict">
SELECT
dict.dict_id AS dictId,
dict.dict_type_code AS dictTypeCode,
dict.dict_name as dictName,
dict.dict_code AS dictCode,
dict.dict_short_name AS dictShortName,
dict.dict_short_code AS dictShortCode,
dict.parent_dict_id AS parentDictId,
dict.dict_sort as dictSort,
dict.status_flag AS statusFlag,
dict.create_time AS createTime,
type.dict_type_name as dictTypeName
FROM
sys_dict dict
INNER JOIN sys_dict_type type ON dict.dict_type_code = type.dict_type_code
SELECT type.dict_type_name as dictTypeName,
dict.dict_id AS dictId,
dict.dict_code AS dictCode,
dict.dict_encode as dictEncode,
dict.dict_sort as dictSort,
dict.dict_name as dictName,
dict.dict_type_code AS dictTypeCode,
dict.dict_short_name AS dictShortName,
dict.dict_short_code AS dictShortCode,
dict.dict_parent_id AS dictParentId,
dict.status_flag AS statusFlag,
dict.create_time AS createTime,
pDict.dict_name AS parentName
FROM sys_dict dict
INNER JOIN sys_dict_type type ON dict.dict_type_code = type.dict_type_code
LEFT JOIN sys_dict pDict ON pDict.dict_id = dict.dict_parent_id
WHERE dict.del_flag = 'N'
<if test="dictRequest.statusFlag != null and dictRequest.statusFlag != ''">
AND dict.status_flag = #{dictRequest.statusFlag}
</if>
<if test="dictRequest.dictTypeCode != null and dictRequest.dictTypeCode != ''">
AND dict.dict_type_code = #{dictRequest.dictTypeCode}
</if>
@ -52,18 +49,45 @@
AND dict.dict_code LIKE CONCAT('%',#{dictRequest.dictCode},'%')
</if>
<if test="dictRequest.dictName != null and dictRequest.dictName != ''">
AND dict.dict_name LIKE CONCAT('%',#{dictRequest.dictName},'%')
AND
(
dict.dict_name LIKE CONCAT('%',#{dictRequest.dictName},'%')
OR
dict.dict_name_pinyin LIKE CONCAT('%',#{dictRequest.dictName},'%')
)
</if>
<if test="dictRequest.parentDictId != null and dictRequest.parentDictId != ''">
AND dict.parent_dict_id = #{dictRequest.parentDictId}
</if>
<if test="dictRequest.statusFlag != null and dictRequest.statusFlag !=''">
AND dict.status_flag = #{dictRequest.statusFlag}
</if>
<if test="dictRequest.parentDictId != null and dictRequest.parentDictId != ''">
and dict.parent_dict_id = #{dictRequest.parentDictId}
<if test="dictRequest.dictParentId != null and dictRequest.dictParentId != ''">
AND dict.dict_parent_id = #{dictRequest.dictParentId}
</if>
ORDER BY dict.dict_sort
</select>
<select id="getDictListExcludeSub" resultType="cn.stylefeng.roses.kernel.dict.modular.entity.SysDict" parameterType="long">
SELECT *
FROM sys_dict
WHERE del_flag = 'N'
AND dict_pids NOT LIKE CONCAT('%', #{dictId}, '%')
<![CDATA[
AND dict_id <> #{dictId}
]]>
ORDER BY dict_sort
</select>
<update id="updateSubPids" parameterType="cn.stylefeng.roses.kernel.dict.api.pojo.dict.request.ParentIdsUpdateRequest">
UPDATE
sys_dict
SET dict_pids = replace(dict_pids, #{paramCondition.oldParentIds}, #{paramCondition.newParentIds}),
update_time = #{paramCondition.updateTime},
update_account_id = #{paramCondition.updateUser}
WHERE dict_pids LIKE CONCAT('%', #{paramCondition.oldParentIds}, '%')
</update>
<update id="deleteSub" parameterType="long">
UPDATE
sys_dict
SET del_flag = 'Y'
WHERE dict_id = #{dictId}
OR dict_pids LIKE CONCAT('%', #{dictId}, '%')
</update>
</mapper>

View File

@ -4,25 +4,29 @@
<!--查询字典类型列表-->
<select id="findList" resultType="cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType">
SELECT
dict_type_id AS dictTypeId,
dict_type_class AS dictTypeClass,
dict_type_code AS dictTypeCode,
dict_type_name AS dictTypeName,
dict_type_desc AS dictTypeDesc,
dict_type_sort AS dictTypeSort,
status_flag AS statusFlag
FROM
sys_dict_type
where del_flag = 'N'
SELECT dict_type_id AS dictTypeId,
dict_type_class AS dictTypeClass,
dict_type_bus_code AS dictTypeBusCode,
dict_type_code AS dictTypeCode,
dict_type_name AS dictTypeName,
dict_type_desc AS dictTypeDesc,
status_flag AS statusFlag,
dict_type_sort AS dictTypeSort
FROM sys_dict_type
WHERE del_flag = 'N'
<if test="dictTypeRequest.dictTypeName != null and dictTypeRequest.dictTypeName !=''">
AND dict_type_name like CONCAT('%',#{dictTypeRequest.dictTypeName},'%')
AND
(
dict_type_name like CONCAT('%', #{dictTypeRequest.dictTypeName}, '%')
OR
dict_type_name_pinyin like concat('%', #{dictTypeRequest.dictTypeName}, '%')
)
</if>
<if test="dictTypeRequest.dictTypeDesc != null and dictTypeRequest.dictTypeDesc !=''">
AND dict_type_desc like CONCAT('%',#{dictTypeRequest.dictTypeDesc},'%')
AND dict_type_desc like CONCAT('%', #{dictTypeRequest.dictTypeDesc}, '%')
</if>
<if test="dictTypeRequest.dictTypeCode != null and dictTypeRequest.dictTypeCode !=''">
AND dict_type_code like CONCAT('%',#{dictTypeRequest.dictTypeCode},'%')
AND dict_type_code like CONCAT('%', #{dictTypeRequest.dictTypeCode}, '%')
</if>
<if test="dictTypeRequest.statusFlag != null and dictTypeRequest.statusFlag !=''">
AND status_flag = #{dictTypeRequest.statusFlag}

View File

@ -34,7 +34,7 @@ public class TreeDictInfo implements AbstractTreeNode {
/**
* id
*/
private Long parentDictId;
private Long dictParentId;
/**
* tree
@ -52,10 +52,10 @@ public class TreeDictInfo implements AbstractTreeNode {
@Override
public String getNodeParentId() {
if (this.parentDictId == null) {
if (this.dictParentId == null) {
return null;
} else {
return this.parentDictId.toString();
return this.dictParentId.toString();
}
}

View File

@ -1,6 +1,7 @@
package cn.stylefeng.roses.kernel.dict.modular.pojo.request;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.validator.validators.status.StatusValue;
import cn.stylefeng.roses.kernel.validator.validators.unique.TableUniqueValue;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -22,28 +23,9 @@ public class DictRequest extends BaseRequest {
/**
* id
*/
@NotNull(message = "dictId不能为空", groups = {edit.class, delete.class, detail.class, updateStatus.class})
@NotNull(message = "id不能为空请检查id参数", groups = {edit.class, delete.class, detail.class, updateStatus.class})
private Long dictId;
/**
*
*/
@NotBlank(message = "字典类型编码不能为空", groups = {add.class, edit.class, treeList.class, validateAvailable.class})
private String dictTypeCode;
/**
*
*/
@NotBlank(message = "字典名称不能为空", groups = {add.class, edit.class})
@TableUniqueValue(
message = "字典名称存在重复",
groups = {add.class, edit.class},
tableName = "sys_dict",
columnName = "dict_name",
idFieldName = "dict_id",
excludeLogicDeleteItems = true)
private String dictName;
/**
*
*/
@ -57,6 +39,35 @@ public class DictRequest extends BaseRequest {
excludeLogicDeleteItems = true)
private String dictCode;
/**
*
*/
@NotBlank(message = "字典名称不能为空", groups = {add.class, edit.class})
@TableUniqueValue(
message = "字典名称存在重复",
groups = {add.class, edit.class},
tableName = "sys_dict",
columnName = "dict_name",
idFieldName = "dict_id",
excludeLogicDeleteItems = true)
private String dictName;
/**
*
*/
private String dictNamePinYin;
/**
*
*/
private String dictEncode;
/**
*
*/
@NotBlank(message = "字典类型编码不能为空", groups = {add.class, edit.class, treeList.class})
private String dictTypeCode;
/**
*
*/
@ -68,31 +79,44 @@ public class DictRequest extends BaseRequest {
private String dictShortCode;
/**
* id(id0)
* id
* <p>
*
* <p>
* id-1
*/
private Long parentDictId;
private Long dictParentId;
/**
* (1:,2:) StatusEnum
*/
@NotNull(message = "状态不能为空", groups = {updateStatus.class})
@StatusValue(groups = updateStatus.class)
private Integer statusFlag;
/**
*
*/
@NotNull(message = "排序不能为空", groups = {add.class, edit.class})
private BigDecimal dictSort;
/**
* 1-2- StatusEnum
* id,
*/
@NotNull(message = "状态不能为空", groups = {updateStatus.class, edit.class})
private Integer statusFlag;
private String dictPids;
/**
*
*/
public @interface treeList {
}
/**
*
*/
public @interface validateAvailable {
}
}

View File

@ -1,6 +1,7 @@
package cn.stylefeng.roses.kernel.dict.modular.pojo.request;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.validator.validators.status.StatusValue;
import cn.stylefeng.roses.kernel.validator.validators.unique.TableUniqueValue;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -22,7 +23,7 @@ public class DictTypeRequest extends BaseRequest {
/**
* id
*/
@NotNull(message = "dictTypeId不能为空", groups = {edit.class, delete.class, detail.class, updateStatus.class})
@NotNull(message = "id不能为空请检查id参数", groups = {edit.class, delete.class, detail.class, updateStatus.class})
private Long dictTypeId;
/**
@ -31,10 +32,15 @@ public class DictTypeRequest extends BaseRequest {
@NotNull(message = "字典类型不能为空", groups = {add.class, edit.class})
private Integer dictTypeClass;
/**
*
*/
private String dictTypeBusCode;
/**
*
*/
@NotBlank(message = "字典类型编码不能为空", groups = {add.class, edit.class})
@NotBlank(message = "字典类型编码不能为空", groups = {add.class, edit.class, validateCode.class})
@TableUniqueValue(
message = "字典类型编码存在重复",
groups = {add.class, edit.class},
@ -48,29 +54,36 @@ public class DictTypeRequest extends BaseRequest {
*
*/
@NotBlank(message = "字典类型名称不能为空", groups = {add.class, edit.class})
@TableUniqueValue(
message = "字典类型名称存在重复",
groups = {add.class, edit.class},
tableName = "sys_dict_type",
columnName = "dict_type_name",
idFieldName = "dict_type_id",
excludeLogicDeleteItems = true)
private String dictTypeName;
/**
*
*/
private String dictTypeNamePinYin;
/**
*
*/
private String dictTypeDesc;
/**
*
*/
private BigDecimal dictTypeSort;
/**
* 1-2- StatusEnum
*/
@NotNull(message = "状态不能为空", groups = {updateStatus.class})
@StatusValue(groups = updateStatus.class)
private Integer statusFlag;
/**
*
*/
@NotNull(message = "排序不能为空", groups = {add.class, edit.class})
private BigDecimal dictTypeSort;
/**
* code
*/
public @interface validateCode {
}
}

View File

@ -1,6 +1,7 @@
package cn.stylefeng.roses.kernel.dict.modular.service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.dict.api.DictApi;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.pojo.TreeDictInfo;
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictRequest;
@ -14,7 +15,7 @@ import java.util.List;
* @author fengshuonan
* @date 2020/10/29 17:43
*/
public interface DictService extends IService<SysDict> {
public interface DictService extends IService<SysDict>, DictApi {
/**
*
@ -55,12 +56,12 @@ public interface DictService extends IService<SysDict> {
/**
*
*
* @param dictRequest
* @param dictId id
* @return
* @author fengshuonan
* @date 2020/10/30 16:15
*/
SysDict findDetail(DictRequest dictRequest);
SysDict findDetail(Long dictId);
/**
*
@ -102,4 +103,14 @@ public interface DictService extends IService<SysDict> {
*/
boolean validateCodeAvailable(DictRequest dictRequest);
/**
*
* <p>
* dictIddictIddictId
*
* @author fengshuonan
* @date 2020/12/11 16:35
*/
List<SysDict> getDictListExcludeSub(Long dictId);
}

View File

@ -4,7 +4,6 @@ package cn.stylefeng.roses.kernel.dict.modular.service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictTypeRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
@ -66,13 +65,12 @@ public interface DictTypeService extends IService<SysDictType> {
/**
*
*
* @param page
* @param dictTypeRequest
* @return
* @author fengshuonan
* @date 2020/10/29 18:55
*/
PageResult<SysDictType> getDictTypePageList(Page<SysDictType> page, DictTypeRequest dictTypeRequest);
PageResult<SysDictType> getDictTypePageList(DictTypeRequest dictTypeRequest);
/**
* code

View File

@ -4,16 +4,20 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.dict.api.exception.DictException;
import cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum;
import cn.stylefeng.roses.kernel.dict.api.pojo.dict.request.ParentIdsUpdateRequest;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.mapper.DictMapper;
import cn.stylefeng.roses.kernel.dict.modular.pojo.TreeDictInfo;
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictRequest;
import cn.stylefeng.roses.kernel.dict.modular.service.DictService;
import cn.stylefeng.roses.kernel.pinyin.api.PinYinApi;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.factory.DefaultTreeBuildFactory;
@ -21,104 +25,135 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.Date;
import java.util.List;
import static cn.stylefeng.roses.kernel.dict.api.constants.DictConstants.DEFAULT_DICT_PARENT_ID;
import static cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum.WRONG_DICT_STATUS;
import static cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum.DICT_CODE_REPEAT;
/**
*
*
* @author majianguo
* @version 1.0
* @date 2020/10/28 9:48
* @author fengshuonan
* @date 2020/12/26 22:36
*/
@Service
@Slf4j
public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements DictService {
@Resource
private PinYinApi pinYinApi;
@Resource
private DictMapper dictMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public void addDict(DictRequest dictRequest) {
// 如果父节点为空则填充为默认的父节点id
if (dictRequest.getParentDictId() == null) {
dictRequest.setParentDictId(DEFAULT_DICT_PARENT_ID);
} else {
// 如果父节点不为空并且不是0则判断父节点存不存在防止脏数据
if (!DEFAULT_DICT_PARENT_ID.equals(dictRequest.getParentDictId())) {
SysDict parentDict = this.getById(dictRequest.getParentDictId());
if (parentDict == null) {
String userTip = StrUtil.format(DictExceptionEnum.PARENT_DICT_NOT_EXISTED.getUserTip(), dictRequest.getParentDictId());
throw new DictException(DictExceptionEnum.PARENT_DICT_NOT_EXISTED, userTip);
if (dictRequest.getDictParentId() == null) {
dictRequest.setDictParentId(DEFAULT_DICT_PARENT_ID);
}
// 如果父节点不为空,并且不是-1则判断父节点存不存在防止脏数据
else {
if (!DEFAULT_DICT_PARENT_ID.equals(dictRequest.getDictParentId())) {
SysDict parentSysDict = this.getById(dictRequest.getDictParentId());
if (parentSysDict == null) {
throw new DictException(DictExceptionEnum.PARENT_DICT_NOT_EXISTED, dictRequest.getDictParentId());
}
}
}
// 赋值pids
setPids(dictRequest);
// dto转化为实体
SysDict dict = new SysDict();
BeanUtil.copyProperties(dictRequest, dict);
SysDict sysDict = new SysDict();
BeanUtil.copyProperties(dictRequest, sysDict);
// dictId是自动生成
sysDict.setDictId(null);
// 设置状态启用
dict.setStatusFlag(StatusEnum.ENABLE.getCode());
sysDict.setStatusFlag(StatusEnum.ENABLE.getCode());
sysDict.setDelFlag(YesOrNotEnum.N.getCode());
this.baseMapper.insert(dict);
// 设置拼音
sysDict.setDictNamePinyin(pinYinApi.parseEveryPinyinFirstLetter(sysDict.getDictName()));
this.baseMapper.insert(sysDict);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateDict(DictRequest dictRequest) {
// 查询字典是否存在
SysDict oldDict = this.getById(dictRequest.getDictId());
if (oldDict == null) {
String userTip = StrUtil.format(DictExceptionEnum.DICT_NOT_EXISTED.getUserTip(), dictRequest.getDictId());
throw new DictException(DictExceptionEnum.DICT_NOT_EXISTED, userTip);
SysDict oldSysDict = this.getById(dictRequest.getDictId());
if (oldSysDict == null) {
throw new DictException(DictExceptionEnum.DICT_NOT_EXISTED, dictRequest.getDictId());
}
// 不能修改字典类型和编码
dictRequest.setDictTypeCode(null);
dictRequest.setDictCode(null);
// model转化为entity
BeanUtil.copyProperties(dictRequest, oldDict, CopyOptions.create().setIgnoreNullValue(true));
// 赋值pids
setPids(dictRequest);
if (!oldSysDict.getDictParentId().equals(dictRequest.getDictParentId())) {
updatePids(dictRequest, oldSysDict);
}
this.updateById(oldDict);
// model转化为entity
BeanUtil.copyProperties(dictRequest, oldSysDict, CopyOptions.create().setIgnoreNullValue(true));
// 设置拼音
oldSysDict.setDictNamePinyin(pinYinApi.parseEveryPinyinFirstLetter(oldSysDict.getDictName()));
this.updateById(oldSysDict);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateDictStatus(DictRequest dictRequest) {
// 检查参数状态是否合法
if (StatusEnum.codeToEnum(dictRequest.getStatusFlag()) == null) {
String userTip = StrUtil.format(WRONG_DICT_STATUS.getUserTip(), dictRequest.getStatusFlag());
throw new DictException(WRONG_DICT_STATUS, userTip);
// 查询对应的字典信息
SysDict sysDict = this.baseMapper.selectById(dictRequest.getDictId());
if (sysDict == null) {
throw new DictException(DictExceptionEnum.DICT_NOT_EXISTED, dictRequest.getDictId());
}
// 查询对应的字典信息
SysDict dict = this.baseMapper.selectById(dictRequest.getDictId());
if (dict == null) {
String userTip = StrUtil.format(DictExceptionEnum.DICT_NOT_EXISTED.getUserTip(), dictRequest.getDictId());
throw new DictException(DictExceptionEnum.DICT_NOT_EXISTED, userTip);
// 如果是禁用 禁用所有下级状态
if (StatusEnum.DISABLE.getCode().equals(dictRequest.getStatusFlag())) {
LambdaUpdateWrapper<SysDict> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.like(SysDict::getDictPids, sysDict.getDictId());
lambdaUpdateWrapper.set(SysDict::getStatusFlag, dictRequest.getStatusFlag());
this.update(lambdaUpdateWrapper);
}
// 修改状态
dict.setStatusFlag(dictRequest.getStatusFlag());
this.updateById(dict);
sysDict.setStatusFlag(dictRequest.getStatusFlag());
this.updateById(sysDict);
}
@Override
public void deleteDict(DictRequest dictRequest) {
LambdaUpdateWrapper<SysDict> sysDictLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
sysDictLambdaUpdateWrapper.set(SysDict::getDelFlag, YesOrNotEnum.Y.getCode());
sysDictLambdaUpdateWrapper.eq(SysDict::getDictId, dictRequest.getDictId());
this.update(sysDictLambdaUpdateWrapper);
//删除自己和下级
dictMapper.deleteSub(dictRequest.getDictId());
}
@Override
public SysDict findDetail(DictRequest dictRequest) {
return this.baseMapper.findDetail(dictRequest);
public SysDict findDetail(Long dictId) {
return this.baseMapper.findDetail(dictId);
}
@Override
@ -126,6 +161,7 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
if (dictRequest == null) {
dictRequest = new DictRequest();
}
return baseMapper.findList(null, dictRequest);
}
@ -136,8 +172,8 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
}
Page<SysDict> page = PageFactory.defaultPage();
baseMapper.findList(page, dictRequest);
List<SysDict> list = baseMapper.findList(page, dictRequest);
page.setRecords(list);
return PageResultFactory.createPageResult(page);
}
@ -145,19 +181,19 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
public List<TreeDictInfo> getTreeDictList(DictRequest dictRequest) {
// 获取字典类型下所有的字典
List<SysDict> dictList = this.findList(dictRequest);
if (dictList == null || dictList.isEmpty()) {
List<SysDict> sysDictList = this.findList(dictRequest);
if (sysDictList == null || sysDictList.isEmpty()) {
return new ArrayList<>();
}
// 构造树节点信息
ArrayList<TreeDictInfo> treeDictInfos = new ArrayList<>();
for (SysDict dict : dictList) {
for (SysDict sysDict : sysDictList) {
TreeDictInfo treeDictInfo = new TreeDictInfo();
treeDictInfo.setDictId(dict.getDictId());
treeDictInfo.setDictCode(dict.getDictCode());
treeDictInfo.setParentDictId(dict.getParentDictId());
treeDictInfo.setDictName(dict.getDictName());
treeDictInfo.setDictId(sysDict.getDictId());
treeDictInfo.setDictCode(sysDict.getDictCode());
treeDictInfo.setDictParentId(sysDict.getDictParentId());
treeDictInfo.setDictName(sysDict.getDictName());
treeDictInfos.add(treeDictInfo);
}
@ -170,18 +206,13 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
// 判断编码是否重复
LambdaQueryWrapper<SysDict> codeRepeatWrapper = new LambdaQueryWrapper<>();
codeRepeatWrapper
.eq(SysDict::getDictTypeCode, dictRequest.getDictTypeCode())
.and(i -> i.eq(SysDict::getDictCode, dictRequest.getDictCode()));
codeRepeatWrapper.eq(SysDict::getDictCode, dictRequest.getDictCode());
// 如果传了字典id代表是编辑字典
if (ObjectUtil.isNotEmpty(dictRequest.getDictId())) {
codeRepeatWrapper.and(i -> i.ne(SysDict::getDictId, dictRequest.getDictId()));
}
// 不查询被删除的
codeRepeatWrapper.eq(SysDict::getDelFlag, YesOrNotEnum.N.getCode());
// 如果重复,抛出异常
int codeCount = this.baseMapper.selectCount(codeRepeatWrapper);
@ -189,4 +220,86 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
return codeCount <= 0;
}
@Override
public List<SysDict> getDictListExcludeSub(Long dictId) {
if (dictId != null) {
return dictMapper.getDictListExcludeSub(dictId);
}
return baseMapper.findList(null, new DictRequest());
}
@Override
public String getDictNameByDictCode(String dictCode) {
LambdaQueryWrapper<SysDict> sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysDictLambdaQueryWrapper.eq(SysDict::getDictCode, dictCode);
sysDictLambdaQueryWrapper.ne(SysDict::getDelFlag, YesOrNotEnum.Y.getCode());
List<SysDict> list = this.list(sysDictLambdaQueryWrapper);
// 如果查询不到字典,则返回空串
if (list.isEmpty()) {
return StrUtil.EMPTY;
}
// 字典code存在多个重复的返回空串并打印错误日志
if (list.size() > 1) {
log.error(DICT_CODE_REPEAT.getUserTip(), "", dictCode);
return StrUtil.EMPTY;
}
String dictName = list.get(0).getDictName();
if (dictName != null) {
return dictName;
} else {
return StrUtil.EMPTY;
}
}
/**
* pids
*
* @author fengshuonan
* @date 2020/12/26 12:19
*/
private static ParentIdsUpdateRequest createParenIdsUpdateRequest(String newParentIds, String oldParentIds) {
ParentIdsUpdateRequest parentIdsUpdateRequest = new ParentIdsUpdateRequest();
parentIdsUpdateRequest.setNewParentIds(newParentIds);
parentIdsUpdateRequest.setOldParentIds(oldParentIds);
parentIdsUpdateRequest.setUpdateTime(new Date());
parentIdsUpdateRequest.setUpdateUser(LoginContext.me().getLoginUser().getUserId());
return parentIdsUpdateRequest;
}
/**
* pids
*
* @author fengshuonan
* @date 2020/12/11 9:48
*/
private void updatePids(DictRequest dictRequest, SysDict oldSysDict) {
String oldPids = oldSysDict.getDictPids();
oldPids = oldPids + StrUtil.COMMA + oldSysDict.getDictId();
ParentIdsUpdateRequest parentIdsUpdateRequest = createParenIdsUpdateRequest(
dictRequest.getDictPids() + StrUtil.COMMA + dictRequest.getDictId(), oldPids);
dictMapper.updateSubPids(parentIdsUpdateRequest);
}
/**
* pids
*
* @author fengshuonan
* @date 2020/12/11 9:48
*/
private void setPids(DictRequest dictRequest) {
Long dictParentId = dictRequest.getDictParentId();
if (RuleConstants.TREE_ROOT_ID.equals(dictParentId)) {
dictRequest.setDictPids(RuleConstants.TREE_ROOT_ID.toString());
} else {
SysDict parentSysDict = dictMapper.selectById(dictParentId);
if (parentSysDict != null) {
dictRequest.setDictPids(parentSysDict.getDictPids() + StrUtil.COMMA + dictParentId);
}
}
}
}

View File

@ -2,16 +2,20 @@ package cn.stylefeng.roses.kernel.dict.modular.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.dict.api.enums.DictTypeClassEnum;
import cn.stylefeng.roses.kernel.dict.api.exception.DictException;
import cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import cn.stylefeng.roses.kernel.dict.modular.mapper.DictTypeMapper;
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictTypeRequest;
import cn.stylefeng.roses.kernel.dict.modular.service.DictService;
import cn.stylefeng.roses.kernel.dict.modular.service.DictTypeService;
import cn.stylefeng.roses.kernel.pinyin.api.PinYinApi;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -19,41 +23,55 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
*
*
* @author majianguo
* @version 1.0
* @date 2020/10/28 9:58
* @author fengshuonan
* @date 2020/12/26 22:36
*/
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, SysDictType> implements DictTypeService {
@Resource
private DictService dictService;
@Resource
private PinYinApi pinYinApi;
@Override
public void addDictType(DictTypeRequest dictTypeRequest) {
// 如果是系统级字典,只允许管理员操作
validateSystemTypeClassOperate(dictTypeRequest);
// 模型转化
SysDictType sysDictType = new SysDictType();
BeanUtil.copyProperties(dictTypeRequest, sysDictType);
// 设置初始状态
sysDictType.setStatusFlag(StatusEnum.ENABLE.getCode());
sysDictType.setDelFlag(YesOrNotEnum.N.getCode());
// 设置首字母拼音
sysDictType.setDictTypeNamePinyin(pinYinApi.parseEveryPinyinFirstLetter(sysDictType.getDictTypeName()));
this.baseMapper.insert(sysDictType);
}
@Override
public void updateDictType(DictTypeRequest dictTypeRequest) {
// 如果是系统级字典,只允许管理员操作
validateSystemTypeClassOperate(dictTypeRequest);
// 获取字典类型是否存在
SysDictType oldSysDictType = this.baseMapper.selectById(dictTypeRequest.getDictTypeId());
if (oldSysDictType == null) {
String userTip = StrUtil.format(DictExceptionEnum.DICT_TYPE_NOT_EXISTED.getUserTip(), dictTypeRequest.getDictTypeId());
throw new DictException(DictExceptionEnum.DICT_TYPE_NOT_EXISTED, userTip);
throw new DictException(DictExceptionEnum.DICT_TYPE_NOT_EXISTED, dictTypeRequest.getDictTypeId());
}
// 模型转化
@ -62,38 +80,60 @@ public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, SysDictType
// 不能修改字典编码
oldSysDictType.setDictTypeCode(null);
// 设置首字母拼音
oldSysDictType.setDictTypeNamePinyin(pinYinApi.parseEveryPinyinFirstLetter(oldSysDictType.getDictTypeName()));
this.updateById(oldSysDictType);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateDictTypeStatus(DictTypeRequest dictTypeRequest) {
// 如果是系统级字典,只允许管理员操作
validateSystemTypeClassOperate(dictTypeRequest);
// 获取字典类型是否存在
SysDictType oldSysDictType = this.baseMapper.selectById(dictTypeRequest.getDictTypeId());
if (oldSysDictType == null) {
String userTip = StrUtil.format(DictExceptionEnum.DICT_TYPE_NOT_EXISTED.getUserTip(), dictTypeRequest.getDictTypeId());
throw new DictException(DictExceptionEnum.DICT_TYPE_NOT_EXISTED, userTip);
}
// 判断状态是否正确
StatusEnum statusEnum = StatusEnum.codeToEnum(dictTypeRequest.getStatusFlag());
if (statusEnum == null) {
String userTip = StrUtil.format(DictExceptionEnum.WRONG_DICT_STATUS.getUserTip(), dictTypeRequest.getStatusFlag());
throw new DictException(DictExceptionEnum.WRONG_DICT_STATUS, userTip);
throw new DictException(DictExceptionEnum.DICT_TYPE_NOT_EXISTED, dictTypeRequest.getDictTypeId());
}
// 修改状态
oldSysDictType.setStatusFlag(dictTypeRequest.getStatusFlag());
// 修改所有本类型下的字典状态
LambdaUpdateWrapper<SysDict> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
lambdaQueryWrapper.eq(SysDict::getDictTypeCode, oldSysDictType.getDictTypeCode());
lambdaQueryWrapper.eq(SysDict::getDelFlag, YesOrNotEnum.N.getCode());
lambdaQueryWrapper.set(SysDict::getStatusFlag, dictTypeRequest.getStatusFlag());
dictService.update(lambdaQueryWrapper);
// 更新字典类型
this.updateById(oldSysDictType);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteDictType(DictTypeRequest dictTypeRequest) {
LambdaUpdateWrapper<SysDictType> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(SysDictType::getDelFlag, YesOrNotEnum.Y.getCode());
updateWrapper.eq(SysDictType::getDictTypeId, dictTypeRequest.getDictTypeId());
this.update(updateWrapper);
// 如果是系统级字典,只允许管理员操作
validateSystemTypeClassOperate(dictTypeRequest);
// 获取字典类型是否存在
SysDictType sysDictType = this.baseMapper.selectById(dictTypeRequest.getDictTypeId());
if (sysDictType == null) {
throw new DictException(DictExceptionEnum.DICT_TYPE_NOT_EXISTED, dictTypeRequest.getDictTypeId());
}
// 字典类型删除
sysDictType.setDelFlag(YesOrNotEnum.Y.getCode());
this.baseMapper.updateById(sysDictType);
// 逻辑删除所有改类型下的字典
LambdaUpdateWrapper<SysDict> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
lambdaQueryWrapper.eq(SysDict::getDictTypeCode, sysDictType.getDictTypeCode());
lambdaQueryWrapper.set(SysDict::getDelFlag, YesOrNotEnum.Y.getCode());
dictService.update(lambdaQueryWrapper);
}
@Override
@ -102,18 +142,17 @@ public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, SysDictType
}
@Override
public PageResult<SysDictType> getDictTypePageList(Page<SysDictType> page, DictTypeRequest dictTypeRequest) {
if (page == null) {
page = PageFactory.defaultPage();
}
public PageResult<SysDictType> getDictTypePageList(DictTypeRequest dictTypeRequest) {
Page<SysDictType> page = PageFactory.defaultPage();
if (dictTypeRequest == null) {
dictTypeRequest = new DictTypeRequest();
}
this.baseMapper.findList(page, dictTypeRequest);
List<SysDictType> list = this.baseMapper.findList(page, dictTypeRequest);
return PageResultFactory.createPageResult(page);
return PageResultFactory.createPageResult(page.setRecords(list));
}
@Override
@ -128,8 +167,21 @@ public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, SysDictType
}
Integer selectCount = this.baseMapper.selectCount(wrapper);
return selectCount <= 0;
}
/**
* dictTypeClass
*
* @author fengshuonan
* @date 2020/12/25 15:57
*/
private void validateSystemTypeClassOperate(DictTypeRequest dictTypeRequest) {
if (DictTypeClassEnum.SYSTEM_TYPE.getCode().equals(dictTypeRequest.getDictTypeClass())) {
if (!LoginContext.me().getSuperAdminFlag()) {
throw new DictException(DictExceptionEnum.SYSTEM_DICT_NOT_ALLOW_OPERATION);
}
}
}
}