unit U_UptPlanDataToRoll;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxStyles, cxCustomData, cxFilter, cxData,
  cxDataStorage, cxEdit, cxNavigator, dxDateRanges, dxScrollbarAnnotations,
  Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  Vcl.ComCtrls, Vcl.ToolWin, Data.Win.ADODB, Datasnap.DBClient, cxContainer,
  cxLabel, Vcl.ExtCtrls, cxCheckBox, cxTextEdit;

type
  TfrmUptPlanDataToRoll = class(TForm)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    Tbcgs: TToolButton;
    TBClose: TToolButton;
    TV1ssel: TcxGridDBColumn;
    TV1fieldCaption: TcxGridDBColumn;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    CDS_1fieldName: TStringField;
    CDS_1fieldCaption: TStringField;
    CDS_1fieldTable: TStringField;
    CDS_1ssel: TBooleanField;
    ADOQueryPlan: TADOQuery;
    Panel1: TPanel;
    cxLabel1: TcxLabel;
    cxTextEdit1: TcxTextEdit;
    cxLabel2: TcxLabel;
    AllowEmptyValue: TcxCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
  private
    function updteData():boolean;
  public
    fMainId:string;
    fSubId:string;
  end;

var
  frmUptPlanDataToRoll: TfrmUptPlanDataToRoll;

implementation
uses
  U_dataLink;
{$R *.dfm}



procedure TfrmUptPlanDataToRoll.FormCreate(Sender: TObject);
begin
  CDS_1.Close;
  CDS_1.CreateDataSet;
  cxgrid1.Align:=alClient;
  with CDS_1 do
  begin
    append;
    fieldByName('fieldName').value :='OrderNo';
    fieldByName('fieldCaption').value :='订单号';
    fieldByName('fieldTable').value :='Trade_Plan_main';
    post;
    append;
    fieldByName('fieldName').value :='ConNo';
    fieldByName('fieldCaption').value :='合同号';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='BuyConNo';
    fieldByName('fieldCaption').value :='客户订单号';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='c_Name';
    fieldByName('fieldCaption').value :='品名中文';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='c_EName';
    fieldByName('fieldCaption').value :='品名英文';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='C_Color';
    fieldByName('fieldCaption').value :='颜色';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='C_EColor';
    fieldByName('fieldCaption').value :='英文颜色';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='C_ColorNo';
    fieldByName('fieldCaption').value :='色号';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='C_Pattern';
    fieldByName('fieldCaption').value :='花型';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='FtyPCId';
    fieldByName('fieldCaption').value :='染厂缸号';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='BatchNo';
    fieldByName('fieldCaption').value :='本厂缸号';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='C_StyleNo';
    fieldByName('fieldCaption').value :='款号';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='C_Spec';
    fieldByName('fieldCaption').value :='规格';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='C_Width';
    fieldByName('fieldCaption').value :='门幅(cm)';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
    append;
    fieldByName('fieldName').value :='C_GramWeight';
    fieldByName('fieldCaption').value :='克重(g/㎡)';
    fieldByName('fieldTable').value :='Trade_Plan_sub';
    post;
  end;
end;

procedure TfrmUptPlanDataToRoll.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmUptPlanDataToRoll.TBSaveClick(Sender: TObject);
begin
  if not CDS_1.Locate('ssel',true,[]) then
  begin
    application.MessageBox('请选择要更新的数据项！','警告信息',0);
    exit;
  end;
  if application.MessageBox('您好,确定要同步选择的数据项吗?','警告信息',1)=2  then exit;

  if updteData() then
  begin
    application.MessageBox('同步更新成功!！','警告信息',0);
    modalResult:=1;
  end;
end;

function TfrmUptPlanDataToRoll.updteData():boolean;
var
  i:integer;
  fieldName:string;
begin
  try
     result:=false;

     with ADOQueryPlan do
     begin
       close;
       sql.Clear;
       sql.Add('select A.*,B.* from Trade_Plan_Main A left join Trade_Plan_Sub B on A.MainId=B.MainId ');
       sql.Add('where B.mainId='+quotedstr(fMainId));
       if fSubId<>'' then
         sql.Add('and B.subId='+quotedstr(fSubId));
       open;
     end;
     ADOQueryCmd.Connection.BeginTrans;
     ADOQueryPlan.First;

     while not ADOQueryPlan.eof do
     begin
       with ADOQueryCmd do
       begin
         close;
         sql.clear;
         sql.Add('update Trade_Cloth_Inspect set ');
         ////////////////////////////////
         CDS_1.First;
         i:=0;
         while not CDS_1.eof do
         begin
             if not CDS_1.fieldByName('ssel').AsBoolean then
             begin
               CDS_1.Next;
               continue;
             end;
             fieldName:= trim(CDS_1.fieldByName('fieldName').AsString);
             //值不能为空
             if (not AllowEmptyValue.Checked) and (trim(ADOQueryPlan.FieldByName(fieldName).AsString)='') then
             begin
               CDS_1.Next;
               continue;
             end;
             inc(i);
             if i=1 then
             begin
               sql.Add(fieldName+'='+quotedstr(trim(ADOQueryPlan.FieldByName(fieldName).AsString)));
             end
             else
             begin
               sql.Add(','+fieldName+'='+quotedstr(trim(ADOQueryPlan.FieldByName(fieldName).AsString)));
             end;
           CDS_1.Next;
         end;

         if i=0 then
         begin
           ADOQueryPlan.Next ;
           continue;
         end;

         ///////////////////////////////////
         sql.Add('where MainId='+quotedstr(trim(ADOQueryPlan.FieldByName('MainId').AsString)));
         sql.Add('and SubId='+quotedstr(trim(ADOQueryPlan.FieldByName('SubId').AsString)));
         sql.Add('and mainId<>''''');
         execSql;
       end;
       ADOQueryPlan.Next ;
     end;
     ADOQueryCmd.Connection.CommitTrans;
     result:=true;
  except
    if ADOQueryCmd.Connection.InTransaction then
      ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('数据同步更新操作时发生错误!','警告信息',0);
  end;
end;

end.
