<?php
/**
* Created by PhpStorm.
* User: guojuqi
* Email: 735345789@qq.com
* Date: 2020/5/7
* Time: 9:51
*/
namespace app\api\service;
use think\Cache;
use think\Db;
class FxService
{
protected $level;
protected $money;
protected $userDbObj;
/**
* FxService constructor.
* @param $level
*/
public function __construct($level = '')
{
if ($level == '') {
$level = Cache::get('fx_config');
if (!$level) {
$level = $this->getFxLevelConfig();
Cache::set('fx_config', $level, 3600 * 24);
}
}
$this->level = $level;
$this->userDbObj = Db::name('members');
}
public function getFxLevelConfig()
{
$return = false;
$level = Db::name('n_config')->where('name', 'fx')
->order('key asc')->field('value')->select()->toArray();
foreach ($level as $k => $v) {
$return[] = $v['value'];
}
return $return;
}
/**
* 设置金额
* @param $money
*/
public function setMoney($money)
{
$this->money = $money;
}
//$level = [55, 14, 11, 7, 5, 3];
/**
* @param $id
* @return array|bool
*/
public function getYj($id)
{
$member = $this->queryDb($id);
if (!$member) {
return false;
}
$data = [];
foreach ($this->level as $k => $v) {
//用户7 id为71 拿去100的百分之55
$fund = $this->money * $v * 0.01;
$data[$member['id']] = $fund;
//查询 用户7 上级信息
$member = $this->queryDb($member['parent_id']);
if (empty($member)) {
break; //结束循环
}
}
return $data;
}
/**
* 设置db对象
* @param $userDbObj
*/
public function setUserObj($userDbObj)
{
$this->userDbObj = $userDbObj;
}
/**
* db对象
* @param $id
* @return mixed
*/
public function queryDb($id)
{
return $this->userDbObj->where('id', $id)->field('id, parent_id')->find();
}
}
调用
数据入库