bztang-admin/plugins/cultural-space/src/models/ContributionLog.php

97 lines
2.5 KiB
PHP

<?php
namespace Yunshop\CulturalSpace\models;
use app\common\models\BaseModel;
use app\common\models\Member;
use Illuminate\Support\Facades\DB;
class ContributionLog extends BaseModel{
public $table = 'yz_cultural_space_contribution_log';
public $timestamps = false;
public $casts = [
'created_at' => 'datetime:Y-m-d H:i:s'
];
protected $fillable = [
'uniacid',
'uid',
'goods_id',
'order_id',
'order_goods_id',
'change_type',
'change_quantity',
'change_front',
'change_after',
'remark',
'created_at',
'source'
];
/**
* Common: 贡献值变更记录
* Author: wu-hui
* Time: 2023/11/03 9:39
* @param $search
* @param string[] $field
* @return array
*/
public function getList($search,$field = ['*']){
// 条件生成
$where = [];
if($search['uid'] > 0) $where[] = ['uid','=',$search['uid']];
if($search['change_type'] >= 0 && $search['change_type'] != '') $where[] = ['change_type','=',$search['change_type']];
// 列表获取
$list = self::uniacid()
->select($field)
->where($where)
->with([
'member' => function($query){
$query->select(['uid','nickname','realname','avatar']);
}
])
->orderBy('created_at','DESC')
->orderBy('id','DESC')
->paginate(10);
return $list ? $list->toArray() : [];
}
/**
* Common: 根据截至时间 获取全部用户持有的贡献值
* Author: wu-hui
* Time: 2023/11/03 14:13
* @param $endTime
* @return array|false|null
*/
public static function getAllList($endTime){
$idList = self::uniacid()
->select([DB::raw('max(id) as max_id')])
->where('created_at','<=',$endTime)
->groupBy('uid')
->get()
->toArray();
$ids = array_column($idList,'max_id');
$list = self::uniacid()
->select(['uid','change_after'])
->whereIn('id',$ids)
->get()
->toArray();
return array_column($list,'change_after','uid');
}
/**
* Common: 一对一关联 用户信息
* Author: wu-hui
* Time: 2023/11/03 9:35
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function member(){
return $this->hasOne(Member::class,'uid','uid');
}
}