💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 数据库操作类 ~~~ <?php /** * Created by gather * Email: chenruiqiang@yd-x.com * Phone: 16601180687 * Copyright:源动互通(北京)科技有限公司 * Create Time: 2018/6/6 13:55 */ namespace test; class DB{ private static $dbcon = false; private $host; private $port; private $user; private $pass; private $db; private $charset; private $link; public function __construct(){ $this->host = "127.0.0.1"; $this->port = "3306"; $this->user = "root"; $this->pass = "xyq07041103"; $this->db = "email"; $this->charset = "utf8"; $this->db_connect(); $this->db_usedb(); $this->db_charset(); } //连接数据库 private function db_connect(){ //$host = '', $user = '', $password = '', $database = '', $port = '', $socket = '' $this->link = mysqli_connect($this->host,$this->user,$this->pass,$this->db,$this->port); if (!$this->link){ echo "数据库连接失败<br>"; echo "错误编码".mysqli_errno($this->link)."<br>"; echo "错误信息".mysqli_error($this->link)."<br>"; exit(); } } //设置字符集 public function db_charset(){ mysqli_query($this->link,"set names {$this->charset}"); } //选择数据库 public function db_usedb(){ mysqli_query($this->link,"use {$this->db}"); } //私有的克隆 private function __clone(){ // TODO: Implement __clone() method. die("clone is not allowed"); } //公用静态方法 public static function getInstance(){ if (self::$dbcon == false){ self::$dbcon = new self; } return self::$dbcon; } //执行sql语句的方法 public function query($sql){ $res = mysqli_query($this->link,$sql); if(!$res){ echo "sql语句执行失败<br>"; echo "错误编码是".mysqli_errno($this->link)."<br>"; echo "错误信息是".mysqli_error($this->link)."<br>"; } return $res; } //获得最后一条记录id public function getInsertId(){ return mysqli_insert_id($this->link); } /** * 查询莫个字段 * @param $sql * @return string or int */ public function getOne($sql){ $query = $this->query($sql); return mysqli_free_result($query); } //获取一行记录,return array 一维数组 public function getRow($sql,$type="assoc"){ $query = $this->query($sql); if(!in_array($type,array("assoc",'array','row'))){ die("mysql_query error"); } $funcname = "mysqli_fetch_".$type; return $funcname($query); } //获取一条记录,前置条件通过资源获取一条记录 public function getFormSource($query,$type="assoc"){ if(!in_array($type,array("assoc","array","row"))){ die("mysqli_query error"); } $funcname = "mysqli_fetch_".$type; return $funcname($query); } //获取多条数据,二维数组 public function getAll($sql){ $query = $this->query($sql); $list = array(); while ($r=$this->getFormSource($query)){ $list[] = $r; } return $list; } //选择所有 public function selectAll($table,$where,$fields='*',$order='',$skip=0,$limit=1000){ if (is_array($where)){ foreach ($where as $key => $val){ if(is_numeric($val)){ $condition = $key.'='.$val; }else{ $condition = $key.'=\''.$val.'\''; } } }else{ $condition = $where; } if (!empty($order)){ $order = " order by ".$order; } $sql = "select $fields from $table where $condition $order limit $skip,$limit"; $query = $this->query($sql); $list = array(); while ($r=$this->getFormSource($query)){ $list[] = $r; } return $list; } public function insert($table,$data){ $key_str = ""; $v_str=""; foreach ($data as $key=>$v) { $key_str .= $key.','; $v_str .= "'$v'".','; } $key_str = trim($key_str,','); $v_str = trim($v_str,','); $sql = "insert into $table($key_str) values($v_str)"; $this->query($sql); return $this->getInsertId(); } public function deleteOne($table,$where){ if(is_array($where)){ foreach ($where as $key=>$val){ $condition = $key.'='.$val; } }else{ $condition = $where; } $sql = "delete from $table where $condition"; $this->query($sql); return mysqli_affected_rows($this->link); } public function deleteAll($table,$where){ if(is_array($where)){ foreach ($where as $key=>$val){ if(is_array($val)) { $condition = $key.' in('.implode(',',$val).')'; }else{ $condition = $key.'='.$val; } } }else{ $condition = $where; } $sql = "delete from $table where $condition"; $this->query($sql); return mysqli_affected_rows($this->link); } public function update($table,$data,$where,$limit=0){ $str = ''; foreach ($data as $key=>$v){ $str .= "$key='$v'".','; } $str = rtrim($str,','); if (is_array($where)){ foreach ($where as $key=>$val){ if(is_array($val)) { $condition = $key.' in('.implode(',',$val).')'; }else{ $condition = $key.'='.$val; } } }else{ $condition = $where; } if (empty($limit)){ $limit = " limit ".$limit; }else{ $limit = ''; } $sql = "update $table set $str where $condition $limit"; // var_dump($sql); $this->query($sql); return mysqli_affected_rows($this->link); } } ~~~ ## 数据表 ~~~ CREATE TABLE `order_queue` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `order_id` int(11) NOT NULL, `mobile` varchar(2) NOT NULL COMMENT '用户手机号', `created_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `status` tinyint(4) NOT NULL COMMENT '0未处理1已处理2处理中', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 ~~~ ## 增加订单order.php文件 ~~~ <?php /** * Created by gather * Email: chenruiqiang@yd-x.com * Phone: 16601180687 * Copyright:源动互通(北京)科技有限公司 * Create Time: 2018/6/6 18:55 */ //这个文件主要是配送系统处理队列中的订单并进行标记的一个文件 require "DB.php"; use test\DB; $db=DB::getInstance(); //1)先把要处理的记录更新为等待处理, $waiting = array('status'=>0); $data = array( 'status'=>2, ); $res_lock = $db->update('order_queue',$data,$waiting,2); //2)我们要选择出刚刚咱们更新的这些数据,然后进行配送系统的处理。 if ($res_lock){ //选择出要处理的订单内容,有配货系统处理 $res = $db->selectAll('order_queue',$data); //有配货系统进行退货处理 //把订单更新已完成 $success = array( 'status'=>1, 'updated_at'=>date("Y-m-d H:i:s") ); $res_last = $db->update('order_queue',$success,$data,2); if ($res_last){ echo "Success:".$res_last; }else{ echo "Fail:".$res_last; } }else{ echo "没有需要处理的数据"; } //3)处理过的更新为已完成。 ~~~ ## 处理订单goods.php文件 ~~~ <?php /** * Created by gather * Email: chenruiqiang@yd-x.com * Phone: 16601180687 * Copyright:源动互通(北京)科技有限公司 * Create Time: 2018/6/6 18:55 */ //这个文件主要是配送系统处理队列中的订单并进行标记的一个文件 require "DB.php"; use test\DB; $db=DB::getInstance(); //1)先把要处理的记录更新为等待处理, $waiting = array('status'=>0); $data = array( 'status'=>2, ); $res_lock = $db->update('order_queue',$data,$waiting,2); //2)我们要选择出刚刚咱们更新的这些数据,然后进行配送系统的处理。 if ($res_lock){ //选择出要处理的订单内容,有配货系统处理 $res = $db->selectAll('order_queue',$data); //有配货系统进行退货处理 //把订单更新已完成 $success = array( 'status'=>1, 'updated_at'=>date("Y-m-d H:i:s") ); $res_last = $db->update('order_queue',$success,$data,2); if ($res_last){ echo "Success:".$res_last; }else{ echo "Fail:".$res_last; } }else{ echo "没有需要处理的数据"; } //3)处理过的更新为已完成。 ~~~ ## shell 文件 good.sh ~~~ #!/bin/bash date "+%G-%m-%d %H:%M:%S" cd /Applications/XAMPP/xamppfiles/htdocs/study/order_queue/ php goods.php ~~~ ## 定时任务crontab 命令 crontab -e `*/1 * * * * /Applications/XAMPP/xamppfiles/htdocs/study/order_queue/good.sh >> /Applications/XAMPP/xamppfiles/htdocs/study/order_queue/log.log 2>&1`