相同参数的不同 return 值
Different return value for same parameters
我的 grails 应用程序从数据库中的 csv 文件导入数据。每个文件都包含有关办公室的信息及其在给定月份内实现的营业额
用户输入的参数
- 文件位置
- 单一航空公司进口
- 时期
- 合作
用户可以select单个航空公司(导入单个文件)否则,如果该字段留空,即它将导入该位置的所有航空公司
在逐行读取文件的过程中,调用了用于查找办公室与代理机构之间对应关系的服务中的函数。
问题:
如果用户选择导入单个航空公司(params.air != null),函数returns为真值,
但是如果他留下空字段 airline 并且他选择导入所有文件,函数总是 returns null
控制器:
if (params.air=="null")
{
air = Airlines.list()
}
else
{
air = Airlines.get(params.air)
}
def period = Period.get(params.per)
def coo_file = Cooperation.get(params.coo)
air.each
{
def selected_airline = it
// search csv_file for airline selected_airline
....
// read csv_file
cvs_file.toCsvReader(['separatorChar':';']).eachLine { tokens ->
count_tokens++
....
def test_buro = fs.get_valid_buro(tokens[pos_iata],coo_file,period)
println(test_buro)
}
}
服务:
//function
Buro get_valid_buro(String agt,Cooperation coo,Period per)
{
Date per_end = per.end_date
Boolean found = false
Buro selected =null
def sql_query = "SELECT b.id FROM Buro as b,Buro_agency as ba, Agency as a WHERE b.id = ba.buro AND ba.agency = a.id AND a.id = "+Agency.findByNumber_agency(agt)
def data_sql = Data.executeQuery(sql_query)
def bur_serv = []
for(int i=0;i<data_sql.size(); i++)
{
bur_serv.add(Buro.get(data_sql[i]))
}
bur_serv.each{
Date open_date = it.open_date_buro
Date close_date = it.close_date_buro
Cooperation bur_coo = it.cooperation
if((bur_coo == coo && open_date <= per_end )&&(close_date>=per_end|| close_date==null))
{
found=true
selected = it
}
}
我通过在 bur_serv 循环到达循环 if ((bur_coo == coo && open_date <= per )&&(close_date>=per|| close_date==null)) 它传递到下一次迭代,即使条件为真!
怎么了?!
通过我意识到的测试,我发现问题是两个域 class 类型 "Cooperation" 对象之间的比较。
当我测试条件时
if (bur_coo == coo)
它总是 returns false 即使两者是相同的值(具有相同的 id)。
我用谷歌搜索了如何比较两个域 class 我找到了运算符 "equals",我修改了代码
if (bur coo.equals (coo))
{
println("true")
}
else
{
println("false")
}
结果总是错误的。
解决方案
我找到的解决方案是将他们的 id 与
进行比较
if (bur_coo.id == coo.id)
我的 grails 应用程序从数据库中的 csv 文件导入数据。每个文件都包含有关办公室的信息及其在给定月份内实现的营业额 用户输入的参数
- 文件位置
- 单一航空公司进口
- 时期
- 合作
用户可以select单个航空公司(导入单个文件)否则,如果该字段留空,即它将导入该位置的所有航空公司
在逐行读取文件的过程中,调用了用于查找办公室与代理机构之间对应关系的服务中的函数。
问题:
如果用户选择导入单个航空公司(params.air != null),函数returns为真值, 但是如果他留下空字段 airline 并且他选择导入所有文件,函数总是 returns null
控制器:
if (params.air=="null")
{
air = Airlines.list()
}
else
{
air = Airlines.get(params.air)
}
def period = Period.get(params.per)
def coo_file = Cooperation.get(params.coo)
air.each
{
def selected_airline = it
// search csv_file for airline selected_airline
....
// read csv_file
cvs_file.toCsvReader(['separatorChar':';']).eachLine { tokens ->
count_tokens++
....
def test_buro = fs.get_valid_buro(tokens[pos_iata],coo_file,period)
println(test_buro)
}
}
服务:
//function
Buro get_valid_buro(String agt,Cooperation coo,Period per)
{
Date per_end = per.end_date
Boolean found = false
Buro selected =null
def sql_query = "SELECT b.id FROM Buro as b,Buro_agency as ba, Agency as a WHERE b.id = ba.buro AND ba.agency = a.id AND a.id = "+Agency.findByNumber_agency(agt)
def data_sql = Data.executeQuery(sql_query)
def bur_serv = []
for(int i=0;i<data_sql.size(); i++)
{
bur_serv.add(Buro.get(data_sql[i]))
}
bur_serv.each{
Date open_date = it.open_date_buro
Date close_date = it.close_date_buro
Cooperation bur_coo = it.cooperation
if((bur_coo == coo && open_date <= per_end )&&(close_date>=per_end|| close_date==null))
{
found=true
selected = it
}
}
我通过在 bur_serv 循环到达循环 if ((bur_coo == coo && open_date <= per )&&(close_date>=per|| close_date==null)) 它传递到下一次迭代,即使条件为真!
怎么了?!
通过我意识到的测试,我发现问题是两个域 class 类型 "Cooperation" 对象之间的比较。
当我测试条件时
if (bur_coo == coo)
它总是 returns false 即使两者是相同的值(具有相同的 id)。
我用谷歌搜索了如何比较两个域 class 我找到了运算符 "equals",我修改了代码
if (bur coo.equals (coo))
{
println("true")
}
else
{
println("false")
}
结果总是错误的。
解决方案 我找到的解决方案是将他们的 id 与
进行比较if (bur_coo.id == coo.id)