// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\FinanceModel; use App\Services\BaseService; /** * 平台账户(财务)-服务类 * @author laravel开发员 * @since 2020/11/12 * Class ActionLogService * @package App\Services\Common */ class FinanceService extends BaseService { protected static $instance=null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * FinanceService constructor. */ public function __construct() { $this->model = new FinanceModel(); } /** * 静态入口 * @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) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $type = isset($params['type'])? $params['type'] : 0; if($status>0){ $where['a.status'] = $status; } if($type>0){ $where['a.type'] = $type; } $list = $this->model->from('finance as a') ->where($where) ->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)); } }) ->select(['a.*']) ->orderBy('a.create_time','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : ''; } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 平台结算 * @param $money * @param int $changeType 交易类型:1-进账,2-出账 * @param int $type 类型:1-消费,2-佣金,3-充值提现转账,4-退款,99-其他 * @param int $coinType 币种类型: 1-USDT,2-星豆 * @return mixed */ public function saveLog($userId, $money, $changeType = 1, $type=1, $coinType = 1) { $date = date('Y-m-d'); $info = $this->model->where(['user_id'=> $userId,'date'=> $date,'type'=> $type,'mark'=>1])->first(); if(!$info){ $data = ['user_id'=>$userId,'date'=> $date,'type'=>$type,'create_time'=>time(),'update_time'=> time(),'status'=>1]; if($changeType==1){ $data['income'] = $money; }else{ $data['expend'] = $money; } return $this->model->insertGetId($data); }else{ if($changeType == 1){ $info->income += $money; }else{ $info->expend += $money; } return $info->save(); } } }