郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.account.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.account.entity.app.AppVersionInfo;
import com.gkhy.safePlatform.account.enums.AppTypeEnum;
import com.gkhy.safePlatform.account.enums.AppVersionStatusEnum;
import com.gkhy.safePlatform.account.model.bo.ObjectItemBO;
import com.gkhy.safePlatform.account.model.dto.req.AppAddReqDTO;
import com.gkhy.safePlatform.account.model.dto.req.AppModReqDTO;
import com.gkhy.safePlatform.account.model.dto.resp.AppVersionRespDTO;
import com.gkhy.safePlatform.account.model.dto.resp.ObjectItemRespDTO;
import com.gkhy.safePlatform.account.model.query.AppVersionPageQuery;
import com.gkhy.safePlatform.account.model.query.db.AppVersionPageDBQuery;
import com.gkhy.safePlatform.account.service.AppVersionService;
import com.gkhy.safePlatform.account.service.baseService.AppVersionInfoService;
import com.gkhy.safePlatform.account.utils.MinioUtils;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
@Service("appVersionService")
public class AppVersionServiceImpl implements AppVersionService {
 
    @Autowired
    private MinioUtils minioUtils;
    @Value("${accountminio.floders.app}")
    private String appFloder;
    @Autowired
    private AppVersionInfoService appInfoService;
 
    @Override
    public ObjectItemRespDTO getAppPresignedUrl(ContextCacheUser currentUser, String filename) {
        if (StringUtils.isBlank(filename)) {
            throw new AusinessException(E.DATA_PARAM_NULL, "文件名传入为空");
        }
        // todo 校验文件名是否合法
        ObjectItemBO item = minioUtils.getPresignedUrl(appFloder, filename.trim());
        if (item == null) {
            throw new BusinessException(ResultCodes.SERVER_ERROR);
        }
        ObjectItemRespDTO respDTO = new ObjectItemRespDTO();
        respDTO.setObjectName(item.getObjectName());
        respDTO.setPresignedUrl(item.getPresignedUrl());
        return respDTO;
    }
 
    @Override
    public void addAppVersion(ContextCacheUser currentUser, AppAddReqDTO reqDTO) {
        if (StringUtils.isBlank(reqDTO.getAppName())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "文件名称不能为空");
        }
        if (StringUtils.isBlank(reqDTO.getCustomVersion())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "版本号不能为空");
        }
        if (StringUtils.isBlank(reqDTO.getObjectName())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "文件对象名不能为空");
        }
        if (reqDTO.getAppType() == null) {
            throw new AusinessException(E.DATA_PARAM_NULL, "app文件类型不能为空");
        }
        AppTypeEnum typeEnum = AppTypeEnum.parse(reqDTO.getAppType());
        if (typeEnum == null) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "app文件类型非法");
        }
        // todo 校验文件是否存在
        // 1.文件个数查询
        long count = appInfoService.countByAppType(reqDTO.getAppType());
        // 自定义版本名称是否存在
        AppVersionInfo appVersionInfo = appInfoService.getAppVersionInfoByCustomVersion(reqDTO.getCustomVersion().trim(),reqDTO.getAppType());
        if (appVersionInfo != null) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "app自定义版本已经存在");
        }
        // exc
        AppVersionInfo appEntity = new AppVersionInfo();
        appEntity.setName(reqDTO.getAppName().trim());
        appEntity.setInfo(reqDTO.getInfo());
        appEntity.setVersion((int) (count + 1));
        appEntity.setStatus(AppVersionStatusEnum.ENABLED.code);
        appEntity.setObjectName(reqDTO.getObjectName());
        appEntity.setGmtCreate(LocalDateTime.now());
        appEntity.setCreateUid(currentUser.getUid());
        appEntity.setCreateUname(currentUser.getRealName());
        appEntity.setAppType(reqDTO.getAppType());
        appEntity.setCustomVersion(reqDTO.getCustomVersion());
        appInfoService.saveAppInfo(appEntity);
 
    }
 
    @Override
    public void modAppVersion(ContextCacheUser currentUser, AppModReqDTO reqDTO) {
        if (reqDTO.getId() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        AppVersionInfo appInfo = appInfoService.getById(reqDTO.getId());
        if (appInfo == null) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "APP版本信息不存在");
        }
 
        AppVersionInfo appEntity = new AppVersionInfo();
        appEntity.setId(appInfo.getId());
        appEntity.setInfo(reqDTO.getInfo());
        appEntity.setGmtModified(LocalDateTime.now());
        appEntity.setModifiedUid(currentUser.getUid());
        appEntity.setModifiedUname(currentUser.getRealName());
        if (StringUtils.isNotBlank((reqDTO.getAppName()))) {
            appEntity.setName(reqDTO.getAppName().trim());
        }
        if (StringUtils.isNotBlank(reqDTO.getCustomVersion())) {
            AppVersionInfo version = appInfoService.getAppVersionInfoByCustomVersion(reqDTO.getCustomVersion().trim(), reqDTO.getAppType());
            if (version != null && !version.getId().equals(appInfo.getId())) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "app自定义版本已经存在");
            }
            appEntity.setCustomVersion(reqDTO.getCustomVersion().trim());
        }
        // 文件 ObjectName
        if (StringUtils.isNotBlank(reqDTO.getObjectName())) {
            // todo 校验文件是否存在
            appEntity.setObjectName(reqDTO.getObjectName().trim());
        }
        appInfoService.updateAppInfo(appEntity);
    }
 
    @Override
    public void delAppVersion(ContextCacheUser currentUser, Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        AppVersionInfo appInfo = appInfoService.getById(id);
        if (appInfo == null) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "APP版本信息不存在");
        }
        if (!appInfo.getStatus().equals(AppVersionStatusEnum.ENABLED.code)) {
            throw new AusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID, "APP版本信息不存在");
        }
        AppVersionInfo appEntity = new AppVersionInfo();
        appEntity.setId(appInfo.getId());
        appEntity.setGmtModified(LocalDateTime.now());
        appEntity.setModifiedUid(currentUser.getUid());
        appEntity.setModifiedUname(currentUser.getRealName());
        appEntity.setStatus(AppVersionStatusEnum.ABANDONED.code);
        appInfoService.updateAppInfo(appEntity);
    }
 
    @Override
    public SearchResultVO<List<AppVersionRespDTO>>  listAppVersionByPage(ContextCacheUser currentUser, PageQuery<AppVersionPageQuery> pageQuery) {
        AppVersionPageDBQuery dbQuery = new AppVersionPageDBQuery();
        if (pageQuery.getSearchParams() != null) {
            dbQuery.setAppType(pageQuery.getSearchParams().getAppType());
            dbQuery.setAppName(pageQuery.getSearchParams().getAppName());
            dbQuery.setCustomVersion(pageQuery.getSearchParams().getCustomVersion());
        }
        Page<AppVersionInfo> page = new Page<>(pageQuery.getPageIndex(), pageQuery.getPageSize());
        List<AppVersionInfo> dbData = appInfoService.listAppVersionInfoByPage(page, dbQuery);
        List<AppVersionRespDTO> result = new ArrayList<>(dbData.size());
        if (dbData.size() > 0) {
            AppVersionStatusEnum statusEnum;
            AppTypeEnum appTypeEnum;
            AppVersionRespDTO respDTO;
            for (AppVersionInfo appVersionInfo : dbData) {
                respDTO = new AppVersionRespDTO();
                respDTO.setId(appVersionInfo.getId());
                respDTO.setName(appVersionInfo.getName());
                respDTO.setInfo(appVersionInfo.getInfo());
                respDTO.setCustomVersion(appVersionInfo.getCustomVersion());
                respDTO.setVersion(appVersionInfo.getVersion());
                respDTO.setStatus(appVersionInfo.getStatus());
                statusEnum = AppVersionStatusEnum.parse(appVersionInfo.getStatus());
                if (statusEnum != null) {
                    respDTO.setStatusDesc(statusEnum.value);
                }
                respDTO.setAppType(appVersionInfo.getAppType());
                appTypeEnum = AppTypeEnum.parse(appVersionInfo.getAppType());
                if (appTypeEnum != null) {
                    respDTO.setAppTypeDesc(appTypeEnum.getValue());
                }
                respDTO.setObjectName(appVersionInfo.getObjectName());
                respDTO.setObjectUrl(minioUtils.getAccessibleUrl(appFloder,appVersionInfo.getObjectName()));
                respDTO.setGmtCreate(appVersionInfo.getGmtCreate());
                respDTO.setCreateUid(appVersionInfo.getCreateUid());
                respDTO.setCreateUname(appVersionInfo.getCreateUname());
                respDTO.setGmtModified(appVersionInfo.getGmtModified());
                respDTO.setModifiedUid(appVersionInfo.getModifiedUid());
                respDTO.setModifiedUname(appVersionInfo.getModifiedUname());
 
                result.add(respDTO);
            }
        }
 
        return new SearchResultVO<>(
                true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                result,
                ResultCodes.OK);
 
    }
 
    @Override
    public AppVersionRespDTO getLastestAppRelease(ContextCacheUser currentUser, Byte appType) {
        if (appType == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        AppTypeEnum appTypeEnum = AppTypeEnum.parse(appType);
        if (appTypeEnum == null) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "app类型非法");
        }
        AppVersionInfo appVersionInfo = appInfoService.getLastestAppRelease(appType);
 
        AppVersionRespDTO result = null;
        if (appVersionInfo != null) {
            result = new AppVersionRespDTO();
            result.setId(appVersionInfo.getId());
            result.setName(appVersionInfo.getName());
            result.setInfo(appVersionInfo.getInfo());
            result.setCustomVersion(appVersionInfo.getCustomVersion());
            result.setVersion(appVersionInfo.getVersion());
            result.setStatus(appVersionInfo.getStatus());
            AppVersionStatusEnum statusEnum = AppVersionStatusEnum.parse(appVersionInfo.getStatus());
            if (statusEnum != null) {
                result.setStatusDesc(statusEnum.value);
            }
            result.setAppType(appVersionInfo.getAppType());
            appTypeEnum = AppTypeEnum.parse(appVersionInfo.getAppType());
            if (appTypeEnum != null) {
                result.setAppTypeDesc(appTypeEnum.getValue());
            }
            result.setObjectName(appVersionInfo.getObjectName());
            result.setObjectUrl(minioUtils.getAccessibleUrl(appFloder,appVersionInfo.getObjectName()));
            result.setGmtCreate(appVersionInfo.getGmtCreate());
            result.setCreateUid(appVersionInfo.getCreateUid());
            result.setCreateUname(appVersionInfo.getCreateUname());
            result.setGmtModified(appVersionInfo.getGmtModified());
            result.setModifiedUid(appVersionInfo.getModifiedUid());
            result.setModifiedUname(appVersionInfo.getModifiedUname());
        }
 
        return result;
    }
}