ludc
2025-01-16 391eec3114a17e68652434c6eae610799d80290e
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.vci.web.service.impl;
 
import com.vci.corba.common.PLException;
import com.vci.corba.common.data.UserEntityInfo;
import com.vci.corba.framework.data.PvolumeInfo;
import com.vci.dto.OsPvolumeDTO;
import com.vci.starter.poi.bo.WriteExcelData;
import com.vci.starter.poi.bo.WriteExcelOption;
import com.vci.starter.poi.util.ExcelUtil;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.util.LangBaseUtil;
import com.vci.starter.web.util.LocalFileUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.WebThreadLocalUtil;
import com.vci.web.service.OsPvolumesServiceI;
import com.vci.starter.web.util.Lcm.Func;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 文件柜管理的服务实现类
 * @author yuxc
 * @date 2024-10-14
 */
@Service
public class OsPvolumesServiceImpl implements OsPvolumesServiceI {
 
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    /**
     * 分页查询卷
     * @param pageSize 第几页
     * @param pageIndex 页数
     * @return 分页数据
     */
    @Override
    public BaseResult getPvolumesPage(short pageSize, short pageIndex) throws PLException {
        PvolumeInfo[] pvolumesPage = platformClientUtil.getFrameworkService().getPvolumesPage(pageSize, pageIndex);
 
        List<OsPvolumeDTO> pvs = new ArrayList<>();
        for (PvolumeInfo pvolumeInfo : pvolumesPage) {
            OsPvolumeDTO osPvolumeDTO = new OsPvolumeDTO();
            osPvolumeDTO = tranferCorbaObjToPvolume(pvolumeInfo);
            pvs.add(osPvolumeDTO);
        }
        return BaseResult.dataList(pvs);
    }
 
    @Override
    public BaseResult savePvolume(OsPvolumeDTO dto) throws PLException {
        PvolumeInfo pvoInfo = new PvolumeInfo();
        pvoInfo.service = dto.getService();
//        pvoInfo.id = dto.getId();
        pvoInfo.name = dto.getName();
        pvoInfo.host = dto.getHost();
        pvoInfo.isvalid = dto.isIsvalid();
        pvoInfo.path = dto.getPath();
        //卷服务类型,机器类型
        pvoInfo.type = dto.getType();
        UserEntityInfo userInfo = new UserEntityInfo();
        userInfo.setUserName(WebUtil.getCurrentUserId());
        userInfo.setModules("com.vci.client.framework.systemConfig.volumn.PvolumePanel");
        if(pvoInfo.isvalid){
            PvolumeInfo[] allPvolumes = platformClientUtil.getFrameworkService().getAllPvolumes();
            if(allPvolumes.length > 0){
//                将其他卷设置为非首选卷 0
                platformClientUtil.getFrameworkService().updatePvolumeInvalid(userInfo);
            }
        }
        String id= platformClientUtil.getFrameworkService().savePvolume(pvoInfo, userInfo);
        if(StringUtils.isBlank(id)){
            return BaseResult.fail("保存失败!");
        }
        return BaseResult.success("保存成功!");
    }
 
    @Override
    public BaseResult updatePvolume(OsPvolumeDTO dto) throws PLException {
        PvolumeInfo pvoInfo = new PvolumeInfo();
        pvoInfo.service = StringUtils.isBlank(dto.getService()) ? "" : dto.getService();
        pvoInfo.id = StringUtils.isBlank(dto.getId()) ? "" : dto.getId();
        pvoInfo.name = StringUtils.isBlank(dto.getName()) ? "" : dto.getName();
        pvoInfo.host = StringUtils.isBlank(dto.getHost()) ? "" : dto.getHost();
        pvoInfo.isvalid = dto.isIsvalid();
        pvoInfo.path = StringUtils.isBlank(dto.getPath()) ? "" : dto.getPath();
        //卷服务类型,机器类型
        pvoInfo.type = dto.getType();
 
        if(StringUtils.isBlank(pvoInfo.service) || StringUtils.isBlank(pvoInfo.name) || StringUtils.isBlank(pvoInfo.path)){
            throw new PLException("500", new String[]{"字段服务名、卷名称、卷服务路径不能为空!"});
        }
        PvolumeInfo[] allPvo = platformClientUtil.getFrameworkService().getAllPvolumes();
        Map<String, PvolumeInfo> pvInfoMap = Arrays.stream(allPvo).collect(Collectors.toMap(e -> e.id, e -> e));
        //如果是要进行修改,名称不变的情况下可以成功修改,这种情况下不是名称重复。
        if(pvInfoMap.get(pvoInfo.id) == null){
            throw new PLException("500", new String[]{"未查询到相关数据请重新操作!"});
        }
        if ( allPvo.length == 1 && !pvoInfo.isvalid){
            throw new PLException("500", new String[]{"仅一条数据需设置为首选路径"});
        }
        UserEntityInfo userInfo = new UserEntityInfo();
        userInfo.setUserName(WebUtil.getCurrentUserId());
        userInfo.setModules("com.vci.client.framework.systemConfig.volumn.PvolumePanel");
        if(pvoInfo.isvalid && allPvo.length > 0){
//                将其他卷设置为非首选卷 0
            platformClientUtil.getFrameworkService().updatePvolumeInvalid(userInfo);
        }
        boolean b = platformClientUtil.getFrameworkService().updatePvolume(pvoInfo, userInfo);
        if(!b){
            return BaseResult.fail("修改失败!");
        }
        return BaseResult.success("修改成功!");
    }
 
    /**
     * 删除卷
     * @param ids 主键集合
     * @return 删除结果
     */
    @Override
    public BaseResult deletePvolume(String ids) throws PLException {
        if ( StringUtils.isBlank(ids)){
            throw new PLException("500", new String[]{"请选择相关数据!!"});
        }
        for (String id : ids.split(",")) {
            boolean isvalid = platformClientUtil.getFrameworkService().checkIsvalid(id);
            if(isvalid) {
                throw new PLException("500", new String[]{"选择数据为首选路径,不能删除!"});
            }
        }
        UserEntityInfo userInfo = new UserEntityInfo();
        userInfo.setUserName(WebUtil.getCurrentUserId());
        userInfo.setModules("com.vci.client.framework.systemConfig.volumn.PvolumePanel");
        boolean rs = platformClientUtil.getFrameworkService().deletePvolume(ids.split(","), userInfo);
        if(!rs){
            return BaseResult.fail("删除失败!");
        }
        return BaseResult.success("删除成功!");
    }
 
    /**
     * 文件柜导出
     * @param exportFileName 导出的文件名
     * @param pvolumeIds 导出选中的卷id
     * @return
     * @throws PLException
     */
    @Override
    public String exportPvolumes(String exportFileName, String pvolumeIds) throws PLException {
        if(Func.isBlank(pvolumeIds)){
            throw new PLException("500",new String[]{"请勾选要导出的数据!"});
        }
        //界面没传名称,使用默认导出名称
        exportFileName = Func.isBlank(exportFileName) ?  "文件柜导出_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName;
        //设置列名
        List<String> columns = new ArrayList<>(Arrays.asList("卷名","机器类型","路径名称","首选路径","服务器","卷服务"));
 
        //写excel
        String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName +  ".xls";
        try {
            new File(excelPath).createNewFile();
        } catch (Throwable e) {
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e);
        }
        //设置列
        List<WriteExcelData> excelDataList = new ArrayList<>();
        //设置列头
        for (int index = 0; index < columns.size(); index++) {
            excelDataList.add(new WriteExcelData(0,index, columns.get(index)));
        }
        //按照文件柜ID查询文件柜
        List<String> pvolumeIdList = Func.toStrList(pvolumeIds);
        List<PvolumeInfo> pvolumeInfoList = this.getPvolumeInfoByIds(pvolumeIdList);
 
        if(Func.isEmpty(pvolumeInfoList)){
            excelDataList.add(new WriteExcelData(1,1, "未获取到要导出的文件柜信息,请刷新后尝试重新导出!"));
        }else{
            for (int i = 0; i < pvolumeInfoList.size(); i++) {
                PvolumeInfo pvolumeInfo = pvolumeInfoList.get(i);
                excelDataList.add(new WriteExcelData(i+1,0, pvolumeInfo.name));
                excelDataList.add(new WriteExcelData(i+1,1, pvolumeInfo.type));
                excelDataList.add(new WriteExcelData(i+1,2, pvolumeInfo.path));
                excelDataList.add(new WriteExcelData(i+1,3, pvolumeInfo.isvalid));
                excelDataList.add(new WriteExcelData(i+1,4, pvolumeInfo.host));
                excelDataList.add(new WriteExcelData(i+1,5, pvolumeInfo.service));
            }
        }
        WriteExcelOption excelOption = new WriteExcelOption(excelDataList);
        ExcelUtil.writeDataToFile(excelPath, excelOption);
        return excelPath;
    }
 
    /**
     * 根据卷id查询卷
     * @param ids
     * @return
     */
    @Override
    public List<PvolumeInfo> getPvolumeInfoByIds(List<String> ids) throws PLException {
        List<PvolumeInfo> pvolumeInfos = new ArrayList<>();
        if(Func.isEmpty(ids)){
            return pvolumeInfos;
        }
        PvolumeInfo[] allPvolumes = platformClientUtil.getFrameworkService().getAllPvolumes();
        if (Func.isEmpty(allPvolumes)){
            return pvolumeInfos;
        }
        pvolumeInfos = Arrays.stream(allPvolumes).filter(pvolumeInfo -> ids.contains(pvolumeInfo.id)).collect(Collectors.toList());
        return pvolumeInfos;
    }
 
    /**
     * 为文件柜分配成员----这个功能现在没用上,平台的保存方法是注释掉的
     * @param pvolumId
     * @param userIds
     * @return
     */
    @Override
    public boolean savePvolumeUser(String pvolumId ,List<String> userIds) throws PLException {
        VciBaseUtil.alertNotNull(pvolumId,"文件柜主键",userIds,"用户主键集合");
        UserEntityInfo userEntityInfo = new UserEntityInfo();
        userEntityInfo.setUserName(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId());
        boolean resBoolean = platformClientUtil.getFrameworkService().savePvolumeUser(pvolumId, userIds.toArray(new String[userIds.size()]), userEntityInfo);
        return resBoolean;
    }
 
    /**
     * 卷 从Corba端转到Hibernate对象端
     * @param pvoInfo
     * @return
     */
    private OsPvolumeDTO tranferCorbaObjToPvolume(PvolumeInfo pvoInfo) {
        OsPvolumeDTO obj = new OsPvolumeDTO();
        obj.setId(pvoInfo.id);
        obj.setName(pvoInfo.name);
        obj.setHost(pvoInfo.host);
        obj.setService(pvoInfo.service);
        obj.setType(pvoInfo.type);
        obj.setPath(pvoInfo.path);
        obj.setIsvalid(pvoInfo.isvalid);
        return obj;
    }
 
}