火神三层中间件 多表提交代码示例

风神 2021-5-10 374

向数据库提交数据时,需要在 每个数据集 控件属性里填写 表名 以及 主键,如图所示:



核心提交代码在最下面


procedure TfraModuleShangPinAdd.btnSaveClick(Sender: TObject);
const
  ShangPinSql: string = 'select * from bShangPin where 1=2';
  ShangPinImgSql: string = 'select * from bShangPinImg where 1=2';
  ShangPinSKUSql: string = 'select * from bShangPinSKU where 1=2';
  KuCunChangeSql: string = 'select * from dKuCunChange where 1=2';
  KuCunChangeSqlMX: string = 'select * from dKuCunChangeMX where 1=2';
var
  isOpenData: Boolean;
  ShangPinId, vKey, vDanJvId, vMainImgUrl: string;
  vErrMsg: string;
  vShangPinImgItem: TShangPinPhotoClass;
  vSumShuLiang: Integer;
  vQryArray: array of TObject;
begin
  vMainImgUrl := '';
  if FShangPinImglist.Count > 0 then
    vMainImgUrl := FShangPinImglist.Items[0].ImgUrl;

  //商品表
  with QryShangPin do
  begin
    Append;
    ShangPinId := dm.FireConnection1.GetGUID;
    FieldByName('ID').AsString := ShangPinId;
    FieldByName('MC').AsString := edtMC.Text;
    FieldByName('BM').AsString := edtBM.Text;
    FieldByName('ShopId').AsString := dm.ShopID;
    FieldByName('ClassId').AsString := FSelectShangPinClass.Id;
    FieldByName('LingShouJia').AsCurrency := edtLingShouJia.Value;
    FieldByName('ChengBenJia').AsCurrency := edtChengBenJ.Value;
    FieldByName('ImgUrl').AsString := vMainImgUrl;
    Post;
  end;
  //从表:商品图片表
  with QryShangPinImg do
  begin
    for vShangPinImgItem in FShangPinImglist do
    begin
      Append;
      FieldByName('ID').AsString := vShangPinImgItem.Id;
      FieldByName('ImgUrl').AsString := vShangPinImgItem.ImgUrl;
      FieldByName('ShangPinId').AsString := ShangPinId;
      Post;
    end;
  end;
  //从表:商品SKU表 +期初库存
  vSumShuLiang := 0;
  with QryShangPinSKU do
  begin
    vDanJvId := Dm.FireConnection1.GetGUID; //预先创建一个单据表ID
    MemShuLiang.First;
    while not MemShuLiang.eof do
    begin
      Append;
      FieldByName('ID').AsString := Dm.FireConnection1.GetGUID;
      FieldByName('ColorMC').AsString := MemShuLiang.FieldByName('ColorMC').AsString;
      FieldByName('ColorID').AsString := MemShuLiang.FieldByName('ColorID').AsString;
      FieldByName('SizeID').AsString := MemShuLiang.FieldByName('SizeID').AsString;
      FieldByName('SizeMC').AsString := MemShuLiang.FieldByName('SizeMC').AsString;
      FieldByName('ShuLiang').AsInteger := MemShuLiang.FieldByName('ShuLiang').AsInteger;
      FieldByName('TiaoMa').AsString := MemShuLiang.FieldByName('TiaoMa').AsString;
      FieldByName('ShopID').AsString := dm.ShopID;
      FieldByName('ShangPinId').AsString := ShangPinId;
      Post;
      //库存变动明细表
      QryKuCunChangeMX.Append;
      QryKuCunChangeMX.FieldByName('ID').AsString := Dm.FireConnection1.GetGUID;
      QryKuCunChangeMX.FieldByName('SKUId').AsString := FieldByName('ID').AsString;
      QryKuCunChangeMX.FieldByName('ShuLiang').AsInteger := FieldByName('ShuLiang').AsInteger;
      QryKuCunChangeMX.FieldByName('ShopId').AsString := dm.ShopID;
      QryKuCunChangeMX.FieldByName('DanJuId').AsString := ShangPinId;
      QryKuCunChangeMX.Post;

      vSumShuLiang := vSumShuLiang + FieldByName('ShuLiang').AsInteger;
      MemShuLiang.Next;
    end;
  end;
  //从表:库存变动表
  if vSumShuLiang > 0 then
  begin
    with QryKuCunChange do
    begin
      Append;
      FieldByName('ID').AsString := vDanJvId;
      FieldByName('Type').AsString := '期初';
      FieldByName('NO').AsString := '-'; //临时使用
      FieldByName('YuanGongID').AsString := Dm.UserId;
      FieldByName('YuanGongMC').AsString := Dm.UserName;
      FieldByName('ShuLiang').AsInteger := vSumShuLiang;
      FieldByName('ShangPinId').AsString := ShangPinId;
      FieldByName('ShopId').AsString := Dm.ShopID;
      Post;
    end;
    vQryArray := [QryShangPin, QryShangPinImg, QryShangPinSKU, QryKuCunChange, QryKuCunChangeMX];
  end
  else
    vQryArray := [QryShangPin, QryShangPinImg, QryShangPinSKU];
  //火神三层多表提交数据
  if dm.FireConnection1.SaveDatas(vQryArray, vErrMsg) then
    ShowMessage('保存成功')
  else
    ShowMessage(vErrMsg);
end;



DelphiTop论坛申明 1、本网站名称:DelphiTop论坛  网址:www.DelphiTop.com
2、专注于互联网分享精神,专注收藏与分享。你薅网友,我就薅你。
3、本站资源,如发现链接失效,可联系QQ 16643506进行反馈,我们会第一时间更新。
最新回复 (0)
返回