unit U_ColumnSet;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCheckBox, ADODB, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, ComCtrls, ToolWin,
  ImgList, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore,
  dxSkinsDefaultPainters, cxNavigator, dxDateRanges, System.ImageList;

type
  TfrmColumnSet = class(TForm)
    ToolBar1: TToolBar;
    TBTP: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column1: TcxGridDBColumn;
    ClientDataSet1: TClientDataSet;
    ClientDataSet2: TClientDataSet;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    ADOQuery4: TADOQuery;
    ADOLink: TADOConnection;
    ThreeImgList: TImageList;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    FenHongS: TcxStyle;
    procedure FormDestroy(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure SCreateCDS200(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
    procedure SInitCDSData200(fromADO:TADOQuery;toCDS:TclientDataSet);
    function  GetLSNo200(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mFlag:string;
                    mTable:string;
                    mlen:integer;
                    mtype:integer=0):Boolean;
  public
    MKName:String;
    { Public declarations }
  end;

var
  frmColumnSet: TfrmColumnSet;

implementation
uses
  U_DataLink;
{$R *.dfm}

procedure TfrmColumnSet.FormDestroy(Sender: TObject);
begin
  frmColumnSet:=nil;
end;
procedure TfrmColumnSet.SCreateCDS200(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
var
  i:integer;
  mfieldName:string;
  mSize:integer;
begin
  mfieldName:='';
  mClientDataset.FieldDefs.Clear;
  with SADOQry do
  begin
    for i:=0 to fieldCount-1 do     //
    begin
      if (Fields[i].DataType=ftString) and (Fields[i].Size=0) then
      begin
        msize:=1;
      end
      else
        msize:=Fields[i].Size;
      mfieldName:=trim(fields[i].FieldName);
      mClientDataset.FieldDefs.Add(mfieldName,
                                    Fields[i].DataType,msize);

    end;
  end;
  mClientDataset.FieldDefs.Add('Sflag',ftString,1);
  mClientDataset.FieldDefs.Add('Sindex',ftInteger,0);
  mClientDataset.FieldDefs.Add('Ssel',ftBoolean,0);
  mClientDataset.FieldDefs.Add('SDefNote',ftString,10);
  mClientDataset.Close;
  mClientDataset.CreateDataSet;
end;
procedure TfrmColumnSet.SInitCDSData200(fromADO:TADOQuery;toCDS:TclientDataSet);
var
  i:integer;
  k:integer;
begin
  if fromADO.IsEmpty then exit;
  fromADO.first;
  K:=1;
  try
    toCDS.DisableControls;
    toCDS.Filtered:=false;

    while not fromADO.Eof do
    begin
      with toCDS do
      begin
        Append;
        for i:=0 to fromADO.FieldCount-1 do
        begin
          fields[i].value:=fromADO.Fields[i].Value ;
        end;
        fieldByName('Sflag').AsString :='1';
        fieldByName('Sindex').value :=k;
        fieldByName('Ssel').value :=false;
        inc(k);
        Post;
      end;
      fromADO.Next;
    end;
    if not toCDS.IsEmpty then
    begin
      toCDS.First ;
    end;
  finally
    toCDS.EnableControls;
  end;
end;
procedure TfrmColumnSet.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmColumnSet.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmColumnSet.FormShow(Sender: TObject);
begin
  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from SY_User where UserId<>''Admin'' ');
      Open;
    end;
    SCreateCDS200(ADOQuery1,ClientDataSet1);
    SInitCDSData200(ADOQuery1,ClientDataSet1);
  finally
    ADOQuery1.EnableControls;
  end;
end;

procedure TfrmColumnSet.TBTPClick(Sender: TObject);
var
  MaxNo:String;
begin
  if ClientDataSet1.Locate('SSel',True,[])=False then Exit;
  try
    ADOQuery3.Connection.BeginTrans;
    with  ClientDataSet1   do
    begin
      First;
      while not eof do
      begin
        if ClientDataSet1.FieldByName('SSel').AsBoolean=True then
        begin
          with ADOQuery3 do
          begin
            Close;
            sql.Clear;
            sql.Add('Delete Table_Column where CxTabName='''+Trim(ClientDataSet2.fieldbyname('CxTabName').AsString)+'''');
            sql.Add(' and Owner='''+Trim(ClientDataSet1.fieldbyname('UserId').AsString)+'''');
            ExecSQL;
          end;
          with ADOQuery3 do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from Table_Column where 1<>1 ');
            open;
          end;

          with ClientDataSet2  do
          begin
            first;
            while not Eof do
            begin
              with ADOQuery3 do
              begin
                Append;
                if GetLSNo200(ADOQuery4,MaxNo,'CL','Table_Column',4,1)=False then
                begin
                  ADOQuery3.Connection.RollbackTrans;
                  Application.MessageBox('取最大号失败！','提示',0);
                  Exit;
                end;
                FieldByName('TCID').Value:=Trim(MaxNo);
                FieldByName('Owner').Value:=Trim(ClientDataSet1.fieldbyname('UserId').AsString);
                //SSetSaveDataCDSNew(ADOQuery3,Tv2,ClientDataSet2,'Table_Column',0);
                FieldByName('CxTabName').Value:=Trim(ClientDataSet2.fieldbyname('CxTabName').AsString);
                FieldByName('CxColName').Value:=Trim(ClientDataSet2.fieldbyname('CxColName').AsString);
                FieldByName('TCNotVisble').Value:=ClientDataSet2.fieldbyname('TCNotVisble').Value;
                FieldByName('ColName').Value:=Trim(ClientDataSet2.fieldbyname('ColName').AsString);
                Post;
              end;
              Next;
            end;

          end;
        end;
        Next;
      end;
    end;
    ADOQuery3.Connection.CommitTrans;
    Application.MessageBox('保存成功！','提示',0);
  except
    ADOQuery3.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;
function TfrmColumnSet.GetLSNo200(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mFlag:string;
                    mTable:string;
                    mlen:integer;
                    mtype:integer=0):Boolean;
begin
  try
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear ;
      sql.Add('exec Get_SY_MaxBH ');
      sql.Add(' '+quotedStr(mFlag));
      sql.Add(','+quotedStr(mTable));
      sql.Add(','+intTostr(mlen));
      sql.Add(','+intTostr(mtype));
      //ShowMessage(SQL.Text);
      Open;

      if RecordCount>0 then
      begin
        mMaxNo:=trim(fieldByName('MaxBH').AsString) ;
        if mMaxNo<>'' then
          result:=true
        else
          Result:=false;
      end
      else
      begin
        result:=false;
      end;

    end;
    if not Result then
      application.MessageBox(Pchar('无法生成流水号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  Except
    result:=false;
    application.MessageBox(Pchar('无法生成流水号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  end;
end;


procedure TfrmColumnSet.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ADOQuery2.DisableControls;
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Table_Column where CxTabName='''+Trim(ClientDataSet2.fieldbyname('CxTabName').AsString)+'''');
    sql.Add(' and Owner='''+Trim(ClientDataSet1.fieldbyname('UserId').AsString)+'''');
    Open;
  end;
  if ADOQuery2.IsEmpty=False then
  begin
    SCreateCDS200(ADOQuery2,ClientDataSet2);
    SInitCDSData200(ADOQuery2,ClientDataSet2);
  end else
  begin
    while ClientDataSet2.Locate('TCNotVisble',True,[]) do
    begin
      with ClientDataSet2 do
      begin
        Edit;
        FieldByName('TCNotVisble').Value:=0;
        Post;
      end;
    end;
  end;
  ADOQuery2.EnableControls;
end;

procedure TfrmColumnSet.FormCreate(Sender: TObject);
begin
  with ADOLink do
  begin
    if not Connected then
    begin
      Connected:=false;
      ConnectionString:=DConString;
      //LoginPrompt:=false;
      Connected:=true;
    end;
  end;
end;

end.
