123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- // ================================================================================
- // ██╗ ██╗██╗ ██╗██╗
- // ██║ ██║╚██╗ ██╔╝██║
- // ███████║ ╚████╔╝ ██║
- // ██╔══██║ ╚██╔╝ ██║
- // ██║ ██║ ██║ ███████╗
- // ╚═╝ ╚═╝ ╚═╝ ╚══════╝
- // 2018-03-13
- // ================================================================================
- /* 描述:定时任务
- * 技术:crontab 定时执行该文件
- * 目的:每天0:10自动创建昨天的巡检任务
- */
- $Date = date('Y-m-d');
- if (strtotime(date('Y-m-d H:i:s')) < strtotime($Date . ' 00:30:00')) {
- $db = new DB('47.98.201.187', 'root', 'Yt2018IoT', 'jdxf');
- $db -> connect();
- $sql = 'SELECT start_data,end_data,made_user,inspection_id,inspection_user,i_enable,mission_status,finish_date,mission_name,plan,id,inspection_name
- FROM xj_misson where plan=1 and DATE(add_time)=DATE_SUB(curdate(),INTERVAL 1 DAY) ';
- $db ->log('execute start');
- $res = $db -> getAll($sql);
- foreach ($res as $key => $row) {
- $start = date('Y-m-d H:i:s', strtotime($row['start_data'] . "+ 1 day"));
- $end = date('Y-m-d H:i:s', strtotime($row['end_data'] . "+ 1 day"));
- $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'] . "')";
- $db -> query($Insert);
- $IsertId = $db -> getInsertid();
- // copy点位
- $deviceID = 'SELECT deviceID from xj_inspection2Position where missionID=' . $row['id'];
- $Res = $db -> getAll($deviceID);
- $sqlI = "insert into xj_inspection2Position (missionID,deviceID) value";
- foreach ($Res as $key => $DataDevice) {
- $sqlI .= "('" . $IsertId . "','" . $DataDevice['deviceID'] . "'),";
- }
- //copy人员
- $UserID = 'SELECT userid from xj_misson_userid where misson_id=' . $row['id'];
- $ResUser = $db -> getAll($UserID);
- $sqlI2 = "insert into xj_misson_userid (misson_id,userid) value";
- foreach ($ResUser as $key => $DataUser) {
- $sqlI2 .= "('" . $IsertId . "','" . $DataUser['userid'] . "'),";
- }
- $db -> query(substr($sqlI, 0, strlen($sqlI) - 1));
- $db -> query(substr($sqlI2, 0, strlen($sqlI2) - 1));
- }
- $db -> colse();
- } else {
- echo "不在时间段内";
- }
- class DB {
- private $Con;
- private $Locahost;
- private $User;
- private $Password;
- private $Db;
- public function __construct($Locahost, $User, $Password, $Db) {
- $this -> Locahost = $Locahost;
- $this -> User = $User;
- $this -> Password = $Password;
- $this -> Db = $Db;
- }
- public function connect() {
- try {
- $this -> Con = mysql_connect($this -> Locahost, $this -> User, $this -> Password);
- mysql_query("set character set 'utf8'");
- //读库
- mysql_query("set names 'utf8'");
- //写库
- mysql_select_db($this -> Db);
- } catch (Exception $e) {
- $this -> log('mysql could not connect ' . mysql_error());
- }
- }
- public function query($Sql) {
- try {
- $this -> log($Sql);
- return mysql_query($Sql, $this -> Con);
- } catch (Exception $e) {
- $this -> log('mysql error' . $Sql);
- }
- }
- public function getInsertid() {
- return mysql_insert_id($this -> Con);
- }
- public function getRow($sql, $type = "assoc") {
- $query = $this -> query($sql);
- if (!in_array($type, array("assoc", 'array', "row"))) {
- $this -> log("mysql_query error");
- }
- $funcname = "mysql_fetch_" . $type;
- return $funcname($query);
- }
- /**
- * [getAll description]获取所有数据
- * @param [type] $sql [sql语句]
- * @param string $type [返回类型 默认assoc]
- * @return [type] [二维数组数据]
- */
- public function getAll($sql, $type = "assoc") {
- $query = $this -> query($sql);
- if (!in_array($type, array("assoc", 'array', "row"))) {
- $this -> log("mysql_query error");
- }
- if ($query && mysql_num_rows($query) > 0) {
- $funcname = "mysql_fetch_" . $type;
- $List = array();
- while ($res = $funcname($query)) {
- $List[] = $res;
- }
- return $List;
- } else {
- return false;
- }
- }
- /**
- * [colse description]关闭mysql链接
- * @return [type] [description]
- */
- public function colse() {
- try {
- mysql_close($this -> Con);
- } catch (Exception $e) {
- $this -> log('mysql could not colse :' . mysql_error());
- }
- }
- /**
- * [log description] 记录日志
- * @param [type] $msg [description] 信息
- * @return [type] [description]
- */
- public function log($msg) {
- $msg = $msg . "\n";
- // echo $msg;
- $logFile = './plan.log';
- file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "]---" . $msg, FILE_APPEND | LOCK_EX);
- }
- }
- ?>
|