unit U_ZDYHelp_YW;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls, cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxTimeEdit;

type
  TfrmZDYHelpYW = class(TForm)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    V1Code: TcxGridDBColumn;
    V1UserName: TcxGridDBColumn;
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    UserName: TEdit;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure UserNameChange(Sender: TObject);
    procedure V1NamePropertiesEditValueChanged(Sender: TObject);
    procedure V1OrderNoPropertiesEditValueChanged(Sender: TObject);
    procedure V1NotePropertiesEditValueChanged(Sender: TObject);
    procedure V1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure V1HelpTypePropertiesEditValueChanged(Sender: TObject);
    procedure V1ZdyStr1PropertiesEditValueChanged(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    flag,flagname,snote,MainType:string;
    fnote,forderno,fZdyFlag,FZdyStr1,ViewFlag,FZdyStr2:Boolean;
    PPSTE:integer;
    { Public declarations }
  end;

var
  frmZDYHelpYW: TfrmZDYHelpYW;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmZDYHelpYW.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  WriteCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  Action:=caFree;
end;

procedure TfrmZDYHelpYW.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* from SY_User A where UDept=''业务部''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmZDYHelpYW.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  Close;
end;

procedure TfrmZDYHelpYW.FormShow(Sender: TObject);
var
  fsj,fsj1:string;
begin
  InitGrid();
  fsj:=Trim(flag)+'01';
  fsj1:=Trim(flagname)+'01';
  ReadCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  frmZDYHelpYW.Caption:=Trim(flagname);
end;

procedure TfrmZDYHelpYW.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  ModalResult:=1;
end;

procedure TfrmZDYHelpYW.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if TV1.OptionsData.Editing=False then
  begin
    ModalResult:=1;
  end;
end;

procedure TfrmZDYHelpYW.UserNameChange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(UserName.Text)<>'' then
  begin
    fsj:=' zdyname like '''+'%'+Trim(UserName.Text)+'%'+''''
         +' or Note like '''+'%'+Trim(UserName.Text)+'%'+''''
         +' or ZJM like '''+'%'+Trim(UserName.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
   // SDofilter(ADOQueryMain,fsj);
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        if Trim(fsj)='' then
        begin
          Filtered:=False;
        end else
        begin
          Filtered:=False;
          Filter:=fsj;
          Filtered:=True;
        end;
      end;
    finally
      ADOQueryMain.EnableControls;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmZDYHelpYW.V1NamePropertiesEditValueChanged(Sender: TObject);
var
  maxno,mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    //Application.MessageBox('名称不能为空！','提示',0);
    Exit;
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('ZdyName').Value:=Trim(mvalue);
    //Post;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from KH_ZDY where ZdyNo='''+Trim(flag)+'''');
      open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('insert into KH_ZDY(ZDYNo,ZDYName,Type,MainType) select :ZDYNo,:ZDYName,:Type,:MainType ');
        Parameters.ParamByName('ZDYNo').Value:=Trim(flag);
        Parameters.ParamByName('ZDYName').Value:=Trim(flagname);
        Parameters.ParamByName('Type').Value:='Main';
        Parameters.ParamByName('MainType').Value:=Trim(MainType);
        ExecSQL;
      end;
    end;
    with ADOQueryCmd do
    begin
      //ClientDataSet1.DisableControls;
      //with ClientDataSet1 do
      //begin
        //First;
        //while not eof do
        //begin
          if Trim(ClientDataSet1.FieldByName('ZDYNO').AsString)='' then
          begin
            if GetLSNo(ADOQueryTemp,maxno,'SY','KH_ZDY',3,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
              Application.MessageBox('取最大编号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.add('select * from KH_Zdy where Type='''+Trim(flag)+'''');
            if Trim(MainType)<>'' then
              SQL.Add(' and MainType='''+Trim(MainType)+'''');
            sql.Add(' and ZdyName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if ADOQueryTemp.RecordCount>1 then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
      
              //ClientDataSet1.EnableControls;
              Application.MessageBox('名称重复！','提示',0);
              Exit;
            end;
            if Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)='' then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
              Application.MessageBox('名称重复！','提示',0);
              Exit;
            end else
            begin
              if Trim(ADOQueryTemp.fieldbyname('ZdyNo').AsString)<>Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString) then
              begin
                ADOQueryCmd.Connection.RollbackTrans;
                //ClientDataSet1.EnableControls;
                Application.MessageBox('名称重复！','提示',0);
                Exit;
              end;
            end;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('delete KH_ZDY where ZDYNO='''+Trim(ClientDataSet1.fieldbyname('ZDYNO').AsString)+'''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from KH_ZDY where 1<>1');
            Open;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('ZDYNo').Value:=Trim(maxno);
          ADOQueryCmd.FieldByName('ZDYName').Value:=ClientDataSet1.fieldbyname('ZDYName').AsString;
          ADOQueryCmd.FieldByName('Note').Value:=ClientDataSet1.fieldbyname('Note').AsString;
          ADOQueryCmd.FieldByName('ZdyStr1').Value:=ClientDataSet1.fieldbyname('ZdyStr1').AsString;
          ADOQueryCmd.FieldByName('ZdyStr2').Value:=ClientDataSet1.fieldbyname('ZdyStr2').AsString;
          //ADOQueryCmd.FieldByName('orderno').Value:=ClientDataSet1.fieldbyname('Name').AsString;
          ADOQueryCmd.FieldByName('Type').Value:=flag;
          ADOQueryCmd.FieldByName('valid').Value:='Y';
          if Trim(MainType)<>'' then
            ADOQueryCmd.FieldByName('MainType').Value:=Trim(MainType);
          //ADOQueryCmd.FieldByName('sel').Value:=0;
          ADOQueryCmd.Post;
          ClientDataSet1.Edit;
          ClientDataSet1.FieldByName('ZDYNo').Value:=Trim(maxno);
          //ClientDataSet1.Post;
         // Next;
        //end;
      //end;
     // ClientDataSet1.EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    //Application.MessageBox('保存成功！','提示',0);
    //TV1.OptionsData.Editing:=False;
    //TV1.OptionsSelection.CellSelect:=False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmZDYHelpYW.V1OrderNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('OrderNo').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set OrderNo='+mvalue);
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmZDYHelpYW.V1NotePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('Note').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set Note='''+Trim(mvalue)+'''');
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmZDYHelpYW.V1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:String;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('ZdyFlag').Value:=StrToInt(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set ZdyFlag='+Trim(mvalue));
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmZDYHelpYW.V1HelpTypePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('HelpType').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set HelpType='''+Trim(mvalue)+'''');
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmZDYHelpYW.V1ZdyStr1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:string;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=TV1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(mvalue)='' then
  begin
    mvalue:='';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName(FFieldName).Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set '+FFieldName+'='''+Trim(mvalue)+'''');
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;


end.
