【新增】新增业务字典功能,只支持修改显示名称

pull/150/head
xiaonuobase 2023-08-11 19:10:05 +08:00
parent 42f3979bc5
commit b584c17d58
14 changed files with 928 additions and 7 deletions

View File

@ -0,0 +1,37 @@
/**
* Copyright [2022] [https://www.xiaonuo.vip]
* Snowy采用APACHE LICENSE 2.0开源协议您在使用过程中需要注意以下几点
* 1.请不要删除和修改根目录下的LICENSE文件
* 2.请不要删除和修改Snowy源码头部的版权声明
* 3.本项目代码可免费商业使用商业使用请保留源码和相关描述文件的项目出处作者声明等
* 4.分发源码时候请注明软件出处 https://www.xiaonuo.vip
* 5.不可二次分发开源参与同类竞品如有想法可联系团队xiaonuobase@qq.com商议合作
* 6.若您的项目无法满足以上几点需要更多功能代码获取Snowy商业授权许可请在官网购买授权地址为 https://www.xiaonuo.vip
*/
import { baseRequest } from '@/utils/request'
const request = (url, ...arg) => baseRequest(`/biz/dict/${url}`, ...arg)
/**
* 字典
*
* @author yubaoshan
* @date 2022-09-22 22:33:20
*/
export default {
// 获取业务字典分页
dictPage(data) {
return request('page', data, 'get')
},
// 获取业务字典树
dictTree(data) {
return request('tree', data, 'get')
},
// 获取所有字典树
dictTreeAll(data) {
return request('treeAll', data, 'get')
},
// 编辑业务字典
submitForm(data) {
return request('edit', data)
}
}

View File

@ -0,0 +1,108 @@
<template>
<xn-form-container title="编辑字典" :width="550" :visible="visible" :destroy-on-close="true" @close="onClose">
<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical" :label-col="labelCol">
<a-form-item label="上级字典:" name="parentId">
<a-tree-select
:disabled="true"
v-model:value="formData.parentId"
v-model:treeExpandedKeys="defaultExpandedKeys"
style="width: 100%"
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
placeholder="请选择上级字典"
allow-clear
:tree-data="treeData"
:field-names="{
children: 'children',
label: 'name',
value: 'id'
}"
selectable="false"
treeLine
/>
</a-form-item>
<a-form-item label="字典名称:" name="dictLabel">
<a-input v-model:value="formData.dictLabel" placeholder="请输入字典名称" allow-clear />
</a-form-item>
<a-form-item label="字典值:" name="dictValue">
<a-input v-model:value="formData.dictValue" placeholder="请输入字典值" allow-clear :disabled="true" />
</a-form-item>
<a-form-item label="排序:" name="sortCode">
<a-input-number style="width: 100%" v-model:value="formData.sortCode" :max="1000" />
</a-form-item>
</a-form>
<template #footer>
<a-button style="margin-right: 8px" @click="onClose"></a-button>
<a-button type="primary" @click="onSubmit"></a-button>
</template>
</xn-form-container>
</template>
<script setup name="dictForm">
import { required } from '@/utils/formRules'
import bizDictApi from '@/api/biz/bizDictApi'
// emit
const emit = defineEmits({ successful: null })
//
const visible = ref(false)
const formRef = ref()
//
const formData = ref({})
//
const treeData = ref([])
// ()
const defaultExpandedKeys = ref([0])
//
const onOpen = (record, parentId) => {
visible.value = true
formData.value = {
sortCode: 99
}
if (parentId) {
formData.value.parentId = parentId
}
if (record) {
formData.value = Object.assign({}, record)
}
bizDictApi.dictTree().then((res) => {
treeData.value = [
{
id: 0,
parentId: '-1',
name: '顶级',
children: res
}
]
})
}
//
const onClose = () => {
visible.value = false
}
//
const formRules = {
dictLabel: [required('请输入字典名称')],
dictValue: [required('请选择字典值')],
sortCode: [required('请选择排序')]
}
// label
const labelCol = ref({
style: {
width: '100px'
}
})
//
const onSubmit = () => {
formRef.value.validate().then(() => {
bizDictApi.submitForm(formData.value).then(() => {
visible.value = false
emit('successful')
})
})
}
//
defineExpose({
onOpen
})
</script>

View File

@ -0,0 +1,196 @@
<template>
<a-row>
<a-col :span="5">
<a-card class="cardImp" :bordered="false" :loading="cardLoading">
<a-tree
v-if="treeData.length > 0"
v-model:expandedKeys="defaultExpandedKeys"
:tree-data="treeData"
:field-names="treeFieldNames"
@select="treeSelect"
/>
<a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE" />
</a-card>
</a-col>
<a-col :span="19">
<a-card :bordered="false" style="margin-bottom: 10px">
<a-form
ref="searchFormRef"
name="advanced_search"
class="ant-advanced-search-form mb-3"
:model="searchFormState"
>
<a-row :gutter="24">
<a-col :span="8">
<a-form-item name="searchKey" label="字典名称">
<a-input v-model:value="searchFormState.searchKey" placeholder="请输入字典名称" />
</a-form-item>
</a-col>
<a-col :span="8">
<a-button type="primary" @click="table.refresh(true)">
<template #icon><SearchOutlined /></template>
查询
</a-button>
<a-button class="snowy-buttom-left" @click="reset">
<template #icon><redo-outlined /></template>
重置
</a-button>
</a-col>
</a-row>
</a-form>
</a-card>
<a-card :bordered="false" style="margin-bottom: 10px">
<s-table
ref="table"
:columns="columns"
:data="loadData"
:expand-row-by-click="true"
bordered
:tool-config="toolConfig"
:row-key="(record) => record.id"
>
<template #bodyCell="{ column, record }">
<template v-if="column.dataIndex === 'level'">
<a-tag color="blue" v-if="record.level">{{ record.level }}</a-tag>
<a-tag color="green" v-else></a-tag>
</template>
<template v-if="column.dataIndex === 'action'">
<a @click="formRef.onOpen(record)" v-if="hasPerm('bizDictEdit')"></a>
</template>
</template>
</s-table>
</a-card>
</a-col>
</a-row>
<Form ref="formRef" @successful="formSuccessful()" />
</template>
<script setup>
import { Empty } from 'ant-design-vue'
import bizDictApi from '@/api/biz/bizDictApi'
import Form from './form.vue'
import tool from '@/utils/tool'
const columns = [
{
title: '字典名称',
dataIndex: 'dictLabel',
width: 350
},
{
title: '字典值',
dataIndex: 'dictValue',
width: 350
},
{
title: '排序',
dataIndex: 'sortCode'
}
]
if (hasPerm('bizDictEdit')) {
columns.push({
title: '操作',
dataIndex: 'action',
align: 'center',
width: '150px'
})
}
// tableDOM
const table = ref(null)
const formRef = ref()
const cardLoading = ref(true)
const searchFormRef = ref()
const searchFormState = ref({})
//
const defaultExpandedKeys = ref([])
const treeData = ref([])
// treeNode title,key,children
const treeFieldNames = { children: 'children', title: 'dictLabel', key: 'id' }
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
// Promise
const loadData = (parameter) => {
loadTreeData()
return bizDictApi.dictPage(Object.assign(parameter, searchFormState.value)).then((data) => {
if (data.records) {
if (searchFormState.value.parentId) {
let dataArray = []
data.records.forEach((item) => {
const obj = data.records.find((f) => f.id === item.parentId)
if (!obj) {
dataArray.push(item)
}
})
if (dataArray.length === 1) {
data.records.forEach((item) => {
if (item.id === dataArray[0].id) {
item.level = '上级'
}
})
}
dataArray = []
}
}
return data
})
}
//
const reset = () => {
searchFormRef.value.resetFields()
table.value.refresh(true)
}
//
const loadTreeData = () => {
bizDictApi
.dictTree()
.then((res) => {
if (res) {
treeData.value = res
}
})
.finally(() => {
cardLoading.value = false
})
}
//
const treeSelect = (selectedKeys) => {
if (selectedKeys && selectedKeys.length > 0) {
searchFormState.value.parentId = selectedKeys.toString()
if (!columns.find((f) => f.title === '层级')) {
columns.splice(2, 0, {
title: '层级',
dataIndex: 'level',
width: 100
})
}
} else {
delete searchFormState.value.parentId
columns.splice(2, 1)
}
table.value.refresh(true)
}
//
const formSuccessful = () => {
table.value.refresh()
refreshStoreDict()
}
// store
const refreshStoreDict = () => {
nextTick(() => {
bizDictApi.dictTreeAll().then((res) => {
tool.data.set('DICT_TYPE_TREE_DATA', res)
})
})
}
</script>
<style lang="less" scoped>
.cardImp {
margin-right: 10px;
}
.ant-form-item {
margin-bottom: 0 !important;
}
.snowy-buttom-left {
margin-left: 8px;
}
</style>

View File

@ -146,7 +146,7 @@
:org-tree-api="selectorApiFunction.orgTreeApi"
:role-page-api="selectorApiFunction.rolePageApi"
:checked-role-list-api="selectorApiFunction.checkedRoleListApi"
:role-global="false"
:role-global="true"
@onBack="roleBack"
/>
</template>

View File

@ -78,10 +78,10 @@
<a @click="grantResourceFormRef.onOpen(record)"></a>
</a-menu-item>
<a-menu-item>
<a @click="GrantMobileResourceForm.onOpen(record)"></a>
<a @click="grantMobileResourceFormRef.onOpen(record)"></a>
</a-menu-item>
<a-menu-item>
<a @click="GrantPermissionForm.onOpen(record)"></a>
<a @click="grantPermissionFormRef.onOpen(record)"></a>
</a-menu-item>
<a-menu-item>
<a @click="openRoleUserSelector(record)"></a>
@ -96,8 +96,8 @@
</a-col>
</a-row>
<grantResourceForm ref="grantResourceFormRef" @successful="table.refresh()" />
<grantMobileResourceForm ref="GrantMobileResourceForm" @successful="table.refresh()" />
<grantPermissionForm ref="GrantPermissionForm" @successful="table.refresh()" />
<grantMobileResourceForm ref="grantMobileResourceFormRef" @successful="table.refresh()" />
<grantPermissionForm ref="grantPermissionFormRef" @successful="table.refresh()" />
<Form ref="formRef" @successful="table.refresh()" />
<user-selector-plus
ref="userselectorPlusRef"
@ -162,8 +162,8 @@
const table = ref()
const formRef = ref()
const grantResourceFormRef = ref()
const GrantMobileResourceForm = ref()
const GrantPermissionForm = ref()
const grantMobileResourceFormRef = ref()
const grantPermissionFormRef = ref()
const userselectorPlusRef = ref()
const searchFormRef = ref()
const searchFormState = ref({})

View File

@ -0,0 +1,109 @@
/*
* Copyright [2022] [https://www.xiaonuo.vip]
*
* SnowyAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Snowy
* 3.使使
* 4. https://www.xiaonuo.vip
* 5.xiaonuobase@qq.com
* 6.Snowy https://www.xiaonuo.vip
*/
package vip.xiaonuo.biz.modular.dict.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.biz.modular.dict.entity.BizDict;
import vip.xiaonuo.biz.modular.dict.param.BizDictEditParam;
import vip.xiaonuo.biz.modular.dict.param.BizDictPageParam;
import vip.xiaonuo.biz.modular.dict.service.BizDictService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
/**
*
*
* @author xuyuxiang
* @date 2022/6/21 14:58
**/
@Api(tags = "业务字典控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 4)
@RestController
@Validated
public class BizDictController {
@Resource
private BizDictService bizDictService;
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 20:00
*/
@ApiOperationSupport(order = 1)
@ApiOperation("获取业务字典分页")
@SaCheckPermission("/biz/dict/page")
@GetMapping("/biz/dict/page")
public CommonResult<Page<BizDict>> page(BizDictPageParam bizDictPageParam) {
return CommonResult.data(bizDictService.page(bizDictPageParam));
}
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 20:00
*/
@ApiOperationSupport(order = 2)
@ApiOperation("获取业务字典树")
@SaCheckPermission("/biz/dict/tree")
@GetMapping("/biz/dict/tree")
public CommonResult<List<Tree<String>>> tree() {
return CommonResult.data(bizDictService.tree());
}
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 20:00
*/
@ApiOperationSupport(order = 3)
@ApiOperation("获取所有字典树")
@GetMapping("/biz/dict/treeAll")
public CommonResult<List<Tree<String>>> treeAll() {
return CommonResult.data(bizDictService.treeAll());
}
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 20:47
*/
@ApiOperationSupport(order = 4)
@ApiOperation("编辑业务字典")
@CommonLog("编辑业务字典")
@SaCheckPermission("/biz/dict/edit")
@PostMapping("/biz/dict/edit")
public CommonResult<String> edit(@RequestBody @Valid BizDictEditParam bizDictEditParam) {
bizDictService.edit(bizDictEditParam);
return CommonResult.ok();
}
}

View File

@ -0,0 +1,66 @@
/*
* Copyright [2022] [https://www.xiaonuo.vip]
*
* SnowyAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Snowy
* 3.使使
* 4. https://www.xiaonuo.vip
* 5.xiaonuobase@qq.com
* 6.Snowy https://www.xiaonuo.vip
*/
package vip.xiaonuo.biz.modular.dict.entity;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import vip.xiaonuo.common.pojo.CommonEntity;
/**
*
*
* @author xuyuxiang
* @date 2022/2/23 18:27
**/
@Getter
@Setter
@TableName("DEV_DICT")
public class BizDict extends CommonEntity {
/** id */
@ApiModelProperty(value = "id", position = 1)
private String id;
/** 租户id */
@ApiModelProperty(value = "租户id", position = 2)
private String tenantId;
/** 父id */
@ApiModelProperty(value = "父id", position = 3)
private String parentId;
/** 字典文字 */
@ApiModelProperty(value = "字典文字", position = 4)
private String dictLabel;
/** 字典值 */
@ApiModelProperty(value = "字典值", position = 5)
private String dictValue;
/** 分类 */
@ApiModelProperty(value = "分类", position = 6)
private String category;
/** 排序码 */
@ApiModelProperty(value = "排序码", position = 7)
private Integer sortCode;
/** 扩展信息 */
@ApiModelProperty(value = "扩展信息", position = 8)
@TableField(insertStrategy = FieldStrategy.IGNORED, updateStrategy = FieldStrategy.IGNORED)
private String extJson;
}

View File

@ -0,0 +1,44 @@
/*
* Copyright [2022] [https://www.xiaonuo.vip]
*
* SnowyAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Snowy
* 3.使使
* 4. https://www.xiaonuo.vip
* 5.xiaonuobase@qq.com
* 6.Snowy https://www.xiaonuo.vip
*/
package vip.xiaonuo.biz.modular.dict.enums;
import lombok.Getter;
import vip.xiaonuo.common.exception.CommonException;
/**
*
*
* @author xuyuxiang
* @date 2022/7/6 22:21
*/
@Getter
public enum BizDictCategoryEnum {
/**
*
*/
BIZ("BIZ");
private final String value;
BizDictCategoryEnum(String value) {
this.value = value;
}
public static void validate(String value) {
boolean flag = BIZ.getValue().equals(value);
if(!flag) {
throw new CommonException("不支持的字典分类:{}", value);
}
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright [2022] [https://www.xiaonuo.vip]
*
* SnowyAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Snowy
* 3.使使
* 4. https://www.xiaonuo.vip
* 5.xiaonuobase@qq.com
* 6.Snowy https://www.xiaonuo.vip
*/
package vip.xiaonuo.biz.modular.dict.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import vip.xiaonuo.biz.modular.dict.entity.BizDict;
/**
* Mapper
*
* @author xuyuxiang
* @date 2022/4/22 10:40
**/
public interface BizDictMapper extends BaseMapper<BizDict> {
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="vip.xiaonuo.biz.modular.dict.mapper.BizDictMapper">
</mapper>

View File

@ -0,0 +1,50 @@
/*
* Copyright [2022] [https://www.xiaonuo.vip]
*
* SnowyAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Snowy
* 3.使使
* 4. https://www.xiaonuo.vip
* 5.xiaonuobase@qq.com
* 6.Snowy https://www.xiaonuo.vip
*/
package vip.xiaonuo.biz.modular.dict.param;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
*
*
* @author xuyuxiang
* @date 2022/7/30 21:48
*/
@Getter
@Setter
public class BizDictEditParam {
/** id */
@ApiModelProperty(value = "id", position = 1)
@NotBlank(message = "id不能为空")
private String id;
/** 字典文字 */
@ApiModelProperty(value = "字典文字", position = 2)
@NotBlank(message = "dictLabel不能为空")
private String dictLabel;
/** 排序码 */
@ApiModelProperty(value = "排序码", position = 3)
@NotNull(message = "sortCode不能为空")
private Integer sortCode;
/** 扩展信息 */
@ApiModelProperty(value = "扩展信息", position = 4)
private String extJson;
}

View File

@ -0,0 +1,52 @@
/*
* Copyright [2022] [https://www.xiaonuo.vip]
*
* SnowyAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Snowy
* 3.使使
* 4. https://www.xiaonuo.vip
* 5.xiaonuobase@qq.com
* 6.Snowy https://www.xiaonuo.vip
*/
package vip.xiaonuo.biz.modular.dict.param;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
/**
*
*
* @author xuyuxiang
* @date 2022/7/30 21:49
*/
@Getter
@Setter
public class BizDictPageParam {
/** 当前页 */
@ApiModelProperty(value = "当前页码")
private Integer current;
/** 每页条数 */
@ApiModelProperty(value = "每页条数")
private Integer size;
/** 排序字段 */
@ApiModelProperty(value = "排序字段字段驼峰名称userName")
private String sortField;
/** 排序方式 */
@ApiModelProperty(value = "排序方式升序ASCEND降序DESCEND")
private String sortOrder;
/** 父id */
@ApiModelProperty(value = "父id")
private String parentId;
/** 字典文字关键词 */
@ApiModelProperty(value = "字典文字关键词")
private String searchKey;
}

View File

@ -0,0 +1,71 @@
/*
* Copyright [2022] [https://www.xiaonuo.vip]
*
* SnowyAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Snowy
* 3.使使
* 4. https://www.xiaonuo.vip
* 5.xiaonuobase@qq.com
* 6.Snowy https://www.xiaonuo.vip
*/
package vip.xiaonuo.biz.modular.dict.service;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import vip.xiaonuo.biz.modular.dict.entity.BizDict;
import vip.xiaonuo.biz.modular.dict.param.BizDictEditParam;
import vip.xiaonuo.biz.modular.dict.param.BizDictPageParam;
import java.util.List;
/**
* Service
*
* @author xuyuxiang
* @date 2022/4/22 10:41
**/
public interface BizDictService extends IService<BizDict> {
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 20:08
*/
Page<BizDict> page(BizDictPageParam bizDictPageParam);
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 20:08
*/
List<Tree<String>> tree();
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 20:08
*/
List<Tree<String>> treeAll();
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 21:13
*/
void edit(BizDictEditParam bizDictEditParam);
/**
*
*
* @author xuyuxiang
* @date 2022/4/24 21:18
*/
BizDict queryEntity(String id);
}

View File

@ -0,0 +1,157 @@
/*
* Copyright [2022] [https://www.xiaonuo.vip]
*
* SnowyAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Snowy
* 3.使使
* 4. https://www.xiaonuo.vip
* 5.xiaonuobase@qq.com
* 6.Snowy https://www.xiaonuo.vip
*/
package vip.xiaonuo.biz.modular.dict.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhs.trans.service.impl.DictionaryTransService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.dict.entity.BizDict;
import vip.xiaonuo.biz.modular.dict.enums.BizDictCategoryEnum;
import vip.xiaonuo.biz.modular.dict.mapper.BizDictMapper;
import vip.xiaonuo.biz.modular.dict.param.BizDictEditParam;
import vip.xiaonuo.biz.modular.dict.param.BizDictPageParam;
import vip.xiaonuo.biz.modular.dict.service.BizDictService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
* Service
*
* @author xuyuxiang
* @date 2022/4/22 10:41
**/
@Service
public class BizDictServiceImpl extends ServiceImpl<BizDictMapper, BizDict> implements BizDictService, InitializingBean {
private static final String ROOT_PARENT_ID = "0";
@Resource
private DictionaryTransService dictionaryTransService;
@Override
public Page<BizDict> page(BizDictPageParam bizDictPageParam) {
QueryWrapper<BizDict> queryWrapper = new QueryWrapper<>();
// 查询部分字段
queryWrapper.lambda().select(BizDict::getId, BizDict::getParentId, BizDict::getCategory, BizDict::getDictLabel,
BizDict::getDictValue, BizDict::getSortCode).eq(BizDict::getCategory, BizDictCategoryEnum.BIZ.getValue());
if (ObjectUtil.isNotEmpty(bizDictPageParam.getParentId())) {
queryWrapper.lambda().eq(BizDict::getParentId, bizDictPageParam.getParentId())
.or().eq(BizDict::getId, bizDictPageParam.getParentId());
}
if (ObjectUtil.isNotEmpty(bizDictPageParam.getSearchKey())) {
queryWrapper.lambda().like(BizDict::getDictLabel, bizDictPageParam.getSearchKey());
}
if (ObjectUtil.isAllNotEmpty(bizDictPageParam.getSortField(), bizDictPageParam.getSortOrder())) {
CommonSortOrderEnum.validate(bizDictPageParam.getSortOrder());
queryWrapper.orderBy(true, bizDictPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
StrUtil.toUnderlineCase(bizDictPageParam.getSortField()));
} else {
queryWrapper.lambda().orderByAsc(BizDict::getSortCode);
}
return this.page(CommonPageRequest.defaultPage(), queryWrapper);
}
@Override
public List<Tree<String>> tree() {
LambdaQueryWrapper<BizDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(BizDict::getCategory, BizDictCategoryEnum.BIZ.getValue()).orderByAsc(BizDict::getSortCode);
List<BizDict> bizDictList = this.list(lambdaQueryWrapper);
List<TreeNode<String>> treeNodeList = bizDictList.stream().map(bizDict ->
new TreeNode<>(bizDict.getId(), bizDict.getParentId(),
bizDict.getDictLabel(), bizDict.getSortCode()).setExtra(JSONUtil.parseObj(bizDict)))
.collect(Collectors.toList());
return TreeUtil.build(treeNodeList, "0");
}
@Override
public List<Tree<String>> treeAll() {
List<BizDict> bizDictList = this.list();
List<TreeNode<String>> treeNodeList = bizDictList.stream().map(bizDict ->
new TreeNode<>(bizDict.getId(), bizDict.getParentId(),
bizDict.getDictLabel(), bizDict.getSortCode()).setExtra(JSONUtil.parseObj(bizDict)))
.collect(Collectors.toList());
return TreeUtil.build(treeNodeList, "0");
}
@Override
public void edit(BizDictEditParam bizDictEditParam) {
BizDict bizDict = this.queryEntity(bizDictEditParam.getId());
checkParam(bizDictEditParam);
BeanUtil.copyProperties(bizDictEditParam, bizDict);
this.updateById(bizDict);
refreshTransCache();
}
private void checkParam(BizDictEditParam bizDictEditParam) {
boolean hasSameDictLabel = this.count(new LambdaQueryWrapper<BizDict>()
.eq(BizDict::getCategory, BizDictCategoryEnum.BIZ.getValue())
.eq(BizDict::getDictLabel, bizDictEditParam.getDictLabel())
.ne(BizDict::getId, bizDictEditParam.getId())) > 0;
if (hasSameDictLabel) {
throw new CommonException("存在重复的字典文字,名称为:{}", bizDictEditParam.getDictLabel());
}
}
@Override
public BizDict queryEntity(String id) {
BizDict bizDict = this.getById(id);
if (ObjectUtil.isEmpty(bizDict)) {
throw new CommonException("字典不存在id值为{}", id);
}
return bizDict;
}
@Override
public void afterPropertiesSet() {
refreshTransCache();
}
private void refreshTransCache() {
// 异步不阻塞主线程,不会 增加启动用时
CompletableFuture.supplyAsync(() -> {
// 使用redis能解决共享问题但是性能没有直接取缓存的好。
dictionaryTransService.makeUseRedis();
List<BizDict> bizDictList = super.list(new LambdaQueryWrapper<>());
// 非root级别的字典根据ParentId分组
Map<String,List<BizDict>> bizDictGroupByPIDMap = bizDictList.stream().filter(dict -> !ROOT_PARENT_ID
.equals(dict.getParentId())).collect(Collectors.groupingBy(BizDict::getParentId));
Map<String,String> parentDictIdValMap = bizDictList.stream().filter(dict -> ROOT_PARENT_ID
.equals(dict.getParentId())).collect(Collectors.toMap(BizDict::getId, BizDict::getDictValue));
for (String parentId : parentDictIdValMap.keySet()) {
if(bizDictGroupByPIDMap.containsKey(parentId)){
dictionaryTransService.refreshCache(parentDictIdValMap.get(parentId), bizDictGroupByPIDMap.get(parentId).stream()
.collect(Collectors.toMap(BizDict::getDictValue, BizDict::getDictLabel)));
}
}
return null;
});
}
}