|
@@ -10,9 +10,22 @@ import com.dragoninfo.dcuc.app.facade.IApplyInfoFacade;
|
|
|
import com.dragoninfo.dcuc.app.mapper.ServiceResourceMapper;
|
|
|
import com.dragoninfo.dcuc.app.service.IServiceResourceService;
|
|
|
import com.dragoninfo.dcuc.app.vo.ServiceResourceVo;
|
|
|
+import com.dragoninfo.dcuc.auth.auth.enumresources.ServiceProvideWayEnum;
|
|
|
+import com.dragoninfo.dcuc.auth.auth.enumresources.YesNotEnum;
|
|
|
+import com.dragoninfo.dcuc.duceap.facade.IDuceapUploadFacade;
|
|
|
+import com.dragoninfo.dcuc.duceap.upload.dto.DocContentDTO;
|
|
|
+import com.dragoninfo.duceap.core.response.Result;
|
|
|
+import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
|
|
|
import com.dragonsoft.duceap.base.enums.BooleanEnum;
|
|
|
+import com.dragonsoft.duceap.commons.util.json.JsonUtils;
|
|
|
+import com.dragonsoft.duceap.commons.util.string.StringUtils;
|
|
|
import com.dragonsoft.duceap.core.search.Searchable;
|
|
|
import com.dragonsoft.duceap.mybaitsplus.core.services.BaseMybatisService;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
@@ -20,9 +33,11 @@ import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @author huangzqa
|
|
@@ -32,11 +47,15 @@ import java.util.List;
|
|
|
@Service
|
|
|
public class ServiceResourceServiceImpl extends BaseMybatisService<ServiceResource, String> implements IServiceResourceService {
|
|
|
|
|
|
+
|
|
|
private final ServiceResourceMapper serviceResourceMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private IApplyInfoFacade applyInfoFacade;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private IDuceapUploadFacade uploadhandler;
|
|
|
+
|
|
|
public ServiceResourceServiceImpl(ServiceResourceMapper serviceResourceMapper) {
|
|
|
super(serviceResourceMapper);
|
|
|
this.serviceResourceMapper = serviceResourceMapper;
|
|
@@ -93,6 +112,209 @@ public class ServiceResourceServiceImpl extends BaseMybatisService<ServiceResour
|
|
|
return new PageImpl<>(result, searchable.getPage(), serviceResources.getTotalElements());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 服务资源导入
|
|
|
+ * @param fileId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseStatus impExcel(String fileId) {
|
|
|
+ try {
|
|
|
+ fileId = JsonUtils.parseObject(fileId).getString("fileId");
|
|
|
+ DocContentDTO fileContent = uploadhandler.getFileContent(fileId);
|
|
|
+ byte[] fileData = fileContent.getFileData();
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(fileData);
|
|
|
+ List<ServiceResource> list = loadServiceResourceInfo(inputStream);
|
|
|
+ saveAll(list);
|
|
|
+ }catch (IOException e) {
|
|
|
+ return ResponseStatus.fail("300","导入文件类型错误");
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return ResponseStatus.fail("300",e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return ResponseStatus.fail("300","导入失败");
|
|
|
+ }
|
|
|
+ return ResponseStatus.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建服务资源数组
|
|
|
+ * @param inputStream
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public List<ServiceResource> loadServiceResourceInfo(InputStream inputStream) throws IOException {
|
|
|
+ List<ServiceResource> result = new ArrayList<>();
|
|
|
+ //根据指定的文件输入流导入Excel从而产生Workbook对象
|
|
|
+ Workbook wb0 = new HSSFWorkbook(inputStream);
|
|
|
+ //获取Excel文档中的第一个表单
|
|
|
+ Sheet sht0 = wb0.getSheetAt(0);
|
|
|
+ for (Row r : sht0) {
|
|
|
+ if (r.getRowNum() == 0) {
|
|
|
+ checkRowServiceResourceTitle(r);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ //循环每行的每一列
|
|
|
+ ServiceResource resource = new ServiceResource();
|
|
|
+ for (int i = 0; i < 8; i++) {
|
|
|
+ Cell cell = r.getCell(i);
|
|
|
+ checkServiceResourceCellData(cell, i, r.getRowNum());
|
|
|
+ checkServiceResourceCellDataValid(cell, i, r.getRowNum(), resource);
|
|
|
+ }
|
|
|
+ resource.setCreateTime(new Date());
|
|
|
+ resource.setDeleted(YesNotEnum.NO.getValue());
|
|
|
+ result.add(resource);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验参数和设置参数
|
|
|
+ * @param cell
|
|
|
+ * @param num
|
|
|
+ * @param row
|
|
|
+ * @param resource
|
|
|
+ */
|
|
|
+ private void checkServiceResourceCellDataValid(Cell cell, int num, int row, ServiceResource resource) {
|
|
|
+ if (cell == null)
|
|
|
+ return;
|
|
|
+ String value = cell.getStringCellValue();
|
|
|
+ switch (num) {
|
|
|
+ case 0:
|
|
|
+ if (value.length() > 100) {
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务资源标识符''长度大于100!");
|
|
|
+ }
|
|
|
+ resource.setServiceCode(value);
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ if (value.length() > 500) {
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务资源名称''长度大于500!");
|
|
|
+ }
|
|
|
+ resource.setServiceName(value);
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ if (value.length() > 20) {
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务类型''长度大于20!");
|
|
|
+ }
|
|
|
+ resource.setServiceType(value);
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ //TODO 判断服务提供方式的枚举
|
|
|
+ String wayCode = "";
|
|
|
+ for (ServiceProvideWayEnum wayEnum: ServiceProvideWayEnum.values()){
|
|
|
+ if (value.equals(wayEnum.getLabel())){
|
|
|
+ wayCode = wayEnum.getValue();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(wayCode) ){
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务提供方式''填写不正确!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (wayCode.length() > 20) {
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务提供方式''长度大于20!");
|
|
|
+ }
|
|
|
+
|
|
|
+ resource.setServiceProvideWay(wayCode);
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ if (StringUtils.isNotBlank(value)){
|
|
|
+ String code = "";
|
|
|
+ for (YesNotEnum yesNotEnum: YesNotEnum.values()){
|
|
|
+ if (value.equals(yesNotEnum.getLabel())){
|
|
|
+ code = yesNotEnum.getValue();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //是否自用服务
|
|
|
+ resource.setMustSelf(code);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ if (value.length() > 500) {
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务访问地址''长度大于500!");
|
|
|
+ }
|
|
|
+ resource.setServiceUrl(value);
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ //判断是否为正确的应用
|
|
|
+ ApplyInfo applyInfo = applyInfoFacade.getOneByName(value);
|
|
|
+ if (applyInfo == null){
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务提供应用系统名称''不存在!");
|
|
|
+ }
|
|
|
+ resource.setAppCode(applyInfo.getApplyCode());
|
|
|
+ break;
|
|
|
+ case 7:
|
|
|
+ resource.setRemark(value);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验数据是否为空
|
|
|
+ * @param cell
|
|
|
+ * @param i
|
|
|
+ * @param row
|
|
|
+ */
|
|
|
+ private static void checkServiceResourceCellData(Cell cell, int i, int row) {
|
|
|
+ if (!(cell == null || StringUtils.isBlank(cell.getStringCellValue()))) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ switch (row) {
|
|
|
+ case 0:
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务资源标识''为空!");
|
|
|
+ case 1:
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务资源名称''为空!");
|
|
|
+ case 2:
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务类型''为空!");
|
|
|
+ case 3:
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务提供方式''为空!");
|
|
|
+ case 5:
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务访问地址''为空!");
|
|
|
+ case 6:
|
|
|
+ throw new NumberFormatException("第" + (row + 1) + "行''服务提供应用系统名称''为空!");
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验列头
|
|
|
+ * @param r
|
|
|
+ */
|
|
|
+ private static void checkRowServiceResourceTitle(Row r) {
|
|
|
+ if (!r.getCell(0).getStringCellValue().equals("服务资源标识")) {
|
|
|
+ throw new NumberFormatException("缺少''服务资源标识''列!");
|
|
|
+ }
|
|
|
+ if (!r.getCell(1).getStringCellValue().equals("服务资源名称")) {
|
|
|
+ throw new NumberFormatException("缺少''服务资源名称''列!");
|
|
|
+ }
|
|
|
+ if (!r.getCell(2).getStringCellValue().equals("服务类型")) {
|
|
|
+ throw new NumberFormatException("缺少''服务类型''列!");
|
|
|
+ }
|
|
|
+ if (!r.getCell(3).getStringCellValue().equals("服务提供方式")) {
|
|
|
+ throw new NumberFormatException("缺少''服务提供方式''列!");
|
|
|
+ }
|
|
|
+ if (!r.getCell(4).getStringCellValue().equals("是否自用服务")) {
|
|
|
+ throw new NumberFormatException("缺少''是否自用服务''列!");
|
|
|
+ }
|
|
|
+ if (!r.getCell(5).getStringCellValue().equals("服务访问地址")) {
|
|
|
+ throw new NumberFormatException("缺少''服务访问地址''列!");
|
|
|
+ }
|
|
|
+ if (!r.getCell(6).getStringCellValue().equals("服务提供应用系统名称")) {
|
|
|
+ throw new NumberFormatException("缺少''服务提供应用系统名称''列!");
|
|
|
+ }
|
|
|
+ if (!r.getCell(7).getStringCellValue().equals("服务资源描述")) {
|
|
|
+ throw new NumberFormatException("缺少''服务资源描述''列!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设置应用名称
|
|
|
* @param serviceResourceVo
|