cxhxy/app/common/model/Record.php
2023-11-21 15:14:59 +08:00

1 line
6.9 KiB
PHP

<?php
namespace app\common\model;
/**
* 交易记录模型
*/
class Record extends BaseModel
{
// 定义表名
protected $name = 'record';
// 定义主键
protected $pk = 'record_id';
// 追加字段
protected $append = [];
/**
* 关联用户表
*/
public function user()
{
return $this->belongsTo('app\\common\\model\\User','user_id');
}
/**
* 交易模式
*/
public function getModeAttr($value)
{
$status = [10 => '充值', 20 => '消费', 30 => '退款', 40 => '赠送', 50 => '扣减'];
return ['text' => $status[$value], 'value' => $value];
}
/**
* 支付类型
*/
public function getTypeAttr($value)
{
$status = [10 => '余额', 20 => '现金', 30 => '微信', 90 => '积分'];
return ['text' => $status[$value], 'value' => $value];
}
/**
* 获取列表
*/
public function getList($filter = [])
{
// 执行查询
return $this->with(['user'])
->where($filter)
->order('record_id','desc')
->paginate(['list_rows'=>15,'query' => request()->param()]);
}
/**
* 根据条件统计数量
*/
public static function getCount($user_id = 0)
{
// 筛选条件
$filter = [];
$user_id > 0 && $filter['user_id'] = $user_id;
$count = array();
//全部
$model = self::where($filter);
$count['all'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//今天
$star = strtotime(date("Y-m-d"),time());
$model = self::where($filter)->where('create_time','>',$star);
$count['today'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//昨天
$star = strtotime("-1 day");
$end = strtotime(date("Y-m-d"),time());
$model = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end);
$count['today2'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//前天
$star = strtotime("-2 day");
$end = strtotime("-1 day");
$model = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end);
$count['today3'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//-4天
$star = strtotime("-3 day");
$end = strtotime("-2 day");
$model = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end);
$count['today4'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//-5天
$star = strtotime("-4 day");
$end = strtotime("-3 day");
$model = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end);
$count['today5'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//-6天
$star = strtotime("-5 day");
$end = strtotime("-4 day");
$model = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end);
$count['today6'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//-7天
$star = strtotime("-6 day");
$end = strtotime("-5 day");
$model = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end);
$count['today7'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//本月起至时间 - 月度统计
$end = mktime(0,0,0,date('m'),1,date('y'));
$model = self::where($filter)->where('create_time','>',$end);
$count['month'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
//上月开始
$star = mktime(0,0,0,date('m')-1,1,date('y'));
$model = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end);
$count['month2'] = [
10 => $model->where('mode',10)->sum('money'),//充值
20 => $model->where('mode',20)->sum('money'),//消费
30 => $model->where('mode',30)->sum('money'),//退款
40 => $model->where('mode',40)->sum('money'),//赠送
50 => $model->where('mode',50)->sum('money'),//扣减
];
return $count;
}
}