场景:
表单有多种,例如加班、出差、费用申请等,对表单做审批动作时,包含同意、驳回、关闭等操作,所以同一个审批动作必须只有一个入口。
但是:
不同表单在审批时业务不一样,例如,加班表单,在提交加班申请后需要计算加班时长;
出差表单在提交后需要计算出差天数;
显然加班时长和出差天数的字段是不同的,并且需要保存的数据库表也是不同的。
所以:
- 我们可以定义加班套件service、出差套件service
interface FormDataPlugin {
fun delete(data: FormData)
fun processData(data: FormData, form: Form, definition: FormDefinition): ProcessResult
}
// 加班service实现类
@Component
class OvertimeWorkPlugin : FormDataPlugin {
@Autowired
lateinit var repository: OvertimeWorkRepository
@Autowired
private lateinit var checkInRuleRepository: UserCheckInRuleRepository
override fun delete(data: FormData) {
repository.deleteById(data.id)
}
override fun processData(data: FormData, form: Form, definition: FormDefinition): ProcessResult {
val field = definition.children.find { it.componentName == FormFieldType.OvertimeWorkSuit }
if (field != null) {
val formFieldValue = data.value[field.props.id]
val overtime = formFieldValue!! as HrOvertimeWork
overtime.ownerId = getCurrentUser().id
overtime.createTime = Date()
overtime.id = data.id
overtime.state = data.state
validate(overtime)
overtime.duration = getCurrentUserOvertimeWorkDuration(overtime.ownerId!!, overtime.begin!!, overtime.end!!)
if (repository.existsById(overtime.id)) {
repository.updateById(
overtime.id, Update.update(HrOvertimeWork.Q.reason, overtime.reason)
.set(HrOvertimeWork.Q.state, overtime.state)
.set(HrOvertimeWork.Q.begin, overtime.begin)
.set(HrOvertimeWork.Q.end, overtime.end)
.set(HrOvertimeWork.Q.overtimeType, overtime.overtimeType)
.set(HrOvertimeWork.Q.duration, getCurrentUserOvertimeWorkDuration(overtime.ownerId!!, overtime.begin!!, overtime.end!!))
)
} else {
repository.save(overtime)
}
}
return ProcessResult()
}
// 出差service实现类
@Component
class TravelPlugin : FormDataPlugin {
@Autowired
lateinit var travelRepository: TravelRepository
override fun delete(data: FormData) {
travelRepository.deleteById(data.id)
}
override fun processData(data: FormData, form: Form, definition: FormDefinition): ProcessResult {
val field = definition.children.find { it.componentName == FormFieldType.TravelSuit }
// 如果添加了出差套件
if (field != null) {
val formFieldValue = data.value[field.props.id]
val travel: HrTravel = formFieldValue!! as HrTravel
travel.id = data.id
travel.state = data.state
travel.ownerId = getCurrentUser().id
validateDate(travel.begin, travel.end)
travel.travelDays = getWorkDaysByDateInterval(travel.begin!!, travel.end!!)
if (travelRepository.existsById(travel.id)) {
travelRepository.updateById(
travel.id, Update.update(HrTravel.Q.reason, travel.reason)
.set(HrTravel.Q.state, travel.state)
.set(HrTravel.Q.begin, travel.begin)
.set(HrTravel.Q.end, travel.end)
.set(HrTravel.Q.fromCityId, travel.fromCityId)
.set(HrTravel.Q.destinationCityId, travel.destinationCityId)
.set(HrTravel.Q.vehicleId, travel.vehicleId)
.set(HrTravel.Q.travelDays, travel.travelDays)
)
} else {
travelRepository.save(travel)
}
}
return ProcessResult()
}
}
}
此时定义好了加班和出差套件service,审批表单的入口service如下
@Service
@ScheduleCallable(name = "审批表单")
class FormDataServiceImpl : FormDataService {
// 这里是关键,使用Spring的自动注入,将 FormDataPlugin 的实现类全部注入进来
@Autowired(required = false)
private var formDataPlugins: List<FormDataPlugin> = listOf()
// 审批同意、驳回等操作时,遍历我们的套件,执行处理
override fun agree(vo: HandlingOpinionsVO, user: DetailsUser) {
formDataLockService.doWithWriteLock(vo.formDataId) {
val formData = repository.findById(vo.formDataId).get()
mergeFormData(formData, vo.value)
commonDataService.doAgree(formData, vo, user)
doUpdate(formData, vo)
if (formData.dataType == FormDataSubmitType.NormalSubmission) {
formDataPlugins.forEach { plugin ->
plugin.updateState(formData)
}
}
}
}
}
以上代码只做思路记录,为设计表单功能时提供参考