算法在两个表之间验证用户的数据,然后插入另一个表

时间:2017-05-12 03:49:12

标签: mysql hibernate grails gorm

问候我需要从用户那里获取详细信息,在用户拥有的详细信息中,我必须验证所有用户详细信息,使用另一个表验证此详细信息,如果日期与表中的插入不匹配,但是如果匹配则不要插入任何东西,这必须为所有用户,域名。

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.groupBy{it.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}"
}

2 个答案:

答案 0 :(得分:1)

基于未完整的详细信息并查看我可以建议的代码: 无需进行映射查询,您只需查询用户列表并检查所有属性,如user.vehicle。无论如何,你需要检查每一行。 groupBy {it.vehicle}不清楚,但如果需要,您可以使用 createCriteria投影“groupProperty”

  1. 创建2个服务方法1,用于迭代所有用户,为每个用户创建一个:

    validateAppointment(User user){
        /* your validation logic */
        ....
       if (validation term){
       Appointments addUserData = new Appointments()
       ...
       }
    }
    validateAppointments(){
        List users = User. list()
        users.each{User user
        validateAppointment(user)
        }
    }
    
  2. 您可以从代码中的任何位置触发 validateAppointments 服务,也可以创建预定作业,以便根据您的需要自动运行。

  3. 如果您的用户列表很大且效率也可以批量更新 - 请查看我的帖子:https://medium.com/meni-lubetkin/grails-bulk-updates-4d749f24cba1

答案 1 :(得分:0)

我建议使用服务创建一个自定义验证器,如下所示:

class User{

    def appointmentService

    ...    
    Date appointmentDate

    static constraints = {
        appointmentDate validator: { val, obj ->
            obj.appointmentService.isDateAppointmentValid(obj.appointmentDate)
        }
    }

}

但请记住,验证可能比您想象的更频繁。它由您期望的validate()save()方法触发(如user guide (v3.1.15)中所述)。因此,我不确定这种情况是否是验证域中àppointmentDate`的最佳方式,因此您必须小心。

希望得到这个帮助。