unit U_DyeTechnicsinput;

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, cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, Menus, U_BaseList, dxSkinsCore, dxSkinsDefaultPainters,
  U_BaseInput, dxDateRanges, dxScrollbarAnnotations, System.ImageList,
  Vcl.ImgList, cxButtons;

type
  TfrmDyeTechnicsinput = class(TfrmBaseInput)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ADOQuery1: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    PM_1: TPopupMenu;
    N2: TMenuItem;
    cxSplitter1: TcxSplitter;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    Tv2PSType: TcxGridDBColumn;
    Tv2PSNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel1: TPanel;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    VC_PSNO: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Label5: TLabel;
    PSName: TEdit;
    ToolButton2: TToolButton;
    btnSave: TToolButton;
    Panel4: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    DTMName: TEdit;
    DTMNo: TEdit;
    TV1PSName: TcxGridDBColumn;
    Tv3: TcxGridDBTableView;
    cxGrid3Level1: TcxGridLevel;
    cxGrid3: TcxGrid;
    TbDelete: TToolButton;
    Tv3PSID: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    TbMoveUp: TcxButton;
    btnMoveDown: TcxButton;
    TV1DTMID: TcxGridDBColumn;
    TV1DTMName: TcxGridDBColumn;
    Tv3DTMName: TcxGridDBColumn;
    TV1PSID: TcxGridDBColumn;
    GYNote: TEdit;
    Label1: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TbDeleteClick(Sender: TObject);
    procedure Tv3DblClick(Sender: TObject);
    procedure PSNameKeyPress(Sender: TObject; var Key: Char);
    procedure TbMoveUpClick(Sender: TObject);
    procedure btnMoveDownClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

  private
    { Private declarations }

    procedure InitGrid();
    procedure InitGlide();
    procedure SetXH();
    function SaveData(): boolean;
    procedure CheckData();
    procedure InitGridSub();
  public
    fPSIDList: string;
    FDTMID, PState: string;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmDyeTechnicsinput: TfrmDyeTechnicsinput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_DyeProcessSel, U_DyeProcessDefin;

{$R *.dfm}

procedure TfrmDyeTechnicsinput.SetXH();
var
  i: Integer;
begin
  with CDS_1 do
  begin
    First;
    i := 1;
    while not Eof do
    begin
      with CDS_1 do
      begin
        Edit;
        FieldByName('XHNoTemp').Value := i;
        Post;
      end;
      i := i + 1;
      Next;
    end;
  end;

  with CDS_1 do
  begin
    CDS_1.IndexFieldNames := '';
    First;
    i := 1;
    while not Eof do
    begin
      with CDS_1 do
      begin
        Edit;
        FieldByName('PSNO').Value := FieldByName('XHNoTemp').AsInteger;
        Post;
      end;
      i := i + 1;
      Next;
    end;
    CDS_1.IndexFieldNames := 'PSNO';
  end;
end;

procedure TfrmDyeTechnicsinput.InitGlide();
var
  maxNo, maxsubid: string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * FROM Dye_Technics_Main where  DTMID=' + quotedstr(FDTMID));
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel4, 2);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.* FROM Dye_Technics_Sub A inner join Dye_Process B on A.PSId=B.PSId ');
    sql.Add(' Where A.DTMID=''' + trim(FDTMID) + '''');
    SQL.Add(' ORDER BY A.PSNO');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
  CheckData();
  if PState = '复制' then
  begin
    CDS_1.First;
    while not CDS_1.Eof do
    begin

      if GetLSNo(ADOQueryCmd, maxNo, 'JP', 'Dye_Process', 4, 1) = False then
      begin
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec P_CopyDyeProcess ');
        SQL.Add(' @PSID = ' + QuotedStr(Trim(CDS_1.fieldbyname('PSID').AsString)));
        SQL.Add(' ,@maxNo = ' + QuotedStr(Trim(maxNo)));
        SQL.Add(' ,@DName = ' + QuotedStr(Trim(DName)));
//        SQL.Add(' ,@DTMID = ' + QuotedStr(Trim(DTMID.Text)));
        Open;
      end;
      CDS_1.Edit;
      CDS_1.FieldByName('PSID').asstring := maxNo;
      CDS_1.FieldByName('DTMID').asstring := '';
      CDS_1.Post;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add(' select * from BS_Cloth_GY_Sub where PSID = ' + QuotedStr(Trim(maxNo)));
        sql.Add(' order By serialno');
        Open;
      end;
//      ADOQueryCmd.FieldByName('BCPID').ProviderFlags := [pfInKey, pfInWhere];
      ADOQueryCmd.First;
      while not ADOQueryCmd.Eof do
      begin

        if ADOQueryCmd.FieldByName('gytype').Value = '长车清洗' then
        begin
          if GetLSNo(ADOQueryTemp, maxsubid, 'CCQX', 'Bs_Cloth_GY_Sub', 5, 1) = False then
          begin
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else if ADOQueryCmd.FieldByName('gytype').Value = '卷染缸清洗' then
        begin
          if GetLSNo(ADOQueryTemp, maxsubid, 'RGQX', 'Bs_Cloth_GY_Sub', 5, 1) = False then
          begin
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else if ADOQueryCmd.FieldByName('gytype').Value = '机缸清洗' then
        begin
          if GetLSNo(ADOQueryTemp, maxsubid, 'JGQX', 'Bs_Cloth_GY_Sub', 5, 1) = False then
          begin
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else if ADOQueryCmd.FieldByName('gytype').Value = '下水定型' then
        begin
          if GetLSNo(ADOQueryTemp, maxsubid, 'FS', 'Bs_Cloth_GY_Sub', 5, 1) = False then
          begin
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else if ADOQueryCmd.FieldByName('gytype').Value = '刮胶' then
        begin
          if GetLSNo(ADOQueryTemp, maxsubid, 'GJ', 'Bs_Cloth_GY_Sub', 5, 1) = False then
          begin
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else if ADOQueryCmd.FieldByName('gytype').Value = '发泡' then
        begin
          if GetLSNo(ADOQueryTemp, maxsubid, 'FP', 'Bs_Cloth_GY_Sub', 5, 1) = False then
          begin
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end;

        ADOQueryCmd.Edit;
        ADOQueryCmd.fieldByName('BCPID').Value := maxsubid;
        ADOQueryCmd.Post;
        ADOQueryCmd.Next;
      end;

      CDS_1.next;
    end;
  end;
end;

procedure TfrmDyeTechnicsinput.CheckData();
var
  maxno: string;
begin
  if PState = '复制' then
  begin
    DTMNo.Text := '';
    DTMName.Text := '';
//    FDTMID := '';
    if GetLSNo(ADOQueryCmd, maxno, 'GY', 'Dye_Technics_Main', 3, 1) = False then
      raise Exception.Create('取最大号失败！');
    FDTMID := maxno;
  end;
//  if ((PState = '新增') or (PState = '复制'))   and   (FDTMID = '') then
//  begin
//    if GetLSNo(ADOQueryCmd, maxno, 'GY', 'Dye_Technics_Main', 3, 1) = False then
//      raise Exception.Create('取最大号失败！');
//    FDTMID := maxno;
//    DTMID.Text := FDTMID;
//  end;
end;

function TfrmDyeTechnicsinput.SaveData(): boolean;
var
  DTMPSlist, maxno: string;
begin
  result := false;
  ADOQueryCmd.Connection.BeginTrans;
  try

    if Trim(FDTMID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxno, 'GY', 'Dye_Technics_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败！');
    end
    else
    begin
      maxno := FDTMID;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Dye_Technics_Main where DTMNo=''' + Trim(DTMNo.Text) + '''');
      sql.Add(' and DTMID <> ''' + Trim(maxno) + '''');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 0 then
      raise Exception.Create('工艺编号重复!');

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Dye_Technics_Main where DTMName=''' + Trim(DTMName.Text) + '''');
      sql.Add(' and DTMID <> ''' + Trim(maxno) + '''');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 0 then
      raise Exception.Create('工艺名称重复!');

//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//    sql.Add('SELECT PSName,COUNT(*) AS DuplicateCount FROM Dye_Process WHERE DTMNo =''' + Trim(DTMNo.Text) + '''');
//    sql.Add(' GROUP BY PSName HAVING COUNT(*) > 1 ORDER BY DuplicateCount DESC;');

//      sql.Add('SELECT STRING_AGG(PSName, '', '') AS DuplicatePSNames,');
//      sql.Add('COUNT(*) AS TotalPSName');
//      sql.Add('FROM (SELECT PSName');
//      sql.Add('FROM Dye_Process');
//      sql.Add('WHERE DTMID = ''' + Trim(maxno) + '''');
//      sql.Add('GROUP BY PSName');
//      sql.Add('HAVING COUNT(*) > 1) AS Duplicates HAVING Count(*)>0;');
//      Open;
////    showMessage(sql.Text);
//    end;
//    if ADOQueryCmd.RecordCount > 0 then
//      raise Exception.Create('工序' + ADOQueryCmd.FieldByName('DuplicatePSNames').AsString + '重复!');

    if fPSIDList <> '' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Text := 'DELETE FROM Dye_Process WHERE PSID IN (' + fPSIDList + ')';
//        ShowMessage(sql.Text);
        try
          ExecSQL;
        except
          on E: Exception do
            ShowMessage('删除失败: ' + E.Message);
        end;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Text := 'DELETE FROM BS_Cloth_GY_Main WHERE PSID IN (' + fPSIDList + ')';
//        ShowMessage(sql.Text);
        try
          ExecSQL;
        except
          on E: Exception do
            ShowMessage('删除失败: ' + E.Message);
        end;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Text := 'DELETE FROM BS_Cloth_GY_Sub WHERE PSID IN (' + fPSIDList + ')';
//        ShowMessage(sql.Text);
        try
          ExecSQL;
        except
          on E: Exception do
            ShowMessage('删除失败: ' + E.Message);
        end;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Dye_Technics_Sub where DTMID=' + quotedstr(trim(maxno)));
      execsql;
    end;

    DTMPSlist := '';
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('SELECT * FROM Dye_Technics_Sub WHERE 1=2');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('PSNO').Value := Trim(CDS_1.fieldbyname('PSNO').AsString);
          FieldByName('DTMID').Value := Trim(maxno);
          FieldByName('PSId').Value := Trim(CDS_1.fieldbyname('PSId').AsString);
          Post;
        end;
        if DTMPSlist = '' then
        begin
          DTMPSlist := Trim(CDS_1.fieldbyname('PSNO').AsString) + '.' + Trim(CDS_1.fieldbyname('PSName').AsString);
        end
        else
        begin
          DTMPSlist := DTMPSlist + '→' + Trim(CDS_1.fieldbyname('PSNO').AsString) + '.' + Trim(CDS_1.fieldbyname('PSName').AsString);
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('SELECT * FROM Dye_Process WHERE PSID = ' + QuotedStr(Trim(CDS_1.fieldbyname('PSId').AsString)));
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          FieldByName('DTMName').Value := Trim(DTMName.Text);
          FieldByName('DTMID').Value := Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Dye_Technics_Main where DTMID =' + quotedstr(Trim(maxno)));
      Open;
    end;
    with ADOQueryCmd do
    begin
      if ADOQueryCmd.IsEmpty then
      begin
        Append;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
        FieldByName('DTMID').Value := Trim(maxno);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      RTSetsavedata(ADOQueryCmd, 'Dye_Technics_Main', Panel4, 2);
      FieldByName('DTMPSlist').Value := Trim(DTMPSlist);
      Post;

    end;

    //////////////产品信息表内容修改//////////////
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_Info where DTMID =' + quotedstr(Trim(maxno)));
      Open;
    end;
    ADOQueryCmd.First;
    while not ADOQueryCmd.Eof do
    begin
      ADOQueryCmd.Edit;
      ADOQueryCmd.FieldByName('DTMPSlist').Value := Trim(DTMPSlist);
      ADOQueryCmd.FieldByName('Editer').Value := Trim(DName);
      ADOQueryCmd.FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      ADOQueryCmd.Post;
      ADOQueryCmd.Next;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_Dye_Technics where DTMID =' + quotedstr(Trim(maxno)));
      Open;
    end;
    ADOQueryCmd.First;
    while not ADOQueryCmd.Eof do
    begin
      ADOQueryCmd.Edit;
      ADOQueryCmd.FieldByName('DTMPSlist').Value := Trim(DTMPSlist);
      ADOQueryCmd.FieldByName('DTMNo').Value := Trim(DTMNo.Text);
      ADOQueryCmd.FieldByName('DTMName').Value := Trim(DTMName.Text);
      ADOQueryCmd.Post;
      ADOQueryCmd.Next;
    end;
    //////////////产品信息表内容修改//////////////
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    result := false;
  end;
end;

procedure TfrmDyeTechnicsinput.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
//      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select *,PSXH=0 from Dye_Technics ');
      Open;
    end;
//    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDyeTechnicsinput.InitGridSub();
begin
  if CDS_2.IsEmpty then
    Exit;

  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin

      Close;
      SQL.Clear;
      sql.Add(' select * from Dye_Process where PSType = ' + QuotedStr(Trim(CDS_2.FieldByName('PSType').ASSTRING)));
      Open;
    end;
    SDofilter(ADOQueryTemp, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryTemp, CDS_3);
    SInitCDSData(ADOQueryTemp, CDS_3);
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmDyeTechnicsinput.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyeTechnicsinput := nil;
end;

procedure TfrmDyeTechnicsinput.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_ESCAPE then  // VK_ESCAPE 是 ESC 键的常量
  begin
    Key := 0;  // 清除按键，防止继续传递
    Close;     // 或者 ModalResult := mrCancel;
  end;
end;

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

procedure TfrmDyeTechnicsinput.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyeTechnicsinput.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '工艺录入');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '工艺录入');
  InitGrid();
  InitGlide();
  InitGridSub();

  KeyPreview := True;
end;

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

procedure TfrmDyeTechnicsinput.TbDeleteClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;

  if Trim(CDS_1.fieldbyname('PSId').AsString) <> '' then
  begin
    if (Trim(CDS_1.fieldbyname('DTMID').AsString) = '') or CDS_1.fieldbyname('DTMID').isNull then
    begin
      if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
        Exit;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Dye_Process where PSId=''' + Trim(CDS_1.fieldbyname('PSId').AsString) + '''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete BS_Cloth_GY_Main where PSId=''' + Trim(CDS_1.fieldbyname('PSId').AsString) + '''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete BS_Cloth_GY_Sub where PSId=''' + Trim(CDS_1.fieldbyname('PSId').AsString) + '''');
        ExecSQL;
      end;

    end;

    if fPSIDlist = '' then
    begin
      fPSIDlist := QuotedStr(Trim(CDS_1.fieldbyname('PSID').AsString));
    end
    else
    begin
      fPSIDlist := fPSIDlist + ',' + QuotedStr(Trim(CDS_1.fieldbyname('PSID').AsString));
    end;
  end;
  CDS_1.Delete;
  SetXH();
end;

procedure TfrmDyeTechnicsinput.TbMoveUpClick(Sender: TObject);
var
  test: string;
  ACurrentRow, APrevRow: Integer;
begin
  ACurrentRow := Tv1.DataController.FocusedRowIndex + 1;//当前行位置（从1开始）
  if ACurrentRow = 1 then  // 已经是第一条记录
    Exit;

  APrevRow := ACurrentRow - 1;

  CDS_1.DisableControls;
  try
    // 交换当前记录和上一条记录的顺序值
    CDS_1.RecNo := ACurrentRow;
    CDS_1.Edit;
    CDS_1.FieldByName('PSNo').AsInteger := APrevRow;
    test := CDS_1.FieldByName('PSName').AsString;
    CDS_1.Post;

    CDS_1.RecNo := APrevRow;
    CDS_1.Edit;
    CDS_1.FieldByName('PSNo').AsInteger := ACurrentRow;
    test := CDS_1.FieldByName('PSName').AsString;
    CDS_1.Post;

    // 刷新数据
//    CDS_1.Refresh;
    CDS_1.IndexFieldNames := 'PSNo';
    // 重新定位到移动后的记录
    Tv1.DataController.FocusedRowIndex := APrevRow - 1;
  finally
    CDS_1.EnableControls;
  end;
end;

procedure TfrmDyeTechnicsinput.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '工艺录入');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '工艺录入');

end;

procedure TfrmDyeTechnicsinput.cxGridDBColumn1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmDyeProcessDefin := TfrmDyeProcessDefin.Create(Application);
    with frmDyeProcessDefin do
    begin
      FPSType := self.CDS_1.FieldByName('PSType').Value;
      FPSID := Self.CDS_1.FieldByName('PSID').Value;

      if ShowModal = 1 then
      begin
        TBRafresh.Click;
        with self.CDS_1 do
        begin
          Edit;
          FieldByName('PSID').Value := frmDyeProcessDefin.FPSID;
          FieldByName('PSName').Value := frmDyeProcessDefin.PSName.Text;
          Post;
        end;
      end;
    end;

  finally
    frmDyeProcessDefin.Free;
  end;
end;

procedure TfrmDyeTechnicsinput.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDyeTechnicsinput.v2Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
    mvalue := '';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Dye_Technics_Main where DTMNo =' + quotedstr(Trim(mvalue)));
    Open;
  end;
  if ADOQueryCmd.IsEmpty = false then
  begin
    application.MessageBox('已存在工艺编号', '提示');
    CDS_2.Edit;
    CDS_2.FieldByName('DTMNo').AsString := '';
    exit;
  end;
  with CDS_2 do
  begin
    Edit;
    FieldByName('DTMNo').Value := Trim(mvalue);
    //Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update Dye_Technics_Main set DTMNo=''' + trim(mvalue) + ''' ');
    sql.Add(',Editer=''' + trim(DName) + '''');
    sql.Add(',Edittime=getdate()');
    sql.add(' where DTMID=''' + trim(CDS_2.fieldbyname('DTMID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmDyeTechnicsinput.btnMoveDownClick(Sender: TObject);
var
  test: string;
  ACurrentRow, APrevRow: Integer;
begin
  ACurrentRow := Tv1.DataController.FocusedRowIndex + 1;//当前行位置（从1开始）
  if ACurrentRow = CDS_1.RecordCount then  // 已经是第一条记录
    Exit;

  APrevRow := ACurrentRow + 1;

  CDS_1.DisableControls;
  try
    // 交换当前记录和上一条记录的顺序值

    CDS_1.RecNo := APrevRow;
    CDS_1.Edit;
    CDS_1.FieldByName('PSNo').AsInteger := ACurrentRow;
    test := CDS_1.FieldByName('PSName').AsString;
    CDS_1.Post;

    CDS_1.RecNo := ACurrentRow;
    CDS_1.Edit;
    CDS_1.FieldByName('PSNo').AsInteger := APrevRow;
    test := CDS_1.FieldByName('PSName').AsString;
    CDS_1.Post;



    // 刷新数据
//    CDS_1.Refresh;
    CDS_1.IndexFieldNames := 'PSNo';

    Tv1.DataController.FocusedRowIndex := ACurrentRow - 1;
  finally
    CDS_1.EnableControls;
  end;
end;

procedure TfrmDyeTechnicsinput.btnSaveClick(Sender: TObject);
begin
  if trim(DTMNo.Text) = '' then
  begin
    application.MessageBox('工艺编号不能为空', '提示');
    Exit;
  end;
  if trim(DTMName.Text) = '' then
  begin
    application.MessageBox('工艺名称不能为空', '提示');
    Exit;
  end;
  if CDS_1.Locate('PSName', '', []) or CDS_1.Locate('PSName', null, []) then
  begin
    Application.MessageBox('请完善工序名称！', '提示信息', MB_OK);
    Exit;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
  close;
end;

procedure TfrmDyeTechnicsinput.v2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
    mvalue := '';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Dye_Technics_Main where DTMName =' + quotedstr(Trim(mvalue)));
    Open;
  end;
  if ADOQueryCmd.IsEmpty = false then
  begin
    application.MessageBox('已存在工艺名称', '提示');
    CDS_2.Edit;
    CDS_2.FieldByName('DTMName').AsString := '';
    exit;
  end;
  with CDS_2 do
  begin
    Edit;
    FieldByName('DTMName').Value := Trim(mvalue);
    //Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update Dye_Technics_Main set DTMName=''' + trim(mvalue) + ''' ');
    sql.Add(',Editer=''' + trim(DName) + '''');
    sql.Add(',Edittime=getdate()');
    sql.add(' where DTMID=''' + trim(CDS_2.fieldbyname('DTMID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmDyeTechnicsinput.Tv2DblClick(Sender: TObject);
begin

  with CDS_1 do
  begin
    Append;
    FieldByName('PSNO').Value := CDS_1.RecordCount + 1;
    FieldByName('PSType').Value := CDS_2.fieldbyname('PSType').AsString;
    FieldByName('PSId').Value := '';
    post;
  end;

end;

procedure TfrmDyeTechnicsinput.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGridSub();
end;

procedure TfrmDyeTechnicsinput.Tv3DblClick(Sender: TObject);
var
  maxNo: string;
begin
  if GetLSNo(ADOQueryCmd, maxNo, 'JP', 'Dye_Process', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_CopyDyeProcess ');
    SQL.Add(' @PSID = ' + QuotedStr(Trim(CDS_3.fieldbyname('PSID').AsString)));
    SQL.Add(' ,@maxNo = ' + QuotedStr(Trim(maxNo)));
    SQL.Add(' ,@DName = ' + QuotedStr(Trim(DName)));
//    SQL.Add(' ,@DTMID = ' + QuotedStr(Trim(DTMID.Text)));
    Open;
  end;

  with CDS_1 do
  begin
    Append;
    FieldByName('PSNO').Value := CDS_1.RecordCount + 1;
    FieldByName('PSType').Value := CDS_2.fieldbyname('PSType').AsString;
    FieldByName('PSId').Value := maxNo;
    FieldByName('PSName').Value := CDS_3.fieldbyname('PSName').AsString;
    post;
  end;

end;

procedure TfrmDyeTechnicsinput.FormCreate(Sender: TObject);
begin
  inherited;
  fmanage := Trim(DParameters1);
end;

procedure TfrmDyeTechnicsinput.N2Click(Sender: TObject);
var
  i: Integer;
  DTMPSlist: string;
begin
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  CDS_1.Delete;
  SetXH();
end;

procedure TfrmDyeTechnicsinput.PSNameKeyPress(Sender: TObject; var Key: Char);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryTemp, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryTemp, CDS_3);
    SInitCDSData(ADOQueryTemp, CDS_3);
  end;
end;

end.

