OCT 1. 增加删除用户字典功能

This commit is contained in:
黄昕 2024-02-07 11:20:55 +08:00
parent 96a914a380
commit 75a12f4a73
9 changed files with 141 additions and 60 deletions

View File

@ -1,6 +1,5 @@
package com.cmhi.cf.configure;
import com.cmhi.cf.common.CommonStatus;
import com.cmhi.cf.database.common.mapper.DictTypeMapper;
import com.houkunlin.system.dict.starter.bean.DictTypeVo;
import com.houkunlin.system.dict.starter.provider.DictProvider;
@ -27,9 +26,7 @@ public class DictProviderConfigure implements DictProvider {
dictTypeMapper.selectAllWithRelations().forEach(k -> {
DictTypeVo.DictTypeBuilder builder = DictTypeVo.newBuilder(k.getDictType(), k.getDictName(), k.getRemark());
k.getDictDataList()
.stream().filter(m -> m.getStatus().equals(CommonStatus.NORMAL.getValue()))
.forEach(v -> builder.add(v.getDictValue(), v.getDictLabel(), v.getDictSort()));
k.getDictDataList().forEach(v -> builder.add(v.getDictValue(), v.getDictLabel(), v.getDictSort()));
dictItems.add(builder.build());
});

View File

@ -11,6 +11,7 @@ import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.cmhi.cf.restapi.pojo.dto.DictContentReq;
import com.cmhi.cf.restapi.pojo.dto.NewUserDictReq;
import com.cmhi.cf.restapi.pojo.dto.ProtocolReq;
import com.cmhi.cf.restapi.pojo.dto.RemoveDictReq;
import com.cmhi.cf.restapi.pojo.po.DictContent;
import com.cmhi.cf.restapi.pojo.po.PageResults;
import com.cmhi.cf.restapi.pojo.po.UserDictionary;
@ -24,6 +25,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
@ -97,6 +99,26 @@ public class SystemDictController {
}
}
@DeleteMapping("/user/delete")
@ResponseBody
@EncryptionProtocol
@DecryptionProtocol
@OperationLogAnnotation(OperationModule = "字典模块", OperationType = "创建", OperationDesc = "新建用户字典")
public ProtocolResp<? extends BaseRespStatus> removeDictionary(@RequestBody ProtocolReq<RemoveDictReq> mr) {
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.DictReqValid.class);
// 如果校验通过validate为空否则validate包含未校验通过项
if (validate.isEmpty()) {
RemoveDictReq req = mr.getMsgContent();
dictionaryService.removeUserDictionary(req.getDictName());
return ProtocolResp.result(NewUserDictResp.builder()
.dictName(req.getDictName())
.build());
} else {
return ProtocolResp.result(ErrorCode.ERR_PARAMEXCEPTION, ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
validate.toArray(new String[0]));
}
}
@PostMapping("/user/upgrade")
@ResponseBody
@EncryptionProtocol
@ -159,4 +181,24 @@ public class SystemDictController {
validate.toArray(new String[0]));
}
}
// @PutMapping("/user/addDictContent")
// @ResponseBody
// @EncryptionProtocol
// @DecryptionProtocol
// @OperationLogAnnotation(OperationModule = "字典模块", OperationType = "创建", OperationDesc = "新增用户字典内容项")
// public ProtocolResp<? extends BaseRespStatus> createNewDictionaryContent(@RequestBody ProtocolReq<NewUserDictReq> mr) {
// List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.DictReqValid.class);
// // 如果校验通过validate为空否则validate包含未校验通过项
// if (validate.isEmpty()) {
// NewUserDictReq req = mr.getMsgContent();
// dictionaryService.addNewUserDictionary(req.getDictName(), req.getDictAlias(), req.getDictRemark());
// return ProtocolResp.result(NewUserDictResp.builder()
// .dictName(req.getDictName())
// .build());
// } else {
// return ProtocolResp.result(ErrorCode.ERR_PARAMEXCEPTION, ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
// validate.toArray(new String[0]));
// }
// }
}

View File

@ -80,13 +80,6 @@ public class DictData {
@Column(value = "is_default")
private String isDefault;
/**
* 状态0正常 1停用
*/
@Schema(description = "状态 (CommonStatus):0-->正常1-->锁定2-->禁用,3-->删除")
@Column(value = "status")
private Integer status;
/**
* 创建者
*/

View File

@ -43,13 +43,6 @@ public class DictType {
@Column(value = "dict_type")
private String dictType;
/**
* 状态0正常 1停用
*/
@Schema(description = "状态 (CommonStatus):0-->正常1-->锁定2-->禁用,3-->删除")
@Column(value = "status")
private Integer status;
/**
* 创建者
*/

View File

@ -0,0 +1,22 @@
package com.cmhi.cf.restapi.pojo.dto;
import com.cmhi.cf.validation.group.ValidGroups;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(name = "RemoveDictReq", description = "删除用户字典请求参数")
public class RemoveDictReq {
@Schema(description = "字典名,用于标识各个不同的字典")
@NotNull(message = "dictName 字典名不能为NULL", groups = ValidGroups.DictReqValid.class)
private String dictName;
}

View File

@ -19,4 +19,6 @@ public interface DictionaryService {
PageResults<UserDictionary> getUserDictionary(Long pageNumber, Long pageSize, Long totalSize);
List<DictContent> getUserDictionaryContent(List<String> dictName);
void removeUserDictionary(String dictName);
}

View File

@ -1,8 +1,9 @@
package com.cmhi.cf.service.impl;
import com.cmhi.cf.common.CommonStatus;
import com.cmhi.cf.common.ErrorCode;
import com.cmhi.cf.database.common.entity.DictData;
import com.cmhi.cf.database.common.entity.DictType;
import com.cmhi.cf.database.common.mapper.DictDataMapper;
import com.cmhi.cf.database.common.mapper.DictTypeMapper;
import com.cmhi.cf.exception.CommonErrorCodeException;
import com.cmhi.cf.restapi.misc.ApiContextUtils;
@ -15,6 +16,7 @@ import com.houkunlin.system.dict.starter.DictUtil;
import com.houkunlin.system.dict.starter.SystemDictStarter;
import com.houkunlin.system.dict.starter.bean.DictTypeVo;
import com.houkunlin.system.dict.starter.notice.RefreshDictTypeEvent;
import com.houkunlin.system.dict.starter.notice.RefreshDictValueEvent;
import com.houkunlin.system.dict.starter.provider.SystemDictProvider;
import com.houkunlin.system.dict.starter.store.DictStore;
import com.mybatisflex.core.paginate.Page;
@ -37,6 +39,9 @@ public class DictionaryServiceImpl implements DictionaryService {
@Resource
private DictTypeMapper dictTypeMapper;
@Resource
private DictDataMapper dictDataMapper;
@Resource
private ApplicationEventPublisher publisher;
@ -54,10 +59,10 @@ public class DictionaryServiceImpl implements DictionaryService {
if (dictType.contains(k.getType())) {
List<DictDetails> children = new ArrayList<>();
k.getChildren().forEach(m -> children.add(DictDetails.builder()
.title(m.getTitle())
.value((Integer) m.getValue())
.disabled(false)
.build()));
.title(m.getTitle())
.value((Integer) m.getValue())
.disabled(m.isDisabled())
.build()));
sysDictList.add(DictContent.builder()
.dictName(k.getType())
.children(children)
@ -82,7 +87,6 @@ public class DictionaryServiceImpl implements DictionaryService {
dt.setRemark(remark);
dt.setDictName(name);
dt.setDictType(type);
dt.setStatus(CommonStatus.NORMAL.getValue());
dt.setCreateBy(ApiContextUtils.get().getUsername());
try {
@ -173,12 +177,12 @@ public class DictionaryServiceImpl implements DictionaryService {
List<DictDetails> children = new ArrayList<>();
k.getDictDataList().forEach(m -> children.add(DictDetails.builder()
.id(m.getId())
.title(m.getDictLabel())
.value(m.getDictValue())
.sorted(m.getDictSort())
.disabled(m.getStatus() != 0)
.build()));
.id(m.getId())
.title(m.getDictLabel())
.value(m.getDictValue())
.sorted(m.getDictSort())
.disabled(false)
.build()));
resp.add(DictContent.builder()
.dictId(k.getId())
@ -202,4 +206,32 @@ public class DictionaryServiceImpl implements DictionaryService {
return resp;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void removeUserDictionary(String dictName) {
DictType dt = dictTypeMapper.selectOneWithRelationsByCondition(DICT_TYPE.DICT_TYPE_.eq(dictName));
if (dt == null) {
throw new CommonErrorCodeException(ErrorCode.ERR_NOSUCHITEM,
dictName + ": " + ErrorCode.ERR_NOSUCHITEM.getDescription());
}
List<Long> contentId = dt.getDictDataList().stream().map(DictData::getId).toList();
// 首先删除字典内容
if (dictDataMapper.deleteBatchByIds(contentId) != contentId.size()) {
throw new CommonErrorCodeException(ErrorCode.ERR_DATABASE);
}
// 删除用户字典
if (dictTypeMapper.deleteById(dt.getId()) != 1) {
throw new CommonErrorCodeException(ErrorCode.ERR_DATABASE, dictName + ": " + ErrorCode.ERR_DATABASE.getDescription());
}
// 更新字典缓存
DictTypeVo dv = DictUtil.getDictType(dictName);
dv.getChildren().forEach(v -> v.setDisabled(true));
publisher.publishEvent(new RefreshDictValueEvent(dv.getChildren(), true));
}
}

View File

@ -42,8 +42,9 @@ INSERT IGNORE INTO rbac_resource_data VALUES (400, '8ac928d8-5a6a-47c7-8697-0fb9
INSERT IGNORE INTO rbac_resource_data VALUES (401, '9ecf1662-f891-4829-8d3e-81c42bf22e2a', '/api/dict/enum/dictContent', '获取枚举字典详细内容', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (402, 'C083F441-9624-AF2C-B353-0768F9C0A932', '/api/dict/user/add', '新增用户字典', 'PUT', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (403, '953cea37-cc7a-4e7e-bd86-cb3c14daa8c4', '/api/dict/user/upgrade', '修改用户字典', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (404, '54b43614-45c6-4672-a1d8-fa8f326d5e53', '/api/dict/user/allDict', '分页获取所有用户字典', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (405, 'e441dbb4-b844-461c-a5f8-07ee3fa658cc', '/api/dict/user/dictContent', '获取用户字典详细信息', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (404, '247674fb-759d-4b01-8d35-2c0436d2ed99', '/api/dict/user/delete', '删除用户字典', 'DELETE', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (405, '54b43614-45c6-4672-a1d8-fa8f326d5e53', '/api/dict/user/allDict', '分页获取所有用户字典', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (406, 'e441dbb4-b844-461c-a5f8-07ee3fa658cc', '/api/dict/user/dictContent', '获取用户字典详细信息', 'POST', 0);
-- ----------------------------
-- Records of role_resource
-- (role_id, resource_id, authorize)
@ -68,6 +69,7 @@ INSERT IGNORE INTO rbac_role_resource VALUES (1, 402, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 403, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 404, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 405, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 406, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 1, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 2, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 3, 1);
@ -179,14 +181,14 @@ INSERT IGNORE INTO sys_dict_type (id, dict_name, dict_type, create_by, remark) V
INSERT IGNORE INTO sys_dict_type (id, dict_name, dict_type, create_by, remark) VALUES (2, '用户组', 'sys_role_group', 'admin', '用户组列表');
INSERT IGNORE INTO sys_dict_data VALUES (1, 1, '路由', 0, 1, '', '','Y', 0, 'admin', CURRENT_TIMESTAMP(6), '', null, 'URI 路由资源', null);
INSERT IGNORE INTO sys_dict_data VALUES (2, 2, '菜单', 1, 1, '', '','N', 0, 'admin', CURRENT_TIMESTAMP(6), '', null, 'UI 菜单资源', null);
INSERT IGNORE INTO sys_dict_data VALUES (3, 3, '按钮', 2, 1, '', '','N', 0, 'admin', CURRENT_TIMESTAMP(6), '', null, 'UI 按钮资源', null);
INSERT IGNORE INTO sys_dict_data VALUES (1, 1, '路由', 0, 1, '', '','Y', 'admin', CURRENT_TIMESTAMP(6), '', null, 'URI 路由资源', null);
INSERT IGNORE INTO sys_dict_data VALUES (2, 2, '菜单', 1, 1, '', '','N', 'admin', CURRENT_TIMESTAMP(6), '', null, 'UI 菜单资源', null);
INSERT IGNORE INTO sys_dict_data VALUES (3, 3, '按钮', 2, 1, '', '','N', 'admin', CURRENT_TIMESTAMP(6), '', null, 'UI 按钮资源', null);
INSERT IGNORE INTO sys_dict_data VALUES (4, 1, 'ADMIN', 1, 2, '', '','N', 0, 'admin', CURRENT_TIMESTAMP(6), '', null, '超级管理员可以对企业内的所有用户进行管理,请谨慎修改超管权限', null);
INSERT IGNORE INTO sys_dict_data VALUES (5, 2, 'DEVELOPMENT', 2, 2, '', '','N', 0, 'admin', CURRENT_TIMESTAMP(6), '', null, '项目开发人员', null);
INSERT IGNORE INTO sys_dict_data VALUES (6, 3, 'USER', 3, 2, '', '','N', 0, 'admin', CURRENT_TIMESTAMP(6), '', null, '普通的用户', null);
INSERT IGNORE INTO sys_dict_data VALUES (7, 4, 'GUEST', 4, 2, '', '','N', 0, 'admin', CURRENT_TIMESTAMP(6), '', null, '系统访客,不需要认证,最小权限', null);
INSERT IGNORE INTO sys_dict_data VALUES (4, 1, 'ADMIN', 1, 2, '', '','N', 'admin', CURRENT_TIMESTAMP(6), '', null, '超级管理员可以对企业内的所有用户进行管理,请谨慎修改超管权限', null);
INSERT IGNORE INTO sys_dict_data VALUES (5, 2, 'DEVELOPMENT', 2, 2, '', '','N', 'admin', CURRENT_TIMESTAMP(6), '', null, '项目开发人员', null);
INSERT IGNORE INTO sys_dict_data VALUES (6, 3, 'USER', 3, 2, '', '','N', 'admin', CURRENT_TIMESTAMP(6), '', null, '普通的用户', null);
INSERT IGNORE INTO sys_dict_data VALUES (7, 4, 'GUEST', 4, 2, '', '','N', 'admin', CURRENT_TIMESTAMP(6), '', null, '系统访客,不需要认证,最小权限', null);
# INSERT IGNORE INTO sys_dict_data VALUES (4, 1, '正常', '0', 'sys_common_status', '', '','Y', '0', 'admin', CURRENT_TIMESTAMP(6), '', null, 'UI 按钮资源', null);
# INSERT IGNORE INTO sys_dict_data VALUES (5, 2, '已锁定', '1', 'sys_common_status', '', '','N', '0', 'admin', CURRENT_TIMESTAMP(6), '', null, 'UI 按钮资源', null);

View File

@ -177,20 +177,19 @@ CREATE TABLE IF NOT EXISTS `sys_operation_log`
CREATE TABLE IF NOT EXISTS `sys_dict_data`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '字典编码',
`dict_sort` int(4) NULL DEFAULT 0 COMMENT '字典排序',
`dict_label` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '字典标签',
`dict_sort` int(4) NULL DEFAULT 0 COMMENT '字典排序',
`dict_label` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '字典标签',
`dict_value` smallint NOT NULL COMMENT '字典键值',
`type_id` bigint(20) NOT NULL COMMENT '字典类型',
`css_class` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '样式属性(其他样式扩展)',
`list_class` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '表格回显样式',
`is_default` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT 'N' COMMENT '是否默认Y是 N否',
`status` smallint NOT NULL DEFAULT 0 COMMENT '当前状态(CommonStatus):0-->正常1-->锁定2-->禁用,3-->删除',
`create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime(6) NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
`update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`delete_time` datetime NULL DEFAULT NULL COMMENT '逻辑删除标记',
`css_class` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '样式属性(其他样式扩展)',
`list_class` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '表格回显样式',
`is_default` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT 'N' COMMENT '是否默认Y是 N否',
`create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime(6) NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
`update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`delete_time` datetime NULL DEFAULT NULL COMMENT '逻辑删除标记',
CONSTRAINT `FKa68196081fvovjhkekdicttype` FOREIGN KEY (`type_id`) REFERENCES `sys_dict_type` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB
@ -202,15 +201,14 @@ CREATE TABLE IF NOT EXISTS `sys_dict_data`
CREATE TABLE IF NOT EXISTS `sys_dict_type`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '字典主键',
`dict_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '字典名称',
`dict_type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '字典类型',
`status` smallint NOT NULL DEFAULT 0 COMMENT '当前状态(CommonStatus):0-->正常1-->锁定2-->禁用,3-->删除',
`create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime(6) NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
`update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime(6) NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`delete_time` datetime NULL DEFAULT NULL COMMENT '逻辑删除标记',
`dict_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '字典名称',
`dict_type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '字典类型',
`create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime(6) NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
`update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime(6) NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`delete_time` datetime NULL DEFAULT NULL COMMENT '逻辑删除标记',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `dict_type` (`dict_type`) USING BTREE
) ENGINE = InnoDB