-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2023, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

--------------------------------------------------------------------------------------------------------
-- WartungsMessDaten abrufen
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_WartungsMessDaten_list')
  DROP PROCEDURE [dbo].[itcproc_WartungsMessDaten_list]
GO
CREATE PROCEDURE [dbo].[itcproc_WartungsMessDaten_list]
  @Param_Ident varchar(15)  -- Anlagen-Ident
 ,@Param_Datum varchar(10) = ''
AS
  DECLARE @m_Ident integer = dbo.itcfunc_integer(@Param_Ident)
  DECLARE @m_Datum datetime

  IF @Param_Datum = '' BEGIN
    SELECT * FROM WartungsMessDaten WHERE AnlagenId = @m_Ident ORDER By Datum, LfdNr
	RETURN
  END

  SET @m_Datum = dbo.itcfunc_datetime(@Param_Datum)
  DECLARE @m_Bez      varchar(40)
  DECLARE @m_Sollwert varchar(30)
  DECLARE @m_Zusatz   varchar(10)
  DECLARE @m_LfdNr    integer = 0
  DECLARE @m_RecId    integer

  IF 0 = (SELECT COUNT(*) FROM WartungsMessDaten WHERE AnlagenId = @m_Ident AND Datum = @m_Datum) BEGIN
    DECLARE c CURSOR LOCAL FOR SELECT wtd.Bez, wtd.Sollwert, wtd.Zusatz FROM ut_WartungsAnlagen wa
       RIGHT JOIN ut_WartungsTypenDaten wtd ON wtd.Ident = wa.AnlTyp WHERE wa.Ident = 3 ORDER BY LfdNr
	OPEN C
	FETCH NEXT FROM c INTO @m_Bez, @m_Sollwert, @m_Zusatz
	WHILE @@FETCH_STATUS = 0 BEGIN
	  SET @m_RecId = dbo.itcfunc_NextRecId_WartungsMessDaten()
	  SET @m_LfdNr = @m_LfdNr + 1
      INSERT INTO ut_WartungsMessDaten (RecId,AnlagenId,Datum,LfdNr,Bez,Sollwert,Zusatz)
	    VALUEs (@m_RecId,@m_Ident,@m_Datum,@m_LfdNr,@m_Bez,@m_Sollwert,@m_Zusatz)
	  FETCH NEXT FROM c INTO @m_Bez, @m_Sollwert, @m_Zusatz
	END
	CLOSE C
	DEALLOCATE C
  END

  SELECT * FROM WartungsMessDaten WHERE AnlagenId = @m_Ident AND Datum = @m_Datum ORDER By Datum, LfdNr
GO
-- EXECUTE [dbo].[itcproc_WartungsMessDaten_list] '3','2023-08-17'
--------------------------------------------------------------------------------------------------------
-- WartungsMessDaten validieren
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_WartungsMessDaten_validate')
  DROP PROCEDURE [dbo].[itcproc_WartungsMessDaten_validate]
GO
CREATE PROCEDURE [dbo].[itcproc_WartungsMessDaten_validate]
  @Parm_RecId      varchar(10)
 ,@Parm_FieldName  varchar(30)
 ,@Parm_Value      varchar(200) = ''
 ,@Parm_WithResult varchar(10)  = '1'
AS
  DECLARE @m_RecId      integer = dbo.itcfunc_integer(@Parm_RecId)
  DECLARE @m_Success    integer = 0
  DECLARE @mAnlagenId   integer

  ------------------------------------------------------------------------------------
  IF @Parm_FieldName = 'Sollwert' BEGIN
    UPDATE ut_WartungsMessDaten SET Sollwert = @Parm_Value WHERE RecId = @m_RecId
	SET @m_Success = 1
  END ELSE IF @Parm_FieldName = 'Messwert' BEGIN
    UPDATE ut_WartungsMessDaten SET Messwert = @Parm_Value WHERE RecId = @m_RecId
	SET @m_Success = 1
  END ELSE IF @Parm_FieldName = 'Zusatz' BEGIN
    UPDATE ut_WartungsMessDaten SET Zusatz = @Parm_Value WHERE RecId = @m_RecId
	SET @m_Success = 1
  END ELSE IF @Parm_FieldName = 'Bez' BEGIN
    UPDATE ut_WartungsMessDaten SET Bez = @Parm_Value WHERE RecId = @m_RecId
	SET @m_Success = 1
  END

  IF dbo.itcfunc_integer(@Parm_WithResult) <> 0 BEGIN
    IF @m_Success <> 0 
      SELECT * FROM WartungsMessDaten WHERE RecId = @m_RecId
	ELSE
      SELECT 'Der Wert "' + @Parm_Value + '" ist hier im Feld [' + @Parm_FieldName + '] nicht erlaubt.' AS error
  END
  RETURN @m_Success
GO
