全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1716
推到 Plurk!
推到 Facebook!

COM+元件式交易示範寫法

 
speedup
資深會員


發表:19
回覆:259
積分:280
註冊:2003-07-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-11-19 17:15:45 IP:61.224.xxx.xxx 未訂閱
  本範例程式的目的用來展試示COM 元件式交易的寫法,如何透過協調物件來控制交易達到重用物件的目的    /*設計情境說明*/
1.需求
  在修改訂單時,必須要檢查目前的庫存量是否足夠,否則令該次訂單修改失敗,以確保能夠順利出貨
2.Server Tier 設計
  Server 端主要由訂單元件(Order,支援交易) ,庫存元件(WareHouse,支援交易),協調元件(Coor,必須交易)
  等三個COM 元件構成
2.1 訂單元件(Order,支援交易)
   非常單純的訂單維護元件,只擺標準的Data Access Component(DataSetProvider-ADOQuery-ADOConnection)及作必要的設定
    
2.2 庫存元件(WareHouse,支援交易)
   提供庫存資料維護及下述方法供控制產品庫存量
   function ReduceCount(const product_id: WideString;product_count: Integer): Integer;
   //扣減庫存數量(產品代號,扣減數量):成功傳回0,失敗傳回不足數量      2.3 協調元件(Coor,必須交易)
   協調元件件,負責異動定單並執行扣庫存動作,若扣庫存失敗會將不足數量與產品代號由NotEnoughCount,ProductID傳回
   function MyApplyUpdates(Delta: OleVariant;out NotEnoughCount: Integer; out ProductID: WideString): OleVariant;
   
3.Client Tier設計
  簡單的維護程式主要有兩個畫面分別為訂單維護畫面與庫存維護畫面,使用訂單維護畫面時,請預先設定產品的庫存數量
  執行訂單項目更新時會呼叫 協調元件 MyApplyUpdates方法,若失敗會傳回庫存不足的數量    /*資料庫安裝*/
 執行本程式之前請先在Database Create 兩個表格分別為
 1.order 結構如下
   order_no int            //訂單編號 Prime Key  
   product_id varchar(40)  //產品代號 Prime Key
   order_number int        //訂購數量
   price money             //單價
   
 2.WareHouse 結構如下
   product_id  varchar(40) //產品代號 Prime Key
   product_count  int      //庫存數量
   
 Server端元件與Database連結方式採外部檔案控制,因此請將所附檔案DBConn.udl copy 至C:\底下,並雙擊重新設定Database連結方式 
  
/*關鍵程式碼*/
/~Server Tier~/
//扣減庫存數量(產品代號,扣減數量):成功傳回0,失敗傳回不足數量
function TWarehouse.ReduceCount(const product_id: WideString;
  product_count: Integer): Integer;
 var remainCount:integer;
begin
 try
  adoquery1.SQL.Text := 'select * from WareHouse where product_id = ' QuotedStr(product_id);
  adoquery1.Open;
  adoquery1.First;
  if adoquery1.Eof then adoQuery1.AppendRecord([product_id,0]);
  remainCount :=  adoQuery1['product_count'] -  product_count;
  if remainCount >=0 then
  begin//庫存量足夠應付訂單
   adoQuery1.Edit;
   adoQuery1['product_count'] := remainCount;
   adoQuery1.post;
   SetComplete;
   result := 0;
  end else begin//庫存量不足,設定交易失敗
   SetAbort;
   result :=abs( remainCount);
  end;
 except
  SetAbort;
 end;
end;    //協調元件件,負責異動定單並執行扣庫存動作,若扣庫存失敗會將不足數量與產品代號由NotEnoughCount,ProductID傳回
function TCoor.MyApplyUpdates(Delta: OleVariant;
  out NotEnoughCount: Integer; out ProductID: WideString): OleVariant;
var order:IOrder;
     warehouse:IWarehouse;
     OwnerData:OleVariant;
     cds:TClientDataSet;
     product_id:string;
     product_count:integer;
     wc,ErrorCount:integer;
begin
 order := CoOrder.Create;
 warehouse := CoWarehouse.Create;
 cds :=TClientDataSet.Create(self);
 try
  try
   //=====異動訂單資料=====
   result := order.AS_ApplyUpdates('DataSetProvider1',Delta,0,ErrorCount,OwnerData);       //=====從Delta中解出訂單的異動數量來執行扣庫存動作=====
   cds.Data := Delta;
   cds.First;NotEnoughCount := 0;ProductID := '';
   while not cds.Eof do
   begin
    wc := 0;
    if not cds.FieldByName('product_id').IsNull then
      product_id := cds.FieldByName('product_id').AsString;
    product_count := cds.FieldByName('order_number').AsInteger;
    case cds.UpdateStatus of
     //新增訂單項目故必須扣庫存
     usInserted : wc := warehouse.ReduceCount(product_id,product_count);
     //刪除訂單項目故必須回補庫存
     usDeleted : wc := warehouse.ReduceCount(product_id,-product_count);
     //異動庫存-先回補
     usUnmodified        : wc := warehouse.ReduceCount(product_id,-product_count);
     //異動庫存-後扣
     usModified                :  wc := warehouse.ReduceCount(product_id,product_count);
    end;
    //若產品庫存量不足,終止異動動作
    if wc > 0 then begin
     NotEnoughCount := wc;
     ProductID := product_id;
     SetAbort;
     exit;
    end;
    cds.Next;
   end;
   SetComplete;
  finally
   order := nil;
   warehouse := nil;
   cds.Free;
  end;
 except
  SetAbort;
  raise;
 end;
end; 
       
/~Client Tier~/
procedure TForm1.Button1Click(Sender: TObject);
var r:OleVariant;
    NotEnoughCount:integer;
    ProductID:WideString;
begin
  cdsOrder.CheckBrowseMode;
  if cdsOrder.ChangeCount >0 then
  begin
   r:=scCoor.AppServer.MyApplyUpdates(cdsOrder.Delta,NotEnoughCount,ProductID);
   if NotEnoughCount = 0 then
   begin
    if cdsOrder.Reconcile(r) then
    begin
     cdsOrder.MergeChangeLog;
     Showmessage('訂單變更成功');
    end;
   end else begin
    Showmessage('''' ProductID '''產品庫存量不足' InttoStr(NotEnoughCount) ',請重新調整該項目數量。');
   end;
  end
end;  
混心雜欲 棄修身~唉
------
唉~
附加檔案:40782_CoorDemo.rar
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-11-20 12:00:54 IP:210.61.xxx.xxx 未訂閱
您示範的很棒, 我最近剛好在研究這一部分 有實例受益非淺 能否方便提供BOOKSERVER 可執行此示範程式 DataBase之Schema(資料庫結構) 方便我觀摩學習之用 謝謝您!
speedup
資深會員


發表:19
回覆:259
積分:280
註冊:2003-07-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-11-20 13:56:48 IP:61.224.xxx.xxx 未訂閱
/*資料庫安裝*/ 執行本程式之前請先在Database Create 兩個表格分別為 1.order 結構如下 order_no int //訂單編號 Prime Key product_id varchar(40) //產品代號 Prime Key order_number int //訂購數量 price money //單價 2.WareHouse 結構如下 product_id varchar(40) //產品代號 Prime Key product_count int //庫存數量 混心雜欲 棄修身~唉
------
唉~
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-11-20 14:56:35 IP:210.61.xxx.xxx 未訂閱
謝謝... 原來您已經在文章裡提供 沒有看清楚 是我的疏忽 不好意思 己正常執行,而且 已經看完您示範的整個意思了 受教了
系統時間:2024-07-04 4:18:17
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!