unit uWorkMeasure;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uWorkPlaceBase, dxExEdtr, ADODB, DB, ExtCtrls, dxBar,uFunMesClient,
  dxBarExtItems, ImgList, StdCtrls, dxTL, dxCntner, RzPanel, RzSplit,uComm,Common,
  SPComm, dxEditor, dxEdLib, Buttons,uFunCom;

type
  TfrmWorkMeasure = class(TfrmWorkPlaceBase)
    Panel3: TPanel;
    ImgGreen: TImage;
    ImgYel: TImage;
    ImgRed: TImage;
    Label3: TLabel;
    edtNum: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    dxBarLargeButton3: TdxBarLargeButton;
    Comm1: TComm;
    ImageOn: TImage;
    ImageOff: TImage;
    edtMin: TdxSpinEdit;
    edtMax: TdxSpinEdit;
    Timer2: TTimer;
    dxBarButton1: TdxBarButton;
    dxBarLargeButton4: TdxBarLargeButton;
    Image2: TImage;
    Panel5: TPanel;
    plCurRout: TPanel;
    Image3: TImage;
    moCurRout: TMemo;
    procedure dxBarLargeButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    procedure Timer2Timer(Sender: TObject);
    procedure dxBarLargeButton4Click(Sender: TObject);
    procedure edtCodeKeyPress(Sender: TObject; var Key: Char);
    procedure moCurRoutKeyPress(Sender: TObject; var Key: Char);
    procedure LbtnNewClick(Sender: TObject);
  private
    meNum: Real;
    procedure OpenCom;
    procedure ComStop;
    procedure InitWrkCheckFace; 
    function  CDuctBarCodeIn(Barcode: String): Boolean;

    { Private declarations }
  public
    x: integer ;
    function Getlight(Str: String): Real;
    function MeasurebyRoutBarcode(CurrootBar: String): Boolean;
    { Public declarations }
  end;

var
  frmWorkMeasure: TfrmWorkMeasure;

implementation

uses uWorkBase, PublicDBUnit;

{$R *.dfm}

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

end;

procedure TfrmWorkMeasure.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;

procedure TfrmWorkMeasure.FormCreate(Sender: TObject);
begin
  inherited;
  OpenCom;
  edtMax.Value :=  MyWork.MaxNum ;
  edtMin.Value :=  MyWork.MinNum ;
  x := 0 ;
end;

procedure TfrmWorkMeasure.FormDestroy(Sender: TObject);
begin
  inherited;
  Comm1.StopComm ;
end;

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

procedure TfrmWorkMeasure.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);
    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;
      ImgYel.Visible := not ImgYel.Visible ;
      edtNum.Text := FloatToStr(meNum)+ 'g' ;
    end;
  end;
end;

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

function TfrmWorkMeasure.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 TfrmWorkMeasure.dxBarLargeButton4Click(Sender: TObject);
begin
  inherited;
  OpenCom;
end;

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

function TfrmWorkMeasure.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 BarcodeIsOK(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 ShowMessage('商品条码没有登记');
end;

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

end;


function TfrmWorkMeasure.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
    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 TfrmWorkMeasure.LbtnNewClick(Sender: TObject);
begin
  inherited;
  InitWrkCheckFace ;
end;
procedure TfrmWorkMeasure.InitWrkCheckFace;
begin
  moCurRout.Clear ;
  edtCode.Text := '';
  MyDuct.Barcode := '' ;
  edtCode.SetFocus ;
end;

end.
