95 lines
3.1 KiB
PHP
95 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
class MerchantCommission extends BaseModel
|
||
{
|
||
//
|
||
public function getStatusNameAttribute(){
|
||
$type=config('adconfig.commission_status');
|
||
return $type[$this->status];
|
||
}
|
||
public function merchants(){
|
||
return $this->belongsTo('App\Models\Merchant','merchant_id','id');
|
||
}
|
||
public function fromMerchants(){
|
||
return $this->belongsTo('App\Models\Merchant','from_merchant_id','id');
|
||
}
|
||
public static function getOrCreate($data=[],$where){
|
||
$has_user=self::where($where)->first();
|
||
if(!empty($has_user))
|
||
{
|
||
return $has_user;
|
||
}else
|
||
{
|
||
return self::create($data);
|
||
}
|
||
}
|
||
//取得推广资金总余额
|
||
public static function getMoneyTotal($merchant_id,$status=''){
|
||
$where=[
|
||
'from_merchant_id'=>$merchant_id
|
||
];
|
||
if($status)
|
||
{
|
||
$where['status']=$status;
|
||
}
|
||
$money=self::where($where)->sum('money');
|
||
return $money;
|
||
}
|
||
|
||
//自动结算佣金
|
||
public static function autoStatus($limit=50){
|
||
$list=self::where('status',2)->limit($limit)->get();
|
||
//dump();
|
||
$arr_id=[];
|
||
$up_count=0;
|
||
|
||
if(count($list)>0)
|
||
{
|
||
|
||
Log::channel('auto_commission')->info('--------------'.$limit.'条----自动结算部分采集开始');
|
||
foreach($list as $k=>$v)
|
||
{
|
||
|
||
//插入资金变动
|
||
$money_log_r=MerchantMoneyLog::addCommissionMoney($v,$v['money']);
|
||
// dump($money_log_r);
|
||
//插入账号钱包推广增加
|
||
if($money_log_r)
|
||
{
|
||
Log::channel('auto_commission')->info('推广结算ID:'.$v->id.',商户ID:'.$v->from_merchant_id.'----插入资金变动:'.$money_log_r?'成功':'失败');
|
||
//钱包增加推广结算金额
|
||
//
|
||
$wallet_r=MerchantWallet::moneyIncr($v->from_merchant_id,$v['money'],'commission_money');
|
||
//增加金额
|
||
$wallet_r2=MerchantWallet::moneyIncr($v->from_merchant_id,$v['money'],'money');
|
||
if($wallet_r)
|
||
{
|
||
Log::channel('auto_commission')->info('推广结算ID:'.$v->id.',商户ID:'.$v->from_merchant_id.'----钱包推广变动:'.$wallet_r?'成功':'失败');
|
||
}
|
||
}
|
||
|
||
$arr_id[]=$v['id'];//ID
|
||
//插入资金变动数据
|
||
|
||
}
|
||
//更新数据
|
||
$up_count=self::whereIn('id',$arr_id)->update(['status'=>1]);
|
||
if($up_count==count($list))
|
||
{
|
||
Log::channel('auto_commission')->info('--------------'.$limit.'条----自动结算采集完成');
|
||
}else
|
||
{
|
||
Log::channel('auto_commission')->info('--------------'.$limit.'条----自动结算部分失败,失败条目是'.(count($list)-$up_count));
|
||
}
|
||
|
||
}
|
||
return $up_count;
|
||
}
|
||
}
|