unit U_YarnCFInfoSel;

interface

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

type
  TfrmYarnCFInfoSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSaveForMat: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    CF: TEdit;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Tv1Column4: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    TbSave: TToolButton;
    Tv1SSel: TcxGridDBColumn;
    Tv1CHNName: TcxGridDBColumn;
    Tv1VieName: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBSaveForMatClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure CFChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TbSaveClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    function SaveData(): Boolean;
    { Private declarations }
  public
    FSTKName: string;
    { Public declarations }
  end;

var
  frmYarnCFInfoSel: TfrmYarnCFInfoSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmYarnCFInfoSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmYarnCFInfoSel.CFChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;

end;

procedure TfrmYarnCFInfoSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  CF.SetFocus;

  Action := cahide;
end;

procedure TfrmYarnCFInfoSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('SELECT A.*');
      SQL.Add('FROM BS_Yarn_Info_CF A');
      SQL.Add('WHERE A.BYIID IS NULL');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYarnCFInfoSel.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid('纱线成分选择' + Trim(FSTKName), TV1, '纱线成分选择');
  InitGrid();
end;

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

procedure TfrmYarnCFInfoSel.TbSaveClick(Sender: TObject);
begin
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示信息', MB_OK);
  end;

end;

function TfrmYarnCFInfoSel.SaveData(): Boolean;
var
  maxCFID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
      //////////////////       保存成分表          //////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin

        if Trim(CDS_1.fieldbyname('CFID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxCFID, 'CF', 'BS_Yarn_Info_CF', 5, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;

          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from BS_Yarn_Info_CF where 1 = 2 ');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(CDS_1.fieldbyname('CFID').AsString) = '' then
            begin
              Append;

            end;
            FieldByName('CFID').Value := Trim(maxCFID);
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'BS_Yarn_Info_CF', 0);
            Post;
          end;

        end
        else
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from BS_Yarn_Info_CF where CFID = ' + QuotedStr(Trim(CDS_1.fieldbyname('CFID').AsString)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(CDS_1.fieldbyname('CFID').AsString) <> '' then
            begin
              edit;

            end;
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'BS_Yarn_Info_CF', 0);
            Post;
          end;
        end;
        Next;
      end;
    end;
////////////////       保存成分表结束          //////////////////////


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

end;

procedure TfrmYarnCFInfoSel.TBSaveForMatClick(Sender: TObject);
begin
  WriteCxGrid('纱线成分选择' + Trim(FSTKName), TV1, '纱线成分选择');
end;

procedure TfrmYarnCFInfoSel.ToolButton1Click(Sender: TObject);
begin
  CF.SetFocus;
  ModalResult := 1;
end;

procedure TfrmYarnCFInfoSel.ToolButton2Click(Sender: TObject);
begin
  CDS_1.Append;
  CDS_1.FieldByName('SSel').Value := False;
  CDS_1.Post;
end;

procedure TfrmYarnCFInfoSel.ToolButton3Click(Sender: TObject);
begin
//  CDS_1.First;
//  while not CDS_1.Eof do
//  begin
//    if CDS_1.FieldByName('SSel').AsBoolean = True then
//    begin
//
//
//
//
//    end;
//    CDS_1.Next;  // 移动到下一条记录
//  end;

  if CDS_1.FieldByName('CFID').AsString <> '' then
  begin
    if MessageDlg('确定要删除这条记录吗？', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('delete from BS_Yarn_Info_CF where CFID = ' + QuotedStr(Trim(CDS_1.FieldByName('CFID').AsString)));
        ExecSQL;
      end;
      CDS_1.Delete;
      ShowMessage('删除成功！');
    end
    else
    begin
      Exit;
    end;
  end
  else
  begin
    CDS_1.Delete;
  end;
end;

procedure TfrmYarnCFInfoSel.TV1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmYarnCFInfoSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmYarnCFInfoSel := nil;
end;

end.

