shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.iplatform.base.service;
 
import com.iplatform.model.po.S_dept;
import com.iplatform.model.po.S_dict_data;
import com.iplatform.model.po.S_dict_type;
import com.walker.db.page.GenericPager;
import com.walker.db.page.ListPageContext;
import com.walker.db.page.PageSearch;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 数据字典表操作。
 * @date 2022-09-19
 */
@Service
public class CodeServiceImpl extends BaseServiceImpl {
 
    private static final String SQL_PAGE_TYPE_PREFIX = "select * from s_dict_type where 1=1";
    private static final String SQL_PAGE_DATA_PREFIX = "select * from s_dict_data where 1=1";
 
    private static final String SQL_QUERY_EXIST_DATA = "select * from s_dict_data where dict_type=? and dict_value=?";
 
    public void execDeleteDictData(Long[] dictCodes){
        if(dictCodes == null || dictCodes.length == 0){
            return;
        }
        List<Object[]> parameters = new ArrayList<>(8);
        for(long dictCode : dictCodes){
            Object[] p = new Object[]{dictCode};
            parameters.add(p);
        }
        this.execBatchUpdate("delete from s_dict_data where dict_code=?", parameters);
    }
 
    private static final String SQL_DICT_TREE_LIST = "select * from s_dict_data where dict_type=? order by parent_id, dict_sort";
 
    /**
     * 返回代码树结构,需要的列表集合。<p></p>
     * 为前端展示树结构准备数据,集合已经按照父节点顺序排好顺序。
     * @param dictType 代码类型,如:question_root_catalog
     * @return
     * @date 2023-03-13
     */
    public List queryDictTreeList(String dictType){
        return this.select(SQL_DICT_TREE_LIST, new Object[]{dictType}, new S_dict_data());
    }
 
    /**
     * 返回所有代码项集合。<p></p>
     * 方法用在代码缓存功能。
     * @return
     * @date 2023-03-10
     */
    public List<S_dict_data> queryAllCodeItemList(){
        return this.select("select * from s_dict_data order by dict_sort", new Object[]{}, new S_dict_data());
    }
 
    /**
     * 返回数据字典所有根集合,即:字典表集合。<p></p>
     * 该方法缓存使用。
     * @return
     * @date 2023-03-10
     */
    public List<S_dict_type> queryRootCodeList(){
        return this.selectAll(new S_dict_type());
    }
 
    public S_dict_data queryOneDictData(long dictCode){
        return this.get(new S_dict_data(dictCode));
    }
 
    /**
     * 字典表中是否存在给定值一样的字典项。并返回字典项。
     * @param dictType
     * @param value
     * @return
     * @date 2022-11-20
     */
    public S_dict_data queryOneDictData(String dictType, String value){
        return this.get(SQL_QUERY_EXIST_DATA, new Object[]{dictType, value}, new S_dict_data());
    }
 
    /**
     * 获取一个字典类型详情。
     * @param dictId 字典id
     * @return
     * @date 2022-11-19
     */
    public S_dict_type queryOneDictType(long dictId){
        return this.get("select * from s_dict_type where dict_id=?", new Object[]{dictId}, new S_dict_type());
    }
 
    /**
     * 分页返回字典类型(代码表)列表
     * @param dictName
     * @param dictType
     * @param status
     * @param startTime
     * @param endTime
     * @return
     * @date 2022-11-16
     */
    public GenericPager<S_dict_type> queryPageDictType(String dictName, String dictType, int status, long startTime, long endTime){
        Map<String, Object> parameters = new HashMap<>();
        StringBuilder sql = new StringBuilder(SQL_PAGE_TYPE_PREFIX);
 
        if(StringUtils.isNotEmpty(dictName)){
            sql.append(" and dict_name like :dictName");
            parameters.put("dictName", "%" + dictName + "%");
        }
        if(StringUtils.isNotEmpty(dictType)){
            sql.append(" and dict_type = :dictType");
            parameters.put("dictType", dictType);
        }
        if(status >= 0){
            sql.append(" and status = :status");
            parameters.put("status", status);
        }
        if(startTime > 0){
            sql.append(" and create_time >= :startTime");
            parameters.put("startTime", startTime);
        }
        if(endTime > 0){
            sql.append(" and create_time <= :endTime");
            parameters.put("endTime", endTime);
        }
 
        PageSearch pageSearch = ListPageContext.getPageSearch();
        return this.selectSplit(sql.toString(), parameters, pageSearch.getPageIndex(), pageSearch.getPageSize(), new S_dict_type());
    }
 
    /**
     * 分页查询数据字典项列表。
     * @param dictType 字典类型(名称)
     * @param dictLabel 模糊查询字典项名称
     * @return
     * @date 2022-11-19
     */
    public GenericPager<S_dict_data> queryPageDictData(String dictType, String dictLabel){
        Map<String, Object> parameters = new HashMap<>();
        StringBuilder sql = new StringBuilder(SQL_PAGE_DATA_PREFIX);
        if(StringUtils.isNotEmpty(dictType)){
            sql.append(" and dict_type = :dictType");
            parameters.put("dictType", dictType);
        }
        if(StringUtils.isNotEmpty(dictLabel)){
            sql.append(" and dict_label like :dictLabel");
            parameters.put("dictLabel", "%" + dictLabel + "%");
        }
        PageSearch pageSearch = ListPageContext.getPageSearch();
        return this.selectSplit(sql.toString(), parameters, pageSearch.getPageIndex(), pageSearch.getPageSize(), new S_dict_data());
    }
 
    /**
     * 根据字典类型名称(代码表),返回子代码集合。
     * @param dictType 代码类型
     * @return
     */
    public List<S_dict_data> queryDictDataByType(String dictType){
        return this.select(new S_dict_data(), "where status=0 and dict_type=? order by dict_sort asc", new Object[]{dictType});
    }
}