package cn.iocoder.yudao.module.bpm.service.definition;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.util.StrUtil;
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormImportExcelVO;
|
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormImportReqVO;
|
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormImportRespVO;
|
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormPageReqVO;
|
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormSaveReqVO;
|
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
|
import cn.iocoder.yudao.module.bpm.dal.mysql.definition.BpmFormMapper;
|
import cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants;
|
import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmFormFieldRespDTO;
|
import jakarta.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.util.*;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
|
|
/**
|
* 动态表单 Service 实现类
|
*
|
* @author 风里雾里
|
*/
|
@Service
|
@Validated
|
public class BpmFormServiceImpl implements BpmFormService {
|
|
@Resource
|
private BpmFormMapper formMapper;
|
|
@Override
|
public Long createForm(BpmFormSaveReqVO createReqVO) {
|
this.validateFields(createReqVO.getFields());
|
// 插入
|
BpmFormDO form = BeanUtils.toBean(createReqVO, BpmFormDO.class);
|
formMapper.insert(form);
|
// 返回
|
return form.getId();
|
}
|
|
@Override
|
public void updateForm(BpmFormSaveReqVO updateReqVO) {
|
validateFields(updateReqVO.getFields());
|
// 校验存在
|
validateFormExists(updateReqVO.getId());
|
// 更新
|
BpmFormDO updateObj = BeanUtils.toBean(updateReqVO, BpmFormDO.class);
|
formMapper.updateById(updateObj);
|
}
|
|
@Override
|
public void deleteForm(Long id) {
|
// 校验存在
|
this.validateFormExists(id);
|
// 删除
|
formMapper.deleteById(id);
|
}
|
|
private void validateFormExists(Long id) {
|
if (formMapper.selectById(id) == null) {
|
throw exception(ErrorCodeConstants.FORM_NOT_EXISTS);
|
}
|
}
|
|
@Override
|
public BpmFormDO getForm(Long id) {
|
return formMapper.selectById(id);
|
}
|
|
@Override
|
public List<BpmFormDO> getFormList() {
|
return formMapper.selectList();
|
}
|
|
@Override
|
public List<BpmFormDO> getFormList(Collection<Long> ids) {
|
if (CollUtil.isEmpty(ids)) {
|
return Collections.emptyList();
|
}
|
return formMapper.selectByIds(ids);
|
}
|
|
@Override
|
public PageResult<BpmFormDO> getFormPage(BpmFormPageReqVO pageReqVO) {
|
return formMapper.selectPage(pageReqVO);
|
}
|
|
/**
|
* 校验 Field,避免 field 重复
|
*
|
* @param fields field 数组
|
*/
|
private void validateFields(List<String> fields) {
|
if (true) { // TODO 芋艿:兼容 Vue3 工作流:因为采用了新的表单设计器,所以暂时不校验
|
return;
|
}
|
Map<String, String> fieldMap = new HashMap<>(); // key 是 vModel,value 是 label
|
for (String field : fields) {
|
BpmFormFieldRespDTO fieldDTO = JsonUtils.parseObject(field, BpmFormFieldRespDTO.class);
|
Assert.notNull(fieldDTO);
|
String oldLabel = fieldMap.put(fieldDTO.getVModel(), fieldDTO.getLabel());
|
// 如果不存在,则直接返回
|
if (oldLabel == null) {
|
continue;
|
}
|
// 如果存在,则报错
|
throw exception(ErrorCodeConstants.FORM_FIELD_REPEAT, oldLabel, fieldDTO.getLabel(), fieldDTO.getVModel());
|
}
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public BpmFormImportRespVO importFormList(List<BpmFormImportExcelVO> importForms, BpmFormImportReqVO importReqVO) {
|
// 1. 校验非空
|
importForms = filterList(importForms, item -> StrUtil.isNotBlank(item.getName()));
|
if (CollUtil.isEmpty(importForms)) {
|
throw exception(ErrorCodeConstants.FORM_IMPORT_LIST_IS_EMPTY);
|
}
|
// 2. 逐条处理
|
BpmFormImportRespVO respVO = BpmFormImportRespVO.builder()
|
.createFormNames(new ArrayList<>())
|
.updateFormNames(new ArrayList<>())
|
.failureFormNames(new LinkedHashMap<>())
|
.build();
|
importForms.forEach(importForm -> {
|
try {
|
// 2.1 按名字匹配已存在记录
|
BpmFormDO existForm = formMapper.selectOne(BpmFormDO::getName, importForm.getName());
|
if (existForm == null) {
|
BpmFormDO form = BeanUtils.toBean(importForm, BpmFormDO.class);
|
form.setFields(parseFields(importForm.getFieldsJson()));
|
formMapper.insert(form);
|
respVO.getCreateFormNames().add(importForm.getName());
|
return;
|
}
|
// 2.2 已存在,判断是否允许更新
|
if (!importReqVO.getUpdateSupport()) {
|
respVO.getFailureFormNames().put(importForm.getName(),
|
StrUtil.format(ErrorCodeConstants.FORM_IMPORT_EXISTS.getMsg(), importForm.getName()));
|
return;
|
}
|
BpmFormDO updateForm = BeanUtils.toBean(importForm, BpmFormDO.class)
|
.setId(existForm.getId());
|
updateForm.setFields(parseFields(importForm.getFieldsJson()));
|
formMapper.updateById(updateForm);
|
respVO.getUpdateFormNames().add(importForm.getName());
|
} catch (ServiceException ex) {
|
respVO.getFailureFormNames().put(importForm.getName(), ex.getMessage());
|
}
|
});
|
return respVO;
|
}
|
|
/**
|
* 解析表单项 JSON 数组字符串
|
*
|
* @param fieldsJson 表单项 JSON 数组字符串
|
* @return 表单项数组
|
*/
|
private List<String> parseFields(String fieldsJson) {
|
if (StrUtil.isBlank(fieldsJson)) {
|
return Collections.emptyList();
|
}
|
return JsonUtils.parseArray(fieldsJson, String.class);
|
}
|
|
}
|