unit U_CPManage;

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, jpeg, U_SLT, ComObj, Menus,
  cxPC, math, StrUtils;

type
  FdDy = record
    inc: integer; //客户端套接字句柄
    FDdys: string[32]; //客户端套接字
    FdDysName: string[32]; //客户端套接字
  end;

  TfrmCPManage = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryTree20: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxSplitter1: TcxSplitter;
    Panel1: TPanel;
    Label2: TLabel;
    CYID: TEdit;
    TBFilter: TToolButton;
    v1CYNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    v1Column12: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    Label3: TLabel;
    CYNO: TEdit;
    Panel2: TPanel;
    v1Column19: TcxGridDBColumn;
    DSCYNO: TDataSource;
    CDS_CYNO: TClientDataSet;
    ADOQueryTree: TClientDataSet;
    Panel5: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    Label11: TLabel;
    Label12: TLabel;
    Panel3: TPanel;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    v1Column39: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label4: TLabel;
    CYName: TEdit;
    v1Column6: TcxGridDBColumn;
    Label1: TLabel;
    CYColor: TEdit;
    Label6: TLabel;
    CYCF: TEdit;
    Label7: TLabel;
    CYNote: TEdit;
    v1Column7: TcxGridDBColumn;
    OpenDialog1: TOpenDialog;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label9: TLabel;
    CYKZ: TEdit;
    Label8: TLabel;
    CYMF: TEdit;
    v1Column13: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxgrdCYCNHS: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    cxgrdbclmnv1Column17: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column29: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1Column32: TcxGridDBColumn;
    v1Column34: TcxGridDBColumn;
    Panel6: TPanel;
    Label23: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Panel4: TPanel;
    Label14: TLabel;
    LBCPAP1: TLabel;
    Button1: TButton;
    Button2: TButton;
    TCBNOR1: TComboBox;
    v1Column35: TcxGridDBColumn;
    v1Column36: TcxGridDBColumn;
    v1Column37: TcxGridDBColumn;
    v1Column38: TcxGridDBColumn;
    Label5: TLabel;
    CPFL: TEdit;
    Label10: TLabel;
    ZuZhi: TEdit;
    Label13: TLabel;
    OldCYNo: TEdit;
    v1Column40: TcxGridDBColumn;
    Label15: TLabel;
    btn1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBFilterClick(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoChange(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBImportClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CYNOKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TCBNOR1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
    CurrentPage, RecordsNumber: Integer;
    CPID: string;
    PState: Integer;
    FCPID, FTopID: string;
    FJurisdiction: string;
    procedure SetJurisdiction();
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();
  public
    dFdDy: array[0..20] of FdDy; //客户端连接数组
    { Public declarations }
    canshu1: string;
  end;

var
  frmCPManage: TfrmCPManage;
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_Fun;


{$R *.dfm}
procedure TfrmCPManage.SetJurisdiction();
begin
  if trim(self.Caption) = '产品信息录入' then
  begin
    FJurisdiction := '登记';
  end
  else if trim(self.Caption) = '产品信息管理' then
  begin
    FJurisdiction := '管理';
  end
  else
  begin
    FJurisdiction := '查询';
  end;

  if FJurisdiction = '管理' then
  begin
//    case cxTabControl1.TabIndex of
//      0:
//        begin
//          ToolButton1.Visible := true;
//        end;
//      1:
//        begin
//          ToolButton2.Visible := true;
//        end;
//    end;

  end
  else if FJurisdiction = '登记' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin

        end;
      1:
        begin

        end;
    end;

  end;

  if trim(self.Caption) = '产品信息查询(低权限)' then
  begin
    cxgrdCYCNHS.Visible := False;
    cxgrdCYCNHS.Hidden := True;
  end;
end;

procedure TfrmCPManage.InitTree();
var
  i: Integer;
begin
  with ADOQueryTree20 do
  begin
    Close;
    SQL.Clear;

    SQL.Add('exec P_Select_CP_Type ''CP2106060003''');

    Open;
  end;
  SCreateCDS20(ADOQueryTree20, ADOQueryTree);
  SInitCDSData20(ADOQueryTree20, ADOQueryTree);
  cxDBTreeList1.Items[0].Expand(false);
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmCPManage.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Panel2.Visible := True;
  Panel2.Refresh;
  fwhere := 'where status=''1'' ';
     
//  case cxTabControl1.TabIndex of
//    0:
//      begin
//        fwhere := 'where status=''0'' ';
//      end;
//    1:
//      begin
//        fwhere := 'where status=''1'' ';
//      end;
//  end;

  Pwhere := SGetFilters(Panel1, 1, 2);
  if CYNo.TexT <> '' then
  begin
    if POS('*', CYNo.TexT) > 0 then
    begin
      CYNO.Tag := 9;
      if Pwhere = '' then
        Pwhere := ' CYNo  like ' + quotedstr(StringReplace(CYNo.TexT, '*', '%', [rfReplaceAll]));
    end
    else
    begin
      CYNO.Tag := 2;
      Pwhere := SGetFilters(Panel1, 1, 2);
    end;

  end;

  if trim(Pwhere) <> '' then
  begin
    if fwhere <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere)
    else
      fwhere := ' where ' + trim(Pwhere);
  end;
  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add(' exec P_Page_YP  ');
      sql.Add('@CYType=' + quotedstr(Trim(ADOQueryTree.fieldbyname('CPID').AsString)));
      sql.Add(',@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

    LBCPAP1.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));

  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible := False;
end;

procedure TfrmCPManage.FormDestroy(Sender: TObject);
begin
  //frmCPManage:=nil;
end;

procedure TfrmCPManage.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
//  writeCxGrid(self.Caption+tv1.Name,tv1);
 // Application := mainApplication;
  Action := cafree;
end;

procedure TfrmCPManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '样品管理');
  if DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209', sw_hide);
  Close;
  ModalResult := 1;
end;

procedure TfrmCPManage.FormShow(Sender: TObject);
begin
  RecordsNumber := 100;
  Panel3.Align := alclient;
  CurrentPage := 1;
  InitTree();
  ReadCxGrid(self.Caption, Tv1, '样品管理');
  SetJurisdiction();
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select CYID='''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_CYNO);
  SInitCDSData20(ADOQueryTemp, CDS_CYNO);
  with ADOQueryMain do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    SQL.Add('exec P_Select_YP_YDang :Code,:PState,:CYType');
    Parameters.ParamByName('Code').Value := '';
    Parameters.ParamByName('PState').Value := 0;
    Parameters.ParamByName('CYType').Value := 'XXXXXXX';
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_Main);
  SInitCDSData20(ADOQueryMain, CDS_Main);
  CDS_CYNO.Delete;
  ModalResult := 1;
end;

procedure TfrmCPManage.cxDBTreeList1DblClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();

end;

procedure TfrmCPManage.TBRafreshClick(Sender: TObject);
begin
  InitTree();
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCPManage.TBFilterClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;

end;

procedure TfrmCPManage.CYIDKeyPress(Sender: TObject; var Key: Char);
begin

  if Key = #13 then
  begin
    if Trim(CYID.Text) = '' then
      Exit;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('exec P_Select_CP_YDang ');
      SQL.Add('@Code='+quotedstr(Trim(Trim(CYID.Text))));
      SQL.Add(',@PState=''1'' ');
      SQL.Add(',@CYType='''' ');
      //Parameters.ParamByName('Code').Value := Trim(CYID.Text);
      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    CYID.Text := '';
    ADOQueryTree.Locate('CPID', CPID, []);
  end;
end;

procedure TfrmCPManage.CYNoChange(Sender: TObject);
begin
  //if Length(Trim(TEdit(Sender).Text))<4 then Exit;
//  if ADOQueryMain.Active then
//  begin
////    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
////    SCreateCDS20(ADOQueryMain, CDS_Main);
////    SInitCDSData20(ADOQueryMain, CDS_Main);
//    CurrentPage := 1;
//    InitGrid();
//  end;

end;

procedure TfrmCPManage.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel('产品档案', cxGrid1);
end;

procedure TfrmCPManage.TBImportClick(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, FCYNo, t1, t2, t3: string;
begin
  FCPID := Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
  FCPName := Trim(Self.ADOQueryTree.fieldbyname('CPName').AsString);

  if GetLSNo(ADOQueryCmd, maxId, 'Y', 'CP_YDang', 2, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;

  if trim(FCPID) = '' then
    exit;
  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;

  try
    k := 0;
    for i := 1 to 20 do
    begin
      if trim(excelApp.Cells[1, i].value) = '' then
        break;
      if trim(excelApp.Cells[1, i].value) = '类型' then
      begin
        dFdDy[k].inc := i;
        LX := i;
        dFdDy[k].FDdys := 'CPName';
        dFdDy[k].FdDysName := '类型';
      end;
      if trim(excelApp.Cells[1, i].value) = '产品编号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYNo';
        dFdDy[k].FdDysName := '产品编号';
      end;
      if trim(excelApp.Cells[1, i].value) = '产品名称' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYName';
        dFdDy[k].FdDysName := '产品名称';
      end;
      if trim(excelApp.Cells[1, i].value) = '原料成分' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYSpec';
        dFdDy[k].FdDysName := '原料成分';
      end;
      if trim(excelApp.Cells[1, i].value) = '成分' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYCF';
        dFdDy[k].FdDysName := '成分';
      end;
      if trim(excelApp.Cells[1, i].value) = '线长' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ShaChang';
        dFdDy[k].FdDysName := '线长';
      end;
      if trim(excelApp.Cells[1, i].value) = '门幅' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYMF';
        dFdDy[k].FdDysName := '门幅';
      end;
      if trim(excelApp.Cells[1, i].value) = '克重' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYKZ';
        dFdDy[k].FdDysName := '克重';
      end;
      if trim(excelApp.Cells[1, i].value) = '花型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYHX';
        dFdDy[k].FdDysName := '花型';
      end;

      if trim(excelApp.Cells[1, i].value) = '颜色' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYColor';
        dFdDy[k].FdDysName := '颜色';
      end;

      if trim(excelApp.Cells[1, i].value) = '间距' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZhenXing';
        dFdDy[k].FdDysName := '间距';
      end;

      if trim(excelApp.Cells[1, i].value) = '机型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CarType';
        dFdDy[k].FdDysName := '机型';
      end;
      if trim(excelApp.Cells[1, i].value) = '产量' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYCL';
        dFdDy[k].FdDysName := '产量';
      end;
//
//      if trim(excelApp.Cells[1, i].value) = '纱长' then
//      begin
//        dFdDy[k].inc := i;
//        dFdDy[k].FDdys := 'ShaChang';
//        dFdDy[k].FdDysName := '纱长';
//      end;

      if trim(excelApp.Cells[1, i].value) = '备注' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYNote';
        dFdDy[k].FdDysName := '备注';
      end;
      if trim(excelApp.Cells[1, i].value) = '订单号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYConNo';
        dFdDy[k].FdDysName := '订单号';
      end;
      if trim(excelApp.Cells[1, i].value) = '成本核算' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYCNHS';
        dFdDy[k].FdDysName := '成本核算';
      end;
      if trim(excelApp.Cells[1, i].value) = '保护期' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'BaoHuDate';
        dFdDy[k].FdDysName := '保护期';
      end;
      if trim(excelApp.Cells[1, i].value) = '最新订单号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'NewConNo';
        dFdDy[k].FdDysName := '最新订单号';
      end;
      if trim(excelApp.Cells[1, i].value) = '英文品名' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYEName';
        dFdDy[k].FdDysName := '英文品名';
      end;
      if trim(excelApp.Cells[1, i].value) = '产品分类' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYTypeNo';
        dFdDy[k].FdDysName := '产品分类';
      end;
      if trim(excelApp.Cells[1, i].value) = '工艺' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'GYGG';
        dFdDy[k].FdDysName := '工艺备注';
      end;
      if trim(excelApp.Cells[1, i].value) = '斜路' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYXL';
        dFdDy[k].FdDysName := '斜路';
      end;
      if trim(excelApp.Cells[1, i].value) = '老编号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'OldCYNo';
        dFdDy[k].FdDysName := '老编号';
      end;
      if trim(excelApp.Cells[1, i].value) = '订单号前缀' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYConNoQZ';
        dFdDy[k].FdDysName := '订单号前缀';
      end;
      if trim(excelApp.Cells[1, i].value) = '最新订单号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZXCYConNo';
        dFdDy[k].FdDysName := '最新订单号';
      end;
      if trim(excelApp.Cells[1, i].value) = '合同号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ConNo';
        dFdDy[k].FdDysName := '合同号';
      end;
      if trim(excelApp.Cells[1, i].value) = '序号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'XuHao';
        dFdDy[k].FdDysName := '序号';
      end;
      if trim(excelApp.Cells[1, i].value) = '加工厂' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'JiaFactory';
        dFdDy[k].FdDysName := '加工厂';
      end;
      if trim(excelApp.Cells[1, i].value) = '密度' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'MiDu';
        dFdDy[k].FdDysName := '密度';
      end;
      if trim(excelApp.Cells[1, i].value) = '英文成分' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYECF';
        dFdDy[k].FdDysName := '英文成分';
      end;
      if trim(excelApp.Cells[1, i].value) = '坯布' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'PiBu';
        dFdDy[k].FdDysName := '坯布';
      end;
      if trim(excelApp.Cells[1, i].value) = '组织' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZuZhi';
        dFdDy[k].FdDysName := '组织';
      end;
      if trim(excelApp.Cells[1, i].value) = '工艺备注' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'GYNote';
        dFdDy[k].FdDysName := '工艺备注';
      end;
      if trim(excelApp.Cells[1, i].value) = '规格' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'GuiGe';
        dFdDy[k].FdDysName := '规格';
      end;
      k := k + 1;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    for i := 2 to ExcelRowCount do
    begin

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from CP_YDang where 1=2');
        open;
      end;
      if trim(excelApp.Cells[i, LX].value) <> trim(FCPName) then
        continue;
      ADOQueryCmd.Append;
      for j := 0 to k - 1 do
      begin
        if trim(dFdDy[j].FDdys) = '' then
          continue;
        t1 := dFdDy[j].FDdys;
        t2 := inttostr(dFdDy[j].inc);
        t3 := excelApp.Cells[i, dFdDy[j].inc].Value;
        if trim(dFdDy[j].FDdys) <> 'CPName' then
          ADOQueryCmd.fieldbyname(dFdDy[j].FDdys).Value := excelApp.Cells[i, dFdDy[j].inc].Value;
      end;
      ADOQueryCmd.FieldByName('CYValid').Value := 'Y';
      ADOQueryCmd.FieldByName('CYType').Value := Trim(FCPID);
      ADOQueryCmd.FieldByName('CYID').Value := maxId + inttostr(i);
      ADOQueryCmd.Post;

    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.CommitTrans;
    CurrentPage := 1;
    InitGrid();
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存数据错误！', '错误信息', MB_ICONERROR);
    exit;
  end;
end;

procedure TfrmCPManage.ReadINIFile();
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  programIni := Tinifile.create(FileName);
  server := programIni.ReadString('SERVER', '服务器地址', '127.0.0.1');
  programIni.Free;
end;

procedure TfrmCPManage.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty then
    Exit;
{  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang where CYID='''+Trim(CDS_Main.fieldbyname('CYID').asstring)+'''');
    Open;
  end;
  CPID:=Trim(ADOQueryTemp.fieldbyname('CYType').AsString);
  ADOQueryTree.Locate('CPID',CPID,[]);  }
  InitImage();
end;

procedure TfrmCPManage.InitImage();
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if CDS_Main.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName from TP_File A,XD_File B ');
      sql.add('where A.TFID =B.XFID and A.WBID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
      open;
    end;
    j := adoqueryPicture.RecordCount;
    if j < 1 then
      exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(adoqueryPicture.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSlt.Create(Self);
        Mach[i].Name := trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString, adoqueryPicture.fieldbyname('FileName').AsString, jpg);
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmCPManage.CYNOKeyPress(Sender: TObject; var Key: Char);
begin

  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
    TBRafresh.Click;
//    if Trim(CYNO.Text) = '' then
//      Exit;
//    if Length(Trim(CYNO.Text)) < 4 then
//      Exit;
//    with ADOQueryMain do
//    begin
//      Close;
//      Filtered := False;
//      sql.Clear;
//      sql.Add('exec P_Select_CP_YDang :Code,2,'''' ');
//      Parameters.ParamByName('Code').Value := '%' + Trim(CYNO.Text) + '%';
//      Open;
//    end;
//    SCreateCDS20(ADOQueryMain, CDS_Main);
//    SInitCDSData20(ADOQueryMain, CDS_Main);
//    CYID.Text := '';
//    ADOQueryTree.Locate('CPID', CPID, []);
  end;
end;

procedure TfrmCPManage.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmCPManage.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmCPManage.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('BaoHuDate').Index] = null then
    Exit;
  if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('BaoHuDate').Index]) < Now() then
  begin
    ACanvas.Brush.Color := $ADDEFF;
  end;

end;

procedure TfrmCPManage.cxTabControl1Change(Sender: TObject);
begin
  SetJurisdiction();
  CurrentPage := 1;
  initgrid();
end;

procedure TfrmCPManage.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  try
    with CDS_Main do
    begin
      DisableControls;
      while CDS_Main.Locate('SSel', true, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  CP_YDang SET status=1,Chker=' + quotedstr(trim(DName)) + ',Chktime=getdate() ');
          sql.Add('where CYID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
          execsql;
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    application.MessageBox('审核成功！', '提示信息');

  except
    CDS_Main.EnableControls;
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPManage.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  try
    with CDS_Main do
    begin
      DisableControls;
      First;
      while CDS_Main.Locate('SSel', true, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  CP_YDang SET status=0,Chker=null,Chktime=null ');
          sql.Add('where CYID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
          execsql;
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    application.MessageBox('撤销审核成功！', '提示信息');
  except
    CDS_Main.EnableControls;
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPManage.Button1Click(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmCPManage.Button2Click(Sender: TObject);
begin
  if CurrentPage < CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmCPManage.TCBNOR1Change(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR1.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCPManage.FormCreate(Sender: TObject);
begin
  canshu1 := trim(DParameters1);
end;

procedure TfrmCPManage.btn1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

end.

