| | |
| | | |
| | | 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.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 实现类 |
| | |
| | | } |
| | | } |
| | | |
| | | @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); |
| | | } |
| | | |
| | | } |