Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. /*Creazione Manutenzione Automatica
  2. Possiamo supporre la manutenzione Automatica venga generata se non ci sono manutenzioni
  3. effettuate di una determinata tipologia per un periodo che diventa sempre più distante al diminuire
  4. dell'indice di manutenzione. La stima delle manutenzioni automatiche avviene anche in basse alle richieste
  5. di una determinata manutenzione che potrebbe essere stata ignorata nell'ultimo periodo.
  6. */
  7.  
  8. drop event if exists Avviso_Manutenzioni;
  9.  
  10. delimiter $$
  11.  
  12. create event Avviso_Manutenzioni
  13. on schedule every 1 month
  14. do
  15. begin
  16.  
  17. insert into Manutenzione_Automatica(Data_Scadenza, Tipo_Manutenzione, Cod_Scheda)
  18. select current_date + interval 1 month, ma.Tipo_Manutenzione, ma.Cod_Scheda
  19. from Manutenzione_Automatica ma
  20. where ma.Accettato=false and
  21. /*non deve esistere una manutenzione automatica, già accettata, riguardante lo stesso
  22. intervento sulla stessa pianta (già effettuata di recente) o da effettuare in un prossimo futuro*/
  23. not exists (
  24. select *
  25. from Manutenzione_Automatica ma2
  26. where ma2.Accettato=true and
  27. ma2.Tipo_Manutenzione=ma.Tipo_Manutenzione and
  28. ma2.Cod_Scheda=ma.Cod_Scheda and
  29. (ma2.Data is not null and
  30. ma2.Data > current_date - interval 1+1/Indice_Accrescimento(ma2.Cod_Scheda) month) or
  31. ma2.Data_Scadenza > current_date)
  32. and not exists(
  33. select *
  34. from Manutenzione_Programmata mp
  35. where mp.Tipo_Manutenzione=ma.Tipo_Manutenzione and
  36. mp.Cod_Scheda=ma.Cod_Scheda and
  37. (mp.Data is not null and
  38. mp.Data > current_date - interval 1+1/Indice_Accrescimento(mp.Cod_Scheda) month)
  39. -- or mp.Mese maggiore del mese in cui siamo month(current_date) classico problema 12 1
  40. )and not exists(
  41. select *
  42. from Manutenzione_Esterna me
  43. where me.Tipo_Manutenzione=ma.Tipo_Manutenzione and
  44. ma.Cod_Scheda=me.Cod_Scheda and
  45. me.Data > current_date - interval 1+1/Indice_Accrescimento(me.Cod_Scheda) month)
  46. and not exists(
  47. select *
  48. from Richiesta_Manutenzione rm
  49. where rm.Cod_Scheda=ma.Cod_Scheda and
  50. rm.Tipo_Manutenzione=ma.Tipo_Manutenzione and
  51. (rm.Data is not null and
  52. rm.Data > current_date - interval 1+1/Indice_Accrescimento(rm.Cod_Scheda) month) or
  53. rm.Data_Scadenza > current_date)
  54. group by ma.Cod_Scheda;
  55.  
  56. insert into Manutenzione_Automatica(Data_Scadenza, Tipo_Manutenzione, Cod_Scheda)
  57. select current_date + interval 1 month, me.Tipo_Manutenzione, s.Cod_Scheda
  58. from Scheda s inner join Ordine o on s.Cod_Ordine=o.Codice
  59. inner join Pianta p on o.Nome_Pianta=p.Nome inner join
  60. Manutenzioni_Necessarie me on p.Nome=me.Nome_Pianta
  61. where me.Tipo_Manutenzione not in(
  62. select ma.Tipo_Manutenzione
  63. from Manutenzione_Automatica ma
  64. where ma.Cod_Scheda=s.Codice and
  65. ma.Data_Scadenza > current_date - interval 6+1/Indice_Accrescimento(s.Cod_Scheda) month)
  66. and me.Tipo_Manutenzione not in(
  67. select mext.Tipo_Manutenzione
  68. from Manutenzione_Esterna mext
  69. where mext.Cod_Scheda=s.Codice and
  70. mext.Data > current_date - interval 6+1/Indice_Accrescimento(s.Cod_Scheda) month)
  71. and me.Tipo_Manutenzione not in(
  72. select mp.Tipo_Manutenzione
  73. from Manutenzione_Programmata mp
  74. where mp.Cod_Scheda=s.Codice)
  75. and me.Tipo_Manutenzione not in(
  76. select rm.Tipo_Manutenzione
  77. from Richiesta_Manutenzione rm
  78. where rm.Cod_Scheda=s.Codice and
  79. rm.Data_Scadenza > current_date - interval 6+1/Indice_Accrescimento(s.Cod_Scheda) month);
  80. end $$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement