package com.qianwen.smartman.modules.system.service.impl;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.ArrayList;
|
import java.util.Comparator;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.function.Function;
|
import java.util.stream.Collectors;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.context.annotation.Lazy;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.google.common.collect.Lists;
|
import com.jeecg.weibo.exception.BusinessException;
|
import com.qianwen.core.log.exception.ServiceException;
|
import com.qianwen.core.mp.base.BaseServiceImpl;
|
import com.qianwen.core.oss.model.BladeFile;
|
import com.qianwen.core.secure.utils.AuthUtil;
|
import com.qianwen.core.tool.api.R;
|
import com.qianwen.core.tool.utils.FileUtil;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.smartman.common.constant.CommonConstant;
|
import com.qianwen.smartman.common.constant.ExcelConstant;
|
import com.qianwen.smartman.common.utils.ExcelUtils;
|
import com.qianwen.smartman.common.utils.MessageUtils;
|
import com.qianwen.smartman.modules.coproduction.constant.PlanExcelConstant;
|
import com.qianwen.smartman.modules.coproduction.dto.CheckParamDTO;
|
import com.qianwen.smartman.modules.coproduction.dto.CheckResultDTO;
|
import com.qianwen.smartman.modules.coproduction.entity.Plan;
|
import com.qianwen.smartman.modules.coproduction.mapper.PlanMapper;
|
import com.qianwen.smartman.modules.cps.constant.ProductExcelConstant;
|
import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder;
|
import com.qianwen.smartman.modules.resource.convert.AttachConvert;
|
import com.qianwen.smartman.modules.resource.entity.Attach;
|
import com.qianwen.smartman.modules.resource.service.IAttachService;
|
import com.qianwen.smartman.modules.system.convert.CustomTemplateConvert;
|
import com.qianwen.smartman.modules.system.dto.CustomTemplateFieldRelationDTO;
|
import com.qianwen.smartman.modules.system.entity.CustomTemplate;
|
import com.qianwen.smartman.modules.system.entity.CustomTemplateBusiness;
|
import com.qianwen.smartman.modules.system.entity.CustomTemplateField;
|
import com.qianwen.smartman.modules.system.entity.CustomTemplateFieldRelation;
|
import com.qianwen.smartman.modules.system.enums.BusinessTypeEnums;
|
import com.qianwen.smartman.modules.system.enums.ConfigTypeEnum;
|
import com.qianwen.smartman.modules.system.enums.ImportSpecialHandlingEnum;
|
import com.qianwen.smartman.modules.system.mapper.CustomTemplateMapper;
|
import com.qianwen.smartman.modules.system.service.ICustomTemplateBusinessService;
|
import com.qianwen.smartman.modules.system.service.ICustomTemplateFieldRelationService;
|
import com.qianwen.smartman.modules.system.service.ICustomTemplateFieldService;
|
import com.qianwen.smartman.modules.system.service.ICustomTemplateService;
|
import com.qianwen.smartman.modules.system.vo.CustomTemplateFieldRelationSubmitVO;
|
import com.qianwen.smartman.modules.system.vo.CustomTemplateSubmitVO;
|
import com.qianwen.smartman.modules.system.vo.CustomTemplateVO;
|
|
import cn.hutool.json.JSONObject;
|
|
@Service
|
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/system/service/impl/CustomTemplateServiceImpl.class */
|
public class CustomTemplateServiceImpl extends BaseServiceImpl<CustomTemplateMapper, CustomTemplate> implements ICustomTemplateService {
|
@Autowired
|
@Lazy
|
private ICustomTemplateBusinessService customTemplateBusinessService;
|
@Autowired
|
@Lazy
|
private ICustomTemplateFieldService customTemplateFieldService;
|
@Autowired
|
@Lazy
|
private ICustomTemplateFieldRelationService customTemplateFieldRelationService;
|
@Autowired
|
private OssBuilder ossBuilder;
|
@Autowired
|
private IAttachService attachService;
|
@Autowired
|
private PlanMapper planMapper;
|
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public CustomTemplate savePlan(CustomTemplateSubmitVO customTemplateSubmitVO) {
|
if (checkNameIsExist(customTemplateSubmitVO.getId(), customTemplateSubmitVO.getTemplateName(), customTemplateSubmitVO.getBusinessType())) {
|
throw new ServiceException(MessageUtils.message("plan.name.already.exist", new Object[0]));
|
}
|
CustomTemplate customTemplate = CustomTemplateConvert.INSTANCE.convert(customTemplateSubmitVO);
|
this.customTemplateBusinessService.remove(Wrappers.<CustomTemplateBusiness>lambdaQuery().eq(CustomTemplateBusiness::getTemplateId, customTemplate.getId()));
|
/*
|
this.customTemplateBusinessService.remove((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getTemplateId();
|
}, customTemplate.getId()));*/
|
customTemplate.setDefaultTemplate(CommonConstant.DEACTIVATE);
|
save(customTemplate);
|
CustomTemplate customTemplate2 = handleBusinessRelation(customTemplateSubmitVO, customTemplate);
|
customTemplateSubmitVO.setId(customTemplate2.getId());
|
preSetData(customTemplateSubmitVO);
|
return customTemplate2;
|
}
|
|
private void preSetData(CustomTemplateSubmitVO customTemplateSubmitVO) {
|
Long id = customTemplateSubmitVO.getId();
|
Long copySourceId = customTemplateSubmitVO.getCopySourceId();
|
List<Integer> configTypes = customTemplateSubmitVO.getConfigTypes();
|
List<CustomTemplateFieldRelation> customTemplateFieldRelations = new ArrayList<>();
|
if (Func.isEmpty(copySourceId)) {
|
List<CustomTemplateField> list = this.customTemplateFieldService.list(Wrappers.<CustomTemplateField>lambdaQuery()
|
.eq(CustomTemplateField::getSystemField, CommonConstant.ENABLE));
|
/*
|
List<CustomTemplateField> list = this.customTemplateFieldService.list((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getSystemField();
|
}, CommonConstant.ENABLE));*/
|
if (Func.isEmpty(configTypes)) {
|
return;
|
}
|
for (Integer configType : configTypes) {
|
CustomTemplateFieldRelationSubmitVO customTemplateFieldRelationSubmitVO = new CustomTemplateFieldRelationSubmitVO();
|
customTemplateFieldRelationSubmitVO.setTemplateId(id);
|
customTemplateFieldRelationSubmitVO.setFieldList(Lists.newArrayList());
|
customTemplateFieldRelationSubmitVO.setConfigType(configType);
|
if (Func.equals(configType, ConfigTypeEnum.VIEW_PAGE)) {
|
customTemplateFieldRelations.addAll(list.stream().filter(item -> {
|
return Func.equals(item.getViewShow(), CommonConstant.ENABLE);
|
}).map(item2 -> {
|
CustomTemplateFieldRelation field = new CustomTemplateFieldRelation();
|
field.setTemplateId(id);
|
field.setFieldId(item2.getId());
|
field.setSort(Integer.valueOf(list.indexOf(item2)));
|
field.setConfigType(configType);
|
field.setMustField(CommonConstant.DEACTIVATE);
|
return field;
|
}).collect(Collectors.toList()));
|
} else if (Func.equals(configType, ConfigTypeEnum.EDIT_PAGE)) {
|
customTemplateFieldRelations.addAll(list.stream().filter(item3 -> {
|
return Func.equals(item3.getUpdateShow(), CommonConstant.ENABLE);
|
}).map(item4 -> {
|
CustomTemplateFieldRelation field = new CustomTemplateFieldRelation();
|
field.setTemplateId(id);
|
field.setFieldId(item4.getId());
|
field.setSort(Integer.valueOf(list.indexOf(item4)));
|
field.setConfigType(configType);
|
field.setMustField(CommonConstant.DEACTIVATE);
|
return field;
|
}).collect(Collectors.toList()));
|
} else {
|
customTemplateFieldRelations.addAll(list.stream().map(item5 -> {
|
CustomTemplateFieldRelation field = new CustomTemplateFieldRelation();
|
field.setTemplateId(id);
|
field.setFieldId(item5.getId());
|
field.setSort(Integer.valueOf(list.indexOf(item5)));
|
field.setConfigType(configType);
|
field.setMustField(CommonConstant.DEACTIVATE);
|
return field;
|
}).collect(Collectors.toList()));
|
}
|
}
|
} else {
|
List<CustomTemplateFieldRelation> relations = this.customTemplateFieldRelationService.list(Wrappers.<CustomTemplateFieldRelation>lambdaQuery().eq(CustomTemplateFieldRelation::getTemplateId, copySourceId));
|
/*
|
List<CustomTemplateFieldRelation> relations = this.customTemplateFieldRelationService.list((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getTemplateId();
|
}, copySourceId));*/
|
relations.forEach(item6 -> {
|
item6.setId(null);
|
item6.setTemplateId(id);
|
});
|
customTemplateFieldRelations.addAll(relations);
|
}
|
this.customTemplateFieldRelationService.saveBatch(customTemplateFieldRelations);
|
}
|
|
private CustomTemplate handleBusinessRelation(CustomTemplateSubmitVO customTemplateSubmitVO, CustomTemplate customTemplate) {
|
this.customTemplateBusinessService.remove(Wrappers.<CustomTemplateBusiness>lambdaQuery().eq(CustomTemplateBusiness::getTemplateId, customTemplate.getId()));
|
/*
|
this.customTemplateBusinessService.remove((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getTemplateId();
|
}, customTemplate.getId()));*/
|
if (Func.isNotEmpty(customTemplateSubmitVO.getRelationIds())) {
|
List<CustomTemplateBusiness> batchInsert = new ArrayList<>();
|
for (Long relationId : customTemplateSubmitVO.getRelationIds()) {
|
CustomTemplateBusiness customTemplateBusiness = new CustomTemplateBusiness();
|
customTemplateBusiness.setTemplateId(customTemplate.getId());
|
customTemplateBusiness.setRelationId(relationId);
|
customTemplateBusiness.setBusinessType(customTemplate.getBusinessType());
|
batchInsert.add(customTemplateBusiness);
|
}
|
this.customTemplateBusinessService.saveBatch(batchInsert);
|
}
|
return customTemplate;
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public CustomTemplate updatePlan(CustomTemplateSubmitVO customTemplateSubmitVO) {
|
if (checkNameIsExist(customTemplateSubmitVO.getId(), customTemplateSubmitVO.getTemplateName(), customTemplateSubmitVO.getBusinessType())) {
|
throw new ServiceException(MessageUtils.message("plan.name.already.exist", new Object[0]));
|
}
|
CustomTemplate customTemplate = CustomTemplateConvert.INSTANCE.convert(customTemplateSubmitVO);
|
updateById(customTemplate);
|
return handleBusinessRelation(customTemplateSubmitVO, customTemplate);
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
@Transactional(rollbackFor = {Exception.class})
|
public R removePlan(List<Long> ids) {
|
long count = this.planMapper.selectCount(Wrappers.<Plan>lambdaQuery().in(Plan::getTypeId, ids)).longValue();
|
/*
|
long count = this.planMapper.selectCount((Wrapper) Wrappers.lambdaQuery().in((v0) -> {
|
return v0.getTypeId();
|
}, ids)).longValue();*/
|
if (count > 0) {
|
throw new BusinessException(MessageUtils.message("plan.already.use", new Object[0]));
|
}
|
this.customTemplateBusinessService
|
.remove(Wrappers.<CustomTemplateBusiness>lambdaQuery().in(CustomTemplateBusiness::getTemplateId, ids));
|
/*
|
this.customTemplateBusinessService.remove((Wrapper) Wrappers.lambdaQuery().in((v0) -> {
|
return v0.getTemplateId();
|
}, ids));*/
|
this.customTemplateFieldRelationService.removeRelationByTemplateIds(ids);
|
return R.status(removeByIds(ids));
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public List<CustomTemplateVO> listCustomTemplate(Integer businessType) {
|
List<CustomTemplate> list = list(Wrappers.<CustomTemplate>lambdaQuery().eq(CustomTemplate::getBusinessType, businessType)
|
.orderByDesc(CustomTemplate::getId));
|
/*
|
List<CustomTemplate> list = list((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getBusinessType();
|
}, businessType)).orderByDesc((v0) -> {
|
return v0.getId();
|
}));*/
|
if (Func.isEmpty(list)) {
|
return new ArrayList<>();
|
}
|
List<Long> ids = list.stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
|
List<CustomTemplateBusiness> data = this.customTemplateBusinessService.list(Wrappers.<CustomTemplateBusiness>lambdaQuery().in(CustomTemplateBusiness::getTemplateId, ids));
|
/*
|
List<CustomTemplateBusiness> data = this.customTemplateBusinessService.list(Wrappers.lambdaQuery().in((v0) -> {
|
return v0.getTemplateId();
|
}, ids));*/
|
Map<Long, List<Long>> collect = data.stream().collect(Collectors.groupingBy((v0) -> {
|
return v0.getTemplateId();
|
}, Collectors.mapping((v0) -> {
|
return v0.getRelationId();
|
}, Collectors.toList())));
|
List<CustomTemplateVO> convert = CustomTemplateConvert.INSTANCE.convert(list);
|
convert.forEach(item -> {
|
item.setRelationIds(collect.get(item.getId()));
|
});
|
return convert;
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public CustomTemplateVO detail(String id) {
|
List<CustomTemplateBusiness> list = this.customTemplateBusinessService.list(Wrappers.<CustomTemplateBusiness>lambdaQuery().eq(CustomTemplateBusiness::getTemplateId, id));
|
/*
|
List<CustomTemplateBusiness> list = this.customTemplateBusinessService.list((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getTemplateId();
|
}, id));*/
|
CustomTemplate customTemplate = (CustomTemplate) getById(id);
|
CustomTemplateVO convert = CustomTemplateConvert.INSTANCE.convert(customTemplate);
|
if (Func.isNotEmpty(list)) {
|
convert.setRelationIds( list.stream().map((v0) -> {
|
return v0.getRelationId();
|
}).collect(Collectors.toList()));
|
}
|
return convert;
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public BladeFile excelTemplate(Long templateId, String templateName, ExcelUtils.ExcelTemplateDTO excelTemplateDTO) {
|
List<CustomTemplateFieldRelation> relations = this.customTemplateFieldRelationService.list(
|
Wrappers.<CustomTemplateFieldRelation>lambdaQuery()
|
.eq(CustomTemplateFieldRelation::getConfigType, ConfigTypeEnum.NEW_PAGE.getCode())
|
.eq(CustomTemplateFieldRelation::getImportField, CommonConstant.ENABLE)
|
.eq(CustomTemplateFieldRelation::getTemplateId, templateId)
|
.orderByAsc(CustomTemplateFieldRelation::getImportSort));
|
/*
|
List<CustomTemplateFieldRelation> relations = this.customTemplateFieldRelationService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getConfigType();
|
}, ConfigTypeEnum.NEW_PAGE.getCode())).eq((v0) -> {
|
return v0.getImportField();
|
}, CommonConstant.ENABLE)).eq((v0) -> {
|
return v0.getTemplateId();
|
}, templateId)).orderByAsc((v0) -> {
|
return v0.getImportSort();
|
}));*/
|
List<Long> fieldIds = relations.stream().map((v0) -> {
|
return v0.getFieldId();
|
}).collect(Collectors.toList());
|
|
List<CustomTemplateField> fields = this.customTemplateFieldService.list(Wrappers.<CustomTemplateField>lambdaQuery().in(CustomTemplateField::getId, fieldIds));
|
/*
|
List<CustomTemplateField> fields = this.customTemplateFieldService.list((Wrapper) Wrappers.lambdaQuery().in((v0) -> {
|
return v0.getId();
|
}, fieldIds));*/
|
Map<Long, CustomTemplateField> fieldMap = fields.stream().collect(Collectors.toMap((v0) -> {
|
return v0.getId();
|
}, item -> {
|
return item;
|
}));
|
for (CustomTemplateFieldRelation customTemplateFieldRelation : relations) {
|
Long fieldId = customTemplateFieldRelation.getFieldId();
|
CustomTemplateField customTemplateField = fieldMap.get(fieldId);
|
excelTemplateDTO.addTableHead(customTemplateField.getFieldName(), Func.equals(customTemplateFieldRelation.getMustField(), CommonConstant.ENABLE));
|
}
|
Workbook workbook = ExcelUtils.generateTemplate(excelTemplateDTO);
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
try {
|
workbook.write(bos);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
InputStream is = new ByteArrayInputStream(bos.toByteArray());
|
return this.ossBuilder.systemTemplate().putFile(templateName + ExcelConstant.SUFFIX, is);
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public Boolean createTemplate(List<Long> ids, Integer businessType) {
|
buildTemplate(ids, businessType);
|
return true;
|
}
|
|
private Attach buildTemplate(List<Long> ids, Integer businessType) {
|
ExcelUtils.ExcelTemplateDTO excelTemplateDTO = getExcelTemplateDTO(businessType);
|
List<CustomTemplateField> fields = this.customTemplateFieldService.list(Wrappers.<CustomTemplateField>lambdaQuery()
|
.in(CustomTemplateField::getId, ids)
|
.eq(CustomTemplateField::getAddShow, CommonConstant.ENABLE)
|
.eq(CustomTemplateField::getSupportImport, CommonConstant.ENABLE)
|
.eq(CustomTemplateField::getBusinessType, businessType));
|
/*
|
List<CustomTemplateField> fields = this.customTemplateFieldService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().in((v0) -> {
|
return v0.getId();
|
}, ids)).eq((v0) -> {
|
return v0.getAddShow();
|
}, CommonConstant.ENABLE)).eq((v0) -> {
|
return v0.getSupportImport();
|
}, CommonConstant.ENABLE)).eq((v0) -> {
|
return v0.getBusinessType();
|
}, businessType));*/
|
fields.forEach(item -> {
|
item.setImportSort(Integer.valueOf(ids.indexOf(item.getId())));
|
});
|
fields.sort(Comparator.comparing((v0) -> {
|
return v0.getImportSort();
|
}));
|
for (CustomTemplateField customTemplateField : fields) {
|
String importName = ImportSpecialHandlingEnum.getImportName(customTemplateField.getFieldName(), businessType);
|
excelTemplateDTO.addTableHead(importName, Func.equals(customTemplateField.getMustImport(), CommonConstant.ENABLE));
|
}
|
Workbook workbook = ExcelUtils.generateTemplate(excelTemplateDTO);
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
try {
|
workbook.write(bos);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
InputStream is = new ByteArrayInputStream(bos.toByteArray());
|
BusinessTypeEnums businessTypeEnums = BusinessTypeEnums.of(businessType);
|
if (Func.isEmpty(businessTypeEnums)) {
|
return null;
|
}
|
String fileName = businessTypeEnums.getTemplateEnum().getFileName() + ExcelConstant.SUFFIX;
|
BladeFile bladeFile = this.ossBuilder.systemTemplate().putFile(fileName, is);
|
Attach attach = (Attach)this.attachService.getOne(new QueryWrapper<Attach>().lambda().eq(Attach::getOriginalName, fileName));
|
/*
|
Attach attach = (Attach) this.attachService.getOne((Wrapper) new QueryWrapper().lambda().eq((v0) -> {
|
return v0.getOriginalName();
|
}, fileName));*/
|
String fileExtension = FileUtil.getFileExtension(fileName);
|
if (Func.isEmpty(attach)) {
|
attach = new Attach();
|
}
|
attach.setDomainName(bladeFile.getDomain());
|
attach.setLink(bladeFile.getLink());
|
attach.setName(bladeFile.getName());
|
attach.setOriginalName(bladeFile.getOriginalName());
|
attach.setAttachSize(Long.valueOf(bos.size()));
|
attach.setExtension(fileExtension);
|
this.attachService.saveOrUpdate(attach);
|
return attach;
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public BladeFile getTemplate(Integer businessType) {
|
BusinessTypeEnums businessTypeEnums = BusinessTypeEnums.of(businessType);
|
if (Func.isEmpty(businessTypeEnums)) {
|
return null;
|
}
|
String fileName = businessTypeEnums.getTemplateEnum().getFileName() + ExcelConstant.SUFFIX;
|
Attach target = this.attachService.getOne(new QueryWrapper<Attach>().lambda().eq(Attach::getOriginalName, fileName));
|
/*
|
Attach target = (Attach) this.attachService.getOne((Wrapper) new QueryWrapper().lambda().eq((v0) -> {
|
return v0.getOriginalName();
|
}, fileName));*/
|
if (!Func.isEmpty(target)) {
|
return AttachConvert.INSTANCE.convert(target);
|
}
|
List<CustomTemplateField> fields = this.customTemplateFieldService.list(Wrappers.<CustomTemplateField>lambdaQuery()
|
.select(CustomTemplateField::getId, CustomTemplateField::getImportSort, CustomTemplateField::getFieldName )
|
.eq(CustomTemplateField::getAddShow, CommonConstant.ENABLE)
|
.eq(CustomTemplateField::getSupportImport, CommonConstant.ENABLE)
|
.eq(CustomTemplateField::getBusinessType, businessType)
|
.orderByAsc(CustomTemplateField::getImportSort));
|
|
/*
|
List<CustomTemplateField> fields = this.customTemplateFieldService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().select(new SFunction[]{(v0) -> {
|
return v0.getId();
|
}, (v0) -> {
|
return v0.getImportSort();
|
}, (v0) -> {
|
return v0.getFieldName();
|
}}).eq((v0) -> {
|
return v0.getAddShow();
|
}, CommonConstant.ENABLE)).eq((v0) -> {
|
return v0.getSupportImport();
|
}, CommonConstant.ENABLE)).eq((v0) -> {
|
return v0.getBusinessType();
|
}, businessType)).orderByAsc((v0) -> {
|
return v0.getImportSort();
|
}));*/
|
fields.forEach(item -> {
|
if (Func.isEmpty(item.getImportSort())) {
|
Integer systemField = item.getSystemField();
|
if (systemField == null) {
|
systemField = 0;
|
}
|
item.setImportSort(Integer.valueOf(Integer.MAX_VALUE - systemField.intValue()));
|
}
|
});
|
fields.sort(Comparator.comparing((v0) -> {
|
return v0.getImportSort();
|
}));
|
List<Long> ids = (List) fields.stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
Attach attach = buildTemplate(ids, businessType);
|
return AttachConvert.INSTANCE.convert(attach);
|
}
|
|
private ExcelUtils.ExcelTemplateDTO getExcelTemplateDTO(Integer businessType) {
|
ExcelUtils.ExcelTemplateDTO excelTemplateDTO = ExcelUtils.buildExcelTemplateDTO();
|
if (Func.equals(businessType, BusinessTypeEnums.PLAN_ORDER.getCode())) {
|
excelTemplateDTO.appendRemind("填写须知:", false, true);
|
excelTemplateDTO.appendRemind("一. 请勿修改表格结构;", true, false);
|
excelTemplateDTO.appendRemind("二. 标红字段为必填项,黑色字段为选填项;", true, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_4, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_5, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_6, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_7, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_8, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_9, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_10, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_11, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_12, false, false);
|
excelTemplateDTO.appendRemind(PlanExcelConstant.REMIND_13, false, false);
|
} else if (Func.equals(businessType, BusinessTypeEnums.PRODUCT.getCode())) {
|
excelTemplateDTO.appendRemind("填写须知:", false, true);
|
excelTemplateDTO.appendRemind("一. 请勿修改表格结构;", true, false);
|
excelTemplateDTO.appendRemind("二. 标红字段为必填项,黑色字段为选填项;", true, false);
|
excelTemplateDTO.appendRemind(ProductExcelConstant.REMIND_4, false, false);
|
excelTemplateDTO.appendRemind(ProductExcelConstant.REMIND_5, false, false);
|
excelTemplateDTO.appendRemind(ProductExcelConstant.REMIND_6, false, false);
|
excelTemplateDTO.appendRemind(ProductExcelConstant.REMIND_7, false, false);
|
}
|
return excelTemplateDTO;
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public List<CheckResultDTO> checkImport(Function<List<Map<String, String>>, List<CheckParamDTO>> function, Integer businessType, List<Map<String, String>> dataList) {
|
if (Func.isEmpty(dataList)) {
|
throw new ServiceException("无数据");
|
}
|
if (dataList.size() > 200) {
|
throw new ServiceException("最大支持导入200条数据");
|
}
|
Set<String> heads = dataList.get(0).keySet();
|
|
List<CustomTemplateField> fields = this.customTemplateFieldService.list(Wrappers.<CustomTemplateField>lambdaQuery()
|
.eq(CustomTemplateField::getAddShow, CommonConstant.ENABLE)
|
.eq(CustomTemplateField::getSupportImport, CommonConstant.ENABLE)
|
.eq(CustomTemplateField::getBusinessType, businessType));
|
/*
|
List<CustomTemplateField> fields = this.customTemplateFieldService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getAddShow();
|
}, CommonConstant.ENABLE)).eq((v0) -> {
|
return v0.getSupportImport();
|
}, CommonConstant.ENABLE)).eq((v0) -> {
|
return v0.getBusinessType();
|
}, businessType));*/
|
fields.forEach(item -> {
|
if (Func.equals(item.getSystemField(), CommonConstant.ENABLE) && Func.equals(item.getMustImport(), CommonConstant.ENABLE) && !heads.contains(item.getFieldName())) {
|
throw new ServiceException("缺少" + item.getFieldName() + "列请重新下载模板录入");
|
}
|
});
|
Map<String, CustomTemplateField> fieldMap = fields.stream().collect(Collectors.toMap((v0) -> {
|
return v0.getFieldName();
|
}, item2 -> {
|
return item2;
|
}));
|
heads.forEach(importName -> {
|
String fieldName = ImportSpecialHandlingEnum.getFieldName(importName, businessType);
|
if (!Func.equals(importName, ExcelConstant.FAIL_REASON) && !fieldMap.containsKey(fieldName)) {
|
throw new ServiceException(importName + "系统以及拓展字段中不存在");
|
}
|
});
|
List<Map<String, String>> list = reHandleData(businessType, dataList, fieldMap);
|
List<CheckParamDTO> paramDtos = function.apply(list);
|
List<CheckResultDTO> checkResultDTOS = new ArrayList<>();
|
for (int i = 0; i < list.size(); i++) {
|
Map<String, String> data = list.get(i);
|
CheckParamDTO checkParamDTO = paramDtos.get(i);
|
checkResultDTOS.add(checkAndHandleImportData(data, checkParamDTO));
|
}
|
return checkResultDTOS;
|
}
|
|
List<Map<String, String>> reHandleData(Integer businessType, List<Map<String, String>> dataList, Map<String, CustomTemplateField> fieldMap) {
|
List<Map<String, String>> data = new ArrayList<>();
|
dataList.forEach(item -> {
|
HashMap<String, String> hashMap = new HashMap<>();
|
data.add(hashMap);
|
item.forEach((importName, value) -> {
|
String fieldName = ImportSpecialHandlingEnum.getFieldName(importName, businessType);
|
if (fieldMap.containsKey(fieldName)) {
|
CustomTemplateField field = fieldMap.get(fieldName);
|
String fieldCode = field.getFieldCode();
|
hashMap.put(fieldCode, value);
|
}
|
});
|
});
|
return data;
|
}
|
|
private CheckResultDTO checkAndHandleImportData(Map<String, String> data, CheckParamDTO checkParamDTO) {
|
Map<String, CustomTemplateFieldRelationDTO> relationMap = checkParamDTO.getRelationMap();
|
JSONObject extendFieldJson = new JSONObject();
|
CheckResultDTO checkResultDTO = new CheckResultDTO();
|
checkResultDTO.setCode(CommonConstant.ENABLE);
|
CustomTemplate customTemplate = checkParamDTO.getCustomTemplate();
|
Map<String, String> specialData = checkParamDTO.getSpecialData();
|
if (Func.isEmpty(customTemplate)) {
|
checkResultDTO.error("拓展字段模板不存在");
|
}
|
data.forEach((k, v) -> {
|
if (Func.equals(ExcelConstant.FAIL_REASON, k) || !relationMap.containsKey(k)) {
|
return;
|
}
|
CustomTemplateFieldRelationDTO relation = (CustomTemplateFieldRelationDTO) relationMap.get(k);
|
Integer systemField = relation.getSystemField();
|
Integer mustField = relation.getMustField();
|
Integer mustImport = relation.getMustImport();
|
String fieldCode = relation.getFieldCode();
|
String fieldName = relation.getFieldName();
|
if (Func.isNotEmpty(specialData) && specialData.containsKey(k)) {
|
v = (String) specialData.get(k);
|
}
|
if (Func.equals(systemField, CommonConstant.ENABLE)) {
|
if (Func.equals(mustImport, CommonConstant.ENABLE) && Func.isEmpty(v)) {
|
checkResultDTO.setError(fieldName + ExcelConstant.REQUIRED_FAIL_TEMPLATE);
|
checkResultDTO.setCode(CommonConstant.DEACTIVATE);
|
return;
|
}
|
} else if (Func.equals(mustField, CommonConstant.ENABLE) && Func.isEmpty(v)) {
|
checkResultDTO.setError(fieldName + ExcelConstant.REQUIRED_FAIL_TEMPLATE);
|
checkResultDTO.setCode(CommonConstant.DEACTIVATE);
|
return;
|
}
|
if (!Func.equals(systemField, CommonConstant.ENABLE)) {
|
try {
|
Integer businessType = customTemplate.getBusinessType();
|
Object o = this.customTemplateFieldService.fieldCheck(businessType, fieldCode, v);
|
extendFieldJson.put(fieldCode, String.valueOf(o));
|
} catch (RuntimeException e) {
|
checkResultDTO.setError(e.getMessage());
|
checkResultDTO.setCode(CommonConstant.DEACTIVATE);
|
}
|
}
|
});
|
if (Func.equals(checkResultDTO.getCode(), CommonConstant.ENABLE)) {
|
checkResultDTO.setJson(extendFieldJson.toString());
|
}
|
return checkResultDTO;
|
}
|
|
@Override // org.springblade.modules.system.service.ICustomTemplateService
|
public BladeFile excelExport(List<Long> relationIds, String templateName, ExcelUtils.ExcelTemplateDTO excelTemplateDTO) {
|
return null;
|
}
|
|
private boolean checkNameIsExist(Long id, String name, Integer businessType) {
|
List<CustomTemplate> result = this.baseMapper.selectList(Wrappers.<CustomTemplate>query().lambda()
|
.eq(Func.isNotEmpty(AuthUtil.getTenantId()), CustomTemplate::getTenantId, AuthUtil.getTenantId())
|
.eq(Func.isNotBlank(name), CustomTemplate::getTemplateName, name)
|
.eq(Func.isNotEmpty(businessType), CustomTemplate::getBusinessType, businessType)
|
.notIn(Func.isNotEmpty(id), CustomTemplate::getId, new Object[] { id }));
|
/*
|
List<CustomTemplate> result = ((CustomTemplateMapper) this.baseMapper).selectList(Wrappers.query().lambda().eq(Func.isNotEmpty(AuthUtil.getTenantId()), (v0) -> {
|
return v0.getTenantId();
|
}, AuthUtil.getTenantId()).eq(Func.isNotBlank(name), (v0) -> {
|
return v0.getTemplateName();
|
}, name).eq(Func.isNotEmpty(businessType), (v0) -> {
|
return v0.getBusinessType();
|
}, businessType).notIn(Func.isNotEmpty(id), (v0) -> {
|
return v0.getId();
|
}, new Object[]{id}));*/
|
return result != null && result.size() > 0;
|
}
|
}
|