houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
package com.iailab.module.knowledge.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iailab.framework.common.constant.Constant;
import com.iailab.framework.common.page.PageData;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.module.knowledge.dao.KnowledgeDetDao;
import com.iailab.module.knowledge.dto.KnowledgeDetDTO;
import com.iailab.module.knowledge.entity.KnowledgeDetEntity;
import com.iailab.module.knowledge.service.KnowledgeDetService;
import com.iailab.module.oss.dto.OssFileDTO;
import com.iailab.module.oss.service.OssFileService;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2023年12月25日 17:33:00
 */
@Service
public class KnowledgeDetServiceImpl extends BaseServiceImpl<KnowledgeDetDao, KnowledgeDetEntity> implements KnowledgeDetService {
 
    @Resource
    private OssFileService ossAppService;
 
    private final String BUSINESS_TYPE = "knowledge";
 
    @Override
    public PageData<KnowledgeDetDTO> page(Map<String, Object> params) {
        IPage<KnowledgeDetEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        PageData<KnowledgeDetDTO> result = getPageData(page, KnowledgeDetDTO.class);
        result.getList().forEach(item -> {
            List<OssFileDTO> fileDTOS = ossAppService.getByBusinessId(item.getId());
            if (!CollectionUtils.isEmpty(fileDTOS)) {
                OssFileDTO fileDTO = fileDTOS.get(0);
                item.setFileName(fileDTO.getFileName());
                item.setFilePostfix(fileDTO.getFilePostfix());
                item.setUrl(fileDTO.getUrl());
            }
        });
        return result;
    }
 
    private QueryWrapper<KnowledgeDetEntity> getWrapper(Map<String, Object> params) {
        String treeId = (String) params.get("treeId");
        String title = (String) params.get("title");
        String keyWords = (String) params.get("keyWords");
        QueryWrapper<KnowledgeDetEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(treeId), "tree_id", treeId)
                .like(StringUtils.isNotBlank(title), "title", title)
                .like(StringUtils.isNotBlank(keyWords), "key_words", keyWords);
        return wrapper;
    }
 
    @Override
    public KnowledgeDetDTO get(String id) {
        KnowledgeDetEntity entity = baseDao.selectById(id);
        KnowledgeDetDTO dto = ConvertUtils.sourceToTarget(entity, KnowledgeDetDTO.class);
        List<OssFileDTO> fileDTOS = ossAppService.getByBusinessId(dto.getId());
        if (!CollectionUtils.isEmpty(fileDTOS)) {
            OssFileDTO fileDTO = fileDTOS.get(0);
            dto.setFileName(fileDTO.getFileName());
            dto.setFilePostfix(fileDTO.getFilePostfix());
            dto.setUrl(fileDTO.getUrl());
        }
        return dto;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(KnowledgeDetDTO dto) {
        KnowledgeDetEntity entity = ConvertUtils.sourceToTarget(dto, KnowledgeDetEntity.class);
        entity.setId(UUID.randomUUID().toString());
        entity.setCreateDate(new Date());
        insert(entity);
        OssFileDTO fileDTO = new OssFileDTO();
        fileDTO.setBusinessId(entity.getId());
        fileDTO.setType(BUSINESS_TYPE);
        fileDTO.setFileName(dto.getFileName());
        fileDTO.setFilePostfix(dto.getFilePostfix());
        fileDTO.setUrl(dto.getUrl());
        fileDTO.setSort(0);
        ossAppService.save(fileDTO);
    }
 
    @Override
    public void update(KnowledgeDetDTO dto) {
        KnowledgeDetEntity entity = ConvertUtils.sourceToTarget(dto, KnowledgeDetEntity.class);
        updateById(entity);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }
}