test_service d3170b4d1c 1
2023-12-01 15:43:29 +08:00

175 lines
4.8 KiB
PHP

<?php
namespace app\common\model\food;
/**
* 预约模型
*/
class Pact extends BaseModel
{
// 定义表名
protected $name = 'food_pact';
// 定义主键
protected $pk = 'pact_id';
// 追加字段
protected $append = [
'user'
];
/**
* 关联门店表
*/
public function shop()
{
return $this->belongsTo('app\\common\\model\\food\\Shop','shop_id');
}
/**
* 约定时间
*/
public function getPactTimeAttr($value)
{
return ['date' => date("Y-m-d H:i:s",$value),'text' => (int)date("m",$value) .'月'. (int)date("d",$value) . '日' . date("H:i",$value), 'value' => $value];
}
/**
* 关联用户表
*/
public function getUserAttr($value,$data)
{
if($data['user_id'] == 0){
return [
'user_id' => '--',
'nickname' => '--',
'avatar' => [
'url' => base_url() . 'addons/food/img/avatar.png'
]
];
}
return User::get($data['user_id']);
}
/**
* 状态
*/
public function getStatusAttr($value)
{
$status = [10 => '预约中', 20 => '已过期', 30 => '已守约', 40 => '已取消'];
return ['text' => $status[$value], 'value' => $value];
}
/**
* 获取列表
*/
public function getList($shop_id = 0, $status = 0,$search='')
{
$model = $this->with(['shop']);
//筛选
$filter = [];
$shop_id > 0 && $filter['shop_id'] = $shop_id;
$status > 0 && $filter['status'] = $status;
if(!empty($search)){
//是否是手机号
if(is_phone($search)){
$filter['phone'] = $search;
}else{
$model->where('linkman','like',"%{$search}%");
}
}
// 排序规则
$sort = [];
$sort = ['pact_time' => 'desc'];//按照约定时间排序
// 执行查询
return $model->where($filter)
->order($sort)
->paginate(['list_rows'=>15,'query' => request()->param()]);
}
/**
* 获取详情
*/
public static function detail($id)
{
return self::with(['shop'])->find($id);
}
/**
* 添加
*/
public function add(array $data)
{
$data['applet_id'] = self::$applet_id;
return $this->save($data);
}
/**
* 删除
*/
public function remove()
{
return $this->delete();
}
/**
* 状态操作
*/
public function status($status)
{
return $this->save(['status' => $status]);
}
/**
* 解除餐桌预定
*/
public function rescind()
{
return $this->save(['status' => 40]);
}
/**
* 进行中的统计
*/
public static function getNowCount($shop_id = 0)
{
// 筛选条件
$filter['status'] = 10;
$shop_id > 0 && $filter['shop_id'] = $shop_id;
return self::where($filter)->count();
}
/**
* 根据时间段统计数量
*/
public static function getDateCount($data)
{
// 筛选条件
$filter = [];
$data['shop_id'] > 0 && $filter['shop_id'] = $data['shop_id'];
return self::where('create_time','>',$data['star'])
->where('create_time','<',$data['end'])
->where($filter)
->count();
}
/**
* 根据条件统计数量
*/
public static function getCount($shop_id = 0)
{
$self = new static;
// 筛选条件
$filter = [];
$shop_id > 0 && $filter['shop_id'] = $shop_id;
$applet_id = $self::$applet_id;
empty($applet_id) && $filter['applet_id'] = $applet_id;
$count = array();
//全部
$count['count'] = self::where($filter)->count();
//今天
$star = strtotime(date("Y-m-d"),time());
$count['today'] = self::where('create_time','>',$star)->where($filter)->count();
//昨天
$star = strtotime("-1 day");
$end = strtotime(date("Y-m-d"),time());
$count['today2'] = self::where('create_time','>',$star)->where('create_time','<',$end)->where($filter)->count();
//本月起至时间 - 月度统计
$end = mktime(0,0,0,date('m'),1,date('y'));
$count['month'] = self::where('create_time','>',$end)->where($filter)->count();
//上月开始
$star = mktime(0,0,0,date('m')-1,1,date('y'));
$count['month2'] = self::where('create_time','>',$star)->where('create_time','<',$end)->where($filter)->count();
return $count;
}
}