dengzedong
2024-11-06 db184afd0c5bf3359b44eb0251fa5b07386eb3ff
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
package com.iailab.module.data.channel.http.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.module.data.channel.http.dao.HttpTagDao;
import com.iailab.module.data.channel.http.entity.HttpApiEntity;
import com.iailab.module.data.channel.http.entity.HttpTagEntity;
import com.iailab.module.data.channel.http.service.HttpApiService;
import com.iailab.module.data.channel.http.service.HttpTagService;
import com.iailab.module.data.channel.http.vo.HttpTagPageReqVO;
import com.iailab.module.data.channel.tag.vo.TagImportExcelVO;
import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
import com.iailab.module.data.common.enums.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
 
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.iailab.module.data.enums.ErrorCodeConstants.*;
 
@Slf4j
@Service
public class HttpTagServiceImpl extends ServiceImpl<HttpTagDao, HttpTagEntity> implements HttpTagService {
 
    @Resource
    private HttpApiService httpApiService;
 
    @Resource
    private HttpTagDao httpTagDao;
 
    @Override
    public PageResult<HttpTagEntity> queryPage(HttpTagPageReqVO reqVO) {
        return httpTagDao.selectPage(reqVO);
    }
 
    @Override
    public HttpTagEntity info(String id) {
        return httpTagDao.selectById(id);
    }
 
    @Override
    public void add(HttpTagEntity httpTagEntity) {
        httpTagDao.insert(httpTagEntity);
    }
 
    @Override
    public void update(HttpTagEntity httpTagEntity) {
        httpTagDao.updateById(httpTagEntity);
    }
 
    @Override
    public void delete(String id) {
        httpTagDao.deleteById(id);
    }
 
    @Override
    public List<HttpTagEntity> list() {
        return httpTagDao.selectList(new QueryWrapper<>());
    }
 
 
    @Override
    public List<HttpTagEntity> selectList(Map<String, Object> params) {
        String httpId = (String) params.get("httpId");
        HttpApiEntity httpApiEntity = httpApiService.info(httpId);
        return httpTagDao.selectList(new QueryWrapper<HttpTagEntity>()
                        .eq("http_api_code", httpApiEntity.getCode())
                        .orderByDesc("create_time"));
    }
 
    @Override
    public List<HttpTagEntity> getApiId(String apiId) {
        return httpTagDao.selectList(new QueryWrapper<HttpTagEntity>()
                .eq("api_id", apiId)
                .orderByDesc("create_time"));
    }
 
    @Override
    public List<HttpTagEntity> getInfoByTagNoAndSourceId(String sourceId, String tagName) {
        return httpTagDao.selectList(new QueryWrapper<HttpTagEntity>()
        .eq("api_id",sourceId)
        .eq("tag_name",tagName)
        );
    }
 
    @Override
    public TagImportRespVO importHttpTagList(List<TagImportExcelVO> importTags, boolean isUpdateSupport, String apiId) {
        // 1.1 参数校验
        if (CollUtil.isEmpty(importTags)) {
            throw exception(TAG_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 遍历,逐个创建 or 更新
        TagImportRespVO respVO = TagImportRespVO.builder().createTagNames(new ArrayList<>())
                .updateTagNames(new ArrayList<>()).failureTagNames(new LinkedHashMap<>()).build();
        importTags.forEach(importTag -> {
            // 判断如果不存在,再进行插入
            HttpTagEntity existTag = httpTagDao.selectOne(new QueryWrapper<HttpTagEntity>()
                    .eq("api_id", apiId)
                    .eq("tag_name",importTag.getTagName()));
            if (existTag == null) {
                HttpTagEntity httpTagEntity = ConvertUtils.sourceToTarget(importTag, HttpTagEntity.class);
                httpTagEntity.setId(UUID.randomUUID().toString());
                httpTagEntity.setEnabled(CommonConstant.IS_ENABLE);
                httpTagEntity.setApiId(apiId);
                httpTagEntity.setCreateTime(new Date());
                httpTagDao.insert(httpTagEntity);
 
                respVO.getCreateTagNames().add(httpTagEntity.getTagName());
                return;
            }
 
            // 如果存在,判断是否允许更新
            if (!isUpdateSupport) {
                respVO.getFailureTagNames().put(importTag.getTagName(), TAG_EXISTS.getMsg());
                return;
            }
 
            HttpTagEntity updateTag = BeanUtils.toBean(importTag, HttpTagEntity.class);
            updateTag.setId(existTag.getId());
            baseMapper.updateById(updateTag);
            respVO.getUpdateTagNames().add(importTag.getTagName());
        });
        return respVO;
    }
 
}