wincan2ctrl.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. '''
  2. Met à jour la base controle avec les données d'un chantier Wincan
  3. olivier.massot, mai 2018
  4. '''
  5. import logging
  6. from path import Path
  7. from core import logconf
  8. from core.db import AccessSqlHelper
  9. from core.pde import ControlesDb, WincanDb, CommunDb
  10. from core.sqlformatter import SqlFormatter
  11. logger = logging.getLogger("wincan2ctrl")
  12. logconf.start("wincan2ctrl", logging.DEBUG)
  13. # # POUR TESTER, décommenter les lignes suivantes
  14. # > Lancer le script /resources/test_wincan2ctrl.py pour reinitialiser les données de la base de test
  15. ##-----------------------------------------------
  16. ControlesDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\cg67Parc_data.mdb")
  17. WincanDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\Wincan\parc_2007\DB\PARC_2007.mdb")
  18. CommunDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\Commun_Data.mdb")
  19. logger.handlers = [h for h in logger.handlers if (type(h) == logging.StreamHandler)]
  20. logger.warning("<<<<<<<<<<<<<< Mode TEST >>>>>>>>>>>>>>>>>")
  21. ##-----------------------------------------------
  22. logger.info("Initialisation...")
  23. Sql = SqlFormatter()
  24. sqlHelper = AccessSqlHelper
  25. chantier = 185000
  26. interv = 1
  27. commande_id = 1
  28. # Connexion à Controles
  29. controles_db = ControlesDb(autocommit=False)
  30. # Connexion à Wincan
  31. wincan_db = WincanDb(autocommit=False)
  32. # Connexion à CommunDb
  33. commun_db = CommunDb(autocommit=False)
  34. def get_lib(tbl_id, lib_id):
  35. return wincan_db.first(Sql.format("""SELECT LIB_ID, LIB_LIB, TLB_ID
  36. FROM LISTE_LIBELLE_WINCAN
  37. WHERE TLB_ID='{}' AND LIB_ID={}""", tbl_id, lib_id)).LIB_LIB
  38. logger.info("# Traitement du chantier %s, intervention %s", chantier, interv)
  39. sql = """SELECT SI_InspName, Max(SI_Date) AS DateMax, SI_Spare1, SI_Spare2
  40. FROM SI_T
  41. GROUP BY SI_InspName, SI_Spare1, SI_Spare2
  42. HAVING SI_spare1 Is Null OR Len([SI_spare1])=0
  43. ORDER BY Max(SI_T.SI_Date) DESC
  44. """
  45. for nontraite in wincan_db.read(sql):
  46. # Liste les inspections pas encore traitées
  47. sql = """SELECT SI_T.SI_InspName, Sum(S_T.S_Sectionlength) AS Long_insp, Min(SI_T.SI_Date) AS mindate, Max(SI_T.SI_Date) AS maxdate, Max(SI_T.SI_Operator) AS Equipe, Max(SI_T.SI_Vehicle) AS Mat, Max(SI_T.SI_InspMethod) AS InspMethod, Max(SI_T.SI_ReasonOfInspection) AS MaxDeSI_ReasonOfInspection, Min(S_T.S_SectionPurpose) AS nature, Min(S_T.S_SectionUse) AS Fonction, Min(S_T.S_SectionType) AS Type
  48. FROM S_T INNER JOIN SI_T ON S_T.S_ID = SI_T.SI_Section_ID
  49. GROUP BY SI_T.SI_InspName
  50. HAVING Sum(S_T.S_Sectionlength) Is Not Null AND Max(SI_T.SI_Operator) Is Not Null AND Max(SI_T.SI_Vehicle) Is Not Null AND Max(SI_T.SI_InspMethod) Is Not Null AND SI_InspName='{}'
  51. """.format(nontraite.SI_InspName)
  52. for inspection in wincan_db.read(sql):
  53. reason_lib = get_lib("SI_REASONOfINSPECTION", inspection.SI_ReasonOfInspection)
  54. # insère les inspections dans la table tblVideoIntervs de Controles
  55. logger.info("Mise à jour de tblVideoIntervs")
  56. sql = """INSERT INTO tblVideoIntervs ( lngChantierId, bytIntervId, dtmIntervDu, dtmIntervAu, strEquipeId, intlMaterielID, bytCommandeId, lngTroncon, SI_InspMethod, SI_ReasonOfInspection )
  57. SELECT {} AS chantier, {} AS interv, {} AS mindate, {} AS maxdate, {} AS equipe, {} AS mat, {} AS commande, {} AS long_insp, {} AS methode, {} AS reason
  58. """.format(chantier, interv, inspection.mindate, inspection.maxdate, inspection.Equipe, inspection.Mat, commande_id, inspection.long_insp, inspection.InspMethod, reason_lib)
  59. controles_db.execute(sql)
  60. # Met a jour les champs SI_Spare1 et SI_Spare2 de la table Wincan pour marquer les lignes comme importées
  61. logger.info("Mise à jour en retour de SI_T")
  62. sql = "update SI_T set SI_Spare1={}, SI_Spare2={} where SI_InspName='{}'".format(chantier, interv, inspection.SI_InspName)
  63. wincan_db.execute(sql)
  64. # Met a jour la table tbl_so_rate de Controles
  65. logger.info("Mise à jour de tblso_Rate_Analyse")
  66. # 1. Extrait les données des essais
  67. # sql = """SELECT SI_T.SI_Spare1, SI_T.SI_Spare2, SI_T.SI_MediaNumber1, S_T.S_SectionFlow, S_T.S_StartNode, S_T.S_StartNodeType,
  68. # S_T.S_StartNodeCoord_Z, S_T.S_EndNode, S_T.S_EndNodeType, S_T.S_EndNodeCoord_Z, S_T.S_Sectionlength, S_T.S_SectionPurpose,
  69. # S_T.S_SectionUse, S_T.S_SectionType, S_T.S_PipeMaterial, S_T.S_YearLayed, S_T.S_PipeDia, S_T.S_Situation, S_T.S_Spare1,
  70. # S_T.S_EndStreet, P_T.P_Name, P_T.P_Date, S_T.S_Location, S_T.S_StartStreet, S_T.S_Spare2, SI_T.SI_AutoNumber,
  71. # S_T.S_PipeShape, S_T.S_Pipelength, S_T.S_Spare3, SI_T.SI_ID, S_T.S_ID, SI_T.SI_InspName, S_T.S_SectionPurpose,
  72. # S_T.S_SectionUse, S_T.S_SectionType, SI_T.SI_Spare0 AS Test_ecoulement, SI_T.SI_Date, SI_T.SI_ReasonOfInspection,
  73. # S_T.S_SectionPurpose, SO_T.SO_ID, SO_T.SO_Rate, SO_T.SO_Photonumber1, SO_T.SO_Photonumber2
  74. # FROM ((P_T INNER JOIN S_T ON P_T.P_ID = S_T.S_Project_ID) INNER JOIN SI_T ON S_T.S_ID = SI_T.SI_Section_ID)
  75. # INNER JOIN SO_T ON SI_T.SI_ID = SO_T.SO_Inspecs_ID
  76. # WHERE SO_T.SO_Rate <= 6 or SO_T.SO_Rate Is Null
  77. # ORDER BY SI_T.SI_InspName, SI_T.SI_MediaNumber1
  78. # """
  79. # sql = """
  80. # 2. Enrichir / transformer les données
  81. # * SI_Spare1 => lngChantierId (convertir en num)
  82. # * SI_Spare2 => bytIntervId (convertir en num)
  83. # * SI_MediaNumber1 => [Classement troncons]
  84. # * S_StartNode, S_EndNode, S_SectionFlow => Nom_troncon ("Start-End" si S_SectionFlow == 2, sinon "End-Start")
  85. # * S_StartNodeType => S_StartNodeType (get_lib, tbl='S_StartNodeType'; trim)
  86. # * S_SectionFlow => Sens_ecoul ('>>' si SectionFlow = '1', '<<' si SectionFlow=2, '' sinon)
  87. # * S_StartNodeCoord_Z => S_StartNodeCoord_Z
  88. # * S_EndNodeCoord_Z => S_EndNodeCoord_Z
  89. # * S_Sectionlength
  90. # * S_EndNodeType => S_EndNodeType (get_lib, tbl='S_EndNodeType'; trim)
  91. # ** S_SectionPurpose => [Nature Reseau] (get_lib, tbl='S_SectionPurpose')
  92. # ** S_SectionUse => [Fonction Reseau] (get_lib, tbl='S_SectionUse')
  93. # ** S_SectionType => [Type de Réseau] (get_lib, tbl='S_SectionType')
  94. # S_PipeMaterial => Materiau (get_lib, tbl='S_PipeMaterial')
  95. # * ET => MateriauCourt (strMateriauCourt FROM tblMateriaux WHERE strWincanId='{}') (c'est celui qui est utilisé)
  96. # * S_YearLayed => ANNEE_POSE
  97. # * S_PipeDia => Diametre (get_lib, tbl='S_PipeDia')
  98. # * S_Situation => Route (get_lib, tbl='S_Situation')
  99. # * S_Spare1 => NRoute
  100. # S_EndStreet => Rue
  101. # P_Name => P_Name
  102. # P_Date => P_Date
  103. # S_Location => Loc (strTiersMnemo FROM tblTiers WHERE strCompteComptable='{}')
  104. # S_StartStreet => MOu
  105. # S_Spare2 => N°CHIMERE
  106. # SI_AutoNumber => SI_AutoNumber
  107. # * S_PipeShape => S_PipeShape
  108. # * S_Pipelength => S_Pipelength
  109. # * S_Spare3 => nb_Arbres (get_lib, tbl='S_Spare3')
  110. # SI_ID
  111. # S_ID
  112. # ? SI_InspName
  113. # * SI_Spare0 => Test_ecoulement
  114. # * SI_Date => SI_Date
  115. # SI_ReasonOfInspection => raison_insp (get_lib, tbl='SI_ReasonOfInspection')
  116. # * SO_Photonumber1, SO_Photonumber2 => nb_photos (VraiFaux([SO_Photonumber2] Est Pas Null;1;0)+VraiFaux([SO_Photonumber1] Est Pas Null;1;0))
  117. # 3. Reorganisation
  118. # Les données sont croisées de cette manière:
  119. # Regroupement des lignes sur la plupart des champs
  120. # Compte de SO_ID
  121. # cpt_photos = somme de nb_photos
  122. # Les résultats (SO_Rate) passent en colonnes, avec un decompte par valeur (comprise entre 1 et 5)
  123. # 4. Insertion
  124. sql = """INSERT INTO tblso_Rate_Analyse (
  125. lngChantierId, bytIntervId, SI_InspName, SI_AutoNumber, [Classement tronons], Nom_troncon, S_StartNodeType, Sens_ecoul, S_EndNodeType,
  126. S_PipeShape, MateriauCourt, SI_Date, nb_Arbres, ANNEE_POSE, Route, NRoute, Test_ecoulement, MaxDeS_StartNodeCoord_Z, MaxDeS_EndNodeCoord_Z,
  127. MaxDeS_Sectionlength, MaxDeS_Pipelength, MaxDeDiametre, cpt_Photos, [Total de SO_ID], 1, 2, 3, 4, 5
  128. )
  129. SELECT lngChantierId, bytIntervId, SI_InspName, SI_AutoNumber, [Classement tronons], Nom_troncon, S_StartNodeType,
  130. Sens_ecoul, S_EndNodeType, S_PipeShape, MateriauCourt, SI_Date, nb_Arbres, ANNEE_POSE, Route,
  131. NRoute, Test_ecoulement, MaxDeS_StartNodeCoord_Z, MaxDeS_EndNodeCoord_Z, MaxDeS_Sectionlength, MaxDeS_Pipelength,
  132. MaxDeDiametre, cpt_Photos, [Total de SO_ID], [1], [2], [3], [4], [5]
  133. FROM r_so_rate_Analyse
  134. WHERE lngChantierId= {} AND bytIntervId= {}
  135. """.format(chantier, interv)
  136. controles_db.execute(sql)
  137. # Met à jour tblvideointervs.strResGlobal avec le resultat global
  138. # le resultat global vaut '-' si un de ces trois champs n'est pas nul: tbl_so_rate.1, tbl_so_rate.2, tbl_so_rate.3
  139. logger.info("Mise à jour du resultat global dans tblvideointervs")
  140. resGlobal = ''
  141. "Update tblvideointervs set strResGlobal='{}' WHERE lngChantierId= {} AND bytIntervId= {}".format(resGlobal, chantier, interv)
  142. # Met à jour la table tblVideoBases pour marquer le chantier comme traité
  143. logger.info("Mise à jour de tblVideoBases")
  144. sql = "update tblVideoBases set blnWincan=-1,bytNbInterv={} where lngChantierId={}".format(interv, chantier)
  145. # Met à jour les données du réseau dans tblChantiers
  146. logger.info("Mise à jour des données du réseau dans tblChantiers")
  147. fctReseauId, natureReseauId, typeReseauId = 0, 0, 0
  148. sql = "update tblChantiers set bytFoncReseauId ={} , bytNatureReseauId={}, bytTypeReseauId={} where lngChantierId={}".format(fctReseauId, natureReseauId, typeReseauId, chantier)
  149. logger.info("Commit")
  150. controles_db.commit()
  151. wincan_db.commit()