com.hn.dingtalk.domain.ProcessModel Maven / Gradle / Ivy
package com.hn.dingtalk.domain;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.dingtalk.api.request.OapiProcessSaveRequest;
import com.hn.dingtalk.annotation.Process;
import com.hn.dingtalk.constant.ProcessFormType;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 描述:
* 审批模板
*
* @author fei
* 2019-07-10 10:19
*/
public class ProcessModel {
/**
* 注意,每种表单组件,对应的componentName是固定的,参照一下示例代码
*/
private List formComponentList = new ArrayList<>();
/**
* 单行文本框
* @param label 表单名称
* @param required 是否必填
* @return {@link ProcessModel}
*/
public ProcessModel singleInput(String label, Boolean required) {
OapiProcessSaveRequest.FormComponentVo singleInput = setBasicProp(
"TextField", label, required);
OapiProcessSaveRequest.FormComponentPropVo singleInputProp = singleInput.getProps();
formComponentList.add(singleInput);
return this;
}
/**
* 多行文本框
* @param label 表单名称
* @param required 是否必填
* @return {@link ProcessModel}
*/
public ProcessModel multipleInput(String label, Boolean required) {
OapiProcessSaveRequest.FormComponentVo multipleInput = setBasicProp(
"TextareaField", label, required);
OapiProcessSaveRequest.FormComponentPropVo multipleInputProp = multipleInput.getProps();
formComponentList.add(multipleInput);
return this;
}
/**
* 金额组件
*
* @param label 表单名称
* @param required 是否必填
* @param notUpper 是否需要大写 默认是需要;
* 1:不需要大写,
* 空或者0:需要大写
* @return {@link ProcessModel}
*/
public ProcessModel moneyComponent(String label, Boolean required, String notUpper) {
OapiProcessSaveRequest.FormComponentVo moneyComponent = setBasicProp(
"MoneyField", label, required);
OapiProcessSaveRequest.FormComponentPropVo moneyComponentProp = moneyComponent.getProps();
// 是否禁用大写
moneyComponentProp.setNotUpper(notUpper);
formComponentList.add(moneyComponent);
return this;
}
/**
* 数字输入框
* @param label 表单组件名称
* @param required 是否必填
* @param unit 数字组件单位属性。
* @return {@link ProcessModel}
*/
public ProcessModel numberComponent(String label, Boolean required, String unit) {
OapiProcessSaveRequest.FormComponentVo numberComponent = setBasicProp(
"NumberField", label, required);
OapiProcessSaveRequest.FormComponentPropVo numberComponentProp = numberComponent.getProps();
numberComponentProp.setUnit(unit);
formComponentList.add(numberComponent);
return this;
}
/**
* 计算公式
* @param label 表单组件名称
* @param required 是否必填
* @return {@link ProcessModel}
*/
public ProcessModel calculateComponent(String label, Boolean required) {
OapiProcessSaveRequest.FormComponentVo calculateComponent = setBasicProp(
"CalculateField", label, required);
OapiProcessSaveRequest.FormComponentPropVo calculateComponentProp = calculateComponent.getProps();
calculateComponentProp.setPlaceholder("自动计算数值");
formComponentList.add(calculateComponent);
return this;
}
/**
* 单选框
* @param label 表单组件名称
* @param required 是否必填
* @param options 单选框的选项 选项最多200项,每项最多50个字
* @return {@link ProcessModel}
*/
public ProcessModel selectComponent(String label, Boolean required, List options) {
OapiProcessSaveRequest.FormComponentVo selectComponent = setBasicProp(
"DDSelectField", label, required);
OapiProcessSaveRequest.FormComponentPropVo selectComponentProp = selectComponent.getProps();
// 选项最多200项,每项最多50个字
selectComponentProp.setOptions(options);
formComponentList.add(selectComponent);
return this;
}
/**
* 多选框
* @param label 表单组件名称
* @param required 是否必填
* @param options 选项值 比如:男 女
* @return {@link ProcessModel}
*/
public ProcessModel multiSelectComponent(String label, Boolean required, List options) {
OapiProcessSaveRequest.FormComponentVo multiSelectComponent = setBasicProp(
"DDMultiSelectField", label, required);
OapiProcessSaveRequest.FormComponentPropVo multiSelectComponentProp = multiSelectComponent.getProps();
multiSelectComponentProp.setOptions(options);
formComponentList.add(multiSelectComponent);
return this;
}
/**
* 日期
* @param label 表单组件名称
* @param required 是否必填
* @param unit 日期区间组件单位属性 只能填小时和天
* @return {@link ProcessModel}
*/
public ProcessModel dateComponent(String label, Boolean required, String unit) {
OapiProcessSaveRequest.FormComponentVo dateComponent = setBasicProp(
"DDDateField", label, required);
OapiProcessSaveRequest.FormComponentPropVo dateComponentProp = dateComponent.getProps();
// 小时或天
dateComponentProp.setUnit(unit);
formComponentList.add(dateComponent);
return this;
}
/**
* 日期区间
*
* @param beginLabel 开始时间表单名称
* @param endLabel 结束时间表单名称
* @param required 是否必填
* @param unit 日期区间组件单位属性 只能填小时和天
* @return {@link ProcessModel}
*/
public ProcessModel dateRangeComponent(String beginLabel, String endLabel, Boolean required, String unit) {
// 日期区间
OapiProcessSaveRequest.FormComponentVo dateRangeComponent = new OapiProcessSaveRequest.FormComponentVo();
dateRangeComponent.setComponentName("DDDateRangeField");
OapiProcessSaveRequest.FormComponentPropVo dateRangeComponentProp = new OapiProcessSaveRequest.FormComponentPropVo();
dateRangeComponentProp.setRequired(required);
dateRangeComponentProp.setLabel(JSONUtil.toJsonStr(Arrays.asList(beginLabel, endLabel)));
dateRangeComponentProp.setPlaceholder("请选择");
// 小时或天
dateRangeComponentProp.setUnit(unit);
dateRangeComponentProp.setNotPrint("1");
dateRangeComponentProp.setId("DDDateRangeField-" + RandomUtil.randomString(8));
dateRangeComponent.setProps(dateRangeComponentProp);
formComponentList.add(dateRangeComponent);
return this;
}
/**
* 关联组件
* @param label 表单组件名称
* @param required 是否必填
* @return {@link ProcessModel}
*/
public ProcessModel relateComponent(String label, Boolean required) {
OapiProcessSaveRequest.FormComponentVo relateComponent = setBasicProp(
"RelateField", label, required);
OapiProcessSaveRequest.FormComponentPropVo relateComponentProp = relateComponent.getProps();
formComponentList.add(relateComponent);
return this;
}
/**
* 图片
* @param label 表单组件名称
* @param required 是否必填
* @return {@link ProcessModel}
*/
public ProcessModel photoComponent(String label, Boolean required) {
OapiProcessSaveRequest.FormComponentVo photoComponent = setBasicProp(
"DDPhotoField", label, required);
OapiProcessSaveRequest.FormComponentPropVo photoComponentProp = photoComponent.getProps();
formComponentList.add(photoComponent);
return this;
}
/**
* 附件
* @param label 表单组件名称
* @param required 是否必填
* @return {@link ProcessModel}
*/
public ProcessModel attachmentComponent(String label, Boolean required) {
OapiProcessSaveRequest.FormComponentVo attachmentComponent = setBasicProp(
"DDAttachment", label, required);
OapiProcessSaveRequest.FormComponentPropVo attachmentComponentProp = attachmentComponent.getProps();
formComponentList.add(attachmentComponent);
return this;
}
/**
* 内部联系人
* @param label 表单组件名称
* @param required 是否必填
* @param choice 是否支持多选 "1" or "0"
* @return {@link ProcessModel}
*/
public ProcessModel innerContactComponent(String label, Boolean required, Long choice) {
OapiProcessSaveRequest.FormComponentVo innerContactComponent = setBasicProp(
"InnerContactField", label, required);
OapiProcessSaveRequest.FormComponentPropVo innerContactComponentProp = innerContactComponent.getProps();
// 是否支持多选 "1" or "0"
innerContactComponentProp.setChoice(choice);
formComponentList.add(innerContactComponent);
return this;
}
/**
* 明细组件
* @param label 表单组件名称
* @param children 明细子组件
* @return {@link ProcessModel}
*/
public ProcessModel tableComponent(String label,List children) {
// 明细组件
OapiProcessSaveRequest.FormComponentVo formComponentVo = new OapiProcessSaveRequest.FormComponentVo();
// 设置组件名称
formComponentVo.setComponentName("TableField");
// 设置组件属性
OapiProcessSaveRequest.FormComponentPropVo prop = new OapiProcessSaveRequest.FormComponentPropVo();
prop.setActionName("增加明细");
prop.setLabel(label);
prop.setId("TableField-"+ RandomUtil.randomString(8));
formComponentVo.setChildren(children);
formComponentVo.setProps(prop);
formComponentList.add(formComponentVo);
return this;
}
/**
* 配置基础属性
* @param componentName 组件类型。每种表单组件有固定的类型
* @param label 表单组件名称
* @param required 是否必填
* @return {@link OapiProcessSaveRequest.FormComponentVo}
*/
private OapiProcessSaveRequest.FormComponentVo setBasicProp(String componentName, String label, Boolean required) {
OapiProcessSaveRequest.FormComponentVo formComponent = new OapiProcessSaveRequest.FormComponentVo();
formComponent.setComponentName(componentName);
OapiProcessSaveRequest.FormComponentPropVo formComponentProp = new OapiProcessSaveRequest.FormComponentPropVo();
// 表单组件id,必须在模板里唯一。有两段字符串组成。第一段为表单的component_name;第二段为8位随机字符串
formComponentProp.setId(componentName + "-" + RandomUtil.randomString(8));
// 是否必填
formComponentProp.setRequired(required);
// 表单名称
formComponentProp.setLabel(label);
// 占位提示(仅输入类组件)
formComponentProp.setPlaceholder("请输入" + label);
// 是否参与打印(1表示不打印, 0表示打印)
formComponentProp.setNotPrint("1");
// 表单属性
formComponent.setProps(formComponentProp);
return formComponent;
}
public List getFormComponentList() {
return formComponentList;
}
/**
* class 转 ProcessModel
*
* @param clazz Class
* @return ProcessModel
*/
public static ProcessModel convert(Class clazz) {
ProcessModel processModel = new ProcessModel();
// 有数据时才处理 得到类的所有field.
Field[] allFields = clazz.getDeclaredFields();
for (int col = 0; col < allFields.length; col++) {
Field field = allFields[col];
Process process = field.getAnnotation(Process.class);
if (process != null) {
// 取得类型
ProcessFormType processFormType = process.formType();
if (ProcessFormType.TextField.equals(processFormType)) {
processModel.singleInput(process.name(), process.required());
} else if (ProcessFormType.TextareaField.equals(processFormType)) {
processModel.multipleInput(process.name(), process.required());
} else if (ProcessFormType.DDMultiSelectField.equals(processFormType)) {
String options = process.options();
List optionList = null;
if (StrUtil.isNotBlank(options)) {
String[] split = options.split(",");
optionList = Arrays.asList(split);
}
processModel.multiSelectComponent(process.name(), process.required(),optionList);
} else if (ProcessFormType.DDDateField.equals(processFormType)) {
processModel.dateComponent(process.name(), process.required(), process.unit());
} else if (ProcessFormType.MoneyField.equals(processFormType)) {
processModel.moneyComponent(process.name(), process.required(), process.unit());
} else if (ProcessFormType.DDAttachment.equals(processFormType)) {
processModel.attachmentComponent(process.name(), process.required());
} else if (ProcessFormType.DDPhotoField.equals(processFormType)) {
processModel.photoComponent(process.name(), process.required());
} else if (ProcessFormType.DDSelectField.equals(processFormType)) {
String options = process.options();
List optionList = null;
if (StrUtil.isNotBlank(options)) {
String[] split = options.split(",");
optionList = Arrays.asList(split);
}
processModel.selectComponent(process.name(), process.required(), optionList);
} else if (ProcessFormType.TableField.equals(processFormType)) {
// 子组件
Class childrenClazz = process.children();
ProcessDetailModel processDetailModel = ProcessDetailModel.convert(childrenClazz);
processModel.tableComponent(process.name(),processDetailModel.getChildren());
}
}
}
return processModel;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy