95082fb2de9ac200bada77a490de9be5b9320c44.svn-base 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using iBemsDataService.Model;
  2. using Quartz;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Transactions;
  8. using System.Web;
  9. using System.Diagnostics;
  10. using DevExpress.Office.Utils;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using iBemsDataService.Util;
  14. using iBemsDataService.Controllers.Bems.Formula;
  15. using iBemsDataService.Controllers.Bems.Common;
  16. namespace iBemsDataService.Controllers.Bems.ScheduledTask
  17. {
  18. public class ControlBems : IJob
  19. {
  20. private iBemsEntities db = new iBemsEntities();
  21. private static Regex regexParameter = new Regex(@"\b[A-Z]\b", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
  22. private static Regex regexFunction = new Regex(@"\$([\w]+).+\$", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
  23. public double GetTempSetValue(int month, CmFacilityTempSet ft)
  24. {
  25. switch (month)
  26. {
  27. case 1: return ft.T1;
  28. case 2: return ft.T2;
  29. case 3: return ft.T3;
  30. case 4: return ft.T4;
  31. case 5: return ft.T5;
  32. case 6: return ft.T6;
  33. case 7: return ft.T7;
  34. case 8: return ft.T8;
  35. case 9: return ft.T9;
  36. case 10: return ft.T10;
  37. case 11: return ft.T11;
  38. case 12: return ft.T12;
  39. }
  40. return 0;
  41. }
  42. public CmFacility GetCmFacility(CmFacilityTempSet tempset)
  43. {
  44. var FTS = from f in db.CmFacility where f.SiteId == tempset.SiteId && f.FacilityCode == tempset.FacilityCode select f;
  45. return (CmFacility)FTS.First();
  46. }
  47. public void Execute( IJobExecutionContext context )
  48. {
  49. var today = DateTime.Now;
  50. //var tomorrow = today.AddDays( 1 );
  51. //BemsFormulaController BFC = new BemsFormulaController();
  52. using (var transaction = new TransactionScope())
  53. {
  54. var site = from s in db.CmSite select s;
  55. List<CmSite> sitelist = site.ToList();
  56. foreach (CmSite s in sitelist)
  57. {
  58. if (s.IsControlSchedule == true && s.ScheduleCheckHour == today.Hour && today.Minute == 0)
  59. {
  60. var FTS = from f in db.CmFacilityTempSet where f.SiteId == s.SiteId select f;
  61. List<CmFacilityTempSet> list = FTS.ToList();
  62. foreach (CmFacilityTempSet ts in list)
  63. {
  64. CmFacility facility = GetCmFacility(ts);
  65. var queryFormula = from f in db.BemsFormula
  66. where f.SiteId == ts.SiteId && f.FacilityTypeId == facility.FacilityTypeId && f.FacilityCode == ts.FacilityCode && f.FormulaId == 20
  67. select f;
  68. if (queryFormula == null || queryFormula.Count() <= 0) continue;
  69. BemsFormula formula = (BemsFormula)queryFormula.First();
  70. ArrayList parameters = new ArrayList();
  71. MathCalculator calculator = new MathCalculator();
  72. if (false == calculator.Compile(formula.Formula))
  73. {
  74. throw new Exception("Failed to compile formula: " + formula.Formula);
  75. }
  76. var match = regexParameter.Match(formula.Formula);
  77. if (match.Success)
  78. {
  79. var queryParameters = db.BemsFormulaParameter.Where(x =>
  80. x.SiteId == ts.SiteId && x.FacilityTypeId == facility.FacilityTypeId &&
  81. x.FacilityCode == ts.FacilityCode && x.FormulaId == 20);
  82. if (queryParameters.Any() == false)
  83. {
  84. //; throw new Exception(string.Format("Not Found 2222 Parameters: {0},{1},{2},{3}", ts.SiteId, 99, ts.FacilityCode, 20));
  85. ;
  86. }
  87. foreach (var p in queryParameters)
  88. {
  89. var control = new BemsControlPointHistory
  90. {
  91. SiteId = ts.SiteId,
  92. //FacilityTypeId = 99,
  93. FacilityTypeId = facility.FacilityTypeId,
  94. FacilityCode = p.ParameterFacilityCode,
  95. PropertyId = p.ParameterPropertyId,
  96. CreateDateTime = today,
  97. ControlValue = GetTempSetValue(today.Month, ts),
  98. WriteServiceName = null,
  99. ActionDateTime = null
  100. };
  101. db.BemsControlPointHistory.Add(control);
  102. db.SaveChanges();
  103. }
  104. }
  105. }
  106. }
  107. }
  108. transaction.Complete();
  109. }
  110. }
  111. private void InsertWorkOrder( FmsWorkRequest workRequest , List<FmsWorkScheduleToFacility> facilities )
  112. {
  113. var workOrder = new FmsWorkOrder
  114. {
  115. SiteId = workRequest.SiteId ,
  116. WorkRequestId = workRequest.WorkRequestId ,
  117. BusinessFieldId = workRequest.BusinessFieldId ,
  118. OrderUserId = workRequest.RequestUserId ,
  119. OrderDate = DateTime.Now.Date ,
  120. Title = workRequest.Title ,
  121. Content = workRequest.Content ,
  122. StartWorkDate = workRequest.StartWorkDate
  123. };
  124. workOrder = db.FmsWorkOrder.Add( workOrder );
  125. foreach( var f in facilities )
  126. {
  127. db.FmsWorkOrderToFacility.Add( new FmsWorkOrderToFacility
  128. {
  129. SiteId = f.SiteId ,
  130. WorkRequestId = workOrder.WorkRequestId ,
  131. FacilityCode = f.FacilityCode
  132. } );
  133. }
  134. workRequest.WorkProgressId = (short)WorkProgress.WORK_STANDBY;
  135. db.SaveChanges();
  136. }
  137. }
  138. }