算法在两个表之间验证来自用户的数据,然后插入另一个表
Posted
技术标签:
【中文标题】算法在两个表之间验证来自用户的数据,然后插入另一个表【英文标题】:algorithm verifying data from user beween two tables then insert into another table 【发布时间】:2017-10-11 06:10:11 【问题描述】:问候我需要从用户那里获取详细信息,在用户拥有的这些详细信息中,我必须验证所有用户详细信息使用另一个表验证此详细信息,如果日期不匹配,则插入表中,但如果匹配则不要插入任何东西,这必须为所有用户、域完成。
User
String orderNumber
String dealer
Int UserKm
String dateUser
String adviser
Vehicle vehicle
String dateCreated
Date appointmentDate //this date has to be validated with DateNext
appointmentDate from Appointments domain of it doesn’t exit then you can
insert on that table.
Appointments
User user
Date managementDate
Date lasDataApointies
DateNext appointmentDate
Date NextdAteAppointment
Date callDate
String observations
def result = User.executeQuery("""select new map(
mmt.id as id, mmt.orderNumber as orderNumber, mmt.dealer.dealer as
dealer, mmt.UserKm as UserKm, mmt.dateUser as dateUser, mmt.adviser as
adviser, mmt.technician as technician, mmt.vehicle.placa as vehicle,
mmt.dateCreated as dateCreated, mmt.currenKm as currenKm) from User as
mmt """)
def result1=result.groupByit.vehicle
List detailsReslt=[]
result1?.each SlasDataApointing placa, listing ->
def firsT = listing.first()
int firstKM = firsT.UserKm
def lasT = listing.last()
def lasDataApoint = lasT.id
int lastKM = lasT.UserKm
int NextAppointmentKM = lastKM + 5000
int dayBetweenLastAndNext = lastKM - NextAppointmentKM
def tiDur = getDifference(firsT.dateUser,lasT.dateUser)
int dayToInt = tiDur.days
int restar = firstKM - lastKM
int kmPerDay = restar.div(dayToInt)
int nextMaintenaceDays = dayBetweenLastAndNext.div(kmPerDay)
def nextAppointment = lasT.dateUser + nextMaintenaceDays
detailsReslt<<[placa:placa, nextAppointment:
nextAppointment, manageId:lasDataApoint, nextKmUser: NextAppointmentKM]
detailsReslt?.each
Appointments addUserData = new Appointments()
addUserData.User = User.findById(it.manageId)
addUserData.managementDate = null
addUserData.NextdAteAppointment = null
addUserData.observations = null
addUserData.callDate = it.nextAppointment
addUserData.save(flush: true)
println "we now have $detailsReslt"
【问题讨论】:
很难理解您尝试实现的逻辑,但我建议创建一个 grails 服务类并开始编写一些代码来操作您上面提到的实体。 【参考方案1】:我建议使用服务创建自定义验证器,如下所示:
class User
def appointmentService
...
Date appointmentDate
static constraints =
appointmentDate validator: val, obj ->
obj.appointmentService.isDateAppointmentValid(obj.appointmentDate)
但请记住,验证运行的频率可能比您想象的要高。如您所料,它由validate()
和save()
方法触发(如user guide (v3.1.15) 中所述)。所以我不确定这种情况是否是在您的域中验证 àappointmentDate` 的最佳方式,因此您必须小心。
希望对您有所帮助。
【讨论】:
【参考方案2】:基于不完整的细节并查看我可以建议的代码:
无需查询映射,您只需查询用户列表并检查所有属性,如user.vehicle
。无论如何,您需要检查每一行。
groupByit.vehicle 不清楚,但如果需要,您可以使用 createCriteria projections "groupProperty"
创建 2 种服务方法,一种用于迭代所有用户,一种用于每个用户:
validateAppointment(User user)
/* your validation logic */
....
if (validation term)
Appointments addUserData = new Appointments()
...
validateAppointments()
List users = User. list()
users.eachUser user
validateAppointment(user)
您可以从代码中的任何位置触发 validateAppointments 服务,或创建计划作业,以便它根据您的需要自动运行。
如果您的用户列表很大并且为了提高效率,您可以进行批量更新 - 请查看我的帖子:https://medium.com/meni-lubetkin/grails-bulk-updates-4d749f24cba1
【讨论】:
以上是关于算法在两个表之间验证来自用户的数据,然后插入另一个表的主要内容,如果未能解决你的问题,请参考以下文章
使用存储过程(SQL + C#)将数据插入一个表,然后将多行插入另一个表