unit U_DyePCDetailsList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView, Clipbrd,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, MovePanel, cxButtonEdit, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu,
  U_BaseList, dxScrollbarAnnotations, cxContainer, cxTextEdit, cxMaskEdit,
  Vcl.Menus, cxButtons, Vcl.Buttons, cxCheckBox;

type
  TfrmDyePCDetailsList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM1: TcxGridPopupMenu;
    Label3: TLabel;
    pcid: TEdit;
    CDS_1: TClientDataSet;
    MovePanel2: TMovePanel;
    RMGridReport1: TRMGridReport;
    customerNoName: TEdit;
    Label13: TLabel;
    ywy: TEdit;
    Label17: TLabel;
    lbHx: TEdit;
    Label7: TLabel;
    ToolButton1: TToolButton;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    BegTime: TDateTimePicker;
    EndTime: TDateTimePicker;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TbUpdateTime: TToolButton;
    Panel2: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv1PCID: TcxGridDBColumn;
    Tv1OrderNo: TcxGridDBColumn;
    Tv1C_Name: TcxGridDBColumn;
    Tv1PSName: TcxGridDBColumn;
    Tv1CALL_temperature6: TcxGridDBColumn;
    Tv1PCQty: TcxGridDBColumn;
    Tv1time: TcxGridDBColumn;
    Tv1BegTime: TcxGridDBColumn;
    Tv1EndTime: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    GPM2: TcxGridPopupMenu;
    Tv2PC_FLAG: TcxGridDBColumn;
    Tv2isComplete: TcxGridDBColumn;
    MCNO: TcxButtonEdit;
    MCName: TEdit;
    Label4: TLabel;
    Tv1GlideID: TcxGridDBColumn;
    Tv2GlideID: TcxGridDBColumn;
    Tv1IsConflict: TcxGridDBColumn;
    Tv1GlideNo: TcxGridDBColumn;
    Tv2GlideNo: TcxGridDBColumn;
    Panel3: TPanel;
    cxButton1: TcxButton;
    cxButton2: TcxButton;
    TbMoveUp: TcxButton;
    TbMoveDown: TcxButton;
    cbALL_temperature6: TCheckBox;
    cbAll_zhuji: TCheckBox;
    cxComboBox1: TcxComboBox;
    Label5: TLabel;
    Label6: TLabel;
    cbTime: TCheckBox;
    Tv1All_zhuji: TcxGridDBColumn;
    Tv1All_rate: TcxGridDBColumn;
    Tv2CAll_zhuji: TcxGridDBColumn;
    Tv2All_rate: TcxGridDBColumn;
    TbEdit: TToolButton;
    Label8: TLabel;
    C_Name: TEdit;
    Label9: TLabel;
    OrderNo: TEdit;
    SBCCQX1: TSpeedButton;
    SBDX3: TSpeedButton;
    SBDX2: TSpeedButton;
    SBDX1: TSpeedButton;
    SBJG1: TSpeedButton;
    SBDX6: TSpeedButton;
    SBDX5: TSpeedButton;
    SBDX4: TSpeedButton;
    SBJRG1: TSpeedButton;
    SBJRG3: TSpeedButton;
    SBZM1: TSpeedButton;
    SBJRG2: TSpeedButton;
    SBZM2: TSpeedButton;
    SBCCQX2: TSpeedButton;
    SBPD1: TSpeedButton;
    cbPC0: TCheckBox;
    cbPC1: TCheckBox;
    cbBG: TCheckBox;
    cbSC: TCheckBox;
    Tv2BG: TcxGridDBColumn;
    PM_1: TPopupMenu;
    PM_2: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    Panel7: TPanel;
    ToolButton2: TToolButton;
    cbGlideName: TCheckBox;
    Tv2MCNo: TcxGridDBColumn;
    SBTK1: TSpeedButton;
    SBTK2: TSpeedButton;
    Tv2fillTime: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure PRTColorChange(Sender: TObject);
    procedure TBdelClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);

    procedure tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TbUpdateTimeClick(Sender: TObject);
    procedure MCNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

    procedure cxButton1Click(Sender: TObject);
    procedure Tv2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure cxButton2Click(Sender: TObject);
    procedure Tv1BegTimePropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure TbMoveUpClick(Sender: TObject);
    procedure TbMoveDownClick(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure cxComboBox1PropertiesChange(Sender: TObject);
    procedure cbALL_temperature6Click(Sender: TObject);
    procedure TbEditClick(Sender: TObject);
    procedure pcidKeyPress(Sender: TObject; var Key: Char);
    procedure SBCCQX1Click(Sender: TObject);
    procedure cbPC0Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);

  private
    FLeft, FTop: Integer;
    procedure InitGrid1();
    procedure InitGrid2();
    function SaveData(): boolean;
    function GetLastLine(const AText: string): string;
    function SaveMove(const Flag: string): Boolean;
    function GetNextTenMinutes(ACurrentTime: TDateTime): TDateTime;

    function BuildOrderBySQL: string;

    procedure CheckFocusLocation;
    procedure AppIdleSetFocus(Sender: TObject; var Done: Boolean);
    procedure AutoCloseMessage(const Msg: string; CloseTime: Integer = 2000);
    procedure TimerCloseForm(Sender: TObject);
    { Private declarations }
  public
    fsyrName, canshu1: string;
    fsql2: string;

    { Public declarations }
  end;

var
  frmDyePCDetailsList: TfrmDyePCDetailsList;
  OrderList: array of string;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_MachTaskList, U_BS_MachineSel;

{$R *.dfm}

procedure TfrmDyePCDetailsList.CheckFocusLocation;
var
  ActiveForm: TCustomForm;
  FocusedControl: TWinControl;
begin
  // 1. 获取当前活动窗体
  ActiveForm := Screen.ActiveForm;
  if Assigned(ActiveForm) then
    ShowMessage('当前活动窗体: ' + ActiveForm.Name)
  else
    ShowMessage('没有活动窗体');

  // 2. 获取当前拥有焦点的控件
  FocusedControl := Screen.ActiveControl;
  if Assigned(FocusedControl) then
  begin
    ShowMessage('焦点控件: ' + FocusedControl.Name + #13#10'所属窗体: ' + (FocusedControl.Parent as TCustomForm).Name);
  end
  else
    ShowMessage('没有控件获得焦点');

  // 3. 检查本窗体是否拥有焦点
  if Self.Focused then
    ShowMessage('本窗体拥有焦点')
  else if Assigned(Self.ActiveControl) then
    ShowMessage('本窗体的焦点控件: ' + Self.ActiveControl.Name)
  else
    ShowMessage('本窗体没有焦点');
end;

procedure TfrmDyePCDetailsList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyePCDetailsList := nil;
end;

procedure TfrmDyePCDetailsList.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  case Key of

    VK_ESCAPE:
      begin
        Key := 0;  // 清除按键，防止继续传递
        Close;
      end;

    VK_LEFT, Ord('A'), Ord('a'):
      begin
      // 向左移动的逻辑
//        ShowMessage('左');
        cxButton1.Click;
        Key := 0;  // 阻止后续处理
      end;

    VK_RIGHT, Ord('D'), Ord('d'):
      begin
      // 向右移动的逻辑
        cxButton2.Click;
        Key := 0;
      end;

    VK_UP, Ord('W'), Ord('w'):
      begin
      // 向上移动的逻辑
        TbMoveUp.Click;
        Key := 0;
      end;

    VK_DOWN, Ord('S'), Ord('s'):
      begin
      // 向下移动的逻辑
        TbMoveDown.Click;
        Key := 0;
      end;
  end;
end;

function TfrmDyePCDetailsList.SaveData(): boolean;
var
  mLoopNum: Integer;
  mMCName, mNewValue, mStartTime: string;
  mEndTime, mBegTime: string;
begin
  result := false;
  ADOQueryCmd.Connection.BeginTrans;
  try

    mLoopNum := StrToInt(CDS_1.FieldByName('Time').AsString) div 10;

    if (MCNO.Text <> '') and (CDS_1.FieldByName('BegTime').AsString <> '') then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add(' update Dye_Plan_Glide set PC_FLAG = 1 , EndTime = DATEADD(MINUTE, ' + CDS_1.FieldByName('time').AsString + ', ' + QuotedStr(trim(CDS_1.FieldByName('BegTime').AsString)) + ')');
        SQL.Add(' , begTime = ' + QuotedStr(Trim(CDS_1.FieldByName('BegTime').AsString)));
        SQL.Add(' , MCNO = ' + QuotedStr(Trim(MCNO.Text)));
        SQL.Add(' , MCName = ' + QuotedStr(MCName.Text));
        SQL.Add('  where GlideID = ' + QuotedStr(Trim(CDS_1.FieldByName('GlideID').asString)));
//            ShowMessage(sql.Text);
        ExecSQL;
      end;
      mMCName := MCName.Text;
      mStartTime := CDS_1.FieldByName('BegTime').AsString;
//          mNewValue := '计划单:' + CDS_1.FieldByName('OrderNo').AsString + #13;
      mNewValue := mNewValue + CDS_1.FieldByName('C_Name').AsString + #13;
      mNewValue := mNewValue + '成品门幅：' + CDS_1.FieldByName('C_Width').AsString + #13;
      mNewValue := mNewValue + '工序 刮胶次数:' + CDS_1.FieldByName('GlideName').AsString;

      if CDS_1.FieldByName('PSType').AsString = '长车清洗' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;

      end
      else if CDS_1.FieldByName('PSType').AsString = '干定型' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '下水定型' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;

      end
      else if CDS_1.FieldByName('PSType').AsString = '刮胶' then
      begin
        mNewValue := mNewValue + '' + CDS_1.FieldByName('gj_number').AsString + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;

      end
      else if CDS_1.FieldByName('PSType').AsString = '发泡' then
      begin
        mNewValue := mNewValue + ' ' + CDS_1.FieldByName('fp_gjNumber').AsString + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '轧毛' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '机缸清洗' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '卷染缸清洗' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else
      begin
        mNewValue := mNewValue + '' + #13;
      end;
      //////////////////////显示助剂
      mNewValue := mNewValue + '助剂:' + CDS_1.FieldByName('All_zhuji').AsString + ' ' + CDS_1.FieldByName('All_rate').AsString + #13;

      mNewValue := mNewValue + CDS_1.FieldByName('PCID').AsString + #13;
      mNewValue := mNewValue + CDS_1.FieldByName('GlideID').AsString;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('EXEC P_PC_UpdatePCAboutTime ' + '@MCName = :MCName, ' + '@NewValue = :NewValue, ' + '@StartTime = :StartTime, ' + '@LoopNum = :LoopNum');

        Parameters.ParamByName('MCName').Value := mMCName;
        Parameters.ParamByName('NewValue').Value := mNewValue;
        Parameters.ParamByName('StartTime').Value := mStartTime;
        Parameters.ParamByName('LoopNum').Value := mLoopNum;

        ExecSQL;
      end;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select * from PCAboutTime ');
        SQL.Add(' where time >= ' + quotedStr(Trim(mStartTime)));

        Open;
      end;
      ADOQueryCmd.First;
      mNewValue := ADOQueryCmd.FieldByName(mMCName).AsString;
      mBegTime := ADOQueryCmd.FieldByName('time').AsString;
      mEndTime := ADOQueryCmd.FieldByName('time').AsString;
      while not ADOQueryCmd.Eof do
      begin
        if (ADOQueryCmd.FieldByName(mMCName).AsString = null) or (ADOQueryCmd.FieldByName(mMCName).AsString = '') then
        begin
          mEndTime := ADOQueryCmd.FieldByName('time').AsString;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('update Dye_Plan_Glide set BegTime = ' + QuotedStr(Trim(mBegTime)));
            sql.Add(' , EndTime = ' + QuotedStr(Trim(mEndTime)));
            SQL.Add(' where GlideID = ' + QuotedStr(Trim(GetLastLine(mNewValue))));
            ExecSQL;
          end;
          ADOQueryCmd.Connection.CommitTrans;
          result := true;
          Exit;
        end;

        if mNewValue <> ADOQueryCmd.FieldByName(mMCName).AsString then
        begin
          mEndTime := ADOQueryCmd.FieldByName('time').AsString;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('update Dye_Plan_Glide set BegTime = ' + QuotedStr(Trim(mBegTime)));
            sql.Add(' , EndTime = ' + QuotedStr(Trim(mEndTime)));
            SQL.Add(' where GlideID = ' + QuotedStr(Trim(GetLastLine(mNewValue))));
            ExecSQL;
          end;
          mBegTime := ADOQueryCmd.FieldByName('time').AsString;
          mNewValue := ADOQueryCmd.FieldByName(mMCName).AsString;
        end;

        ADOQueryCmd.Next;
      end;

    end;

    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    result := false;
  end;
end;

function TfrmDyePCDetailsList.SaveMove(const Flag: string): Boolean;
var
  mLoopNum: Integer;
  mMCName, mNewValue, mStartTime: string;
  mEndTime, mBegTime: string;
begin
  Result := False; // 默认返回失败
  ADOQueryCmd.Connection.BeginTrans;
  try
    if Flag = '上移' then
    begin
      CDS_1.Prior;
      mStartTime := CDS_1.FieldByName('BegTime').AsString;
      CDS_1.Next;
    end
    else if Flag = '下移' then
    begin
      mStartTime := CDS_1.FieldByName('BegTime').AsString;
      CDS_1.Next;
    end;

    mLoopNum := StrToInt(CDS_1.FieldByName('Time').AsString) div 10;

    if (MCNO.Text <> '') and (CDS_1.FieldByName('BegTime').AsString <> '') then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add(' update Dye_Plan_Glide set PC_FLAG = 1 , EndTime = DATEADD(MINUTE, ' + CDS_1.FieldByName('time').AsString + ', ' + QuotedStr(trim(mStartTime)) + ')');
        SQL.Add(' , begTime = ' + QuotedStr(Trim(mStartTime)));
        SQL.Add(' , MCNO = ' + QuotedStr(Trim(MCNO.Text)));
        SQL.Add(' , MCName = ' + QuotedStr(MCName.Text));
        SQL.Add('  where GlideID = ' + QuotedStr(Trim(CDS_1.FieldByName('GlideID').asString)));
        ExecSQL;
      end;
      mMCName := MCName.Text;

      mNewValue := mNewValue + CDS_1.FieldByName('C_Name').AsString + #13;
      mNewValue := mNewValue + '成品门幅：' + CDS_1.FieldByName('C_Width').AsString + #13;
      mNewValue := mNewValue + '工序 刮胶次数:' + CDS_1.FieldByName('GlideName').AsString;

      if CDS_1.FieldByName('PSType').AsString = '长车清洗' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '干定型' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '下水定型' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '刮胶' then
      begin
        mNewValue := mNewValue + '' + CDS_1.FieldByName('gj_number').AsString + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '发泡' then
      begin
        mNewValue := mNewValue + ' ' + CDS_1.FieldByName('fp_gjNumber').AsString + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '轧毛' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '机缸清洗' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else if CDS_1.FieldByName('PSType').AsString = '卷染缸清洗' then
      begin
        mNewValue := mNewValue + '' + #13;
        mNewValue := mNewValue + '车速:' + CDS_1.FieldByName('All_Speed').AsString + #13;
        mNewValue := mNewValue + '温度6#:' + CDS_1.FieldByName('ALL_temperature6').AsString + #13;
      end
      else
      begin
        mNewValue := mNewValue + '' + #13;
      end;

      // 显示助剂
      mNewValue := mNewValue + '助剂:' + CDS_1.FieldByName('All_zhuji').AsString + ' ' + CDS_1.FieldByName('All_rate').AsString + #13;
      mNewValue := mNewValue + CDS_1.FieldByName('PCID').AsString + #13;
      mNewValue := mNewValue + CDS_1.FieldByName('GlideID').AsString;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('UPDATE PCAboutTime SET ');
        SQL.Add(mMCName + ' = null WHERE ');
        SQL.Add(mMCName + ' = ' + QuotedStr(Trim(mNewValue)));
        ExecSQL;
      end;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('EXEC P_PC_UpdatePCAboutTime ' + '@MCName = :MCName, ' + '@NewValue = :NewValue, ' + '@StartTime = :StartTime, ' + '@LoopNum = :LoopNum');

        Parameters.ParamByName('MCName').Value := mMCName;
        Parameters.ParamByName('NewValue').Value := mNewValue;
        Parameters.ParamByName('StartTime').Value := mStartTime;
        Parameters.ParamByName('LoopNum').Value := mLoopNum;

        ExecSQL;
      end;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select * from PCAboutTime ');
        SQL.Add(' where time >= ' + quotedStr(Trim(mStartTime)));
        Open;
      end;

      ADOQueryCmd.First;
      mNewValue := ADOQueryCmd.FieldByName(mMCName).AsString;
      mBegTime := ADOQueryCmd.FieldByName('time').AsString;
      mEndTime := ADOQueryCmd.FieldByName('time').AsString;

      while not ADOQueryCmd.Eof do
      begin
        if (ADOQueryCmd.FieldByName(mMCName).AsString = null) or (ADOQueryCmd.FieldByName(mMCName).AsString = '') then
        begin
          mEndTime := ADOQueryCmd.FieldByName('time').AsString;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('update Dye_Plan_Glide set BegTime = ' + QuotedStr(Trim(mBegTime)));
            sql.Add(' , EndTime = ' + QuotedStr(Trim(mEndTime)));
            SQL.Add(' where GlideID = ' + QuotedStr(Trim(GetLastLine(mNewValue))));
            ExecSQL;
          end;
          ADOQueryCmd.Connection.CommitTrans;
          Result := True; // 操作成功
          Exit;
        end;

        if mNewValue <> ADOQueryCmd.FieldByName(mMCName).AsString then
        begin
          mEndTime := ADOQueryCmd.FieldByName('time').AsString;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('update Dye_Plan_Glide set BegTime = ' + QuotedStr(Trim(mBegTime)));
            sql.Add(' , EndTime = ' + QuotedStr(Trim(mEndTime)));
            SQL.Add(' where GlideID = ' + QuotedStr(Trim(GetLastLine(mNewValue))));
            ExecSQL;
          end;
          mBegTime := ADOQueryCmd.FieldByName('time').AsString;
          mNewValue := ADOQueryCmd.FieldByName(mMCName).AsString;
        end;

        ADOQueryCmd.Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True; // 操作成功
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Result := False; // 操作失败
  end;
end;

procedure TfrmDyePCDetailsList.SBCCQX1Click(Sender: TObject);
begin
  if Sender is TSpeedButton then
  begin
    case (Sender as TSpeedButton).Tag of  // 用Tag区分不同按钮
      1:
        begin
          MCNO.Text := '长车清洗机1';
          MCName.Text := 'CCQX1';
          InitGrid1();
        end;
      2:
        begin
          MCNO.Text := '长车清洗机2';
          MCName.Text := 'CCQX2';
          InitGrid1();
        end;
      3:
        begin
          MCNO.Text := '定型机1';
          MCName.Text := 'DX1';
          InitGrid1();
        end;
      4:
        begin
          MCNO.Text := '定型机2';
          MCName.Text := 'DX2';
          InitGrid1();
        end;
      5:
        begin
          MCNO.Text := '定型机3';
          MCName.Text := 'DX3';
          InitGrid1();
        end;
      6:
        begin
          MCNO.Text := '定型机4';
          MCName.Text := 'DX4';
          InitGrid1();
        end;
      7:
        begin
          MCNO.Text := '定型机5';
          MCName.Text := 'DX5';
          InitGrid1();
        end;
      8:
        begin
          MCNO.Text := '定型机6';
          MCName.Text := 'DX6';
          InitGrid1();
        end;
      9:
        begin
          MCNO.Text := '机缸1';
          MCName.Text := 'JG1';
          InitGrid1();
        end;
      10:
        begin
          MCNO.Text := '卷染缸1';
          MCName.Text := 'JRG1';
          InitGrid1();
        end;
      11:
        begin
          MCNO.Text := '卷染缸2';
          MCName.Text := 'JRG2';
          InitGrid1();
        end;
      12:
        begin
          MCNO.Text := '卷染缸3';
          MCName.Text := 'JRG3';
          InitGrid1();
        end;
      13:
        begin
          MCNO.Text := '轧毛单排针1';
          MCName.Text := 'ZM1';
          InitGrid1();
        end;
      14:
        begin
          MCNO.Text := '轧毛双排针2';
          MCName.Text := 'ZM2';
          InitGrid1();
        end;
      15:
        begin
          MCNO.Text := '拍打机1';
          MCName.Text := 'PD1';
          InitGrid1();
        end;
      16:
        begin
          MCNO.Text := '推框机1';
          MCName.Text := 'TK1';
          InitGrid1();
        end;
      17:
        begin
          MCNO.Text := '推框机2';
          MCName.Text := 'TK2';
          InitGrid1();
        end;
    end;
  end;

end;

function TfrmDyePCDetailsList.GetLastLine(const AText: string): string;
var
  StringList: TStringList;
begin
  Result := '';
  StringList := TStringList.Create;
  try
    StringList.Text := AText; // 自动按回车分割
    if StringList.Count > 0 then
      Result := StringList[StringList.Count - 1]; // 获取最后一行
  finally
    StringList.Free;
  end;
end;

function TfrmDyePCDetailsList.GetNextTenMinutes(ACurrentTime: TDateTime): TDateTime;
begin
  // 计算到下一个整十分数
  Result := Trunc(ACurrentTime * 24 * 6) / (24 * 6);  // 取整到10分钟
  if Result <= ACurrentTime then
    Result := Result + (10 / (24 * 60));  // 如果取整后小于等于当前时间，加10分钟
end;

procedure TfrmDyePCDetailsList.cxButton1Click(Sender: TObject);
var
  MaxEndTime: TDateTime;
  mGlideID1, mGlideID2: string;
  mConflict1, mConflict2: Integer;
begin
  mConflict1 := 0;
  mConflict2 := 0;
  if CDS_2.FieldByName('PC_FLAG').AsString = '1' then
  begin
    Application.MessageBox('工序已排产！', '提示信息', MB_OK);
    exit;
  end
  else if CDS_2.FieldByName('BG').asString = '1' then
  begin
    Application.MessageBox('工序已报工！', '提示信息', MB_OK);
    exit;
  end;

  if (CDS_2.FieldByName('time').AsString = '0') or (CDS_2.FieldByName('time').IsNull) then
  begin
    Application.MessageBox('总用时错误！', '提示信息', MB_OK);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryMain do
    begin
      close;
      filtered := false;
      sql.Clear;
      SQL.Add('select Max(EndTime) as MaxTime from V_Dye_Cloth_PaiGang where (BegTime between :StartTime and :EndTime) and PC_FLAG = 1 and MCNO = :MCNO ');
      Parameters.ParamByName('StartTime').Value := Trunc(BegDate.Date) + Frac(BegTime.Time);
      Parameters.ParamByName('EndTime').Value := Trunc(EndDate.Date) + Frac(EndTime.Time);
      Parameters.ParamByName('MCNO').Value := MCNO.Text;
      Open;
    end;

    if (ADOQueryMain.RecordCount > 0) and (not ADOQueryMain.FieldByName('MaxTime').IsNull) then
      MaxEndTime := ADOQueryMain.FieldByName('MaxTime').AsDateTime
    else
      MaxEndTime := GetNextTenMinutes(Now);  // 使用下一个整十分数


    with CDS_1 do
    begin

      append;

      FieldByName('PCID').value := CDS_2.FieldByName('PCID').AsString;
      FieldByName('OrderNo').value := CDS_2.FieldByName('OrderNo').AsString;
      FieldByName('GlideName').value := CDS_2.FieldByName('GlideName').AsString;
      FieldByName('C_name').value := CDS_2.FieldByName('C_name').AsString;
      FieldByName('ALL_temperature6').value := CDS_2.FieldByName('ALL_temperature6').AsString;
      FieldByName('time').value := CDS_2.FieldByName('time').AsString;
      FieldByName('GlideID').value := CDS_2.FieldByName('GlideID').AsString;
      FieldByName('PSType').value := CDS_2.FieldByName('PSType').AsString;
      FieldByName('C_Width').value := CDS_2.FieldByName('C_Width').AsString;
      FieldByName('All_Speed').value := CDS_2.FieldByName('All_Speed').AsString;

      FieldByName('GlideNo').value := CDS_2.FieldByName('GlideNo').AsString;
      FieldByName('All_zhuji').value := CDS_2.FieldByName('All_zhuji').AsString;
      FieldByName('All_rate').value := CDS_2.FieldByName('All_rate').AsString;
      FieldByName('gj_number').AsString := CDS_2.FieldByName('gj_number').AsString;
      FieldByName('fp_gjNumber').AsString := CDS_2.FieldByName('fp_gjNumber').AsString;

      FieldByName('PCQty').AsString := CDS_2.FieldByName('PCQty').AsString;

      FieldByName('begTime').value := MaxEndTime;

      Post;
    end;

              //////////////排产顺序时间////////
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_2.FieldByName('PCID').AsString)));
      SQL.Add(' and PC_FLAG = 1 and endTime >  ' + quotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', MaxEndTime)));
      SQL.Add(' and GlideNo < ' + QuotedStr(Trim(CDS_2.FieldByName('GlideNo').AsString)));
//      ShowMessage(sql.Text);
      open;
      if not IsEmpty then
      begin
        if Application.MessageBox('工序时间段冲突！是否继续？', '提示信息', MB_OKCANCEL + MB_ICONQUESTION) = IDOK then
        begin
          mConflict1 := 1;
        end
        else
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          InitGrid1();
          Exit;
        end;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_2.FieldByName('PCID').AsString)));
      SQL.Add(' and PC_FLAG = 1 and BegTime <  DATEADD(MINUTE, ' + CDS_2.FieldByName('time').AsString + ', ' + QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', MaxEndTime)) + ')');
      SQL.Add(' and GlideNo > ' + QuotedStr(Trim(CDS_2.FieldByName('GlideNo').AsString)));
//      ShowMessage(sql.Text);
      open;
      if not IsEmpty then
      begin
        if Application.MessageBox('工序时间段冲突！是否继续？', '提示信息', MB_OKCANCEL + MB_ICONQUESTION) = IDOK then
        begin
          mConflict2 := 1;
        end
        else
        begin
      // 用户点击"取消"，退出
          ADOQueryCmd.Connection.RollbackTrans;
          InitGrid1();
          Exit;
        end;
      end;
    end;
/////////////////////////////////////////////
    ADOQueryCmd.Connection.CommitTrans;
  except
    if ADOQueryCmd.Connection.InTransaction then
      ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//      showMessage('插入任务数据时发生错误!');
  end;
  if SaveData() then
  begin
    if mConflict1 = 1 then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('UPDATE Dye_Plan_Glide SET IsConflict = 1');
        SQL.Add('WHERE PCID = ' + QuotedStr(Trim(CDS_2.FieldByName('PCID').AsString)));
        SQL.Add('AND PC_FLAG = 1 AND endTime > ' + QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', MaxEndTime)));
        SQL.Add('AND GlideNo <= ' + QuotedStr(Trim(CDS_2.FieldByName('GlideNo').AsString)));
        ExecSQL;
      end;
    end;
    if mConflict2 = 1 then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('UPDATE Dye_Plan_Glide SET IsConflict = 1');
        SQL.Add('WHERE PCID = ' + QuotedStr(Trim(CDS_2.FieldByName('PCID').AsString)));
        SQL.Add('AND PC_FLAG = 1 AND BegTime < DATEADD(MINUTE, ' + CDS_2.FieldByName('time').AsString + ', ' + QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', MaxEndTime)) + ')');
        SQL.Add('AND GlideNo >= ' + QuotedStr(Trim(CDS_2.FieldByName('GlideNo').AsString)));
        ExecSQL;
      end;
    end;

    Application.MessageBox('排产成功', '提示信息！', MB_OK);
  end;

  if not CDS_1.IsEmpty then
    mGlideID1 := Trim(CDS_1.FieldByName('GlideID').AsString)
  else
    mGlideID1 := '';
  if not CDS_2.IsEmpty then
    mGlideID2 := Trim(CDS_2.FieldByName('GlideID').AsString)
  else
    mGlideID2 := '';
  initGrid1();
  initGrid2();
  // 最后尝试定位回原记录
  if mGlideID1 <> '' then
    CDS_1.Locate('GlideID', mGlideID1, []);
  // 最后尝试定位回原记录
  if mGlideID2 <> '' then
    CDS_2.Locate('GlideID', mGlideID2, []);

end;

procedure TfrmDyePCDetailsList.cxButton2Click(Sender: TObject);
var
  mGlideID1, mGlideID2: string;
  mConflict: Integer;
begin
  if CDS_1.IsEmpty then
  begin
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add(' select A.* from V_Dye_Cloth_PaiGang A ');
    SQL.Add(' where A.PCID = ' + QuotedStr(Trim(CDS_2.FieldByName('PCID').AsString)) );
    sql.Add(' and A.GlideNo = ' + CDS_2.FieldByName('GlideNo').AsString );
    sql.Add(' and (A.KaZuState = 10 ');
    sql.Add(' or (SELECT COUNT(*) FROM Dye_Plan_Glide_OutPut X WHERE X.PCID = A.PCID AND X.GlideNo = A.GlideNo)>1) ');
//    ShowMessage(sql.Text);
    Open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('已完成或已下机工序不能取消！','提示信息',MB_OK);
    Exit;
  end;

  if MessageDlg('确定取消？此操作无法撤销！', mtWarning, [mbOK, mbCancel], 0) = mrOK then
  begin
  // 开始事务
    ADOQueryCmd.Connection.BeginTrans;

    try

      mConflict := 0;
    // 第一个更新操作
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('UPDATE PCAboutTime SET ');
        SQL.Add(MCName.Text + ' = null WHERE ');
        SQL.Add(' time >= ' + QuotedStr(CDS_1.FieldByName('BegTime').AsString));
        SQL.Add(' and time < ' + QuotedStr(CDS_1.FieldByName('EndTime').AsString));
//        showMessage(sql.Text);
        ExecSQL;
      end;

    // 第二个更新操作
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add(' update Dye_Plan_Glide set PC_FLAG = 0,IsConflict = 0,endTime = null,begTime = null,MCNO = null,MCName = null where GlideID = ' + QuotedStr(Trim(CDS_1.FieldByName('GlideID').asString)));
        ExecSQL;
      end;
/////////////////////////////检查时间冲突/////////////////////
      with ADOQueryBaseCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
        sql.Add(' and PC_FLAG = 1');
        Open;
      end;
      ADOQueryBaseCmd.First;
      while not ADOQueryBaseCmd.Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
          SQL.Add(' and PC_FLAG = 1 and endTime >  ' + quotedStr(ADOQueryBaseCmd.FieldByName('BegTime').AsString));
          SQL.Add(' and GlideNo < ' + QuotedStr(Trim(ADOQueryBaseCmd.FieldByName('GlideNo').AsString)));
//        ShowMessage(sql.Text);
          open;
          if not IsEmpty then
          begin

            mConflict := 1;

          end;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
          SQL.Add(' and PC_FLAG = 1 and BegTime <  ' + quotedStr(ADOQueryBaseCmd.FieldByName('EndTime').AsString));
          SQL.Add(' and GlideNo > ' + QuotedStr(Trim(ADOQueryBaseCmd.FieldByName('GlideNo').AsString)));
//      ShowMessage(sql.Text);
          open;
          if not IsEmpty then
          begin
            mConflict := 1;
          end;
        end;

        if mConflict = 0 then
        begin
          ADOQueryBaseCmd.Edit;
          ADOQueryBaseCmd.FieldByName('IsConflict').Value := 0;
          ADOQueryBaseCmd.Post;
        end;

/////////////////////////////////////////////
        ADOQueryBaseCmd.Next;
      end;

                 //////////////排产顺序时间////////

    // 两个操作都成功，提交事务
      ADOQueryCmd.Connection.CommitTrans;

      Application.MessageBox('取消成功！', '提示', 0);
      if not CDS_1.IsEmpty then
        mGlideID1 := Trim(CDS_1.FieldByName('GlideID').AsString)
      else
        mGlideID1 := '';
      if not CDS_2.IsEmpty then
        mGlideID2 := Trim(CDS_2.FieldByName('GlideID').AsString)
      else
        mGlideID2 := '';
      initGrid1();
      initGrid2();
  // 最后尝试定位回原记录
      if mGlideID1 <> '' then
        CDS_1.Locate('GlideID', mGlideID1, []);
  // 最后尝试定位回原记录
      if mGlideID2 <> '' then
        CDS_2.Locate('GlideID', mGlideID2, []);

    except
      on E: Exception do
      begin
      // 发生异常，回滚事务
        ADOQueryCmd.Connection.RollbackTrans;
        ShowMessage('操作失败：' + E.Message);
      end;
    end;
  end;
end;

procedure TfrmDyePCDetailsList.cxComboBox1PropertiesChange(Sender: TObject);
begin
  InitGrid2();
end;

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

procedure TfrmDyePCDetailsList.FormCreate(Sender: TObject);
begin
  inherited;

//  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
//  BegDate.DateTime := EndDate.DateTime;
  fsyrName := trim(DdataBase);
  canshu1 := trim(DParameters1);
//  SInitComBoxBySql(ADOQueryTemp, GlideName, false, 'select PSName as Name from Dye_Process');

end;

procedure TfrmDyePCDetailsList.InitGrid1();
var
  mBegDate, mendDate: string;
  mAction: string;
  rs: _Recordset;
  r: integer;
begin
  // 任务时间

  try
    tv1.BeginUpdate;
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      close;
      filtered := false;
      sql.Clear;
      SQL.Add('select * from V_Dye_Cloth_PaiGang where (BegTime between :StartTime and :EndTime) and PC_FLAG = 1 and MCName = :MCName order By BegTime ');
      Parameters.ParamByName('StartTime').Value := Trunc(BegDate.Date) + Frac(BegTime.Time);
      Parameters.ParamByName('EndTime').Value := Trunc(EndDate.Date) + Frac(EndTime.Time);
      Parameters.ParamByName('MCName').Value := MCName.Text;
//      ShowMessage(sql.Text);
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    tv1.EndUpdate;
   // tv1.ApplyBestFit();
  end;

end;

procedure TfrmDyePCDetailsList.InitGrid2();
var
  mBegDate, mendDate: string;
  mAction: string;
  rs: _Recordset;
  r: integer;
  msql1: string;
  CheckBox: TCheckBox;
begin
  // 任务时间
  try
    tv1.BeginUpdate;
    ADOQueryMain.DisableControls;

    if cbPC0.Checked then
    begin
      if msql1 = '' then
      begin
        msql1 := ' (PC_FLAG = 0 and BG=0 and Complete=0) ';
      end
      else
      begin
        msql1 := msql1 + ' or (PC_FLAG = 0) ';
      end;
//      msql1 := msql1 + ' or (PC_FLAG = 0) ';
    end;
//    else
//    begin
//      if msql1 = '' then
//      begin
//        msql1 := ' (PC_FLAG <> 0) ';
//      end
//      else
//      begin
//        msql1 := msql1 + ' and (PC_FLAG <> 0) ';
//      end;
//    end;

    if cbPC1.Checked then
    begin
      if msql1 = '' then
      begin
        msql1 := ' (PC_FLAG = 1 and BG=0 and Complete=0) ';
      end
      else
      begin
        msql1 := msql1 + ' or (PC_FLAG = 1) ';
      end;
//      msql1 := msql1 + ' or (PC_FLAG = 1) ';
    end;
//    else
//    begin
//      if msql1 = '' then
//      begin
//        msql1 := ' (PC_FLAG <> 1) ';
//      end
//      else
//      begin
//        msql1 := msql1 + ' and (PC_FLAG <> 1) ';
//      end;
//    end;

    if cbSC.Checked then
    begin
      if msql1 = '' then
      begin
        msql1 := ' (Complete > 0) ';
      end
      else
      begin
        msql1 := msql1 + ' or (Complete > 0) ';
      end;
//      msql1 := msql1 + ' or (Complete > 0) ';
    end;
//    else
//    begin
//      if msql1 = '' then
//      begin
//        msql1 := ' (Complete <> 1) ';
//      end
//      else
//      begin
//        msql1 := msql1 + ' and (Complete <> 1) ';
//      end;
//    end;

    if cbBG.Checked then
    begin
      if msql1 = '' then
      begin
        msql1 := ' (BG = 1)';
      end
      else
      begin
        msql1 := msql1 + ' or (BG = 1)';
      end;
//      msql1 := msql1 + ' or (BG = 1)';
    end;
//    else
//    begin
//      if msql1 = '' then
//      begin
//        msql1 := ' (BG <> 1) ';
//      end
//      else
//      begin
//        msql1 := msql1 + ' and (BG <> 1) ';
//      end;
//    end;


    fsql2 := BuildOrderBySQL;
    with ADOQueryMain do
    begin
      close;
      filtered := false;
      sql.Clear;
      SQL.Add(' select A.* from V_Dye_Cloth_PaiGang A where 1=1  ');
      sql.Add(' AND  A.KaZuState = 9 ');
      sql.Add(' and (SELECT COUNT(*) FROM Dye_Plan_Glide_OutPut X WHERE X.PCID = A.PCID AND X.GlideNo = A.GlideNo)<2 ');
      if msql1 <> '' then
      begin
        sql.Add(' and ( ' + msql1 + ' )');
      end;

      SQL.Add(fsql2);
//            ShowMessage(sql.Text);
      Open;

    end;
    if Tv2.DataController.Filter.FilterText <> '' then
    begin
      SDofilter(ADOQueryMain, Tv2.DataController.Filter.FilterText);
    end;

    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);

  finally
    ADOQueryMain.EnableControls;
    tv1.EndUpdate;
   // tv1.ApplyBestFit();
  end;
end;



// 处理复选框点击
procedure TfrmDyePCDetailsList.cbALL_temperature6Click(Sender: TObject);
var
  CheckBox: TCheckBox;
  FieldName, OrderText: string;
  I, Index: Integer;
begin
  CheckBox := Sender as TCheckBox;

  if CheckBox = cbALL_temperature6 then
    FieldName := 'ALL_temperature6'
  else if CheckBox = cbAll_zhuji then
    FieldName := 'All_zhuji'
  else if CheckBox = cbTime then
    FieldName := 'Time'
  else if CheckBox = cbGlideName then
    FieldName := 'GlideName'
  else
    Exit;

  OrderText := FieldName + ' ASC';

  // 查找是否已存在
  Index := -1;
  for I := 0 to High(OrderList) do
  begin
    if OrderList[I] = OrderText then
    begin
      Index := I;
      Break;
    end;
  end;

  if CheckBox.Checked then
  begin
    // 添加或移动到末尾
    if Index >= 0 then
    begin
      // 已存在，先删除
      for I := Index to High(OrderList) - 1 do
        OrderList[I] := OrderList[I + 1];
      SetLength(OrderList, Length(OrderList) - 1);
    end;
    // 添加到末尾
    SetLength(OrderList, Length(OrderList) + 1);
    OrderList[High(OrderList)] := OrderText;
  end
  else
  begin
    // 取消选中，移除
    if Index >= 0 then
    begin
      for I := Index to High(OrderList) - 1 do
        OrderList[I] := OrderList[I + 1];
      SetLength(OrderList, Length(OrderList) - 1);
    end;
  end;

  InitGrid2();
end;

procedure TfrmDyePCDetailsList.cbPC0Click(Sender: TObject);
begin
  InitGrid2();

end;

// 构建排序SQL（使用动态数组版本）
function TfrmDyePCDetailsList.BuildOrderBySQL: string;
var
  I: Integer;
  OrderStr: string;
begin
  // 如果没有选择任何排序，使用默认排序
  if Length(OrderList) = 0 then
  begin
    Result := ' order By PCID ASC , GlideNo ASC';
    Exit;
  end;

  // 拼接SQL
  OrderStr := '';
  for I := 0 to High(OrderList) do
  begin
    if I > 0 then
      OrderStr := OrderStr + ' , ';
    OrderStr := OrderStr + OrderList[I];
  end;

  Result := ' Order By ' + OrderStr;
end;

procedure TfrmDyePCDetailsList.MCNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmBS_MachineSel := TfrmBS_MachineSel.Create(Application);
    with frmBS_MachineSel do
    begin

      if ShowModal = 1 then
      begin
        MCNO.Text := frmBS_MachineSel.Order_Main.FieldByName('MCNO').Value;
        MCName.Text := frmBS_MachineSel.Order_Main.FieldByName('MCName').Value;
        Self.InitGrid1();
      end;
    end;
  finally
    frmBS_MachineSel.Free;
  end;
end;

procedure TfrmDyePCDetailsList.N1Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

procedure TfrmDyePCDetailsList.N2Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_2.fieldbyname(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmDyePCDetailsList.TBRafreshClick(Sender: TObject);
begin
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid1();
  InitGrid2();
  MovePanel2.Visible := False;
end;

procedure TfrmDyePCDetailsList.TbUpdateTimeClick(Sender: TObject);
var
  DeletedCount, InsertedCount: Integer;
  Status, StartDate, EndDate: string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_InsertDelTimeToPC @MinuteInterval = 10');
    Open;

    if not IsEmpty then
    begin
      Status := FieldByName('状态').AsString;
      DeletedCount := FieldByName('删除记录数').AsInteger;
      InsertedCount := FieldByName('插入记录数').AsInteger;
      StartDate := FieldByName('保留开始时间').AsString;
      EndDate := FieldByName('保留结束时间').AsString;

      ShowMessage(Format('时间数据更新成功！' + #13#10 + '删除记录：%d 条' + #13#10 + '插入记录：%d 条' + #13#10 + '时间范围：%s 至 %s', [DeletedCount, InsertedCount, StartDate, EndDate]));
    end
    else
    begin
      ShowMessage('时间数据更新完成！');
    end;

    Close;
  end;
end;

procedure TfrmDyePCDetailsList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '排产信息明细');
  WriteCxGrid(self.Caption + tv2.Name, Tv2, '排产信息明细');
end;

procedure TfrmDyePCDetailsList.ToolButton2Click(Sender: TObject);
begin
  CheckFocusLocation();
end;

procedure TfrmDyePCDetailsList.Tv1BegTimePropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  Begtime: TDateTime;
  Hour, Min, Sec, MSec: Word;
begin
  try
    // 使用 DisplayValue 而不是 Sender.AsString
    if VarIsNull(DisplayValue) or (DisplayValue = '') then
      Exit;

    Begtime := VarToDateTime(DisplayValue);
    DecodeTime(Begtime, Hour, Min, Sec, MSec);

    if Sec <> 0 then
    begin
      ErrorText := '开始时间秒数必须为 0！';
      Error := True;  // 设置错误标志
    end
    else if (Min mod 10) <> 0 then  // 添加分钟验证
    begin
      ErrorText := '开始时间分钟必须为 10 的倍数！当前分钟为：' + IntToStr(Min);
      Error := True;
    end
    else
    begin
      Error := False; // 清除错误标志
    end;

  except
    on E: Exception do
    begin
      ErrorText := '时间格式错误：' + E.Message;
      Error := True;
    end;
  end;
end;

procedure TfrmDyePCDetailsList.tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  mIsConflict: Variant;
begin
  // 获取字段值
  mIsConflict := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('IsConflict').Index];

  // 设置不同状态的颜色和字体
  if mIsConflict = 1 then               // 未排入计划单
  begin
    ACanvas.Brush.Color := clRed;  // 红色背景
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];
  end
  else  // 默认情况
  begin
    ACanvas.Brush.Color := clWindow;
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];
  end;
  if AViewInfo.GridRecord.Selected then
  begin

    ACanvas.Font.Color := $000080FF;
    ACanvas.Font.Style := [fsBold];

  end;
  // 如果需要自定义绘制完成，取消下面的注释
  // ADone := True;
end;

procedure TfrmDyePCDetailsList.Tv2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
//  ShowMessage(CDS_2.FieldByName('isComplete').asString);

end;

procedure TfrmDyePCDetailsList.Tv2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  PC_FLAG, Complete, BG: Variant;
begin
  // 获取字段值
  PC_FLAG := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('PC_FLAG').Index];
  Complete := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('Complete').Index];
  BG := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('BG').Index];
//  if AViewInfo.GridRecord.Selected then
//  begin
//    ADone := False;  // 让控件继续使用默认的选中样式
//    Exit;
//  end;

  // 设置不同状态的颜色和字体
  if (BG = 1) then // 报工未点完成
  begin
    ACanvas.Brush.Color := $00E0E0E0;    // 浅灰色背景
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];
  end
  else if (Complete > 0) then // 生产中
  begin
    ACanvas.Brush.Color := $00E0FFE0;    // 浅绿色背景
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];      //
  end
  else if (PC_FLAG = 1) then      // 已排产
  begin
    ACanvas.Brush.Color := clWindow;     // 默认窗口背景色
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];
  end
  else if (PC_FLAG = 0) then               // 未排产
  begin
    ACanvas.Brush.Color := $00CCCCFF;  // 浅红色背景
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];
  end
  else  // 默认情况
  begin
    ACanvas.Brush.Color := clWindow;
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];
  end;

  if AViewInfo.GridRecord.Selected then
  begin
    ACanvas.Font.Color := $000080FF;
    ACanvas.Font.Style := [fsBold];
  end;


  // 如果需要自定义绘制完成，取消下面的注释
  // ADone := True;
end;

procedure TfrmDyePCDetailsList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmDyePCDetailsList.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmDyePCDetailsList.FormShow(Sender: TObject);
begin
  KeyPreview := True;
//  ShowMessage(BoolToStr(KeyPreview));
  inherited;
//
  ReadCxGrid(self.Caption + tv1.Name, Tv1, '排产信息明细');
  ReadCxGrid(self.Caption + tv2.Name, Tv2, '排产信息明细');
  canshu1 := self.fParameters1;
//  BegDate.DateTime := strToDateTime(FormatDateTime('yyyy-MM-dd 00:00:00', SGetServerDate(ADOQueryTemp))) - 1;
//  EndDate.DateTime := strToDateTime(FormatDateTime('yyyy-MM-dd 00:00:00', SGetServerDate(ADOQueryTemp))) + 1;
  var ServerDate: TDateTime := SGetServerDate(ADOQueryTemp);

  BegDate.Date := Trunc(ServerDate) - 1;  // 前一天
  EndDate.Date := Trunc(ServerDate) + 1;  // 后一天
  Begtime.Time := EncodeTime(0, 0, 0, 0);   // 早上8点
  endTime.Time := EncodeTime(0, 0, 0, 0);  // 晚上6点
  InitGrid1();
  InitGrid2();

  // 在应用程序空闲时设置焦点（确保所有初始化完成）
  Application.OnIdle := AppIdleSetFocus;

//  CheckFocusLocation();
//  ShowMessage(BoolToStr(KeyPreview));
end;

procedure TfrmDyePCDetailsList.AutoCloseMessage(const Msg: string; CloseTime: Integer = 2000);
var
  Form: TForm;
  Label1: TLabel;
  Timer: TTimer;
begin
  Form := TForm.Create(nil);
  try
    Form.BorderStyle := bsDialog;
    Form.Caption := '提示';
    Form.Position := poScreenCenter;
    Form.Width := 300;
    Form.Height := 120;

    Label1 := TLabel.Create(Form);
    Label1.Parent := Form;
    Label1.Caption := Msg + #13#10 + IntToStr(CloseTime div 1000) + '秒后自动关闭...';
    Label1.Align := alClient;
    Label1.Alignment := taCenter;
    Label1.Layout := tlCenter;
    Label1.WordWrap := True;

    Timer := TTimer.Create(Form);
    Timer.Interval := CloseTime;
    Timer.OnTimer := TimerCloseForm;  // 使用独立的方法

    // 需要存储 Form 引用以便在事件中访问
    Tag := Integer(Form);  // 使用 Tag 存储引用

    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

// 独立的计时器事件处理方法
procedure TfrmDyePCDetailsList.TimerCloseForm(Sender: TObject);
var
  Form: TForm;
begin
  // 从 Sender 获取 Timer
  TTimer(Sender).Enabled := False;

  // 获取关联的 Form
  Form := TForm(TTimer(Sender).Owner);
  if Assigned(Form) then
    Form.ModalResult := mrOk;
end;

procedure TfrmDyePCDetailsList.AppIdleSetFocus(Sender: TObject; var Done: Boolean);
begin
  // 确保只执行一次
  Application.OnIdle := nil;

  // 将焦点设置到本窗体
  if Self.CanFocus then
  begin
    Self.SetFocus;
//     可选：输出调试信息

    AutoCloseMessage('操作成功！', 1000); // 1秒后自动关闭

//     ShowMessage('焦点已设置到窗体: ' + Self.Name);

  end;

  Done := True; // 保持默认处理流程
end;

procedure TfrmDyePCDetailsList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(self.Caption, cxGrid1);
end;

procedure TfrmDyePCDetailsList.TBFindClick(Sender: TObject);
begin
  InitGrid1();
  if ADOQueryMain.Active then
  begin
//    ShowMessage(IntToStr(ADOQueryMain.RecordCount));
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
//    ShowMessage(IntToStr(ADOQueryMain.RecordCount));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmDyePCDetailsList.TbMoveDownClick(Sender: TObject);
var
  mGlideID1, mGlideID2: string;
  mConflict: Integer;
begin
  if SaveMove('下移') then
  begin
          /////////////////////////////检查时间冲突/////////////////////
    with ADOQueryBaseCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
      sql.Add(' and PC_FLAG = 1');
      Open;
    end;
    ADOQueryBaseCmd.First;
    while not ADOQueryBaseCmd.Eof do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
        SQL.Add(' and PC_FLAG = 1 and endTime >  ' + quotedStr(ADOQueryBaseCmd.FieldByName('BegTime').AsString));
        SQL.Add(' and GlideNo < ' + QuotedStr(Trim(ADOQueryBaseCmd.FieldByName('GlideNo').AsString)));
//        ShowMessage(sql.Text);
        open;
        if not IsEmpty then
        begin

          mConflict := 1;

        end;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
        SQL.Add(' and PC_FLAG = 1 and BegTime <  ' + quotedStr(ADOQueryBaseCmd.FieldByName('EndTime').AsString));
        SQL.Add(' and GlideNo > ' + QuotedStr(Trim(ADOQueryBaseCmd.FieldByName('GlideNo').AsString)));
//      ShowMessage(sql.Text);
        open;
        if not IsEmpty then
        begin
          mConflict := 1;
        end;
      end;

      if mConflict = 0 then
      begin
        ADOQueryBaseCmd.Edit;
        ADOQueryBaseCmd.FieldByName('IsConflict').Value := 0;
        ADOQueryBaseCmd.Post;
      end
      else
      begin
        ADOQueryBaseCmd.Edit;
        ADOQueryBaseCmd.FieldByName('IsConflict').Value := 1;
        ADOQueryBaseCmd.Post;
      end;

/////////////////////////////////////////////
      ADOQueryBaseCmd.Next;
    end;

    if not CDS_1.IsEmpty then
      mGlideID1 := Trim(CDS_1.FieldByName('GlideID').AsString)
    else
      mGlideID1 := '';
    if not CDS_2.IsEmpty then
      mGlideID2 := Trim(CDS_2.FieldByName('GlideID').AsString)
    else
      mGlideID2 := '';
    initGrid1();
    initGrid2();
  // 最后尝试定位回原记录
    if mGlideID1 <> '' then
      CDS_1.Locate('GlideID', mGlideID1, []);
  // 最后尝试定位回原记录
    if mGlideID2 <> '' then
      CDS_2.Locate('GlideID', mGlideID2, []);
    ShowMessage('下移成功！');
  end
  else
    ShowMessage('下移失败！');
end;

procedure TfrmDyePCDetailsList.TbMoveUpClick(Sender: TObject);
var
  mGlideID1, mGlideID2: string;
  mConflict: Integer;
begin
  if SaveMove('上移') then
  begin

    /////////////////////////////检查时间冲突/////////////////////
    with ADOQueryBaseCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
      sql.Add(' and PC_FLAG = 1');
      Open;
    end;
    ADOQueryBaseCmd.First;
    while not ADOQueryBaseCmd.Eof do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
        SQL.Add(' and PC_FLAG = 1 and endTime >  ' + quotedStr(ADOQueryBaseCmd.FieldByName('BegTime').AsString));
        SQL.Add(' and GlideNo < ' + QuotedStr(Trim(ADOQueryBaseCmd.FieldByName('GlideNo').AsString)));
//        ShowMessage(sql.Text);
        open;
        if not IsEmpty then
        begin

          mConflict := 1;

        end;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(CDS_1.FieldByName('PCID').AsString)));
        SQL.Add(' and PC_FLAG = 1 and BegTime <  ' + quotedStr(ADOQueryBaseCmd.FieldByName('EndTime').AsString));
        SQL.Add(' and GlideNo > ' + QuotedStr(Trim(ADOQueryBaseCmd.FieldByName('GlideNo').AsString)));
//      ShowMessage(sql.Text);
        open;
        if not IsEmpty then
        begin
          mConflict := 1;
        end;
      end;

      if mConflict = 0 then
      begin
        ADOQueryBaseCmd.Edit;
        ADOQueryBaseCmd.FieldByName('IsConflict').Value := 0;
        ADOQueryBaseCmd.Post;
      end
      else
      begin
        ADOQueryBaseCmd.Edit;
        ADOQueryBaseCmd.FieldByName('IsConflict').Value := 1;
        ADOQueryBaseCmd.Post;
      end;

      ADOQueryBaseCmd.Next;
    end;


    /////////////////////////////////////////////
    if not CDS_1.IsEmpty then
      mGlideID1 := Trim(CDS_1.FieldByName('GlideID').AsString)
    else
      mGlideID1 := '';
    if not CDS_2.IsEmpty then
      mGlideID2 := Trim(CDS_2.FieldByName('GlideID').AsString)
    else
      mGlideID2 := '';
    initGrid1();
    initGrid2();
  // 最后尝试定位回原记录
    if mGlideID1 <> '' then
      CDS_1.Locate('GlideID', mGlideID1, []);
  // 最后尝试定位回原记录
    if mGlideID2 <> '' then
      CDS_2.Locate('GlideID', mGlideID2, []);
    ShowMessage('上移成功！');
  end
  else
    ShowMessage('上移失败！');
end;

procedure TfrmDyePCDetailsList.pcidKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid1();
  end;

end;

procedure TfrmDyePCDetailsList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDyePCDetailsList.TBdelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Dye_Need_Up(UType,UDataId) values(''机台刷卡'',' + quotedstr(Trim(CDS_1.fieldbyname('GOID').AsString)) + ')  ');
      sql.Add('delete Dye_Plan_Glide_OutPut where GOID=''' + Trim(CDS_1.fieldbyname('GOID').AsString) + '''');

      ExecSQL;
    end;
//////////////////       更新进度       //////////////////
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Dye_Card_upCL @PCID=' + QuotedStr(CDS_1.fieldbyname('pcid').AsString));

      execsql;
    end;
//////////////////       更新进度       //////////////////
    CDS_1.Delete;
  except
    application.MessageBox('删除失败！', '提示信息', 0);
  end;

end;

procedure TfrmDyePCDetailsList.TbEditClick(Sender: TObject);
var
  mConflict: Integer;
begin

  if CDS_1.IsEmpty then
  begin
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add('select * from PCAboutTime where time = ' + QuotedStr(Trim(CDS_1.FieldByName('begtime').AsString)));
    Open;
  end;
//
  frmMachTaskList := TfrmMachTaskList.create(self);
  with frmMachTaskList do
  begin
    fBegTime := Self.CDS_1.FieldByName('Begtime').AsString;
    fMCName := Self.MCName.Text;
    fText := self.ADOQueryCmd.FieldByName(fMCName).AsString;
    showModal;
    free;
  end;


  /////////////////////////////检查时间冲突/////////////////////
  with ADOQueryBaseCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(Self.CDS_1.FieldByName('PCID').AsString)));
    sql.Add(' and PC_FLAG = 1');
    Open;
  end;
  ADOQueryBaseCmd.First;
  while not ADOQueryBaseCmd.Eof do
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(Self.CDS_1.FieldByName('PCID').AsString)));
      SQL.Add(' and PC_FLAG = 1 and endTime >  ' + quotedStr(ADOQueryBaseCmd.FieldByName('BegTime').AsString));
      SQL.Add(' and GlideNo < ' + QuotedStr(Trim(ADOQueryBaseCmd.FieldByName('GlideNo').AsString)));
//        ShowMessage(sql.Text);
      open;
      if not IsEmpty then
      begin

        mConflict := 1;

      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add(' select * from Dye_Plan_Glide where PCID = ' + QuotedStr(Trim(Self.CDS_1.FieldByName('PCID').AsString)));
      SQL.Add(' and PC_FLAG = 1 and BegTime <  ' + quotedStr(ADOQueryBaseCmd.FieldByName('EndTime').AsString));
      SQL.Add(' and GlideNo > ' + QuotedStr(Trim(ADOQueryBaseCmd.FieldByName('GlideNo').AsString)));
//      ShowMessage(sql.Text);
      open;
      if not IsEmpty then
      begin
        mConflict := 1;
      end;
    end;

    if mConflict = 0 then
    begin
      ADOQueryBaseCmd.Edit;
      ADOQueryBaseCmd.FieldByName('IsConflict').Value := 0;
      ADOQueryBaseCmd.Post;
    end
    else
    begin
      ADOQueryBaseCmd.Edit;
      ADOQueryBaseCmd.FieldByName('IsConflict').Value := 1;
      ADOQueryBaseCmd.Post;
    end;

    ADOQueryBaseCmd.Next;
  end;

  initgrid1();
end;

end.

