// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AccountLogModel; use App\Services\BaseService; use App\Services\RedisService; /** * 交易管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class AccountService */ class AccountService extends BaseService { public static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * AccountService constructor. */ public function __construct() { $this->model = new AccountLogModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $query = $this->getQuery($params); $list = $query->select(['a.*']) ->orderBy('a.create_time','desc') ->orderBy('a.id','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } public function getQuery($params) { $where = ['a.mark' => 1]; $type = isset($params['type'])? $params['type'] : 0; if($type>0){ $where['a.type'] = $type; } return $this->model->with(['member'])->from("account_logs as a") ->leftJoin('member as b','b.id','=','a.user_id') ->where($where) ->where(function ($query) use($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; $userId = isset($params['user_id'])? $params['user_id'] : 0; if($userId){ $query->where('a.user_id',$userId); } $status = isset($params['status'])? $params['status'] : 0; if($status){ $query->where('a.status',$status); }else{ $query->whereNotIn('a.status',[2]); } $changeType = isset($params['change_type'])? $params['change_type'] : 0; if($changeType==1){ $query->where('money','<', 0); }else if($changeType==2){ $query->where('money','>', 0); } if ($keyword) { $query->where(function($query) use($keyword){ $query->where('b.nickname','like',"%{$keyword}%") ->orWhere('b.mobile','like',"%{$keyword}%") ->orWhere('b.realname','like',"%{$keyword}%"); }); } $orderNo = isset($params['order_no'])? trim($params['order_no']) : ''; if($orderNo){ $query->where(function($query) use($orderNo){ $query->where('a.source_order_no','like',"%{$orderNo}%"); }); } }) ->where(function ($query) use($params){ // 日期 $date = isset($params['date']) ? $params['date'] : []; $start = isset($date[0])? $date[0] : ''; $end = isset($date[1])? $date[1] : ''; $end = $start>=$end? '' : $end; if ($start) { $query->where('a.create_time','>=', strtotime($start)); } if($end){ $query->where('a.create_time','<=', strtotime($end)); } }); } /** * 账单 * @param $id * @return array|mixed */ public function getInfo($id) { $cacheKey = "caches:accounts:info_{$id}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $info = $this->model->where(['id'=> $id,'mark'=>1]) ->first(); $info = $info? $info->toArray() : []; if($info){ RedisService::set($cacheKey, $info, rand(5,10)); } return $info; } }