function AutoConvertUnit(UnitID1,UnitID2:Integer;AConvert
ouble =1;AStrCon:string= '')
ouble;var vstr1,vstr2,vstr_con:string;
vConvert
ouble;
vUnitID:Integer;
begin
result:=0;
vstr1:= 'select * from unitconversion where (unitid_1='+inttostr(UnitID1)+ ' and unitid_2='+ inttostr(UnitID2)+ ') or (unitid_1='+inttostr(UnitID2)+' and unitid_2='+inttostr(UnitID1)+')';
with dm.ExecSqlQuery(vstr1)do
begin
if not IsEmpty then
begin
if FieldByName('unitid_1').AsInteger= UnitID1 then
Result:= fieldbyname('Convert').AsFloat * AConvert;
if FieldByName('unitid_2').AsInteger= UnitID1 then
//这里未做分母不为零的验证,在添加的时候做验证 Result:= (1/fieldbyname('Convert').AsFloat) * AConvert;
Exit;
end;
end;
vstr2:= 'select * from unitconversion where (unitid_1='+inttostr(UnitID1)+ ' or unitid_2='+ inttostr(UnitID1) + AStrCon +')';
with DM.ExecSqlQuery(vstr2)do
begin
First;
while not Eofdo
begin
if FieldByName('unitid_1').AsInteger = UnitID1 then
begin
vConvert:= fieldbyname('Convert').AsFloat;
vUnitID:=FieldByName('unitid_2').AsInteger;
vstr_con:=' and not (UnitID_1='+FieldByName('unitid_1').AsString + ' and unitid_2='+FieldByName('unitid_2').AsString +')';
end else
begin
vConvert:= 1/fieldbyname('Convert').AsFloat;
vUnitID:=FieldByName('unitid_1').AsInteger;
vstr_con:=' and not (UnitID_1='+FieldByName('unitid_2').AsString + ' and unitid_2='+FieldByName('unitid_1').AsString +')';
end;
Result:=AutoConvertUnit(vUnitID,UnitID2,vConvert* AConvert,vstr_con);
if Result <> 0 then
exit;
Next;
end;
end;
end;