delphi rest代码工厂使用

delphi rest代码工厂使用

代码工厂所生成的代码,以轻量化、跨平台、跨语言为目的。

支持基础资料(单表)、单据主从表(多表)、一对多的代码生成。

支持JSON和GOOGLE PROTOBUF二种数据序列格式。

配置数据库连接

1)商品资料(基础资料)代码生成

unit rest.tgoods;
//代码由代码工厂自动生成
//2022-07-19
{$I def.inc}
interface

uses
  {$IFDEF firedac}  db.firedac, db.firedacPool, {$ENDIF}
  {$IFDEF unidac}db.unidac, db.unidacpool,  {$ENDIF}
  classes, db, System.NetEncoding,
  system.JSON.Serializers, yn.log, system.JSON, SysUtils, json.help;

type
  Ttgoods = record
    [Serialize(1)] goodsid: string;
    [Serialize(2)] barcode: string;
    [Serialize(3)] goodsname: string;
    [Serialize(4)] pyjm: string;
    [Serialize(5)] gg: string;
    [Serialize(6)] kindid: string;
    [Serialize(7)] jj: double;
    [Serialize(8)] lsj: double;
    [Serialize(9)] kcxx: double;
    [Serialize(10)] kcsx: double;
    [Serialize(11)] unitid: string;
    [Serialize(12)] kindname: string;
    [Serialize(13)] unitname: string;
  end;

  TtgoodsArray = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tgoodss: TArray<Ttgoods>;
  end;

  TRes = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
  end;

function select(url: string; body: TBytes): string;

function insert(url: string; body: TBytes): string;

function update(url: string; body: TBytes): string;

function delete(url: string; body: TBytes): string;

implementation

function select(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: TArray<string>;
  serial: TJsonSerializer;
  rows: TtgoodsArray;
  i: integer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.qry.Close;
      db.qry.SQL.Clear;
      var where: string := '';
      if high(arr) >= 5 then
        where := ' where ' + TNetEncoding.URL.Decode(arr[5]);
      db.qry.SQL.Text := 'select * from tgoods' + where;
      db.qry.Open;
      if db.qry.isempty then
      begin
        rows.status := 0;
        rows.exception := 'No found any data.';
        result := serial.Serialize<TtgoodsArray>(rows);
        exit;
      end;
      SetLength(rows.tgoodss, db.qry.RecordCount);
      db.qry.First;
      i := 0;
      while not db.qry.Eof do
      begin
        rows.tgoodss[i].goodsid := db.qry.fieldbyname('goodsid').asstring;
        rows.tgoodss[i].barcode := db.qry.fieldbyname('barcode').asstring;
        rows.tgoodss[i].goodsname := db.qry.fieldbyname('goodsname').asstring;
        rows.tgoodss[i].pyjm := db.qry.fieldbyname('pyjm').asstring;
        rows.tgoodss[i].gg := db.qry.fieldbyname('gg').asstring;
        rows.tgoodss[i].kindid := db.qry.fieldbyname('kindid').asstring;
        rows.tgoodss[i].jj := db.qry.fieldbyname('jj').AsFloat;
        rows.tgoodss[i].lsj := db.qry.fieldbyname('lsj').AsFloat;
        rows.tgoodss[i].kcxx := db.qry.fieldbyname('kcxx').AsFloat;
        rows.tgoodss[i].kcsx := db.qry.fieldbyname('kcsx').AsFloat;
        rows.tgoodss[i].unitid := db.qry.fieldbyname('unitid').asstring;
        rows.tgoodss[i].kindname := db.qry.fieldbyname('kindname').asstring;
        rows.tgoodss[i].unitname := db.qry.fieldbyname('unitname').asstring;
        inc(i);
        db.qry.Next;
      end;
      rows.status := 1;
      rows.message := 'success';
      result := serial.Serialize<TtgoodsArray>(rows);
    except
      on E: Exception do
      begin
        res.status := 0;
        res.exception := E.message;
        result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function insert(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      var rows: TtgoodsArray;
      rows := serial.Deserialize<TtgoodsArray>(TEncoding.UTF8.GetString(body));
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.startTrans;
      for var row: Ttgoods in rows.tgoodss do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'insert into tgoods (goodsid,barcode,goodsname,pyjm,gg,kindid,jj,lsj,kcxx,kcsx,unitid,kindname,unitname) values (:goodsid,:barcode,:goodsname,:pyjm,:gg,:kindid,:jj,:lsj,:kcxx,:kcsx,:unitid,:kindname,:unitname)';
        db.qry.ParamByName('goodsid').AsString := row.goodsid;
        db.qry.ParamByName('barcode').AsString := row.barcode;
        db.qry.ParamByName('goodsname').AsString := row.goodsname;
        db.qry.ParamByName('pyjm').AsString := row.pyjm;
        db.qry.ParamByName('gg').AsString := row.gg;
        db.qry.ParamByName('kindid').AsString := row.kindid;
        db.qry.ParamByName('jj').AsFloat := row.jj;
        db.qry.ParamByName('lsj').AsFloat := row.lsj;
        db.qry.ParamByName('kcxx').AsFloat := row.kcxx;
        db.qry.ParamByName('kcsx').AsFloat := row.kcsx;
        db.qry.ParamByName('unitid').AsString := row.unitid;
        db.qry.ParamByName('kindname').AsString := row.kindname;
        db.qry.ParamByName('unitname').AsString := row.unitname;
        db.qry.ExecSQL;
      end;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function update(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      var rows: TtgoodsArray;
      rows := serial.Deserialize<TtgoodsArray>(TEncoding.UTF8.GetString(body));
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.startTrans;
      for var row: Ttgoods in rows.tgoodss do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'update tgoods set goodsid=:goodsid,barcode=:barcode,goodsname=:goodsname,pyjm=:pyjm,gg=:gg,kindid=:kindid,jj=:jj,lsj=:lsj,kcxx=:kcxx,kcsx=:kcsx,unitid=:unitid,kindname=:kindname,unitname=:unitname where goodsid=:key0';
        db.qry.ParamByName('goodsid').AsString := row.goodsid;
        db.qry.ParamByName('key0').value := row.goodsid;
        db.qry.ParamByName('barcode').AsString := row.barcode;
        db.qry.ParamByName('goodsname').AsString := row.goodsname;
        db.qry.ParamByName('pyjm').AsString := row.pyjm;
        db.qry.ParamByName('gg').AsString := row.gg;
        db.qry.ParamByName('kindid').AsString := row.kindid;
        db.qry.ParamByName('jj').AsFloat := row.jj;
        db.qry.ParamByName('lsj').AsFloat := row.lsj;
        db.qry.ParamByName('kcxx').AsFloat := row.kcxx;
        db.qry.ParamByName('kcsx').AsFloat := row.kcsx;
        db.qry.ParamByName('unitid').AsString := row.unitid;
        db.qry.ParamByName('kindname').AsString := row.kindname;
        db.qry.ParamByName('unitname').AsString := row.unitname;
        db.qry.ExecSQL;
      end;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function delete(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.qry.Close;
      db.qry.SQL.Clear;
      var where: string := ' where ' + TNetEncoding.URL.Decode(arr[5]);
      db.qry.SQL.Text := 'delete from tgoods' + where;
      db.qry.ExecSQL;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

end.

  2)生成采购订单(主从表单据)代码

 

unit rest.tcgdd,tcgdd2;
//代码由代码工厂自动生成
//2022-07-19
{$I def.inc}
interface

uses
  {$IFDEF firedac}  db.firedac, db.firedacPool, {$ENDIF}
  {$IFDEF unidac}db.unidac, db.unidacpool,  {$ENDIF}
  classes, db, System.NetEncoding,
  system.JSON.Serializers, yn.log, system.JSON, SysUtils, json.help;

type
  Ttcgdd = record
    [Serialize(1)] billid: string;
    [Serialize(2)] adddate: TDateTime;
    [Serialize(3)] supplierid: string;
    [Serialize(4)] suppliername: string;
    [Serialize(5)] warehouseid: string;
    [Serialize(6)] warehousename: string;
    [Serialize(7)] makerid: string;
    [Serialize(8)] note: string;
    [Serialize(9)] shenhe: string;
    [Serialize(10)] maker: string;
  end;

  TtcgddArray = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tcgdds: TArray<Ttcgdd>;
  end;

type
  Ttcgdd2 = record
    [Serialize(1)] billid: string;
    [Serialize(2)] recno: integer;
    [Serialize(3)] goodsid: string;
    [Serialize(4)] goodsname: string;
    [Serialize(5)] gg: string;
    [Serialize(6)] dw: string;
    [Serialize(7)] dj: double;
    [Serialize(8)] qty: double;
    [Serialize(9)] amount: double;
  end;

  Ttcgdd2Array = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tcgdd2s: TArray<Ttcgdd2>;
  end;

  Tmodels = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tcgdds: TArray<Ttcgdd>;
    [Serialize(5)] tcgdd2s: TArray<Ttcgdd2>;
  end;

  TRes = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
  end;

function select(url: string; body: TBytes): string;

function insert(url: string; body: TBytes): string;

function update(url: string; body: TBytes): string;

function delete(url: string; body: TBytes): string;

implementation

function select(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: TArray<string>;
  serial: TJsonSerializer;
  rows: Tmodels;
  i: integer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      var where: string;
      db.qry.Close;
      db.qry.SQL.Clear;
      if high(arr) >= 5 then
        where := ' where ' + TNetEncoding.URL.Decode(arr[5]);
      db.qry.SQL.Text := 'select * from tcgdd' + where;
      db.qry.Open;
      if db.qry.isempty then
      begin
        rows.status := 0;
        rows.exception := 'No found any data.';
        result := serial.Serialize<Tmodels>(rows);
        exit;
      end;
      SetLength(rows.tcgdds, db.qry.RecordCount);
      db.qry.First;
      i := 0;
      while not db.qry.Eof do
      begin
        rows.tcgdds[i].billid := db.qry.fieldbyname('billid').asstring;
        rows.tcgdds[i].adddate := db.qry.fieldbyname('adddate').AsDateTime;
        rows.tcgdds[i].supplierid := db.qry.fieldbyname('supplierid').asstring;
        rows.tcgdds[i].suppliername := db.qry.fieldbyname('suppliername').asstring;
        rows.tcgdds[i].warehouseid := db.qry.fieldbyname('warehouseid').asstring;
        rows.tcgdds[i].warehousename := db.qry.fieldbyname('warehousename').asstring;
        rows.tcgdds[i].makerid := db.qry.fieldbyname('makerid').asstring;
        rows.tcgdds[i].note := db.qry.fieldbyname('note').asstring;
        rows.tcgdds[i].shenhe := db.qry.fieldbyname('shenhe').asstring;
        rows.tcgdds[i].maker := db.qry.fieldbyname('maker').asstring;
        inc(i);
        db.qry.Next;
      end;
      db.qry.Close;
      db.qry.SQL.Clear;
      if high(arr) >= 5 then
        where := ' where ' + TNetEncoding.URL.Decode(arr[6]);
      db.qry.SQL.Text := 'select * from tcgdd2' + where;
      db.qry.Open;
      if db.qry.isempty then
      begin
        rows.status := 0;
        rows.exception := 'No found any data.';
        result := serial.Serialize<Tmodels>(rows);
        exit;
      end;
      SetLength(rows.tcgdd2s, db.qry.RecordCount);
      db.qry.First;
      i := 0;
      while not db.qry.Eof do
      begin
        rows.tcgdd2s[i].billid := db.qry.fieldbyname('billid').asstring;
        rows.tcgdd2s[i].recno := db.qry.fieldbyname('recno').asinteger;
        rows.tcgdd2s[i].goodsid := db.qry.fieldbyname('goodsid').asstring;
        rows.tcgdd2s[i].goodsname := db.qry.fieldbyname('goodsname').asstring;
        rows.tcgdd2s[i].gg := db.qry.fieldbyname('gg').asstring;
        rows.tcgdd2s[i].dw := db.qry.fieldbyname('dw').asstring;
        rows.tcgdd2s[i].dj := db.qry.fieldbyname('dj').AsFloat;
        rows.tcgdd2s[i].qty := db.qry.fieldbyname('qty').AsFloat;
        rows.tcgdd2s[i].amount := db.qry.fieldbyname('amount').AsFloat;
        inc(i);
        db.qry.Next;
      end;
      rows.status := 1;
      rows.message := 'success';
      result := serial.Serialize<Tmodels>(rows);
    except
      on E: Exception do
      begin
        res.status := 0;
        res.exception := E.message;
        result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function insert(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      var rows: Tmodels;
      rows := serial.Deserialize<Tmodels>(TEncoding.UTF8.GetString(body));
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.startTrans;
      for var row: Ttcgdd in rows.tcgdds do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'insert into tcgdd (billid,adddate,supplierid,suppliername,warehouseid,warehousename,makerid,note,shenhe,maker) values (:billid,:adddate,:supplierid,:suppliername,:warehouseid,:warehousename,:makerid,:note,:shenhe,:maker)';
        db.qry.ParamByName('billid').AsString := row.billid;
        db.qry.ParamByName('adddate').AsDateTime := row.adddate;
        db.qry.ParamByName('supplierid').AsString := row.supplierid;
        db.qry.ParamByName('suppliername').AsString := row.suppliername;
        db.qry.ParamByName('warehouseid').AsString := row.warehouseid;
        db.qry.ParamByName('warehousename').AsString := row.warehousename;
        db.qry.ParamByName('makerid').AsString := row.makerid;
        db.qry.ParamByName('note').AsString := row.note;
        db.qry.ParamByName('shenhe').AsString := row.shenhe;
        db.qry.ParamByName('maker').AsString := row.maker;
        db.qry.ExecSQL;
      end;
      for var row: Ttcgdd2 in rows.tcgdd2s do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'insert into tcgdd2 (billid,recno,goodsid,goodsname,gg,dw,dj,qty,amount) values (:billid,:recno,:goodsid,:goodsname,:gg,:dw,:dj,:qty,:amount)';
        db.qry.ParamByName('billid').AsString := row.billid;
        db.qry.ParamByName('recno').AsInteger := row.recno;
        db.qry.ParamByName('goodsid').AsString := row.goodsid;
        db.qry.ParamByName('goodsname').AsString := row.goodsname;
        db.qry.ParamByName('gg').AsString := row.gg;
        db.qry.ParamByName('dw').AsString := row.dw;
        db.qry.ParamByName('dj').AsFloat := row.dj;
        db.qry.ParamByName('qty').AsFloat := row.qty;
        db.qry.ParamByName('amount').AsFloat := row.amount;
        db.qry.ExecSQL;
      end;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function update(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      var rows: Tmodels;
      rows := serial.Deserialize<Tmodels>(TEncoding.UTF8.GetString(body));
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.startTrans;
      for var row: Ttcgdd in rows.tcgdds do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'update tcgdd set billid=:billid,adddate=:adddate,supplierid=:supplierid,suppliername=:suppliername,warehouseid=:warehouseid,warehousename=:warehousename,makerid=:makerid,note=:note,shenhe=:shenhe,maker=:maker where billid=:key0';
        db.qry.ParamByName('billid').AsString := row.billid;
        db.qry.ParamByName('key0').value := row.billid;
        db.qry.ParamByName('adddate').AsDateTime := row.adddate;
        db.qry.ParamByName('supplierid').AsString := row.supplierid;
        db.qry.ParamByName('suppliername').AsString := row.suppliername;
        db.qry.ParamByName('warehouseid').AsString := row.warehouseid;
        db.qry.ParamByName('warehousename').AsString := row.warehousename;
        db.qry.ParamByName('makerid').AsString := row.makerid;
        db.qry.ParamByName('note').AsString := row.note;
        db.qry.ParamByName('shenhe').AsString := row.shenhe;
        db.qry.ParamByName('maker').AsString := row.maker;
        db.qry.ExecSQL;
      end;
      for var row: Ttcgdd2 in rows.tcgdd2s do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'update tcgdd2 set billid=:billid,recno=:recno,goodsid=:goodsid,goodsname=:goodsname,gg=:gg,dw=:dw,dj=:dj,qty=:qty,amount=:amount where billid=:key0 and recno=:key1';
        db.qry.ParamByName('billid').AsString := row.billid;
        db.qry.ParamByName('key0').value := row.billid;
        db.qry.ParamByName('recno').AsInteger := row.recno;
        db.qry.ParamByName('key1').value := row.recno;
        db.qry.ParamByName('goodsid').AsString := row.goodsid;
        db.qry.ParamByName('goodsname').AsString := row.goodsname;
        db.qry.ParamByName('gg').AsString := row.gg;
        db.qry.ParamByName('dw').AsString := row.dw;
        db.qry.ParamByName('dj').AsFloat := row.dj;
        db.qry.ParamByName('qty').AsFloat := row.qty;
        db.qry.ParamByName('amount').AsFloat := row.amount;
        db.qry.ExecSQL;
      end;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function delete(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      var where: string;
      db.startTrans;
      db.qry.Close;
      db.qry.SQL.Clear;
      where := ' where ' + TNetEncoding.URL.Decode(arr[5]);
      db.qry.SQL.Text := 'delete from tcgdd' + where;
      db.qry.ExecSQL;
      db.qry.Close;
      db.qry.SQL.Clear;
      where := ' where ' + TNetEncoding.URL.Decode(arr[6]);
      db.qry.SQL.Text := 'delete from tcgdd2' + where;
      db.qry.ExecSQL;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

end.

3)“一对多”代码生成

sql语句

select a.* from tcgdd a left join twarehouse b on a.warehouseid=b.ckid;select a.* from tcgdd2 a left join tgoods b on a.goodsid=b.goodsid

  

unit rest.;
//代码由代码工厂自动生成
//2022-07-19
{$I def.inc}
interface

uses
  {$IFDEF firedac}  db.firedac, db.firedacPool, {$ENDIF}
  {$IFDEF unidac}db.unidac, db.unidacpool,  {$ENDIF}
  classes, db, System.NetEncoding,
  system.JSON.Serializers, yn.log, system.JSON, SysUtils, json.help;

type
  Ttcgdd = record
    [Serialize(1)] billid: string;
    [Serialize(2)] adddate: TDateTime;
    [Serialize(3)] supplierid: string;
    [Serialize(4)] suppliername: string;
    [Serialize(5)] warehouseid: string;
    [Serialize(6)] warehousename: string;
    [Serialize(7)] makerid: string;
    [Serialize(8)] note: string;
    [Serialize(9)] shenhe: string;
    [Serialize(10)] maker: string;
  end;

  TtcgddArray = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tcgdds: TArray<Ttcgdd>;
  end;

type
  Ttcgdd2 = record
    [Serialize(1)] billid: string;
    [Serialize(2)] recno: integer;
    [Serialize(3)] goodsid: string;
    [Serialize(4)] goodsname: string;
    [Serialize(5)] gg: string;
    [Serialize(6)] dw: string;
    [Serialize(7)] dj: double;
    [Serialize(8)] qty: double;
    [Serialize(9)] amount: double;
  end;

  Ttcgdd2Array = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tcgdd2s: TArray<Ttcgdd2>;
  end;

  Tmodels = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tcgdds: TArray<Ttcgdd>;
    [Serialize(5)] tcgdd2s: TArray<Ttcgdd2>;
  end;

  TRes = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
  end;

function select(url: string; body: TBytes): string;

function insert(url: string; body: TBytes): string;

function update(url: string; body: TBytes): string;

function delete(url: string; body: TBytes): string;

implementation

function select(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: TArray<string>;
  serial: TJsonSerializer;
  rows: Tmodels;
  i: integer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      var where: string;
      db.qry.Close;
      db.qry.SQL.Clear;
      if high(arr) >= 5 then
        where := ' where ' + TNetEncoding.URL.Decode(arr[5]);
      db.qry.SQL.Text := 'select a.* from tcgdd a left join twarehouse b on a.warehouseid=b.ckid' + where;
      db.qry.Open;
      if db.qry.isempty then
      begin
        rows.status := 0;
        rows.exception := 'No found any data.';
        result := serial.Serialize<Tmodels>(rows);
        exit;
      end;
      SetLength(rows.tcgdds, db.qry.RecordCount);
      db.qry.First;
      i := 0;
      while not db.qry.Eof do
      begin
        rows.tcgdds[i].billid := db.qry.fieldbyname('billid').asstring;
        rows.tcgdds[i].adddate := db.qry.fieldbyname('adddate').AsDateTime;
        rows.tcgdds[i].supplierid := db.qry.fieldbyname('supplierid').asstring;
        rows.tcgdds[i].suppliername := db.qry.fieldbyname('suppliername').asstring;
        rows.tcgdds[i].warehouseid := db.qry.fieldbyname('warehouseid').asstring;
        rows.tcgdds[i].warehousename := db.qry.fieldbyname('warehousename').asstring;
        rows.tcgdds[i].makerid := db.qry.fieldbyname('makerid').asstring;
        rows.tcgdds[i].note := db.qry.fieldbyname('note').asstring;
        rows.tcgdds[i].shenhe := db.qry.fieldbyname('shenhe').asstring;
        rows.tcgdds[i].maker := db.qry.fieldbyname('maker').asstring;
        inc(i);
        db.qry.Next;
      end;
      db.qry.Close;
      db.qry.SQL.Clear;
      if high(arr) >= 5 then
        where := ' where ' + TNetEncoding.URL.Decode(arr[6]);
      db.qry.SQL.Text := 'select a.* from tcgdd2 a left join tgoods b on a.goodsid=b.goodsid' + where;
      db.qry.Open;
      if db.qry.isempty then
      begin
        rows.status := 0;
        rows.exception := 'No found any data.';
        result := serial.Serialize<Tmodels>(rows);
        exit;
      end;
      SetLength(rows.tcgdd2s, db.qry.RecordCount);
      db.qry.First;
      i := 0;
      while not db.qry.Eof do
      begin
        rows.tcgdd2s[i].billid := db.qry.fieldbyname('billid').asstring;
        rows.tcgdd2s[i].recno := db.qry.fieldbyname('recno').asinteger;
        rows.tcgdd2s[i].goodsid := db.qry.fieldbyname('goodsid').asstring;
        rows.tcgdd2s[i].goodsname := db.qry.fieldbyname('goodsname').asstring;
        rows.tcgdd2s[i].gg := db.qry.fieldbyname('gg').asstring;
        rows.tcgdd2s[i].dw := db.qry.fieldbyname('dw').asstring;
        rows.tcgdd2s[i].dj := db.qry.fieldbyname('dj').AsFloat;
        rows.tcgdd2s[i].qty := db.qry.fieldbyname('qty').AsFloat;
        rows.tcgdd2s[i].amount := db.qry.fieldbyname('amount').AsFloat;
        inc(i);
        db.qry.Next;
      end;
      rows.status := 1;
      rows.message := 'success';
      result := serial.Serialize<Tmodels>(rows);
    except
      on E: Exception do
      begin
        res.status := 0;
        res.exception := E.message;
        result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function insert(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      var rows: Tmodels;
      rows := serial.Deserialize<Tmodels>(TEncoding.UTF8.GetString(body));
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.startTrans;
      for var row: Ttcgdd in rows.tcgdds do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'insert into tcgdd (billid,adddate,supplierid,suppliername,warehouseid,warehousename,makerid,note,shenhe,maker) values (:billid,:adddate,:supplierid,:suppliername,:warehouseid,:warehousename,:makerid,:note,:shenhe,:maker)';
        db.qry.ParamByName('billid').AsString := row.billid;
        db.qry.ParamByName('adddate').AsDateTime := row.adddate;
        db.qry.ParamByName('supplierid').AsString := row.supplierid;
        db.qry.ParamByName('suppliername').AsString := row.suppliername;
        db.qry.ParamByName('warehouseid').AsString := row.warehouseid;
        db.qry.ParamByName('warehousename').AsString := row.warehousename;
        db.qry.ParamByName('makerid').AsString := row.makerid;
        db.qry.ParamByName('note').AsString := row.note;
        db.qry.ParamByName('shenhe').AsString := row.shenhe;
        db.qry.ParamByName('maker').AsString := row.maker;
        db.qry.ExecSQL;
      end;
      for var row: Ttcgdd2 in rows.tcgdd2s do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'insert into tcgdd2 (billid,recno,goodsid,goodsname,gg,dw,dj,qty,amount) values (:billid,:recno,:goodsid,:goodsname,:gg,:dw,:dj,:qty,:amount)';
        db.qry.ParamByName('billid').AsString := row.billid;
        db.qry.ParamByName('recno').AsInteger := row.recno;
        db.qry.ParamByName('goodsid').AsString := row.goodsid;
        db.qry.ParamByName('goodsname').AsString := row.goodsname;
        db.qry.ParamByName('gg').AsString := row.gg;
        db.qry.ParamByName('dw').AsString := row.dw;
        db.qry.ParamByName('dj').AsFloat := row.dj;
        db.qry.ParamByName('qty').AsFloat := row.qty;
        db.qry.ParamByName('amount').AsFloat := row.amount;
        db.qry.ExecSQL;
      end;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function update(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      var rows: Tmodels;
      rows := serial.Deserialize<Tmodels>(TEncoding.UTF8.GetString(body));
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.startTrans;
      for var row: Ttcgdd in rows.tcgdds do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'update tcgdd set billid=:billid,adddate=:adddate,supplierid=:supplierid,suppliername=:suppliername,warehouseid=:warehouseid,warehousename=:warehousename,makerid=:makerid,note=:note,shenhe=:shenhe,maker=:maker where billid=:key0';
        db.qry.ParamByName('billid').AsString := row.billid;
        db.qry.ParamByName('key0').value := row.billid;
        db.qry.ParamByName('adddate').AsDateTime := row.adddate;
        db.qry.ParamByName('supplierid').AsString := row.supplierid;
        db.qry.ParamByName('suppliername').AsString := row.suppliername;
        db.qry.ParamByName('warehouseid').AsString := row.warehouseid;
        db.qry.ParamByName('warehousename').AsString := row.warehousename;
        db.qry.ParamByName('makerid').AsString := row.makerid;
        db.qry.ParamByName('note').AsString := row.note;
        db.qry.ParamByName('shenhe').AsString := row.shenhe;
        db.qry.ParamByName('maker').AsString := row.maker;
        db.qry.ExecSQL;
      end;
      for var row: Ttcgdd2 in rows.tcgdd2s do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'update tcgdd2 set billid=:billid,recno=:recno,goodsid=:goodsid,goodsname=:goodsname,gg=:gg,dw=:dw,dj=:dj,qty=:qty,amount=:amount where billid=:key0 and recno=:key1';
        db.qry.ParamByName('billid').AsString := row.billid;
        db.qry.ParamByName('key0').value := row.billid;
        db.qry.ParamByName('recno').AsInteger := row.recno;
        db.qry.ParamByName('key1').value := row.recno;
        db.qry.ParamByName('goodsid').AsString := row.goodsid;
        db.qry.ParamByName('goodsname').AsString := row.goodsname;
        db.qry.ParamByName('gg').AsString := row.gg;
        db.qry.ParamByName('dw').AsString := row.dw;
        db.qry.ParamByName('dj').AsFloat := row.dj;
        db.qry.ParamByName('qty').AsFloat := row.qty;
        db.qry.ParamByName('amount').AsFloat := row.amount;
        db.qry.ExecSQL;
      end;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

function delete(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      var where: string;
      db.startTrans;
      db.qry.Close;
      db.qry.SQL.Clear;
      where := ' where ' + TNetEncoding.URL.Decode(arr[5]);
      db.qry.SQL.Text := 'delete from tcgdd' + where;
      db.qry.ExecSQL;
      db.qry.Close;
      db.qry.SQL.Clear;
      where := ' where ' + TNetEncoding.URL.Decode(arr[6]);
      db.qry.SQL.Text := 'delete from tcgdd2' + where;
      db.qry.ExecSQL;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

end.

  

 

posted @ 2022-07-19 08:56  delphi中间件  阅读(468)  评论(0编辑  收藏  举报