unit U_SCSJJDList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxMemo, cxCheckBox,
  cxPC, Menus, OleCtrls, SHDocVw, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, jpeg, U_SLT1;

type
  TfrmSCSJJDList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N4: TMenuItem;
    http: TIdHTTP;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    CDS_Url: TClientDataSet;
    Tv1Column7: TcxGridDBColumn;
    Panel8: TPanel;
    Panel9: TPanel;
    Image4: TImage;
    Button6: TButton;
    XGNote: TMemo;
    lblJdid: TLabel;
    adoqueryPicture: TADOQuery;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure N4Click(Sender: TObject);
    procedure Image4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    FInt, PFInt: Integer;
    procedure InitGrid();
    procedure InitForm();
    procedure ChangImg();
    procedure InitImage();
    { Private declarations }
  public
    FMainId: string;
    { Public declarations }
  end;

var
  frmSCSJJDList: TfrmSCSJJDList;
  Mach: array of TfrmSlt1;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmSCSJJDList.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 Order_Main.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,FileName,url  from TP_File A  ');
      sql.add('where   A. WBID=  ' + quotedstr(trim(Order_Main.fieldbyname('Jdid').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] := TfrmSlt1.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, adoqueryPicture.fieldbyname('url').AsString);
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmSCSJJDList.ChangImg();
begin
//  if not CDS_Url.IsEmpty then
//  begin
//    CDS_Url.Locate('keyno', CurrentPage, []);
//    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(RecordsNumber);
//    WebBrowser1.Navigate(trim(CDS_Url.fieldbyname('url').AsString));
//  end
//  else
//  begin
//    LBCPAP.Caption := '0/0';
//    WebBrowser1.Navigate('about:blank');
//  end;
end;

procedure TfrmSCSJJDList.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);
  Action := caFree;
end;

procedure TfrmSCSJJDList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(self.Caption), Tv1, '手机生产管理');
  Close;
end;

procedure TfrmSCSJJDList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select   *   from JyOrder_Main_Jd  ');
      sql.Add(' where MainId=  ' + quotedstr(trim(FMainId)));
      sql.Add(' order by idx ');
      Open;
    end;

    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSCSJJDList.InitForm();
begin
  ReadCxGrid(Trim(self.Caption), Tv1, '手机生产管理');
  CurrentPage := 1;
  InitGrid();

end;

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

procedure TfrmSCSJJDList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmSCSJJDList.N1Click(Sender: TObject);
begin
  if not Order_Main.Locate('SSel', True, []) then
  begin
    Application.MessageBox('请选择数据！', '提示', 0);
    Exit;
  end;

  try
    ADOQueryMain.DisableControls;
    while Order_Main.Locate('SSel', True, []) do
    begin
      if Order_Main.FieldByName('P_Code_PB').AsString = '' then
      begin
        with Order_Main do
        begin
          Edit;
          FieldByName('SSel').Value := False;
          Post;
        end;
        Application.MessageBox('产品未指定坯布！', '提示', 0);
        Exit;
      end
      else
      begin
        with ADOQueryMain do
        begin
          Close;
          SQL.Clear;

          SQL.Add('UPDATE PD_Plan_Sub SET ISCL=1 where SubId=' + quotedstr(Trim(Order_Main.fieldbyname('SubId').AsString)));

          SQL.Add('exec P_PD_Insert_PD @MainId=' + quotedstr(Trim(Order_Main.fieldbyname('MainId').AsString)));
          SQL.Add(',@SubId=' + quotedstr(Trim(Order_Main.fieldbyname('SubId').AsString)));
          SQL.Add(',@Filler=' + quotedstr(DName));
          SQL.Add(',@OrdStatus=''正常单''');
//          ShowMessage(SQL.Text);
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSCSJJDList.N2Click(Sender: TObject);
begin
  if not Order_Main.Locate('SSel', True, []) then
  begin
    Application.MessageBox('请选择数据！', '提示', 0);
    Exit;
  end;

  try
    ADOQueryMain.DisableControls;
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;
        SQL.Add('UPDATE PD_Plan_Sub SET ISCL=1 where SubId=' + quotedstr(Trim(Order_Main.fieldbyname('SubId').AsString)));
        ExecSQL;
      end;
      Order_Main.Delete;
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSCSJJDList.N3Click(Sender: TObject);
var
  MMainid: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*');
    sql.Add('from PD_Plan_Main A');
    sql.Add(' where  A.FromMainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.RecordCount > 1 then
  begin
    Application.MessageBox('安排数据有多条不能删除!', '提示', 0);
    Exit;
  end;
  MMainid := Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select TOP 1 A.*');
    sql.Add('from CK_Cloth_CR A');
    sql.Add(' where A.MainId=''' + MMainid + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已产生检验数据不能删除!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try

    ADOQueryCmd.Connection.BeginTrans;
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete PD_Plan_Main where MainId=''' + MMainid + '''');
        sql.Add('delete PD_Plan_Sub where MainId=''' + MMainid + '''');
        ExecSQL;
      end;
      Order_Main.Delete;
    end;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;

    Application.MessageBox('数据删除异常！', '提示', 0);
  end;

end;

procedure TfrmSCSJJDList.FormDestroy(Sender: TObject);
begin
  frmSCSJJDList := nil;
end;

procedure TfrmSCSJJDList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select  cast((ROW_NUMBER() over(order by url )) as int) as keyNo,url from TP_File ');
//    sql.Add(' where WBID=  ' + quotedstr(trim(Order_Main.fieldbyname('Jdid').AsString)));
//    Open;
//  end;
//
//  SCreateCDS20(ADOQueryTemp, CDS_Url);
//  SInitCDSData20(ADOQueryTemp, CDS_Url);
//  RecordsNumber := CDS_Url.RecordCount;
//  ChangImg();
  if Order_Main.IsEmpty then
    Exit;

  InitImage();

end;

procedure TfrmSCSJJDList.N4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty = True then
  begin
    Exit;
  end;
  Panel8.Visible := True;
  xgnote.Text := '';
  XGNote.Text := Trim(Order_Main.fieldbyname('CLNote').AsString);
  lblJdid.Caption := Trim(Order_Main.fieldbyname('Jdid').AsString);
end;

procedure TfrmSCSJJDList.Image4Click(Sender: TObject);
begin
  Panel8.Visible := False;
end;

procedure TfrmSCSJJDList.Button6Click(Sender: TObject);
begin
  if trim(XGNote.Text) = '' then
  begin
    Application.MessageBox('内容不能为空！', '提示', 0);
    Exit;
  end;
  try

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JyOrder_Main_Jd SET hasProblem=0,CLNote=' + QUOTEDSTR(Trim(XGNOTE.Text)));

      sql.Add('where Jdid=' + quotedstr(trim(lblJdid.Caption)));

      execsql;
    end;

    application.MessageBox('处理成功！', '提示信息');
    TBRafresh.Click;
    Panel8.Visible := False;
  except
    application.MessageBox('处理失败！', '提示信息', 0);
  end;
end;

end.

