unit U_DictList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseInput, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, System.ImageList, Vcl.Menus, U_BaseList;

type
  TfrmDictList = class(TfrmBaseList)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    V1Code: TcxGridDBColumn;
    V1Name: TcxGridDBColumn;
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBAdd: TToolButton;
    TBSave: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    TBEdit: TToolButton;
    V1Note: TcxGridDBColumn;
    V1OrderNo: TcxGridDBColumn;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    DictName: TEdit;
    ThreeImgList: TImageList;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ImageList1: TImageList;
    V1Description: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Code: TcxGridDBColumn;
    V2OrderNo: TcxGridDBColumn;
    V2Name: TcxGridDBColumn;
    V2Note: TcxGridDBColumn;
    V2Description: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure DictNameChange(Sender: TObject);
    procedure V1NamePropertiesEditValueChanged(Sender: TObject);
    procedure V1OrderNoPropertiesEditValueChanged(Sender: TObject);
    procedure V1NotePropertiesEditValueChanged(Sender: TObject);
    procedure V1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure V1DescriptionPropertiesEditValueChanged(Sender: TObject);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure V2NamePropertiesEditValueChanged(Sender: TObject);
    procedure V2NotePropertiesEditValueChanged(Sender: TObject);
    procedure V2OrderNoPropertiesEditValueChanged(Sender: TObject);
    procedure V2DescriptionPropertiesEditValueChanged(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    procedure InitGrid();
    { Private declarations }
  public
    flag, flagname, snote: string;
    f1note, f1orderno, f2note, f2orderno: Boolean;
    PPSTE: integer;
    { Public declarations }
  end;

var
  frmDictList: TfrmDictList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmDictList.FormCreate(Sender: TObject);
begin
  inherited;
  try
//    cxGrid1.Align := alClient;
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except

  end;
end;

procedure TfrmDictList.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  DictName.SetFocus;

  Action := cafree;
end;

procedure TfrmDictList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,ZJM=dbo.getPinYin(A.DictName) from Sy_Dict A where A.Type=''' + flag + '''');
      Open;
    end;
    SCreateCDS(ADOQueryMain, ClientDataSet1);
    SInitCDSData(ADOQueryMain, ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDictList.N1Click(Sender: TObject);
var
  i: Integer;
begin
  DictName.SetFocus;
  TV2.OptionsData.Editing := True;
  TV2.OptionsSelection.CellSelect := True;
  for i := 0 to 0 do
  begin
    with ClientDataSet2 do
    begin
      Append;
      Post;
    end;
  end;

end;

procedure TfrmDictList.N2Click(Sender: TObject);
begin
  inherited;
  if ClientDataSet2.IsEmpty then
    Exit;
  if (Trim(ClientDataSet2.FieldByName('itemId').AsString) <> '') or (Trim(ClientDataSet2.FieldByName('itemtext').AsString) <> '') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from BS_Cloth_Glide A ');
      sql.Add('inner join bs_cloth_info X on A.bciid=X.bciid');
      sql.Add('where isnull(A.ProcessNO,'''')+isnull(A.ProcessTypeNO,'''')  ');
      sql.Add('=(select isnull(A.Note,'''')+isnull(B.ItemNote,'''') from SY_Dict A  ');
      sql.Add('inner join SY_Dict_item B on A.DictId=B.DictId where B.itemId=''' + Trim(ClientDataSet2.fieldbyname('itemId').AsString) + ''' ) ');
      sql.Add('and isnull(A.ProcessName,'''')+isnull(A.ProcessTypeName,'''')  ');
      sql.Add('=(select isnull(A.DictName,'''')+isnull(B.ItemText,'''') from SY_Dict A  ');
      sql.Add('inner join SY_Dict_item B on A.DictId=B.DictId where B.itemId=''' + Trim(ClientDataSet2.fieldbyname('itemId').AsString) + ''' ) ');
      //    showmessage(sql.Text);
      open;
    end;

    if ADOQueryTemp.IsEmpty = false then
    begin
      Application.MessageBox('已有产品登记该工序，不能删除!', '提示', 0);
      Exit;
    end;

    if application.MessageBox('确定要删除吗?', '提示信息', 1) = 2 then
      exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SY_Dict_item where itemId=''' + Trim(ClientDataSet2.fieldbyname('itemId').AsString) + '''');
      ExecSQL;
    end;
  end;
  ClientDataSet2.Delete;
end;

procedure TfrmDictList.N3Click(Sender: TObject);
begin
  inherited;
  TV2.OptionsData.Editing := True;
  TV2.OptionsSelection.CellSelect := True;
end;

procedure TfrmDictList.TBAddClick(Sender: TObject);
var
  i: Integer;
begin
  DictName.SetFocus;
  TV1.OptionsData.Editing := True;
  TV1.OptionsSelection.CellSelect := True;
  for i := 0 to 0 do
  begin
    with ClientDataSet1 do
    begin
      Append;
      Post;
    end;
  end;

end;

procedure TfrmDictList.TBSaveClick(Sender: TObject);
var
  maxno: string;
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  DictName.SetFocus;

  if ClientDataSet1.Locate('DictName', null, []) then
  begin
    Application.MessageBox('名称不能为空！', '提示', 0);
    Exit;
  end;
  if ClientDataSet1.Locate('DictName', '', []) then
  begin
    Application.MessageBox('名称不能为空！', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from SY_Dict where DictId=''' + Trim(flag) + '''');
      open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from SY_Dict where 1=2');
        open;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('DictId').Value := Trim(flag);
        ADOQueryCmd.FieldByName('DictName').Value := trim(flagname);
        ADOQueryCmd.FieldByName('Type').Value := 'Main';
        ADOQueryCmd.Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      ClientDataSet1.DisableControls;
      with ClientDataSet1 do
      begin
        First;
        while not eof do
        begin
          if Trim(ClientDataSet1.FieldByName('DictId').AsString) = '' then
          begin
            if GetLSNo(ADOQueryTemp, maxno, 'SY', 'SY_Dict', 3, 1) = False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              ClientDataSet1.EnableControls;
              Application.MessageBox('取最大编号失败！', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            maxno := Trim(ClientDataSet1.fieldbyname('DictId').AsString);
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.add('select * from SY_Dict where Type=''' + Trim(flag) + '''');
            sql.Add(' and DictName=''' + Trim(ClientDataSet1.fieldbyname('DictName').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('DictId').AsString) = '' then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              ClientDataSet1.EnableControls;
              Application.MessageBox('名称重复！', '提示', 0);
              Exit;
            end
            else
            begin
              if Trim(ADOQueryTemp.fieldbyname('DictId').AsString) <> Trim(ClientDataSet1.fieldbyname('DictId').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 SY_Dict where DictId=''' + Trim(ClientDataSet1.fieldbyname('DictId').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from SY_Dict where 1<>1');
            Open;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('DictId').Value := Trim(maxno);
          ADOQueryCmd.FieldByName('DictName').Value := ClientDataSet1.fieldbyname('DictName').Value;
          ADOQueryCmd.FieldByName('note').Value := Trim(snote);
          ADOQueryCmd.FieldByName('Type').Value := flag;
          ADOQueryCmd.FieldByName('valid').Value := '1';
          ADOQueryCmd.Post;
          ClientDataSet1.Edit;
          ClientDataSet1.FieldByName('DictId').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 TfrmDictList.TBDelClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  if (Trim(ClientDataSet1.FieldByName('DictId').AsString) <> '') or (Trim(ClientDataSet1.FieldByName('DictName').AsString) <> '') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from SY_Dict_Item where DictId=''' + Trim(ClientDataSet1.fieldbyname('DictId').AsString) + '''');
      open;
    end;

    if ADOQueryTemp.IsEmpty = false then
    begin
      Application.MessageBox('已经登记工序类型，不允许删除!', '提示', 0);
      Exit;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from BS_Cloth_Glide A ');
      sql.Add('where isnull(A.ProcessName,'''')=''' + Trim(ClientDataSet1.fieldbyname('DictName').AsString) + '''  ');
      open;
    end;

    if ADOQueryTemp.IsEmpty = false then
    begin
      Application.MessageBox('已有产品登记该工序，不能删除!', '提示', 0);
      Exit;
    end;

    if application.MessageBox('确定要删除吗?', '提示信息', 1) = 2 then
      exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SY_Dict where DictId=''' + Trim(ClientDataSet1.fieldbyname('DictId').AsString) + '''');
      SQL.Add(' and Type=''' + Trim(flag) + '''');

      sql.Add('delete SY_Dict_Item where DictId=''' + Trim(ClientDataSet1.fieldbyname('DictId').AsString) + '''');
      ExecSQL;
    end;
  end;
  ClientDataSet1.Delete;
end;

procedure TfrmDictList.TBCloseClick(Sender: TObject);
begin
  DictName.SetFocus;

  Close;
end;

procedure TfrmDictList.FormShow(Sender: TObject);
var
  fsj, fsj1: string;
begin
  inherited;

  flag := 'GYName';
  flagname := '工序名称';

  InitGrid();
//  fsj := Trim(flag) + '01';
//  fsj1 := Trim(flagname) + '01';

  ReadCxGrid(self.Caption + Trim(flag), TV1, '自定义数据');


//  V1Note.Visible := f1note;
//  V1OrderNo.Visible := f1orderno;
//
//  V2Note.Visible := f2note;
//  V2OrderNo.Visible := f2orderno;
//  if ViewFlag = True then
//  begin
//    TBAdd.Visible := False;
//    TBSave.Visible := False;
//    TBDel.Visible := False;
//    TBEdit.Visible := False;
//    Label2.Visible := False;
//  end;
end;

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

procedure TfrmDictList.TBEditClick(Sender: TObject);
begin
  TV1.OptionsData.Editing := True;
  TV1.OptionsSelection.CellSelect := True;
end;

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

procedure TfrmDictList.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  inherited;
  if ClientDataSet1.IsEmpty then
    exit;

  with ADOQuerySub do
  begin
    close;
    SQL.Clear;
    sql.Add('select * from SY_Dict_Item where DictId =''' + ClientDataSet1.FieldByName('DictId').asstring + ''' ');
    open;
  end;
  SCreateCDS(ADOQuerySub, ClientDataSet2);
  SInitCDSData(ADOQuerySub, ClientDataSet2);
end;

procedure TfrmDictList.TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  if TV2.OptionsData.Editing = true then
  begin
    TV2.OptionsData.Editing := false;
  end;
  if TV2.OptionsData.Editing = False then
  begin
    ModalResult := 1;
  end;
end;

procedure TfrmDictList.DictNameChange(Sender: TObject);
var
  fsj: string;
begin
  if Trim(DictName.Text) <> '' then
  begin
    fsj := ' DictName like ''' + '%' + Trim(DictName.Text) + '%' + '''' + ' or Note like ''' + '%' + Trim(DictName.Text) + '%' + '''' + ' or ZJM like ''' + '%' + Trim(DictName.Text) + '%' + '''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, fsj);
    SCreateCDS(ADOQueryMain, ClientDataSet1);
    SInitCDSData(ADOQueryMain, ClientDataSet1);
  end;
end;

procedure TfrmDictList.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('DictName').Value := Trim(mvalue);
    //Post;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from SY_Dict where DictID=''' + Trim(flag) + '''');
      open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from SY_Dict where 1=2');
        open;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('DictID').Value := Trim(flag);
        ADOQueryCmd.FieldByName('DictName').Value := trim(flagname);
        ADOQueryCmd.FieldByName('Type').Value := 'Main';
        ADOQueryCmd.Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      if Trim(ClientDataSet1.FieldByName('DictID').AsString) = '' then
      begin
        if GetLSNo(ADOQueryTemp, maxno, 'SY', 'SY_Dict', 3, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
          Application.MessageBox('取最大编号失败！', '提示', 0);
          Exit;
        end;
      end
      else
      begin
        maxno := Trim(ClientDataSet1.fieldbyname('DictID').AsString);

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from SY_Dict_Item where DictId=''' + Trim(ClientDataSet1.fieldbyname('DictId').AsString) + '''');
          open;
        end;

        if ADOQueryTemp.IsEmpty = False then
        begin
          Application.MessageBox('已经登记工序类型，不允许修改!', '提示', 0);
          Exit;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.add('select * from SY_Dict where Type=''' + Trim(flag) + '''');
        sql.Add(' and DictName=''' + Trim(ClientDataSet1.fieldbyname('DictName').AsString) + '''');
        sql.Add(' and note=''' + Trim(ClientDataSet1.fieldbyname('note').AsString) + '''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty = False then
      begin
        if ADOQueryTemp.RecordCount > 1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('名称重复！', '提示', 0);
          Exit;
        end;
        if Trim(ClientDataSet1.fieldbyname('DictID').AsString) = '' then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('名称重复！', '提示', 0);
          Exit;
        end
        else
        begin
          if Trim(ADOQueryTemp.fieldbyname('DictID').AsString) <> Trim(ClientDataSet1.fieldbyname('DictID').AsString) then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('名称重复！', '提示', 0);
            Exit;
          end;
        end;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('delete SY_Dict where DictID=''' + Trim(ClientDataSet1.fieldbyname('DictID').AsString) + '''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from SY_Dict where 1<>1');
        Open;
      end;
      ADOQueryCmd.Append;
      ADOQueryCmd.FieldByName('DictID').Value := Trim(maxno);
      ADOQueryCmd.FieldByName('DictName').Value := ClientDataSet1.fieldbyname('DictName').AsString;
      ADOQueryCmd.FieldByName('note').Value := Trim(snote);
      ADOQueryCmd.FieldByName('Type').Value := flag;
      ADOQueryCmd.FieldByName('valid').Value := '1';
      ADOQueryCmd.Post;
      ClientDataSet1.Edit;
      ClientDataSet1.FieldByName('DictID').Value := Trim(maxno);
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmDictList.V1OrderNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  if Trim(ClientDataSet1.fieldbyname('DictName').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('SortOrder').Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update SY_Dict Set SortOrder=' + mvalue);
    sql.Add(' where DictId=''' + Trim(ClientDataSet1.fieldbyname('DictId').AsString) + '''');
    ExecSQL;
  end;
end;

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

procedure TfrmDictList.V2NamePropertiesEditValueChanged(Sender: TObject);
var
  maxno, mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    //Application.MessageBox('名称不能为空！','提示',0);
    Exit;
  end;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName('ItemText').Value := Trim(mvalue);
    //Post;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      if Trim(ClientDataSet2.FieldByName('ItemId').AsString) = '' then
      begin
        if GetLSNo(ADOQueryTemp, maxno, 'SDI', 'SY_Dict_Item', 3, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
          Application.MessageBox('取最大编号失败！', '提示', 0);
          Exit;
        end;
      end
      else
      begin

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.add('select * from BS_Cloth_Glide A ');
          sql.Add('where isnull(A.ProcessNO,'''')+isnull(A.ProcessTypeNO,'''')  ');
          sql.Add('=(select isnull(A.Note,'''')+isnull(B.ItemNote,'''') from SY_Dict A  ');
          sql.Add('inner join SY_Dict_item B on A.DictId=B.DictId where B.itemId=''' + Trim(ClientDataSet2.fieldbyname('itemId').AsString) + ''' ) ');
          open;
        end;

        if ADOQueryTemp.IsEmpty = false then
        begin
          Application.MessageBox('已有产品登记该工序，不能修改!', '提示', 0);
          Exit;
        end;

        maxno := Trim(ClientDataSet2.fieldbyname('ItemId').AsString);
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.add('select * from SY_Dict_Item where ');
        sql.Add(' ItemText=''' + Trim(ClientDataSet2.fieldbyname('ItemText').AsString) + '''');
        sql.Add(' and ItemNote=''' + Trim(ClientDataSet2.fieldbyname('ItemNote').AsString) + '''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty = False then
      begin
        if ADOQueryTemp.RecordCount > 1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('名称重复！', '提示', 0);
          Exit;
        end;
        if Trim(ClientDataSet2.fieldbyname('ItemID').AsString) = '' then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('名称重复！', '提示', 0);
          Exit;
        end
        else
        begin
          if Trim(ADOQueryTemp.fieldbyname('ItemID').AsString) <> Trim(ClientDataSet2.fieldbyname('ItemID').AsString) then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('名称重复！', '提示', 0);
            Exit;
          end;
        end;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('delete SY_Dict_ITEM where ItemID=''' + Trim(ClientDataSet2.fieldbyname('ItemID').AsString) + '''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from SY_Dict_Item where 1<>1');
        Open;
      end;
      ADOQueryCmd.Append;
      ADOQueryCmd.FieldByName('DictID').Value := Trim(ClientDataSet1.FieldByName('DictID').AsString);
      ADOQueryCmd.FieldByName('ItemId').Value := Trim(maxno);
      ADOQueryCmd.FieldByName('ItemText').Value := ClientDataSet2.fieldbyname('ItemText').AsString;
      ADOQueryCmd.FieldByName('Itemnote').Value := Trim(snote);
      ADOQueryCmd.FieldByName('valid').Value := '1';
      ADOQueryCmd.Post;
      ClientDataSet2.Edit;
      ClientDataSet2.FieldByName('DictID').Value := Trim(ClientDataSet1.FieldByName('DictID').AsString);
      ClientDataSet2.FieldByName('ItemId').Value := Trim(maxno);
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmDictList.V2NotePropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
  FDictID: string;
begin
  if Trim(ClientDataSet2.fieldbyname('ItemText').AsString) = '' then
  begin
    Application.MessageBox('名称不能为空!', '提示', 0);
    Exit;
  end;
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '';
  end;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName('itemNote').Value := mvalue;
    Post;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('select * from BS_Cloth_Glide A ');
    sql.Add('inner join bs_cloth_info X on A.bciid=X.bciid');
    sql.Add('where isnull(A.ProcessNO,'''')+isnull(A.ProcessTypeNO,'''')  ');
    sql.Add('=(select isnull(A.Note,'''')+isnull(B.ItemNote,'''') from SY_Dict A  ');
    sql.Add('inner join SY_Dict_item B on A.DictId=B.DictId where B.itemId=''' + Trim(ClientDataSet2.fieldbyname('itemId').AsString) + ''' ) ');
    sql.Add('and isnull(A.ProcessName,'''')+isnull(A.ProcessTypeName,'''')  ');
    sql.Add('=(select isnull(A.DictName,'''')+isnull(B.ItemText,'''') from SY_Dict A  ');
    sql.Add('inner join SY_Dict_item B on A.DictId=B.DictId where B.itemId=''' + Trim(ClientDataSet2.fieldbyname('itemId').AsString) + ''' ) ');
//    showmessage(sql.Text);
    open;
  end;

  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('已有产品登记该工序，不能修改!', '提示', 0);
    Exit;
  end;

  FDictID := ClientDataSet2.fieldbyname('ItemId').AsString;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update SY_Dict_ITEM Set itemNote=''' + Trim(mvalue) + '''');
      sql.Add(' where ITEMID=''' + Trim(ClientDataSet2.fieldbyname('ITEMID').AsString) + '''');
      ExecSQL;
    end;

    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      SQL.Add(' select isnull(A.Note,'''')+isnull(B.ItemNote,'''') from SY_Dict A  ');
      SQL.Add(' inner join SY_Dict_Item B on A.DictId=B.DictId  ');
      sql.Add(' where isnull(A.Note,'''')<>'''' and isnull(B.ItemNote,'''')<>'''' ');
      SQL.Add(' group by isnull(A.Note,'''')+isnull(B.ItemNote,'''') having count(*)>1');
      open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('编号重复，不能保存!', '提示', 0);

      with ADOQuerySub do
      begin
        close;
        SQL.Clear;
        sql.Add('select * from SY_Dict_Item where DictId =''' + ClientDataSet1.FieldByName('DictId').asstring + ''' ');
        open;
      end;
      SCreateCDS(ADOQuerySub, ClientDataSet2);
      SInitCDSData(ADOQuerySub, ClientDataSet2);

      ClientDataSet2.Locate('ItemId', FDictID, []);

      exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

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

procedure TfrmDictList.V1NotePropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
  FDictID: string;
begin
  if Trim(ClientDataSet1.fieldbyname('DictName').AsString) = '' then
  begin
    Application.MessageBox('名称不能为空!', '提示', 0);
    Exit;
  end;
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('Note').Value := mvalue;
    Post;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SY_Dict_Item where DictId=''' + Trim(ClientDataSet1.fieldbyname('DictId').AsString) + '''');
    open;
  end;

  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('已经登记工序类型，不允许修改!', '提示', 0);
    Exit;
  end;

  FDictID := ClientDataSet1.fieldbyname('DictID').AsString;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update SY_Dict Set Note=''' + Trim(mvalue) + '''');
      sql.Add(' where DictID=''' + Trim(ClientDataSet1.fieldbyname('DictID').AsString) + '''');
      ExecSQL;
    end;

    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      SQL.Add(' select isnull(A.Note,'''')+isnull(B.ItemNote,'''') from SY_Dict A  ');
      SQL.Add(' inner join SY_Dict_Item B on A.DictId=B.DictId  ');
      sql.Add(' where isnull(A.Note,'''')<>'''' and isnull(B.ItemNote,'''')<>'''' ');
      SQL.Add(' group by isnull(A.Note,'''')+isnull(B.ItemNote,'''') having count(*)>1');
      open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('编号重复，不能保存!', '提示', 0);

      initgrid();
      ClientDataSet1.Locate('DictID', FDictID, []);

      exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;

end;

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

procedure TfrmDictList.V1DescriptionPropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  if Trim(ClientDataSet1.fieldbyname('DictName').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('Description').Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update SY_Dict Set Description=' + mvalue);
    sql.Add(' where DictId=''' + Trim(ClientDataSet1.fieldbyname('DictId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmDictList.FormDestroy(Sender: TObject);
begin
  inherited;
  WriteCxGrid(self.Caption, TV1, '自定义数据');
  frmZDYHelp := nil;

end;

end.

