ss
jiangping
2025-07-07 9e1e058b057fc6ef0a9baf636f230c21f1282797
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.doumee.biz.system.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.Constants;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.utils.qiyeweixin.QywxUtil;
import com.doumee.dao.business.CarouselMapper;
import com.doumee.dao.business.InformationMapper;
import com.doumee.dao.business.SeoMapper;
import com.doumee.dao.business.model.Carousel;
import com.doumee.dao.business.model.Information;
import com.doumee.dao.business.model.Seo;
import com.doumee.dao.common.dto.UpdateSortDTO;
import com.doumee.dao.system.model.SystemDict;
import com.doumee.dao.system.model.SystemDictData;
import com.doumee.dao.vo.FootDataVO;
import com.doumee.service.system.SystemDictDataService;
import com.doumee.service.system.SystemDictService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
 
import java.io.*;
import java.util.*;
 
@Service
public class SystemDictDataBizImpl implements SystemDictDataBiz {
 
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private CarouselMapper carouselMapper;
    @Autowired
    private SeoMapper seoMapper;
    @Autowired
    private SystemDictDataService systemDictDataService;
    @Autowired
    private SystemDictService systemDictService;
 
    @Override
    public Integer create(SystemDictData systemDictData) {
        SystemDictData queryDto = new SystemDictData();
        queryDto.setDictId(systemDictData.getDictId());
        queryDto.setCode(systemDictData.getCode());
        queryDto.setDeleted(Boolean.FALSE);
        SystemDictData dictData = systemDictDataService.findOne(queryDto);
        if (dictData != null) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值已存在");
        }
        // 排序处理
        queryDto = new SystemDictData();
        queryDto.setDictId(systemDictData.getDictId());
        queryDto.setDeleted(Boolean.FALSE);
        long maxSort = systemDictDataService.count(queryDto);
        systemDictData.setSort(Integer.valueOf("" + maxSort));
        return systemDictDataService.create(systemDictData);
    }
 
    @Override
    public void updateById(SystemDictData systemDictData) {
        systemDictDataService.updateById(systemDictData);
    }
 
    @Override
    public     String updateQywxToken(){
        String corpid = queryByCode(Constants.QYWX,Constants.QYWX_CORPID).getCode();
        String secret = queryByCode(Constants.QYWX,Constants.QYWX_SECRET).getCode();
        String token = QywxUtil.getAccessToken(corpid,secret);
       SystemDictData dictData = queryByCode(Constants.QYWX,Constants.QYWX_TOKEN);
        if (dictData != null  ) {
            dictData.setCode(token);
            dictData.setUpdateTime(new Date());
            systemDictDataService.updateById(dictData);
            String ticket = QywxUtil.getJsApiTicket(token);
            SystemDictData ticketDictData = queryByCode(Constants.QYWX,Constants.QYWX_JS_API_TICKET);
            if (dictData != null  ) {
                ticketDictData.setCode(ticket);
                ticketDictData.setUpdateTime(new Date());
                systemDictDataService.updateById(ticketDictData);
            }
        }
        return  token;
    }
 
    @Override
    public void sort(UpdateSortDTO dto) {
        SystemDictData dictData = systemDictDataService.findById(dto.getId());
        if (dictData == null || dictData.getDeleted()) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "排序的数据不存在或已被删除");
        }
        SystemDictData queryDto = new SystemDictData();
        queryDto.setDictId(dictData.getDictId());
        queryDto.setDeleted(Boolean.FALSE);
        List<SystemDictData> dictDataList = systemDictDataService.findList(queryDto);
        int index = 0;
        for (int i = 0; i < dictDataList.size(); i++) {
            if (dictDataList.get(i).getId().equals(dto.getId())) {
                index = i;
                break;
            }
        }
        // 上移
        if ("top".equals(dto.getDirection())) {
            if (index - 1 < 0) {
                return;
            }
            SystemDictData preDictData = dictDataList.remove(index - 1);
            dictDataList.add(index, preDictData);
        }
        // 下移
        else {
            if (index + 1 > dictDataList.size() - 1) {
                return;
            }
            SystemDictData nextDictData = dictDataList.remove(index + 1);
            dictDataList.add(index, nextDictData);
        }
        for (int i = 0; i < dictDataList.size(); i++) {
            dictDataList.get(i).setSort(i);
        }
        // 修改
        systemDictDataService.updateByIdInBatch(dictDataList);
    }
 
    @Override
    public SystemDictData queryByCode(String dicCode, String label){
        SystemDict queryDto = new SystemDict();
        queryDto.setCode(dicCode);
        queryDto.setDeleted(Boolean.FALSE);
        SystemDict dictData = systemDictService.findOne(queryDto);
        if (dictData == null) {
            return  new SystemDictData();
//            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值不存在");
        }
        SystemDictData data = new SystemDictData();
        data.setDictId(dictData.getId());
        data.setLabel(label);
        data.setDeleted(Boolean.FALSE);
        data = systemDictDataService.findOne(data);
        if(data == null){
            data = new SystemDictData();
        }
        return data;
    }
    @Override
    public  String  queryCodeById(Integer id){
        SystemDictData data =  systemDictDataService.findById(id);
        if(data != null){
            return  data.getCode();
        }
        return null;
    }
 
 
    @Override
    public List<SystemDictData> queryListByCode(String dicCode, String label){
        SystemDict queryDto = new SystemDict();
        queryDto.setCode(dicCode);
        queryDto.setDeleted(Boolean.FALSE);
        SystemDict dictData = systemDictService.findOne(queryDto);
        if (dictData == null) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值不存在");
        }
        SystemDictData data = new SystemDictData();
        data.setDictId(dictData.getId());
        data.setLabel(label);
        data.setDeleted(Boolean.FALSE);
        List<SystemDictData> list= systemDictDataService.findList(data);
 
        return list;
    }
 
    @Override
    public void updateByIdNew(SystemDictData systemDictData) {
        systemDictDataService.updateById(systemDictData);
    }
 
    @Override
    public List<SystemDictData> queryDataByCode(String dicCode, String code){
        SystemDict queryDto = new SystemDict();
        queryDto.setCode(dicCode);
        queryDto.setDeleted(Boolean.FALSE);
        SystemDict dictData = systemDictService.findOne(queryDto);
        if (dictData == null) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值不存在");
        }
        SystemDictData data = new SystemDictData();
        data.setDictId(dictData.getId());
        data.setCode(code);
        data.setDeleted(Boolean.FALSE);
        List<SystemDictData> list= systemDictDataService.findList(data);
 
        return list;
    }
 
 
    @Override
    public void updateByDicId(List<SystemDictData> list) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        list.stream().forEach(s->{
            s.setUpdateTime(new Date());
            s.setUpdateUser(user.getId());
            systemDictDataService.updateById(s);
        });
 
    }
 
    @Override
    public String getPreFixPath(String resourceCode, String targetCode) {
 
        if (StringUtils.isBlank(resourceCode) || StringUtils.isBlank(targetCode)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"路径参数不完整");
        }
        SystemDict queryDto = new SystemDict();
        queryDto.setCode(Constants.FTP);
        queryDto.setDeleted(Boolean.FALSE);
        SystemDict dictData = systemDictService.findOne(queryDto);
        if (dictData == null) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值不存在");
        }
        List<SystemDictData> list = systemDictDataService.findList(dictData.getId(), Arrays.asList(resourceCode, targetCode));
        String resourcePath = list.stream().filter(s->s.getLabel().equals(resourceCode)).findFirst().map(s->s.getCode()).orElse("");
        String targetPath = list.stream().filter(s -> s.getLabel().equals(targetCode)).findFirst().map(s -> s.getCode()).orElse("");
        return resourcePath+targetPath;
    }
 
    @Override
    public String createPage(Integer pageIndex) {
        Constants.PageIndex objIndex  = Constants.PageIndex.getObj(pageIndex);
        if(objIndex == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        String flt = objIndex.getTemplate();//模版名称
        String html = objIndex.getHtml();//页面名称
        String domain = queryByCode(Constants.SYSTEM,Constants.WEB_DOMAIN).getCode();
        String htmlPath = queryByCode(Constants.SYSTEM,Constants.STATIC_PAGES_PATH).getCode();
        File docFile = new File( htmlPath);
        if(!docFile.exists()){
            docFile.mkdirs();
        }
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("webDomain",domain);
        List<Seo> seoList = seoMapper.selectList(new LambdaQueryWrapper<Seo>().eq(Seo::getIsdeleted,Constants.ZERO));
        dataMap.put("seo",getSeoByHtml(Constants.PageIndex.getObj(pageIndex).getHtml(),seoList));
        initDataMapByPage(objIndex,dataMap);
        if(objIndex.getKey() == Constants.PageIndex.dynamicsDetail.getKey() || objIndex.getKey() == Constants.PageIndex.dynamicsDetail.getKey() ){
            List<Information> dataList =getInfomationListByType(objIndex.getKey() == Constants.PageIndex.dynamics.getKey() ?Constants.ZERO:Constants.ONE,null);
            generateDetailsHtml(dataList,dataMap,flt,htmlPath+html);//单文件数据
        }else{
            if(objIndex.getKey() == Constants.PageIndex.dynamics.getKey() || objIndex.getKey() == Constants.PageIndex.casesDetail.getKey() ){
                dataMap.put("dataList", getInfomationListByType(objIndex.getKey() == Constants.PageIndex.dynamics.getKey() ?Constants.ZERO:Constants.ONE,null));
            }
            if(objIndex.getKey() == Constants.PageIndex.index.getKey()){
                //如果是首页
                dataMap.put("bannerList",  carouselMapper.selectList(new LambdaQueryWrapper<Carousel>()
                        .eq(Carousel::getIsdeleted,Constants.ZERO)
                        .orderByDesc(Carousel::getSortnum ,Carousel::getCreateDate)));
                dataMap.put("caseList", getInfomationListByType( Constants.ONE,12));
                dataMap.put("zixunList", getInfomationListByType( Constants.ZERO,12));
            }
            generateSingleHtml(dataMap,flt,htmlPath+html);//单文件数据
        }
       return  null;
    }
 
    private Seo getSeoByHtml(String html, List<Seo> seoList) {
        if(seoList==null || seoList.size()==0){
            return new Seo();
        }
        for(Seo s :seoList){
            if(StringUtils.equalsIgnoreCase(s.getPageAddr(),html)){
                return s;
            }
        }
        return new Seo();
    }
 
    private List<Information> getInfomationListByType(Integer type,Integer limit) {
        List<Information> dataList =informationMapper.selectList(new LambdaQueryWrapper<Information>()
                .eq(Information::getType,type)
                .eq(Information::getIsdeleted,Constants.ZERO)
                .last(limit!=null,"limit "+limit)
                .orderByDesc(Information::getSortnum ,Information::getCreateDate) );//查询全部资讯
 
        return dataList;
    }
 
    private void generateDetailsHtml(List<Information> dataList, Map<String, Object> dataMap, String flt, String htmlPath) {
        if(dataList== null){
            return;
        }
        for(Information data :dataList){
            dataMap.put("data",data);
            generateSingleHtml(dataMap,flt,htmlPath.replace("${id}",data.getId()+""));//单文件数据
        }
    }
 
    private void generateSingleHtml(Map<String, Object> dataMap, String flt, String htmlPath)   {
        try {
            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            Writer out = null;
 
            // 加载模版文件
            Template template = configuration.getTemplate(flt);
            // 生成数据
            File docFile = new File( htmlPath);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // 输出文件
            template.process(dataMap,out);
        } catch (Exception e) {
            e.printStackTrace();
            throw  new BusinessException(ResponseStatus.SERVER_ERROR);
        }
    }
 
    private void initDataMapByPage(Constants.PageIndex objIndex, Map<String, Object> dataMap) {
        dataMap.put("bottom", getFoodDataVO());//网站底部信息
    }
 
    public FootDataVO getFoodDataVO(){
        FootDataVO footDataVO = new FootDataVO();
        SystemDict d = new SystemDict();
        d.setCode(Constants.SYSTEM);
        d.setDeleted(Boolean.FALSE);
        d = systemDictService.findOne(d);
        if(d ==null){
            return footDataVO;
        }
        SystemDictData param = new SystemDictData();
        param.setDictId(d.getId());
        param.setDeleted(Boolean.FALSE);
        List<SystemDictData> dictDataList = systemDictDataService.findList(param);
 
        String path = findDictDataByLabel(dictDataList,Constants.FILE_DIR);
        footDataVO.setFileDir(path);
        footDataVO.setFootWords(findDictDataByLabel(dictDataList,Constants.FOOT_WORDS));
        footDataVO.setFootImgUrl(findDictDataByLabel(dictDataList,Constants.FOOT_IMGURL));
        footDataVO.setFootFullImgUrl(path+ footDataVO.getFootImgUrl());
        footDataVO.setFootImgUrl2(findDictDataByLabel(dictDataList,Constants.FOOT_IMGURL2));
        footDataVO.setFootFullImgUrl2(path + footDataVO.getFootImgUrl2());
        footDataVO.setAddress(findDictDataByLabel(dictDataList,Constants.ADDRESS));
        footDataVO.setLinkPhone(findDictDataByLabel(dictDataList,Constants.LINK_PHONE));
        footDataVO.setLinkMobile(findDictDataByLabel(dictDataList,Constants.LINK_MOBILE));
        String solveScheme =  findDictDataByLabel(dictDataList,Constants.SOLVE_SCHEME);
        if(Objects.nonNull(solveScheme) & org.apache.commons.lang3.StringUtils.isNotBlank(solveScheme)){
            footDataVO.setSolveScheme(JSONArray.parseArray(solveScheme));
        }
        String wisdomSystem = findDictDataByLabel(dictDataList,Constants.WISDOM_SYSTEM);
        if(Objects.nonNull(wisdomSystem) & org.apache.commons.lang3.StringUtils.isNotBlank(wisdomSystem )){
            footDataVO.setWisdomSystem(JSONArray.parseArray(wisdomSystem ));
        }
        String honors = findDictDataByLabel(dictDataList,Constants.HONORS);
        if(Objects.nonNull(wisdomSystem) & org.apache.commons.lang3.StringUtils.isNotBlank(honors)){
            footDataVO.setHonors(JSONArray.parseArray(honors));
        }
        footDataVO.setServerTime(findDictDataByLabel(dictDataList,Constants.SERVER_TIME));
 
        return footDataVO;
    }
 
    private String findDictDataByLabel(List<SystemDictData> dictDataList, String footWords) {
        if(dictDataList!=null){
            for(SystemDictData data:dictDataList){
                if(StringUtils.equals(footWords,data.getLabel())){
                    return data.getCode();
                }
            }
        }
        return "";
    }
 
 
}