TaskPreconditionCompare.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /// @Brief 任务前置条件比较
  3. ///
  4. class TaskPreconditionCompare
  5. {
  6. private static $instance = null; // 单例
  7. // @function 获取单例
  8. //
  9. // @return TaskPreconditionCompare
  10. public static function getInstance(){
  11. if (is_null(self::$instance)) { self::$instance = new TaskPreconditionCompare();}
  12. return self::$instance;
  13. }
  14. // @function 构造
  15. //
  16. private function __construct()
  17. {
  18. }
  19. // @function 获取结果
  20. //
  21. // @param userDataInfo 用户数据信息<参见UserDataInfo>
  22. // @param taskPreconditionInfo 任务前置条件信息<参考TaskPreconditionInfo>
  23. // @return boolean
  24. public function getResult($userTaskInfo,
  25. $taskPreconditionInfo){
  26. // 用户基本信息比较
  27. if (!is_null($userTaskInfo ->baseInfo)){
  28. if ($this ->compare($taskPreconditionInfo ->age,
  29. $userTaskInfo ->baseInfo ->age))
  30. return true;
  31. if ($this ->compare($taskPreconditionInfo ->sex,
  32. $userTaskInfo ->baseInfo ->sex))
  33. return true;
  34. if ($this ->compare($taskPreconditionInfo ->time,
  35. $userTaskInfo ->baseInfo ->time))
  36. return true;
  37. if ($this ->compare($taskPreconditionInfo ->ulevel,
  38. $userTaskInfo ->baseInfo ->ulevel))
  39. return true;
  40. if ($this ->compare($taskPreconditionInfo ->place,
  41. $userTaskInfo ->baseInfo ->place))
  42. return true;
  43. if ($this ->compare($taskPreconditionInfo ->weather,
  44. $userTaskInfo ->baseInfo ->weather))
  45. return true;
  46. // todo
  47. }
  48. // 任务生理数据前置条件信息比较
  49. if (!is_null($userTaskInfo ->taskPhysicalDataPInfo)){
  50. if ($this ->compare($taskPreconditionInfo ->isLikeSports,
  51. $userTaskInfo ->baseInfo ->isLikeSports))
  52. return true;
  53. if ($this ->compare($taskPreconditionInfo ->isHypertension,
  54. $userTaskInfo ->baseInfo ->isHypertension))
  55. return true;
  56. if ($this ->compare($taskPreconditionInfo ->isHeartAttack,
  57. $userTaskInfo ->baseInfo ->isHeartAttack))
  58. return true;
  59. if ($this ->compare($taskPreconditionInfo ->isAgile,
  60. $userTaskInfo ->baseInfo ->isAgile))
  61. return true;
  62. if ($this ->compare($taskPreconditionInfo ->isHardBody,
  63. $userTaskInfo ->baseInfo ->isHardBody))
  64. return true;
  65. // todo
  66. }
  67. return false;
  68. }
  69. // @function 是否是任务条件信息
  70. //
  71. // @param value 值
  72. // @return boolean
  73. private function isTaskCondtionInfo($value)
  74. {
  75. if ($value instanceof TaskCondtionInfo)
  76. return true;
  77. return false;
  78. }
  79. // @function 比较
  80. //
  81. // @param value1 值1
  82. // @param value2 值2
  83. // @return boolean
  84. private function compare($value1,$value2)
  85. {
  86. if (true == $this ->isTaskCondtionInfo($value1))
  87. {
  88. if (true == $this ->isEmpty($value1 ->equalValue)){
  89. return $value2 == $value1 ->equalValue;
  90. }
  91. if (true == $this ->isEmpty($value1 ->minValue)
  92. && true == $this ->isEmpty($value1 ->maxValue)){
  93. return ($value2 > $value1 ->minValue && $value2 <= $value1 ->maxValue);
  94. }
  95. if (true == $this ->isEmpty($value1 ->minValue)){
  96. return $value2 >= $value1 ->minValue;
  97. }
  98. if (true == $this ->isEmpty($value1 ->maxValue)){
  99. return $value2 <= $value1 ->maxValue;
  100. }
  101. }else
  102. {
  103. return $value1 == $value2;
  104. }
  105. }
  106. // @function 是否为空
  107. //
  108. // @param value 值
  109. // @return boolean
  110. private function isEmpty($value){
  111. if (!is_null($value) && !empty($value)){
  112. return true;
  113. }
  114. return false;
  115. }
  116. }