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); } } ?>