planmisson.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // ================================================================================
  3. // ██╗ ██╗██╗ ██╗██╗
  4. // ██║ ██║╚██╗ ██╔╝██║
  5. // ███████║ ╚████╔╝ ██║
  6. // ██╔══██║ ╚██╔╝ ██║
  7. // ██║ ██║ ██║ ███████╗
  8. // ╚═╝ ╚═╝ ╚═╝ ╚══════╝
  9. // 2018-03-13
  10. // ================================================================================
  11. /* 描述:定时任务
  12. * 技术:crontab 定时执行该文件
  13. * 目的:每天0:10自动创建昨天的巡检任务
  14. */
  15. $Date = date('Y-m-d');
  16. if (strtotime(date('Y-m-d H:i:s')) < strtotime($Date . ' 00:30:00')) {
  17. $db = new DB('47.98.201.187', 'root', 'Yt2018IoT', 'jdxf');
  18. $db -> connect();
  19. $sql = 'SELECT start_data,end_data,made_user,inspection_id,inspection_user,i_enable,mission_status,finish_date,mission_name,plan,id,inspection_name
  20. FROM xj_misson where plan=1 and DATE(add_time)=DATE_SUB(curdate(),INTERVAL 1 DAY) ';
  21. $db ->log('execute start');
  22. $res = $db -> getAll($sql);
  23. foreach ($res as $key => $row) {
  24. $start = date('Y-m-d H:i:s', strtotime($row['start_data'] . "+ 1 day"));
  25. $end = date('Y-m-d H:i:s', strtotime($row['end_data'] . "+ 1 day"));
  26. $Insert = "INSERT INTO xj_misson(start_data,end_data,made_user,inspection_id,inspection_user,i_enable,mission_status,finish_date,mission_name,plan,add_time,inspection_name) value('" . $start . "','" . $end . "','" . $row['made_user'] . "','" . $row['inspection_id'] . "','" . $row['inspection_user'] . "','1','1','" . $row['finish_date'] . "','" . $row['mission_name'] . "','" . $row['plan'] . "','" . date('Y-m-d H:i:s') . "','" . $row['inspection_name'] . "')";
  27. $db -> query($Insert);
  28. $IsertId = $db -> getInsertid();
  29. // copy点位
  30. $deviceID = 'SELECT deviceID from xj_inspection2Position where missionID=' . $row['id'];
  31. $Res = $db -> getAll($deviceID);
  32. $sqlI = "insert into xj_inspection2Position (missionID,deviceID) value";
  33. foreach ($Res as $key => $DataDevice) {
  34. $sqlI .= "('" . $IsertId . "','" . $DataDevice['deviceID'] . "'),";
  35. }
  36. //copy人员
  37. $UserID = 'SELECT userid from xj_misson_userid where misson_id=' . $row['id'];
  38. $ResUser = $db -> getAll($UserID);
  39. $sqlI2 = "insert into xj_misson_userid (misson_id,userid) value";
  40. foreach ($ResUser as $key => $DataUser) {
  41. $sqlI2 .= "('" . $IsertId . "','" . $DataUser['userid'] . "'),";
  42. }
  43. $db -> query(substr($sqlI, 0, strlen($sqlI) - 1));
  44. $db -> query(substr($sqlI2, 0, strlen($sqlI2) - 1));
  45. }
  46. $db -> colse();
  47. } else {
  48. echo "不在时间段内";
  49. }
  50. class DB {
  51. private $Con;
  52. private $Locahost;
  53. private $User;
  54. private $Password;
  55. private $Db;
  56. public function __construct($Locahost, $User, $Password, $Db) {
  57. $this -> Locahost = $Locahost;
  58. $this -> User = $User;
  59. $this -> Password = $Password;
  60. $this -> Db = $Db;
  61. }
  62. public function connect() {
  63. try {
  64. $this -> Con = mysql_connect($this -> Locahost, $this -> User, $this -> Password);
  65. mysql_query("set character set 'utf8'");
  66. //读库
  67. mysql_query("set names 'utf8'");
  68. //写库
  69. mysql_select_db($this -> Db);
  70. } catch (Exception $e) {
  71. $this -> log('mysql could not connect ' . mysql_error());
  72. }
  73. }
  74. public function query($Sql) {
  75. try {
  76. $this -> log($Sql);
  77. return mysql_query($Sql, $this -> Con);
  78. } catch (Exception $e) {
  79. $this -> log('mysql error' . $Sql);
  80. }
  81. }
  82. public function getInsertid() {
  83. return mysql_insert_id($this -> Con);
  84. }
  85. public function getRow($sql, $type = "assoc") {
  86. $query = $this -> query($sql);
  87. if (!in_array($type, array("assoc", 'array', "row"))) {
  88. $this -> log("mysql_query error");
  89. }
  90. $funcname = "mysql_fetch_" . $type;
  91. return $funcname($query);
  92. }
  93. /**
  94. * [getAll description]获取所有数据
  95. * @param [type] $sql [sql语句]
  96. * @param string $type [返回类型 默认assoc]
  97. * @return [type] [二维数组数据]
  98. */
  99. public function getAll($sql, $type = "assoc") {
  100. $query = $this -> query($sql);
  101. if (!in_array($type, array("assoc", 'array', "row"))) {
  102. $this -> log("mysql_query error");
  103. }
  104. if ($query && mysql_num_rows($query) > 0) {
  105. $funcname = "mysql_fetch_" . $type;
  106. $List = array();
  107. while ($res = $funcname($query)) {
  108. $List[] = $res;
  109. }
  110. return $List;
  111. } else {
  112. return false;
  113. }
  114. }
  115. /**
  116. * [colse description]关闭mysql链接
  117. * @return [type] [description]
  118. */
  119. public function colse() {
  120. try {
  121. mysql_close($this -> Con);
  122. } catch (Exception $e) {
  123. $this -> log('mysql could not colse :' . mysql_error());
  124. }
  125. }
  126. /**
  127. * [log description] 记录日志
  128. * @param [type] $msg [description] 信息
  129. * @return [type] [description]
  130. */
  131. public function log($msg) {
  132. $msg = $msg . "\n";
  133. // echo $msg;
  134. $logFile = './plan.log';
  135. file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "]---" . $msg, FILE_APPEND | LOCK_EX);
  136. }
  137. }
  138. ?>