unit U_colorHelp;

interface

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

type
  TfrmColorHelp = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    ImageList24: TImageList;
    DataSource1: TDataSource;
    ADOQueryHelp: TADOQuery;
    ADOQueryHelpColorID: TStringField;
    ADOQueryHelpChnName: TStringField;
    ADOQueryHelpEngName: TStringField;
    ADOQueryHelpCustomID: TStringField;
    ADOQueryHelpValid: TStringField;
    ToolBar1: TToolBar;
    BtnOk: TToolButton;
    BtnCancel: TToolButton;
    dbGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1chnname: TcxGridDBColumn;
    v1colorId: TcxGridDBColumn;
    dbGrid1Level1: TcxGridLevel;
    v1engName: TcxGridDBColumn;
    ADOConnection1: TADOConnection;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyleTree: TcxStyle;
    Tadd: TToolButton;
    ADOQueryCmd: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
   // procedure DBGrid1TitleClick(Column: TColumn);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure Edit1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure TaddClick(Sender: TObject);
  private
    procedure DoQuery();
    procedure DoFilter();
    function InsertColor(mcolor:string;var mcolorId:string):boolean;
  public
    { Public declarations }
  end;

var
  frmColorHelp: TfrmColorHelp;

implementation
uses
  U_global,U_formPas;
{$R *.dfm}

procedure TfrmColorHelp.FormCreate(Sender: TObject);
begin
  dbgrid1.Align:=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;
end;

procedure TfrmColorHelp.BtnOkClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
    ModalResult := 1;
end;

procedure TfrmColorHelp.BtnCancelClick(Sender: TObject);
begin
  ModalResult := -1;
end;
procedure TfrmColorHelp.DBGrid1DblClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
    ModalResult := 1;
end;

procedure TfrmColorHelp.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin

end;
//////////////////////////////////////////////////////
  //函数：提交查询
//////////////////////////////////////////////////////
procedure TfrmColorHelp.DoQuery();
var
  mwhere:string;
begin
{
  if trim(edit1.Text)<>'' then
    mwhere:=' and (colorId like '+quotedStr('%'+trim(edit1.Text)+'%')+
            ' or chnName like '+quotedStr('%'+trim(edit1.Text)+'%')+
            ' or engName like '+quotedStr('%'+trim(edit1.Text)+'%')+')';
            }
  try
    adoqueryHelp.DisableControls ;
    with adoqueryHelp do
    begin
      Close;
      sql.Clear ;
      sql.Add('select * from MC_color ');
      sql.Add('where 1=1');
      //sql.Add(mwhere);
      sql.Add('and Valid=''Y''');
      open;
    end;
  finally
    adoqueryHelp.EnableControls ;
  end;
end;

procedure TfrmColorHelp.Edit1Change(Sender: TObject);
begin
  Dofilter();
end;

procedure TfrmColorHelp.FormShow(Sender: TObject);
begin
  DoQuery();
end;

procedure TfrmColorHelp.Tv1DblClick(Sender: TObject);
begin
  BtnOk.Click ;
end;
//////////////////////////////////////////////////////
  //
//////////////////////////////////////////////////////
procedure TfrmColorHelp.DoFilter();
var
  filterStr:string;
begin
  if trim(edit1.Text)<>'' then
    filterStr:=' and (colorId like '+quotedStr('%'+trim(edit1.Text)+'%')+
            ' or chnName like '+quotedStr('%'+trim(edit1.Text)+'%')+
            ' or engName like '+quotedStr('%'+trim(edit1.Text)+'%')+')';
  try
    adoqueryHelp.DisableControls ;
    if trim(filterStr)='' then
    begin
      adoqueryHelp.Filtered:=false;
      adoqueryHelp.EnableControls;
      exit;
    end;
    filterStr:=trim(RightBStr(filterStr,length(filterStr)-4));
    with adoqueryHelp do
    begin
      filtered:=false;
      filter:=filterStr;
      filtered:=true;
    end;
  finally
    adoqueryHelp.EnableControls;
  end;
end;

procedure TfrmColorHelp.TaddClick(Sender: TObject);
var
  mcolorId:string;
begin
  if trim(edit1.Text)='' then
  begin
    application.MessageBox('请输入您要添加的颜色!','提示信息',0);
    edit1.SetFocus ;
    exit;
  end;
  if InsertColor(trim(edit1.Text),mcolorId) then
  begin
    DoQuery();
    Dofilter();
    ModalResult := 1;
  end;
end;
//////////////////////////////////////////////////////////////////
  //插入颜色
//////////////////////////////////////////////////////////////////
function TfrmColorHelp.InsertColor(mcolor:string;var mcolorId:string):boolean;
begin
    if not GetMaxNo20(ADOQueryCmd,mcolorId,'mc_color','C',1,4) then
    begin
      result:=false;
      application.MessageBox('获取颜色编号时发生错误!','提示信息',0);
      exit;
    end;
    try
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from mc_color');
        sql.Add('where chnName='+quotedStr(trim(mcolor)));
        open;
        if RecordCount<=0 then
        begin
          Append;
          fieldByName('colorId').value:=mcolorId;
          fieldByName('chnName').value:=mcolor;
          fieldByName('engName').value:='';
          Post;
        end
        else
        begin
          mColorId:=trim(fieldByName('colorId').AsString);
          edit;
          fieldByName('valid').value:='Y';
          post;
        end;
        close;
      end;
      result:=true;
    except
      result:=false;
    end;
end;

end.
