package com.iailab.module.bpm.service.definition; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.json.JsonUtils; import com.iailab.framework.common.util.object.BeanUtils; import com.iailab.module.bpm.controller.admin.definition.vo.form.BpmFormPageReqVO; import com.iailab.module.bpm.controller.admin.definition.vo.form.BpmFormSaveReqVO; import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO; import com.iailab.module.bpm.dal.mysql.definition.BpmFormMapper; import com.iailab.module.bpm.enums.ErrorCodeConstants; import com.iailab.module.bpm.service.definition.dto.BpmFormFieldRespDTO; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.*; import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; /** * 动态表单 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 getFormList() { return formMapper.selectList(); } @Override public List getFormList(Collection ids) { if (CollUtil.isEmpty(ids)) { return Collections.emptyList(); } return formMapper.selectBatchIds(ids); } @Override public PageResult getFormPage(BpmFormPageReqVO pageReqVO) { return formMapper.selectPage(pageReqVO); } /** * 校验 Field,避免 field 重复 * * @param fields field 数组 */ private void validateFields(List fields) { if (true) { // TODO iailab:兼容 Vue3 工作流:因为采用了新的表单设计器,所以暂时不校验 return; } Map 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()); } } }