| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- <?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\FinanceModel;
- use App\Models\MemberModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 平台账户(财务)-服务类
- * @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();
- }
- }
- /**
- * 升级收益结算
- * @param $userId 升级用户ID
- * @param $userInfo 升级用户信息:parents-上级,points-上级节点,parent_id-推荐人,member_level-升级用户等级
- * @param $usdt 升级消费的USDT
- * @param $orderNo 升级记录单号
- * @param string $remark 升级项目备注说明
- * @return bool
- */
- public function settleBonus($userId, $userInfo, $usdt, $orderNo, $remark='')
- {
- $parents = isset($userInfo['parents'])? $userInfo['parents'] : ''; // 上级
- $points = isset($userInfo['points'])? $userInfo['points'] : ''; // 挂靠节点
- $parentId = isset($userInfo['parent_id'])? $userInfo['parent_id'] : 0;
- $userLevel = isset($userInfo['member_level'])? $userInfo['member_level'] : 0; // 用户等级
- RedisService::set("caches:bonus:temp_{$userId}_{$orderNo}", ['info'=> $userInfo,'usdt'=> $usdt,'remark'=> $remark], 6 * 3600);
- if($userLevel<=0){
- $this->error = 2607;
- return false;
- }
- DB::beginTransaction();
- // TODO 直推奖励结算
- if($usdt>0 && $parentId>0){
- $parentInfo = MemberModel::where(['id'=> $parentId,'mark'=>1])
- ->select(['id','usdt','balance','wait_score'])
- ->first();
- $rate1 = ConfigService::make()->getConfigByCode('award_push_rate',0);
- $rate1 = $rate1>0 && $rate1 <100? $rate1 : 0;
- $bonusUsdt1 = moneyFormat($usdt * $rate1/100, 4);
- if($parentInfo && $bonusUsdt1>0){
- // 账户进账
- $updateData = ['usdt'=> DB::raw("usdt + {$bonusUsdt1}"),'update_time'=>time()];
- if(!MemberModel::where(['id'=> $parentId,'mark'=>1])->update($updateData)){
- DB::rollBack();
- $this->error = 2601;
- return false;
- }
- // 收益明细
- $log = [
- 'user_id' => $parentId,
- 'source_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 12,
- 'coin_type' => 1,
- 'user_type'=> 1,
- 'money' => $bonusUsdt1,
- 'actual_money' => $bonusUsdt1,
- 'balance' => isset($parentInfo['usdt'])? $parentInfo['usdt'] : 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => $remark,
- 'status' => 1,
- 'mark' => 1,
- ];
- if(!AccountLogModel::insertGetId($log)){
- DB::rollBack();
- $this->error = 2602;
- return false;
- }
- // TODO 平台流水(拨出对应比例USDT)
- FinanceService::make()->saveLog(0, $bonusUsdt1, 2);
- }
- }
- // TODO 点对点奖励收益结算 (会员升级数必须小于等于节点会员层数,即按同层给奖励)
- $points = $points? explode(',', $points) : [];
- $points = array_filter($points);
- $pointId = isset($points[$userLevel-1])? $points[$userLevel-1] : 0;
- if($points && count($points) >= $userLevel && $pointId>0){
- // TODO 当前会员升级为N级,且N层上级节点会员等级>=升级会员等级时奖励点对点奖励(否则等级烧伤)
- $pointInfo = MemberModel::where(['id'=> $pointId,'mark'=>1])
- ->select(['id','usdt','balance','member_level','wait_score'])
- ->first();
- $pointLevel = isset($pointInfo['member_level'])? $pointInfo['member_level'] : 0;
- $rate2 = ConfigService::make()->getConfigByCode('award_node_rate',0);
- $rate2 = $rate2>0 && $rate2 <100? $rate2 : 0;
- $bonusUsdt2 = moneyFormat($usdt * $rate2/100, 4);
- if($pointInfo && $bonusUsdt2>0){
- if($pointLevel>= $userLevel){
- // 账户进账
- $updateData = ['usdt'=> DB::raw("usdt + {$bonusUsdt2}"),'update_time'=>time()];
- if(!MemberModel::where(['id'=> $pointId,'mark'=>1])->update($updateData)){
- DB::rollBack();
- $this->error = 2603;
- return false;
- }
- // 收益明细
- $log = [
- 'user_id' => $parentId,
- 'source_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 14,
- 'coin_type' => 1,
- 'user_type'=> 1,
- 'money' => $bonusUsdt2,
- 'actual_money' => $bonusUsdt2,
- 'balance' => isset($pointInfo['usdt'])? $pointInfo['usdt'] : 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => $remark,
- 'status' => 1,
- 'mark' => 1,
- ];
- if(!AccountLogModel::insertGetId($log)){
- DB::rollBack();
- $this->error = 2604;
- return false;
- }
- // TODO 平台流水(拨出对应比例USDT)
- FinanceService::make()->saveLog(0, $bonusUsdt2, 2);
- }
- // 烧伤明细处理(不进账)
- else{
- $log = [
- 'user_id' => $parentId,
- 'source_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 97,
- 'coin_type' => 1,
- 'user_type'=> 1,
- 'money' => $bonusUsdt2,
- 'actual_money' => $bonusUsdt2,
- 'balance' => isset($pointInfo['usdt'])? $pointInfo['usdt'] : 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => $remark,
- 'status' => 1,
- 'mark' => 1,
- ];
- if(!AccountLogModel::insertGetId($log)){
- DB::rollBack();
- $this->error = 2604;
- return false;
- }
- }
- }
- }
- // TODO 管理奖结算(往上找最近的等级为5的倍数且等级大于升级用户的顺位用户,奖励管理奖)
- $parents = $parents? explode(',', $parents) : [];
- $parents = array_filter($parents);
- if($parents){
- $awardIndex = -1;
- $logs = []; // 明细和烧伤明细
- foreach ($parents as $k => $pid){
- $parentInfo = MemberModel::where(['id'=> $parentId,'mark'=>1])
- ->select(['id','usdt','balance','member_level','wait_score'])
- ->first();
- $parentLevel = isset($parentInfo['member_level'])? $parentInfo['member_level'] : 0;
- $rate3 = ConfigService::make()->getConfigByCode('award_manage_rate',0);
- $rate3 = $rate3>0 && $rate3 <100? $rate3 : 0;
- $bonusUsdt3 = moneyFormat($usdt * $rate3/100, 4);
- if($parentLevel>= $userLevel && ($parentLevel%5==0)){
- $awardIndex = $k;
- if($bonusUsdt3>0){
- // 账户进账
- $updateData = ['usdt'=> DB::raw("usdt + {$bonusUsdt3}"),'update_time'=>time()];
- if(!MemberModel::where(['id'=> $pid,'mark'=>1])->update($updateData)){
- DB::rollBack();
- $this->error = 2605;
- return false;
- }
- // 收益明细
- $logs[] = [
- 'user_id' => $pid,
- 'source_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 13,
- 'coin_type' => 1,
- 'user_type'=> 1,
- 'money' => $bonusUsdt3,
- 'actual_money' => $bonusUsdt3,
- 'balance' => isset($parentInfo['usdt'])? $parentInfo['usdt'] : 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => $remark,
- 'status' => 1,
- 'mark' => 1,
- ];
- }
- }
- // (已经找到) 剩下的管理奖烧伤
- else if($awardIndex>=0){
- // 管理烧伤明细
- $logs[] = [
- 'user_id' => $pid,
- 'source_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 98,
- 'coin_type' => 1,
- 'user_type'=> 1,
- 'money' => $bonusUsdt3,
- 'actual_money' => $bonusUsdt3,
- 'balance' => isset($parentInfo['usdt'])? $parentInfo['usdt'] : 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => $remark,
- 'status' => 1,
- 'mark' => 1,
- ];
- }
- // 还没找到,即等级条件不满足,等级烧伤
- else {
- // 等级烧伤明细
- $logs[] = [
- 'user_id' => $pid,
- 'source_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 97,
- 'coin_type' => 1,
- 'user_type'=> 1,
- 'money' => $bonusUsdt3,
- 'actual_money' => $bonusUsdt3,
- 'balance' => isset($parentInfo['usdt'])? $parentInfo['usdt'] : 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => $remark,
- 'status' => 1,
- 'mark' => 1,
- ];
- }
- }
- if($logs && !AccountLogModel::insert($logs)){
- DB::rollBack();
- $this->error = 2606;
- return false;
- }
- }
- DB::commit();
- $this->error = 2608;
- return true;
- }
- /**
- * 任务算力奖励
- * @param $userId 用户ID
- * @param $power 算力
- * @param $type 类型
- * @param int $sourceId 任务ID
- * @param string $remark 备注说明
- * @return false
- */
- public function settleTaskPower($userId, $power, $type, $sourceId=0, $remark='')
- {
- if($power<=0){
- $this->error = 2014;
- return false;
- }
- $userInfo = MemberModel::where(['id'=> $userId, 'status'=>1,'mark'=>1])
- ->select(['id','nickname','usdt','power_num','status'])
- ->first();
- $userPower = isset($userInfo['power_num'])? $userInfo['power_num'] : 0;
- if(empty($userInfo) || $userId<=0)
- {
- $this->error = 1041;
- return false;
- }
- DB::beginTransaction();
- // 更新账户算力
- $updateData = ['power_num'=> DB::raw("power_num + {$power}"),'update_time'=>time()];
- if(!MemberModel::where(['id'=> $userId])->update($updateData)){
- DB::rollBack();
- $this->error = 1042;
- return false;
- }
- // 明细
- $orderNo = get_order_num('TS');
- $log = [
- 'user_id' => $userId,
- 'source_id' => $sourceId,
- 'source_order_no' => $orderNo,
- 'type' => $type?$type:99,
- 'coin_type' => 3,
- 'user_type'=> 1,
- 'money' => $power,
- 'actual_money' => $power,
- 'balance' => $userPower,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => $remark? $remark : '算力奖励',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- $this->error = 2407;
- DB::rollBack();
- return false;
- }
- DB::commit();
- // 站内消息
- $dateTime = date('Y-m-d H:i:s');
- MessageService::make()->pushMessage($userId, $log['remark'] , "您在{$dateTime}(UTC+8){$log['remark']}{$power}算力已到账,请及时查看账户!!!",3);
- }
- }
|