| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\AccountLogModel;
- use App\Models\MemberModel;
- use App\Models\NoticeModel;
- use App\Models\PriceModel;
- use App\Models\PtAccountModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 价格服务-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class PriceService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * NoticeService constructor.
- */
- public function __construct()
- {
- $this->model = new PriceModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 当前价格
- * @param int $type 1-数字资产,2-绿色积分
- * @param false $refresh
- * @return array|mixed|string
- */
- public function getTodayPrice($type=1, $refresh=false)
- {
- $cacheKey = "caches:prices:today_price_{$type}";
- $data = RedisService::get($cacheKey);
- $price = isset($data['price'])?$data['price']: '0.00';
- if($data && $price && !$refresh){
- return floatval($price);
- }
- $data = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
- ->orderBy('date','desc')
- ->first();
- $data = $data?$data->toArray() : [];
- $price = isset($data['price'])?$data['price']: '0.00';
- if($data && $price){
- RedisService::set($cacheKey, $data, rand(300,600));
- }
- return floatval($price);
- }
- /**
- * 是否已经更新过
- * @param $date
- * @param int $type
- * @return array|bool
- */
- public function getPriceData($date,$type=1)
- {
- $cacheKey = "caches:prices:data_{$type}_{$date}";
- if(RedisService::get($cacheKey)){
- return true;
- }
- $data = $this->model->where(['date'=>$date,'type'=>$type,'status'=>1,'mark'=>1])
- ->orderBy('date','desc')
- ->first();
- $data = $data?$data->toArray() : [];
- $price = isset($data['price'])?$data['price']: '0.00';
- if($data && $price){
- RedisService::set($cacheKey, $data, rand(300,600));
- }
- return $data;
- }
- /**
- * 更新价格
- * @param int $type 1-资产
- * @return array|false
- */
- public function updatePrice($type=1)
- {
- try {
- if($type==1){
- $date = date('Y-m-d', strtotime(date('Y-m-d')) - 86400);
- $lastDate = date('Y-m-d', strtotime($date) - 86400);
- $cacheKey = "caches:prices:update_{$type}:".$date;
- if(RedisService::get($cacheKey)){
- $this->error = '价格已更新';
- return false;
- }
- // 是否已经更新过
- if($this->getPriceData($date, $type)){
- $this->error = '价格已更新';
- return false;
- }
- // 平台账户
- $ptAccount = PtAccountModel::where(['mark'=>1])->first();
- $accountId = isset($ptAccount['id'])?$ptAccount['id'] : 0;
- if(empty($ptAccount) || $accountId<=0){
- $ptAccount = ['balance'=>0,'property'=>10000,'pool_total'=>100,'create_time'=>time()];
- $accountId = PtAccountModel::insertGetId($ptAccount);
- }
- // 昨日价格
- $lastPrice = $this->getTodayPrice($type);
- // 昨日全网资产
- $lastProperty = isset($ptAccount['last_property'])?$ptAccount['last_property']:0;
- $ptProperty = isset($ptAccount['property'])?$ptAccount['property']:0; // 平台初始资产
- // 今日新增资产
- $todayPropertyTotal = 0;
- $todayProperty = AccountLogModel::where(['date'=>$date,'type'=>8,'account_type'=>2,'mark'=>1])->sum('money');
- if($todayProperty>=0){
- // 当日全网资产衰减后
- $lastProperty = floatval($lastProperty)>0?$lastProperty:$ptProperty;
- $propertyTotalByReduce = moneyFormat($lastProperty * 0.99,6);
- // 当日全网总资产
- $todayPropertyTotal = moneyFormat($propertyTotalByReduce + $todayProperty, 6);
- }else if($lastPrice<=0){
- // 当日全网总资产
- $todayPropertyTotal = $ptProperty;
- }
- // 平台底池总金额
- $poolTotal = isset($ptAccount['pool_total'])?$ptAccount['pool_total']:0;
- $todayPoolTotal = isset($ptAccount['today_pool'])?$ptAccount['today_pool']:0;
- // 当日价格
- $price = $todayPropertyTotal>0?moneyFormat($poolTotal/$todayPropertyTotal, 6) : 0;
- if($price>0){
- $data = [
- 'type'=>$type,
- 'price'=> $price,
- 'last_price'=> isset($lastPriceData['price'])?$lastPriceData['price'] : 0,
- 'pool_total'=>$poolTotal,
- 'today_pool'=>$todayPoolTotal,
- 'last_property'=>$lastProperty,
- 'today_property'=>$todayProperty,
- 'property_total'=>$todayPropertyTotal,
- 'date'=>$date,
- 'updated_at'=> date('Y-m-d H:i:s'),
- 'create_time'=>time(),
- 'status'=>1,
- ];
- DB::beginTransaction();
- if(!$logId = $this->model->insertGetId($data)){
- DB::rollBack();
- $this->error = '更新资产价格失败';
- return false;
- }
- $updateData = [
- 'property_price'=> $price,
- 'last_property'=> $todayPropertyTotal,
- 'today_property'=> 0,
- 'today_pool'=> 0,
- 'update_time'=>time()
- ];
- if(!PtAccountModel::where(['id'=>$accountId])->update($updateData)){
- DB::rollBack();
- $this->error = '更新平台账户数据失败';
- return false;
- }
- DB::commit();
- $data['id'] = $logId;
- RedisService::set($cacheKey, $data, 600);
- RedisService::clear("caches:prices:today_price_{$type}");
- RedisService::clear("caches:prices:data_{$type}_{$date}");
- $this->error = '更新资产价格成功';
- return ['date'=>$date,'price'=>$price,'pool_total'=>$poolTotal,'property'=>$todayPropertyTotal];
- }else{
- $this->error = '今日资产价格无需更新';
- return false;
- }
- }
- $this->error = '更新资产价格失败';
- return false;
- } catch (\Exception $exception){
- $this->error = "更新资产每日价格失败:".$exception->getMessage();
- return false;
- }
- }
- }
|