当前位置:网站首页>自定义组件,并在组件中注入自定义组件实现多种场景的下的组件切换
自定义组件,并在组件中注入自定义组件实现多种场景的下的组件切换
2022-08-04 17:13:00 【合格的程序员】
场景:
表单有多种,例如加班、出差、费用申请等,对表单做审批动作时,包含同意、驳回、关闭等操作,所以同一个审批动作必须只有一个入口。
但是:
不同表单在审批时业务不一样,例如,加班表单,在提交加班申请后需要计算加班时长;
出差表单在提交后需要计算出差天数;
显然加班时长和出差天数的字段是不同的,并且需要保存的数据库表也是不同的。
所以:
- 我们可以定义加班套件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)
}
}
}
}
}
以上代码只做思路记录,为设计表单功能时提供参考
边栏推荐
- taro 滚动组件ScrollView
- el-date-picker 设置时间范围
- Cron表达式
- 容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
- 谷粒商城笔记
- HCIP WPN 实验
- R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,nrow参数指定行的个数、byrow参数指定按照列顺序排布图
- 【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model
- The second step through MySQL in four steps: MySQL index learning
- 域名哪家便宜?怎么买便宜域名?
猜你喜欢
随机推荐
域名哪家便宜?怎么买便宜域名?
【商家联盟】云平台—异业联盟,打造线上线下商业相结合的系统
知乎高赞:拼多多和国家电网,选哪个?
重新审视分布式系统:永远不会有完美的一致性方案……
Digital-intelligent supply chain management system for chemical manufacturing industry: build a smart supply system and empower enterprises to improve production efficiency
码蹄集 - MT2165 - 小码哥的抽卡之旅1
R语言ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、使用ggarrange函数将表格数据和可视化图像组合起来(表格数据在可视化图像下方)
hi, 请问下这是什么问题, 我看官网的example就是mysql的, 咋提示不支持?
【小程序】实现发动态功能
WEB 渗透之SSTI 模板注入
arm交叉编译
安装失败怎么办
机器人示教编程与离线编程的优缺点对比
【图像分类】2021-DeiT
麒麟信安石勇博士荣获openEuler社区年度开源贡献之星
el-date-picker 设置时间范围
WEB 渗透之XXE&XML
NLP未来,路在何方?从学术前沿和业界热点谈起
谷粒商城笔记
最小区间覆盖