unit U_DyeTechnicsinputKaika;

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
  TfrmDyeTechnicsinputKaika = 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;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    VC_GlideNo: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ToolButton2: TToolButton;
    Panel4: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    DTMName: TEdit;
    DTMNo: TEdit;
    TV1PSName: TcxGridDBColumn;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    TbMoveUp: TcxButton;
    btnMoveDown: TcxButton;
    TV1PSID: TcxGridDBColumn;
    TV1PSType: TcxGridDBColumn;
    TV1MCNO: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);

    procedure v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);

    procedure TbMoveUpClick(Sender: TObject);
    procedure btnMoveDownClick(Sender: TObject);
    procedure TV1PSNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);

  private
    { Private declarations }

    procedure InitGlide();
    procedure SetXH();
  public
    fPSIDList: string;
    FPCID, PState: string;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmDyeTechnicsinputKaika: TfrmDyeTechnicsinputKaika;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_DyeProcessSel, U_DyeProcessDefinKaika,
  U_DyeProcessDefin;

{$R *.dfm}

procedure TfrmDyeTechnicsinputKaika.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('GlideNo').Value := FieldByName('XHNoTemp').AsInteger;
        Post;
      end;
      i := i + 1;
      Next;
    end;
    CDS_1.IndexFieldNames := 'GlideNo';
  end;
end;

procedure TfrmDyeTechnicsinputKaika.InitGlide();
var
  maxNo, maxsubid: string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select C.DTMNo,C.DTMName FROM  Dye_Plan_Glide A ');
    sql.Add(' inner join Dye_Technics_Sub B on A.PSID = B.PSID ');
    sql.Add(' inner join Dye_Technics_Main C on B.DTMID = C.DTMID ');
    sql.Add(' Where A.PCID=''' + trim(FPCID) + '''');
//    ShowMessage(sql.text);
    Open;
//    ShowMessage(sql.Text);
  end;
  SCSHData(ADOQueryTemp, Panel4, 2);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.PSType FROM Dye_Plan_Glide A  inner join Dye_Process B');
    sql.Add(' on  A.PSID = B.PSID ');
    sql.Add(' Where A.PCID=''' + trim(FPCID) + '''');
    SQL.Add(' ORDER BY A.GlideNo');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
end;

procedure TfrmDyeTechnicsinputKaika.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyeTechnicsinputKaika := nil;
end;

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

procedure TfrmDyeTechnicsinputKaika.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyeTechnicsinputKaika.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '开卡工艺查看');

  InitGlide();

end;

procedure TfrmDyeTechnicsinputKaika.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('GlideNo').AsInteger := APrevRow;
    test := CDS_1.FieldByName('PSName').AsString;
    CDS_1.Post;

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

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

procedure TfrmDyeTechnicsinputKaika.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '开卡工艺查看');

end;

procedure TfrmDyeTechnicsinputKaika.TV1PSNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmDyeProcessDefinKaika := TfrmDyeProcessDefinKaika.Create(Application);
    with frmDyeProcessDefinKaika do
    begin

      FPSID := Self.CDS_1.FieldByName('PSID').Value;
      FPSType :=  Self.CDS_1.FieldByName('PSType').Value;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyeProcessDefinKaika.Free;
  end;
end;

procedure TfrmDyeTechnicsinputKaika.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 TfrmDyeTechnicsinputKaika.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('GlideNo').AsInteger := ACurrentRow;
    test := CDS_1.FieldByName('PSName').AsString;
    CDS_1.Post;

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



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

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

procedure TfrmDyeTechnicsinputKaika.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 TfrmDyeTechnicsinputKaika.FormCreate(Sender: TObject);
begin
  inherited;
  fmanage := Trim(DParameters1);
end;

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

  CDS_1.Delete;
  SetXH();
end;



end.

