unit U_MLXXinput;

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, strutils, cxCurrencyEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter, ComObj;

type
  TfrmMLXXinput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column5: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column11: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ToolButton4: TToolButton;
    OpenDialog1: TOpenDialog;
    CDS_LM: TClientDataSet;
    Tv1Column9: 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 ToolButton3Click(Sender: TObject);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v2defstr9PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure Tv2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    { Public declarations }
    RKFlag, FMLID: string;
    fkhType: string;
  end;

var
  frmMLXXinput: TfrmMLXXinput;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmMLXXinput.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_MLXX where MLID=''' + Trim(FMLID) + '''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLXXinput.FormDestroy(Sender: TObject);
begin
  frmMLXXinput := nil;
end;

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

procedure TfrmMLXXinput.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('产品资料录入1', Tv1, '客户自定义');
  Close;
  ModalResult := 1;
end;

procedure TfrmMLXXinput.FormShow(Sender: TObject);
begin
  ReadCxGrid('产品资料录入1', Tv1, '客户自定义');
  InitGrid();
//  if Trim(FMLID) = '' then
//  begin
//    with CDS_HZ do
//    begin
//      Edit;
//      FieldByName('type').Value := '面料';
//      Post;
//    end;
//
//  end;
end;

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

function TfrmMLXXinput.SaveData(): Boolean;
var
  maxId, CRID: string;
begin
  if CDS_HZ.IsEmpty then
    exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_HZ do
    begin
      first;
      while not eof do
      begin
        if (CDS_HZ.FieldByName('MLName').asstring = '') then
        begin
          Next;
        end
        else
        begin
          if Trim(CDS_HZ.fieldbyname('MLid').AsString) = '' then
          begin
            if GetLSNo(ADOQueryCmd, maxId, 'ML', 'KH_Zdy_MLXX', 4, 1) = False then
            begin
              Result := False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取最大号失败！', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            maxId := Trim(FMLID);
          end;

          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from KH_Zdy_MLXX where MLID=''' + Trim(CDS_HZ.fieldbyname('MLid').AsString) + '''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(CDS_HZ.fieldbyname('MLid').AsString) = '' then
            begin
              Append;
              FieldByName('Filler').Value := Trim(DName);
            end
            else
            begin
              Edit;
              FieldByName('Editer').Value := Trim(DName);
              FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
            end;
            FieldByName('MLID').Value := Trim(maxId);
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('Filltime').Value := SGetServerDateTime(ADOQueryTemp);
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_HZ, 'KH_Zdy_MLXX', 0);
            Post;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from KH_Zdy_MLXX where MLName=''' + Trim(CDS_HZ.fieldbyname('MLName').AsString) + ''' and  color=''' + Trim(CDS_HZ.fieldbyname('color').AsString) + '''');
              Open;
            end;
            if ADOQueryCmd.RecordCount > 1 then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('产品编号不能重复!', '提示', 0);
              Exit;
            end;
            with CDS_HZ do
            begin
              Edit;
              FieldByName('MLID').Value := Trim(maxId);
              Post;
            end;
          end;
          next;
        end;
      end;                     
    end;

    ADOQueryCmd.Connection.CommitTrans;
                                                                                                         
    application.MessageBox('保存成功！', '提示');
    Modalresult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmMLXXinput.ToolButton3Click(Sender: TObject);
begin
  cxGrid1.SetFocus;

  if Application.MessageBox('确定要保存数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
//    ModalResult := 1;
  end;
 { try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_MLXX where MLID='' '' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
  if Trim(FMLID) = '' then
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('Type').Value := '面料';
      Post;
    end;
  end;  }
end;

procedure TfrmMLXXinput.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  if rightBstr(trim(mvalue), 1) <> '#' then
  begin
    mvalue := trim(mvalue) + '#';
  end;

  with CDS_HZ do
  begin
    Edit;
    FieldByName('ZdyName').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmMLXXinput.v2defstr9PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  if rightBstr(trim(mvalue), 1) <> '#' then
  begin
    mvalue := trim(mvalue) + '#';
  end;

  with CDS_HZ do
  begin
    Edit;
    FieldByName('ZdyName').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmMLXXinput.v2Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
//  mvalue := TcxTextEdit(Sender).EditingText;
//
//  if trim(mvalue) = '' then
//    exit;
//
//  if rightBstr(trim(mvalue), 1) <> '#' then
//  begin
//    mvalue := trim(mvalue) + '#';
//  end;
//
//  with CDS_HZ do
//  begin
//    Edit;
//    FieldByName('DEFstr3').Value := Trim(mvalue);
//    Post;
//  end;
//  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmMLXXinput.Tv2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  if rightBstr(trim(mvalue), 1) <> '#' then
  begin
    mvalue := trim(mvalue) + '#';
  end;

  with CDS_HZ do
  begin
    Edit;
    FieldByName('CPHH').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmMLXXinput.ToolButton1Click(Sender: TObject);
begin
  CopyAddRow(Tv1, CDS_HZ);
  with CDS_HZ do
  begin
    Edit;
    FieldByName('MLID').Value := '';
    FieldByName('note').Value := '';
    Post;
  end;
end;

procedure TfrmMLXXinput.ToolButton2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if Trim(CDS_HZ.fieldbyname('SubId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete KH_Zdy_MLXX where MLID=''' + Trim(CDS_HZ.fieldbyname('MLID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmMLXXinput.ToolButton4Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
begin
  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;
  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_HZ do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if (CDS_LM.FieldByName('LXH').AsInteger > 0) then
            CDS_HZ.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;
    exit;
  end;

end;

end.

