unit U_UserMoreSel;

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, ExtCtrls, DBClient, cxFilter, cxData, cxDataStorage, cxEdit,
  cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, cxTextEdit,
  cxCheckBox;

type
  TfrmUserMoreSel = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel3: TPanel;
    Panel4: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    Panel5: TPanel;
    Label1: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    PState:Integer;
  public
    KHMainId,FYWY,FKHType,FKOType:string;
    { Public declarations }
  end;

var
  frmUserMoreSel: TfrmUserMoreSel;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}



procedure TfrmUserMoreSel.FormDestroy(Sender: TObject);
begin
  frmUserMoreSel:=nil;
end;

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

procedure TfrmUserMoreSel.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmUserMoreSel.ToolButton1Click(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmUserMoreSel.FormShow(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from SY_User A where ');
    sql.Add(' not exists(select * from KH_Owner B where B.UserId=A.UserId ');
    sql.Add('and B.Valid=''Y'' and B.KHMainId='''+Trim(KHMainId)+''')');
    sql.Add(' and isnull(A.USERID,'''')<>''ADMIN'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_Owner A where A.KHMainId='''+Trim(KHMainId)+''' and Valid=''Y'' ');
    sql.Add(' and isnull(KOType,'''')='''+Trim(FKOType)+'''');
    sql.Add(' and isnull(A.USERID,'''')<>''ADMIN'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

procedure TfrmUserMoreSel.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  maxno:String;
begin
  if Trim(FKHType)='KH' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate  KH_Owner  Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
      sql.Add(' where KHMainId='''+Trim(KHMainId)+''' and Valid=''Y'' ');
      sql.Add(' and isnull(KOType,'''')='''+Trim(FKOType)+'''');
      //sql.Add(' and isnull(USERID,'''')<>''ADMIN'' ');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from KH_Owner A where A.KHMainId='''+Trim(KHMainId)+''' and Valid=''Y'' ');
      sql.Add(' and isnull(KOType,'''')='''+Trim(FKOType)+'''');
     // sql.Add(' and isnull(A.USERID,'''')<>''ADMIN'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet2);
    SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'ZD','KH_Owner',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取指定最大号失败!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Owner where 1=2');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('KHMainId').Value:=Trim(KHMainId);
      FieldByName('KOID').Value:=Trim(maxno);
      FieldByName('KOType').Value:=Trim(FKOType);
      FieldByName('UserId').Value:=ClientDataSet1.fieldbyname('UserId').Value;
      FieldByName('UserName').Value:=ClientDataSet1.fieldbyname('UserName').Value;
      FieldByName('FillerCode').Value:=Trim(DCode);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('Valid').Value:='Y';
      Post;
    end;
    with ClientDataSet2 do
    begin
      Append;
      FieldByName('UserId').Value:=ClientDataSet1.fieldbyname('UserId').Value;
      FieldByName('UserName').Value:=ClientDataSet1.fieldbyname('UserName').Value;
      FieldByName('KOID').Value:=Trim(maxno);
      Post;
    end;
    ClientDataSet1.Delete;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update KH_Main Set FZR=dbo.F_Get_HZStr(KH_Main.KHMainId,''FZR'')');
      sql.Add(' where KHMainId='''+Trim(KHMainId)+'''');
      ExecSQL;
    end;
    if Trim(FKOType)='负责人' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from KH_Main where KHMainId='''+Trim(KHMainId)+'''');
        Open;
      end;
      FYWY:=Trim(ADOQueryTemp.fieldbyname('FZR').AsString);
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        if Trim(FKOType)='业务员' then
        begin
          sql.Add('Update KH_Main Set YWY='''+Trim(ClientDataSet2.fieldbyname('UserName').AsString)+'''');
        end else
        if Trim(FKOType)='理单员' then
        begin
          sql.Add('Update KH_Main Set LiDan='''+Trim(ClientDataSet2.fieldbyname('UserName').AsString)+'''');
        end;
        sql.Add(' where KHMainId='''+Trim(KHMainId)+'''');
        ExecSQL;
      end;
      FYWY:=Trim(ClientDataSet2.fieldbyname('UserName').AsString);
    end;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('指定失败!','提示',0);
    Exit;
  end;
end;

procedure TfrmUserMoreSel.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Application.MessageBox('确定要取消负责人吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate  KH_Owner  Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
    sql.Add(' where KOID='''+Trim(ClientDataSet2.fieldbyname('KOID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update KH_Main Set FZR=dbo.F_Get_HZStr(KH_Main.KHMainId,''FZR'')');
    sql.Add(' where KHMainId='''+Trim(KHMainId)+'''');
    ExecSQL;
  end;
  if Trim(FKOType)='负责人' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Main where KHMainId='''+Trim(KHMainId)+'''');
      Open;
    end;
    FYWY:=Trim(ADOQueryTemp.fieldbyname('FZR').AsString);
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      if Trim(FKOType)='业务员' then
      begin
        sql.Add('Update KH_Main Set YWY=Null');
      end else
      if Trim(FKOType)='理单员' then
      begin
        sql.Add('Update KH_Main Set LiDan=Null');
      end;
      sql.Add(' where KHMainId='''+Trim(KHMainId)+'''');
      ExecSQL;
    end;
    FYWY:='';
  end;

  with ClientDataSet1 do
  begin
    Append;
    FieldByName('UserId').Value:=ClientDataSet2.fieldbyname('UserId').Value;
    FieldByName('UserName').Value:=ClientDataSet2.fieldbyname('UserName').Value;
    Post;
  end;
  ClientDataSet2.Delete;
end;

end.
