杜洪波
2025-05-14 9db1c6c261049fbdcb104e6c0e07fa544bb42013
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.product.administration.service;
 
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.product.admin.service.PublicService;
import com.product.administration.config.CmnConst;
import com.product.administration.service.ide.IKnowledgeSharingService;
import com.product.common.utils.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.file.service.FileManagerService;
import com.product.util.BaseUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Arrays;
import java.util.List;
 
/**
 * Copyright © 6c
 *
 * @Date 2022年06月14日 15:45
 * @Author 6c
 * @Description 知识共享
 */
@Component
public class KnowledgeSharingService implements IKnowledgeSharingService {
    @Autowired
    private BaseDao baseDao;
    @Autowired
    private PublicService publicService;
    @Autowired
    private FileManagerService fileManagerService;
 
    /**
     * 保存
     * @param fse
     * @return
     */
    @Override
    @Transactional
    public String save(FieldSetEntity fse) {
        String uuid = publicService.saveFieldSetEntity(fse);
 
        perfectTypeTag(fse.getString(CmnConst.TYPE), 1);
        perfectTypeTag(fse.getString(CmnConst.TAG), 2);
 
        return uuid;
    }
 
    /**
     * 将系统中没有的标签或者分类存入系统中
     * @param typeTag
     */
    private void perfectTypeTag(String typeTag, int type) {
        DataTableEntity dte = type == 1 ? getType() : getTag();
        List<Object> list = Lists.newArrayList(Arrays.asList(dte.getFieldAllValues(CmnConst.NAME)));
        List<Object> curList = Lists.newArrayList(Arrays.asList(typeTag.split(",")));
        curList.removeAll(list);
        curList.forEach(name -> {
            FieldSetEntity typeTagFse = new FieldSetEntity();
            typeTagFse.setTableName(CmnConst.PRODUCT_OA_KNOWLEDGE_SHARING_TAG);
            typeTagFse.setValue(CmnConst.NAME, name);
            typeTagFse.setValue(CmnConst.TYPE, type);
            publicService.saveFieldSetEntity(typeTagFse);
        });
    }
 
    /**
     * 获取详情
     * @param fse
     * @return
     */
    public FieldSetEntity viewInfo(FieldSetEntity fse) {
        FieldSetEntity resultFse = publicService.getFieldSetEntity(fse, false);
        baseDao.loadPromptData(resultFse);
        return resultFse;
    }
 
    /**
     * 删除
     * @param fse
     */
    @Override
    @Transactional
    public void delete(FieldSetEntity fse) {
        String[] uuidArr = fse.getString(CmnConst.UUID).split(",");
        StringBuilder sql = new StringBuilder(128);
        sql.append("select group_concat(type) type,group_concat(tag) tag");
        sql.append("\nfrom product_oa_knowledge_sharing");
        sql.append("\nwhere ").append(BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, uuidArr.length, true));
        FieldSetEntity paramFse = baseDao.getFieldSetEntityBySQL(sql.toString(), uuidArr, false);
        String types = paramFse.getString(CmnConst.TYPE);
        String tags = paramFse.getString(CmnConst.TAG);
 
        publicService.delete(fse);
 
        // 清理分类
        String[] array = types.split(",");
        sql.setLength(0);
        sql.append("select t.name from product_oa_knowledge_sharing_tag t");
        sql.append("\ninner join (");
        sql.append("\n    select * from product_oa_knowledge_sharing where 1=1");
        for (String type : array) {
            sql.append("\n    or find_in_set('").append(type).append("',type)");
        }
        sql.append("\n) d on find_in_set(t.name,d.type)");
        DataTableEntity dte = baseDao.listTable(sql.toString(), new Object[]{});
        List<Object> residueList = Lists.newArrayList(Arrays.asList(dte.getFieldAllValues(CmnConst.NAME)));
        List<Object> curList = Lists.newArrayList(array);
        curList.removeAll(residueList);
        sql.setLength(0);
        sql.append("delete from product_oa_knowledge_sharing_tag where type=1");
        if (!curList.isEmpty()) {
            sql.append(" and ").append(BaseUtil.buildQuestionMarkFilter(CmnConst.NAME, curList.toArray().length, true));
        }
        baseDao.executeUpdate(sql.toString(), curList.toArray());
 
        // 清理标签
        array = tags.split(",");
        sql.setLength(0);
        sql.append("select t.name from product_oa_knowledge_sharing_tag t");
        sql.append("\ninner join (");
        sql.append("\n    select * from product_oa_knowledge_sharing where 1=1");
        for (String type : array) {
            sql.append("\n    or find_in_set('").append(type).append("',tag)");
        }
        sql.append("\n) d on find_in_set(t.name,d.tag)");
        dte = baseDao.listTable(sql.toString(), new Object[]{});
        residueList = Lists.newArrayList(Arrays.asList(dte.getFieldAllValues(CmnConst.NAME)));
        curList = Lists.newArrayList(array);
        curList.removeAll(residueList);
        sql.setLength(0);
        sql.append("delete from product_oa_knowledge_sharing_tag where type=2");
        if (!curList.isEmpty()) {
            sql.append(" and ").append(BaseUtil.buildQuestionMarkFilter(CmnConst.NAME, curList.toArray().length, true));
        }
        baseDao.executeUpdate(sql.toString(), curList.toArray());
    }
 
    /**
     * 列表
     * @param fse
     */
    public JSONObject list(FieldSetEntity fse) {
        JSONObject resultObj = new JSONObject();
 
        StringBuilder defaultFilter = new StringBuilder(64);
        // 分类过滤
        String types = fse.getString(CmnConst.TYPE);
        if (!StringUtils.isEmpty(types)) {
            String[] typeArr = types.split(",");
            for (String type : typeArr) {
                if (defaultFilter.length() > 0) {
                    defaultFilter.append("\nor ");
                }
                defaultFilter.append("find_in_set('").append(type).append("',type)");
            }
        }
        // 标签过滤
        String tags = fse.getString(CmnConst.TAG);
        if (!StringUtils.isEmpty(tags)) {
            String[] tagArr = tags.split(",");
            for (String tag : tagArr) {
                if (defaultFilter.length() > 0) {
                    defaultFilter.append("\nor ");
                }
                defaultFilter.append("find_in_set('").append(tag).append("',tag)");
            }
        }
        if (defaultFilter.length() > 0) {
            fse.setValue(CmnConst.PARAM_FILTER, BaseUtil.addAimField(fse, CmnConst.PARAM_FILTER, defaultFilter.toString(), " and "));
        }
 
        DataTableEntity dte = publicService.listTable(fse);
        FieldSetEntity tempFse;
        for (int i = 0; i < dte.getRows(); i++) {
            tempFse = dte.getFieldSetEntity(i);
            try {
                byte[] fileContent = fileManagerService.getFileContent(dte.getString(i, CmnConst.IMAGE));
                if (fileContent != null) {
                    String bytes = Base64.encodeBase64String(fileContent);
                    dte.setFieldValue(i, CmnConst.IMAGE, "data:image/*;base64," + bytes);
                } else {
                    dte.setFieldValue(i, CmnConst.IMAGE, null);
                }
            } catch (Exception e) {
                SpringMVCContextHolder.getSystemLogger().error(String.format("《%s》获取图片失败", tempFse.getString(CmnConst.TITLE)));
                e.printStackTrace();
            }
        }
 
        resultObj.put("list", BaseUtil.dataTableEntityToJson(dte));
        resultObj.put("type", BaseUtil.dataTableEntityToJson(getType()));
        resultObj.put("tag", BaseUtil.dataTableEntityToJson(getTag()));
        return resultObj;
    }
 
    /**
     * 获取所有分类
     * @return
     */
    public DataTableEntity getType() {
        return baseDao.listTable(CmnConst.PRODUCT_OA_KNOWLEDGE_SHARING_TAG, "type=1", null, new Object[]{CmnConst.NAME});
    }
 
    /**
     * 获取所有标签
     * @return
     */
    public DataTableEntity getTag() {
        return baseDao.listTable(CmnConst.PRODUCT_OA_KNOWLEDGE_SHARING_TAG, "type=2", null, new Object[]{CmnConst.NAME});
    }
}