unit uMeasureHight;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uWorkBase, dxExEdtr, ADODB, DB, ExtCtrls, dxBar, dxBarExtItems,
  ImgList, dxTL, dxCntner, StdCtrls, SPComm,uComm,uFunMesClient,uFunCom,
  MPlayer;

type
  TfrmMeasureHight = class(TfrmWorkBase)
    Panel1: TPanel;
    dxBarLargeButton3: TdxBarLargeButton;
    dxBarLargeButton4: TdxBarLargeButton;
    Comm1: TComm;
    Panel3: TPanel;
    GroupBox1: TGroupBox;
    Panel5: TPanel;
    Panel6: TPanel;
    plCurRout: TPanel;
    Image6: TImage;
    moCurRout: TMemo;
    Shape1: TShape;
    Label6: TLabel;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    Shape7: TShape;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    edtNum: TEdit;
    edtMin: TEdit;
    edtMax: TEdit;
    Timer2: TTimer;
    ShowNODuct: TTimer;
    lblProductNo: TLabel;
    lblProductName: TLabel;
    Label1: TLabel;
    edtCode: TEdit;
    imgSpeaker: TImage;
    lblSpeaker: TLabel;
    Panel2: TPanel;
    Image2: TImage;
    ImgGreen: TImage;
    Image1: TImage;
    Image3: TImage;
    ImgYel: TImage;
    ImgRed: TImage;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    ImageOff: TImage;
    ImageOn: TImage;
    Timwarning: TTimer;
    ErrPlayer: TMediaPlayer;
    procedure dxBarLargeButton3Click(Sender: TObject);
    procedure dxBarLargeButton4Click(Sender: TObject);
    procedure moCurRoutKeyPress(Sender: TObject; var Key: Char);
    procedure edtCodeKeyPress(Sender: TObject; var Key: Char);
    procedure Timer2Timer(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    procedure FormCreate(Sender: TObject);
    procedure ShowNODuctTimer(Sender: TObject);
    procedure LbtnNewClick(Sender: TObject);
    procedure TimwarningTimer(Sender: TObject);
  private
    PathErr: String;
    meNum: Real;   
    procedure OpenCom;
    procedure ComStop;
    procedure InitWrkCheckFace;
    function  CDuctBarCodeIn(Barcode: String): Boolean;
    function  DuctBWorkIsOK(CurBringID: String): Boolean;
    procedure ToWarning ;
    { Private declarations }
  public
    x,warningx: integer ;
    function Getlight(Str: String): Real;
    function MeasurebyRoutBarcode(CurrootBar: String): Boolean;
    { Public declarations }
  end;

var
  frmMeasureHight: TfrmMeasureHight;

implementation

uses PublicDBUnit;

{$R *.dfm}

procedure TfrmMeasureHight.ComStop;
begin
  Comm1.StopComm ;
  ImageOff.Visible  := true;
  ImageOn.Visible   := false ;
end;

procedure TfrmMeasureHight.dxBarLargeButton3Click(Sender: TObject);
begin
  ComStop;
  With TfrmComm.Create(Self) do
  begin
    if ShowModal= mrOk then
    begin
      Application.Terminate ;
    end;
    Free;
    OpenCom;
  end;

end;

procedure TfrmMeasureHight.dxBarLargeButton4Click(Sender: TObject);
begin
  //inherited;
  OpenCom;
end;

procedure TfrmMeasureHight.OpenCom;
var
  tmpLst: TStringList;
begin
  tmpLst := TStringList.Create;
  tmpLst.Delimiter := ',';
  tmpLst.DelimitedText := ReadFromIni;
  Comm1.StopComm ;
  Comm1.CommName := tmpLst.Values['ComName'];
  Comm1.BaudRate := StrToint(tmpLst.Values['BaudRate']);
  Comm1.Parity := None;
  Comm1.ByteSize := _8;
  Comm1.StopBits := _1;
  try
    Comm1.StartComm;
    ImageOff.Visible  := false;
    ImageOn.Visible   := true ;
  except
    ImageOff.Visible  := true;
    ImageOn.Visible   := false ;
  end;   
end;

function TfrmMeasureHight.CDuctBarCodeIn(Barcode: String): Boolean;
var
  C_barCode: String;
Const
  Selsql = 'SELECT tabProduct.ProductNO, tabProduct.ProductName, tabCrtCode.ID,tabproduct.id FROM tabCrtCode '
    + 'INNER JOIN tabProduct ON tabCrtCode.DuctID = tabProduct.ID WHERE tabCrtCode.TmpCode=''%s''';
begin
  C_barCode := trim(Barcode);
  if C_barCode = '' then Exit ;
  if isSysCode(edtCode.Text) then    Exit;
  if BarcodeIsExist(C_barCode) then  //条码 是否登记
  begin
    MyDuct.Barcode := C_barCode ;
    CurBringID := GetBringID(C_BarCode); // 得到随工单主表ID
    if DuctBWorkIsOK(CurBringID) then   //工位是否 正确
    begin
      if moCurRout.Enabled then
         moCurRout.SetFocus  ;
    end else
    begin
      edtCode.Text := '' ;
      edtCode.SetFocus ;
    end;
  end
  else begin
    edtCode.Text := '' ;
    lblSpeaker.Caption := ' 条码没有登记到系统，请先进行条码登记！';
    imgSpeaker.Visible := True;
    lblSpeaker.Visible := True;
    ShowNODuct.Enabled := True;
    edtCode.SetFocus ;
  end;
end;

function TfrmMeasureHight.DuctBWorkIsOK(CurBringID: String): Boolean;
var
  S: String;
const
   StateArr: array[0..1] of string =('异常', '正常');  
begin
  Result := false;
  with dmPublic do
  begin
    if CurBringID ='' then //A  随工单 不存在
    begin
      S := 'Select top 1 ID,proworkID,WorkNO from tabworkflow '
        + ' Where tabworkflow.DuctID =' + intToStr(MyWork.DuctID )+ ' and tabworkflow.workflag= 0  order by  tabWorkFlow.OrderID  ' ;
      ExcuteSQLCommand(S,1);
      if not qryCom1.IsEmpty then
      begin
        if   qryCom1.Fields[0].AsInteger <> MyWork.DWorkID then
        begin
          ShowMessage(Pchar('正确工位是:'+qryCom1.Fields[2].AsString));
          Exit;
        end;
      end  else
      begin
        ShowMessage('商品工序没有定义');
        Exit;
      end;
    end
    else  begin  //A 存在时  CurDate, WorkTxt, ManName, Remark, ToWork, iState,Remark
      S := ' select toworkID, CurDate, WorkTxt, ManName, ToWork, iState,Remark from tabcodeBringSub where Pid ='+ CurBringID + ' and isEnd = 1';
      ExcuteSQLCommand(S,1);
      if qryCom1.IsEmpty then
      begin
        S :=  'begin tran a1 delete from tabcodeBringSub where pid= ' + CurBringID
          +'  delete from tabcodeBring where id= ' + CurBringID + '  commit tran a1' ;
        ExcuteSQLCommand(S,0);
        exit;
      end else
      begin
        if   qryCom1.Fields[0].AsInteger <> MyWork.DWorkID then
        begin
          ShowMessage(Pchar('正确工位是: '+qryCom1.Fields[4].AsString));
          Exit;
        end;
      end;
    end;
  end;
  Result := true;

end;

function TfrmMeasureHight.Getlight(Str: String): Real;
var
  I: real;
  Code: Integer;
begin
  Val(str, I, Code);
  while  Code >0 do
  begin
    Delete(Str,Code,1);
    Val(str, I, Code);
  end;
  Result := i;

end;

procedure TfrmMeasureHight.InitWrkCheckFace;
begin
  moCurRout.Clear ;
  edtCode.Text := '';
  MyDuct.Barcode := '' ;
  edtCode.SetFocus ;
  ImgGreen.Visible := false ;
  ImgRed.Visible := false ;
  meNum := 0 ;
  edtNum.Text := '0.00g';
end;

function TfrmMeasureHight.MeasurebyRoutBarcode(CurrootBar: String): Boolean;
var
  isOK: Boolean;
  iState: integer;
  Selsql,S: String;
  toWorkID,Towork,Remark: String;
const
  SetEndSQL = 'Update tabCodeBring Set iType =1 Where ID =%s';
begin
  Result := false ;
  if (meNum > MyWork.MaxNum) or (Menum < MyWork.MinNum)then
  begin
    ToWarning;  
    //MessageBox(handle,pchar('工艺条码不正确！'),'系统提示',MB_OK + MB_ICONWARNING);
    Exit;
  end;  
  CurrootBar  :=  trim(CurrootBar) ;
  if CurrootBar = '' then  Exit;

  CurBringID :=  GetBringID(MyDuct.Barcode);
  with dmPublic do
  begin
    if CurBringID = '' then
      CurBringID :=  SaveBringID(MyDuct,IntToStr(curLineID));

    if CurrootBar = OkNo then
    begin
      iState := 1 ;
      toWorkID := intToStr(MyWork.nextWorkID);
      Towork := MyWork.nextWorkName;
      Remark := MyWork.WorkName + ' 测量值:'+ FloatToStr(meNum);
    end  else
    begin
      iState := 0;
      S := 'SELECT ID, toWorkID ToWork, iState,Remark FROM tabRouting WHERE RoutNo ='''+ CurrootBar +''' ';
      with dmpublic do
      begin
        ExcuteSQLCommand(S,1);
        if qryCom1.IsEmpty then
        begin
          MessageBox(handle,pchar('工艺条码不正确！'),'系统提示',MB_OK + MB_ICONWARNING);
          Exit;
        end
        else begin
          toWorkID := qryCom1.Fields[1].AsString ;
          ToWork := qryCom1.Fields[2].AsString ;
          Remark := qryCom1.Fields[4].AsString ;
        end;
      end;
    end;
    isOK := false;
    acnDB.BeginTrans ;
    isOK :=  SaveAutoWork(ManworkList);  //保存排班
    if isOK then isOk := SaveCodeBringSub(CurBringID,CurrootBar ,toWorkID,ToWork,Remark,MyWork,meNum,iState);
    if isOK then
    begin
      SaveMductIDbyList(MDuctAddNumList,inttoStr(CurLineID),MyDuct.Barcode); // 保存自动安装料

      addToRecList(MyDuct.DuctID,MyDuct.DuctNo,MyDuct.DuctName,MyDuct.Barcode);
      // AddtoTask(MyDuct.DuctID,mywork.WorkNo,CurLineID);
      if MyWork.isEnd then
      begin
        ExcuteSQLCommand(Format(SetEndSQL, [CurBringID]), 0);
        AddToTask(MyDuct.DuctID,'DoNumber',CurLineID);
        S := ' update tabCurTask set finish = 1 where finish <> 1 and  Donumber >= sNumber  ';
        dmPublic.ExcuteSQLCommand(S,0);
      end;
    end;
    if isOK then acnDB.CommitTrans
    else acnDB.BeginTrans ;
  end;

  Result := true  ;

end;

procedure TfrmMeasureHight.moCurRoutKeyPress(Sender: TObject; var Key: Char);
begin
  if key <> #13 then Exit;
  if MeasurebyRoutBarcode(moCurRout.Text) then
    LbtnNew.Click
  else moCurRout.SelectAll ;


end;

procedure TfrmMeasureHight.edtCodeKeyPress(Sender: TObject; var Key: Char);
begin
  if Key <> #13 then Exit;
  meNum := 0;
  CDuctBarCodeIn(edtCode.Text) ;
end;

procedure TfrmMeasureHight.Timer2Timer(Sender: TObject);
begin
  ImageOff.Visible  := true;
  ImageOn.Visible   := false ;
end;

procedure TfrmMeasureHight.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var
  i: real;
  str,firchar :string;
begin
  ImageOff.Visible  := false ;
  ImageOn.Visible   := true ;
  if edtCode.Text = '' then Exit;
  x := x + 1 ;
  x := X mod 3;
  if x = 0 then
  begin
    SetLength(Str,BufferLength);
    move(buffer^,pchar(@Str[1])^,bufferlength);
    str := trim(Str);
    firchar := Copy(str,1,1);
    if firchar <> '+' then
    begin
      ImgYel.Visible := not ImgYel.Visible ;
      Exit;
    end;
    i := GetLight(str) ;
    if meNum <> i then
    begin
      meNum := i ;
      if (meNum > MyWork.MaxNum)or (meNum < MyWork.MinNum) then
      begin
        ImgRed.Visible := true   ;
        ImgGreen.Visible := false ;
      end
      else
      begin
        ImgRed.Visible := false   ;
        ImgGreen.Visible := true ;
      end;
      edtNum.Text := FloatToStr(meNum)+ 'g' ;
    end;
  end;

end;

procedure TfrmMeasureHight.FormCreate(Sender: TObject);
begin
  inherited;
  lblProductNo.Caption := '产品型号: '+ MyDuct.DuctNo ;
  lblProductName.Caption := '产品名称: '+ MyDuct.DuctName ;
  edtMax.Text :=  FloatToStr(MyWork.MaxNum );
  edtMin.Text := FloatToStr(MyWork.MinNum );
  x := 0 ;
  OpenCom;
  PathErr := ExtractFilePath(paramStr(0))+'DB\Error.wav';
  ErrPlayer.FileName := PathErr ;
end;

procedure TfrmMeasureHight.ShowNODuctTimer(Sender: TObject);
begin
  imgSpeaker.Visible := False;
  lblSpeaker.Visible := False;
  ShowNODuct.Enabled := False;
end;

procedure TfrmMeasureHight.LbtnNewClick(Sender: TObject);
begin
 // inherited;
  InitWrkCheckFace ;
end;

procedure TfrmMeasureHight.ToWarning;
begin
  warningx := 0 ;
  ErrPlayer.Open ;
  Timwarning.Enabled := true ;
end;

procedure TfrmMeasureHight.TimwarningTimer(Sender: TObject);
begin
  inherited;
  warningx := warningx + 1;
  if warningx < 21 then
  begin
    ImgRed.Visible := not ImgRed.Visible ;
    if imgred.Visible then
      ErrPlayer.Play;
  end else Timwarning.Enabled := false ;
end;

end.
