文件
mashangyoujiu/Application/Common/Model/OrderSyncLogModel.class.php

142 行
3.4 KiB
PHP

<?php
namespace Common\Model;
use Common\Model\BaseModel;
/**
* 订单同步日志模型
* Class OrderSyncLogModel
* @package Common\Model
*/
class OrderSyncLogModel extends BaseModel
{
/**
* 表名
* @var string
*/
protected $tableName = 'rider_order_sync_log';
/**
* 添加同步日志
* @param array $data
* @return int|false
*/
public function addLog($data)
{
return $this->add($data);
}
/**
* 获取同步日志列表
* @param array $where 查询条件
* @param int $page 页码
* @param int $pageSize 每页数量
* @return array
*/
public function getLogList($where = [], $page = 1, $pageSize = 20)
{
$list = $this->where($where)
->order('sync_time DESC')
->page($page, $pageSize)
->select();
$total = $this->where($where)->count();
return [
'list' => $list ?: [],
'total' => $total,
'page' => $page,
'page_size' => $pageSize
];
}
/**
* 获取订单的同步日志
* @param int $billId 原订单ID
* @return array
*/
public function getOrderSyncLogs($billId)
{
return $this->where(['bill_id' => $billId])
->order('sync_time DESC')
->select();
}
/**
* 获取骑手订单的同步日志
* @param int $riderOrderId 骑手订单ID
* @return array
*/
public function getRiderOrderSyncLogs($riderOrderId)
{
return $this->where(['rider_order_id' => $riderOrderId])
->order('sync_time DESC')
->select();
}
/**
* 清理过期日志
* @param int $days 保留天数
* @return int
*/
public function cleanExpiredLogs($days = 30)
{
$expireDate = date('Y-m-d H:i:s', strtotime("-{$days} days"));
return $this->where(['sync_time' => ['lt', $expireDate]])->delete();
}
/**
* 获取记录列表
* @param array $where 查询条件
* @param string $field 查询字段
* @param string $order 排序
* @param string $limit 分页
* @return array
*/
public function getList($where = array(), $field = '*', $order = 'id desc', $limit = '') {
if ($limit) {
return $this->field($field)->where($where)->order($order)->limit($limit)->select();
} else {
return $this->field($field)->where($where)->order($order)->select();
}
}
/**
* 删除日志记录
* @param array $where 删除条件
* @return bool
*/
public function deleteLog($where = array()) {
if (empty($where)) {
return false;
}
return $this->where($where)->delete();
}
/**
* 获取同步统计信息
* @param array $where 查询条件
* @return array
*/
public function getSyncStats($where = [])
{
$total = $this->where($where)->count();
$actionStats = $this->where($where)
->field('action, COUNT(*) as count')
->group('action')
->select();
$stats = [
'total' => $total,
'actions' => []
];
foreach ($actionStats as $stat) {
$stats['actions'][$stat['action']] = $stat['count'];
}
return $stats;
}
}