unit U_ComContactSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDBLookupComboBox, ComObj, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxDateRanges, U_BaseHelp, dxBarBuiltInMenu, System.ImageList,
  Vcl.ImgList, dxScrollbarAnnotations, cxImageList, cxContainer, dxSkinsCore,
  dxSkinsDefaultPainters;

type
  FdDy = record
    inc: integer; //客户端套接字句柄
    FDdys: string[32]; //客户端套接字
    FdDysName: string[32]; //客户端套接字
  end;

  TfrmComContactSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    Label3: TLabel;
    CoName: TcxTextEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Label1: TLabel;
    CoCode: TcxTextEdit;
    btnOK: TToolButton;
    ADOConnection1: TADOConnection;
    ImageList1: TImageList;
    cxImageList_bar: TcxImageList;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TV1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure UserIDPropertiesChange(Sender: TObject);
  private

    { Private declarations }
    procedure InitGrid();
    procedure CheckGrid();
  public
    FCoCode: string;
    FMultiple: Boolean;
    FRTUserID, FRTUserName: string;
  end;

var
  frmComContactSel: TfrmComContactSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}
procedure TfrmComContactSel.CheckGrid();
var
  fsj, fsj1: string;
  FStrs: TStringList;
  i: integer;
begin

  if Trim(FRTUserID) <> '' then
  begin
    FStrs := TStringList.Create();
    ExtractStrings([','], [' '], PChar(FRTUserID), FStrs);

    with CDS_1 do
    begin
      First;
      while not eof do
      begin
        if FStrs.IndexOf(Trim(FieldByName('UserID').AsString)) >= 0 then
        begin
          Edit;
          FieldByName('SSel').Value := true;
          Post;
        end;
        Next;
      end;
    end;
    FStrs.Free;
  end;
end;

procedure TfrmComContactSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('select A.*,B.*    ');
      sql.Add('from BS_Company A  ');
      sql.Add('left join  BS_Company_contact B ON A.COID=B.COID   ');

      sql.Add('where A.CoCode=' + QuotedStr(Trim(FCoCode)));

      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
  CheckGrid();
end;

procedure TfrmComContactSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmComContactSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmComContactSel := nil;
end;

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

procedure TfrmComContactSel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption), Tv1, '账户选择');
  Close;
end;

procedure TfrmComContactSel.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv1, '账户选择');

  InitGrid();
end;

procedure TfrmComContactSel.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmComContactSel.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmComContactSel.Tv1DblClick(Sender: TObject);
begin
  btnOK.Click;
end;

procedure TfrmComContactSel.UserIDPropertiesChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmComContactSel.btnOKClick(Sender: TObject);
var
  RTValues: TArray<string>;
begin
  if FMultiple then
  begin
    if not CDS_1.Locate('SSel', true, []) then
    begin
      Application.MessageBox('请选择数据!', '提示', 0);
      Exit;
    end;
  end;
  RTValues := SelCDSKey(CDS_1, ['UserID', 'UserName']);
  FRTUserID := RTValues[0];
  FRTUserName := RTValues[1];
  ModalResult := 1;
end;

end.

