unit U_DyePBImport;

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

type
  TfrmDyePBImport = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    OpenDialog1: TOpenDialog;
    TbSelFile: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    CDS_LM: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column6: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Tv1ioType: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TbSelFileClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1ioTypePropertiesEditValueChanged(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
    FCTID: string;
  end;

var
  frmDyePBImport: TfrmDyePBImport;

implementation

uses
  U_DataLink, U_RTFun, U_DyeGreyClothInEdit;

{$R *.dfm}

procedure TfrmDyePBImport.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_Cloth_IO where 2= 1 ');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDyePBImport.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyePBImport := nil;
end;

procedure TfrmDyePBImport.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 TfrmDyePBImport.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := cafree;

end;

procedure TfrmDyePBImport.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyePBImport.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption, Tv1, 'BS_Cloth_IO');
  with CDS_LM do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  InitGrid();
  KeyPreview := True;  // 确保表单可以预览按键
end;

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

procedure TfrmDyePBImport.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '样品管理');
end;

procedure TfrmDyePBImport.ToolButton2Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while not eof do
      begin

        with frmDyeGreyClothInEdit.CDS_Sub do
        begin

          Append;
          FieldByName('OrderNo').Value := Trim(CDS_1.fieldbyname('OrderNo').AsString);
          FieldByName('Qty').Value := Trim(CDS_1.fieldbyname('Qty').AsString);
          FieldByName('Piece').Value := Trim(CDS_1.fieldbyname('Piece').AsString);
          FieldByName('Note').Value := Trim(CDS_1.fieldbyname('Note').AsString);
          if (Trim(CDS_1.fieldbyname('iotype').AsString) = '') or (Trim(CDS_1.fieldbyname('iotype').AsString) = null) then
          begin
            FieldByName('iotype').Value := '加工入库';
          end
          else
          begin
            FieldByName('iotype').Value := Trim(CDS_1.fieldbyname('iotype').AsString);
          end;
          Post;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.* from Dye_Plan_Main A  ');
          sql.add(' where MainId = ' + quotedStr(Trim(CDS_1.fieldbyname('OrderNo').AsString)));
          open;
        end;

        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          SQL.Add('select * from BS_Cloth_Info where C_Name = ' + QuotedStr(Trim(ADOQueryCmd.fieldbyname('C_Name').asstring)));
          Open;
        end;

        frmDyeGreyClothInEdit.CDS_Sub.edit;
        frmDyeGreyClothInEdit.CDS_Sub.FieldByName('OrderNo').Value := ADOQueryCmd.fieldbyname('OrderNo').asstring;
        frmDyeGreyClothInEdit.CDS_Sub.FieldByName('C_Name').Value := ADOQueryCmd.fieldbyname('C_Name').asstring;
        frmDyeGreyClothInEdit.CDS_Sub.FieldByName('C_Code').Value := ADOQueryCmd.fieldbyname('C_Code').asstring;
        frmDyeGreyClothInEdit.CDS_Sub.FieldByName('F_Name').Value := ADOQueryCmd.fieldbyname('F_Name').asstring;

        frmDyeGreyClothInEdit.CDS_Sub.FieldByName('finishDescription').Value := ADOQueryCmd.fieldbyname('C_description').asstring;


        frmDyeGreyClothInEdit.CDS_Sub.FieldByName('C_Width').Value := ADOQueryTemp.fieldbyname('C_Width').asstring;
        frmDyeGreyClothInEdit.CDS_Sub.FieldByName('F_Width').Value := ADOQueryTemp.fieldbyname('F_Width').asstring;
        frmDyeGreyClothInEdit.CDS_Sub.FieldByName('Material').Value := ADOQueryTemp.fieldbyname('Material').asstring;

        frmDyeGreyClothInEdit.CDS_Sub.Post;
        Next;
      end;
    end;
    Application.MessageBox('导入成功！', '提示信息', MB_OK);
    ADOQueryCmd.Connection.CommitTrans;
    CDS_1.EnableControls;
    close;
  except
    CDS_1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;

  end;
end;

procedure TfrmDyePBImport.ToolButton3Click(Sender: TObject);
begin
  CDS_1.Delete;
end;

procedure TfrmDyePBImport.Tv1ioTypePropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  CDS_1.First;

  while not CDS_1.Eof do
  begin
    CDS_1.Edit;
    CDS_1.FieldByName('ioType').Value := mvalue;
    CDS_1.Post;
    CDS_1.Next;
  end;

end;

procedure TfrmDyePBImport.TbSelFileClick(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
begin

  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;
    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  CDS_LM.EmptyDataSet;

  for j := 0 to Tv1.ColumnCount - 1 do
  begin
    with CDS_LM do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
  end;

  try
    for i := 1 to 50 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then
        continue;
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin
      with CDS_1 do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_1.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    application.MessageBox('写入数据异常，导入中断！', '错误信息', MB_ICONERROR);
    exit;
  end;

end;

end.

