300 lines
8.3 KiB
PHP
300 lines
8.3 KiB
PHP
<?php
|
||
|
||
|
||
namespace Yunshop\TeamDividend\models;
|
||
|
||
|
||
use app\backend\models\BackendModel;
|
||
use app\common\models\member\MemberChildren;
|
||
use app\common\models\MemberShopInfo;
|
||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Validation\Rule;
|
||
use Yunshop\TeamDividend\observers\DealerObserver;
|
||
|
||
class TeamDividendAgencyModel extends BackendModel
|
||
{
|
||
use SoftDeletes;
|
||
public $table = 'yz_team_dividend_agency';
|
||
|
||
public $guarded = [''];
|
||
|
||
protected $attributes = [
|
||
'subordinate_num' => 0
|
||
];
|
||
|
||
public static function boot()
|
||
{
|
||
parent::boot();
|
||
self::observe(new DealerObserver());
|
||
}
|
||
|
||
public function rules()
|
||
{
|
||
$rules = [
|
||
'uniacid' => 'required',
|
||
'uid' => [
|
||
Rule::unique($this->table)->where('uniacid', \YunShop::app()->uniacid)->whereNull('deleted_at')->ignore($this->id),
|
||
'required'
|
||
],
|
||
'level' => 'required',
|
||
'parent_id' => 'required',
|
||
// 'relation' => 'required',
|
||
];
|
||
return $rules;
|
||
}
|
||
|
||
public function atributeNames()
|
||
{
|
||
return [
|
||
'uniacid' => '公众号ID',
|
||
'uid' => '会员ID',
|
||
'level' => '经销商等级',
|
||
'parent_id' => '会员上级ID',
|
||
'relation' => '会员关系链'
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
*/
|
||
public function toParent()
|
||
{
|
||
return $this->belongsTo('app\common\models\Member', 'parent_id', 'uid');
|
||
}
|
||
|
||
|
||
public function hasOneMemberParent()
|
||
{
|
||
return $this->hasOne(\app\common\models\member\MemberParent::class, 'member_id', 'uid');
|
||
}
|
||
|
||
/**
|
||
* 等级1:1关系
|
||
*
|
||
* @return mixed
|
||
*/
|
||
public function hasOneLevel()
|
||
{
|
||
return $this->hasOne('Yunshop\TeamDividend\models\TeamDividendLevelModel', 'id', 'level');
|
||
}
|
||
|
||
/**
|
||
* 会员1:1关系
|
||
*
|
||
* @return mixed
|
||
*/
|
||
public function hasOneMember()
|
||
{
|
||
return $this->hasOne('app\common\models\Member', 'uid', 'uid');
|
||
}
|
||
|
||
public function hasManyTeamDividend()
|
||
{
|
||
return $this->hasMany(TeamDividendModel::class, 'member_id', 'uid');
|
||
}
|
||
|
||
/*
|
||
* 上下级 1:n 关系
|
||
*
|
||
* */
|
||
public function hasManyChildren()
|
||
{
|
||
return $this->hasMany(MemberChildren::class, 'member_id', 'uid');
|
||
}
|
||
|
||
public static function searchAgency($parame)
|
||
{
|
||
$result = self::uniacid()->select(['yz_team_dividend_agency.*'])
|
||
->join('yz_member', 'yz_team_dividend_agency.uid', '=', 'yz_member.member_id')
|
||
->whereNull('yz_member.deleted_at');
|
||
|
||
if (!empty($parame['realname'])) {
|
||
$result = $result->whereHas('hasOneMember', function ($query) use ($parame) {
|
||
$query->where('nickname', 'like', "%{$parame['realname']}%")
|
||
->orWhere('realname', 'like', "%{$parame['realname']}%")
|
||
->orWhere('mobile', 'like', "%{$parame['realname']}%");
|
||
});
|
||
}
|
||
|
||
if (!empty($parame['level_id'])) {
|
||
$result = $result->where('level', $parame['level_id']);
|
||
}
|
||
|
||
if (!empty($parame['member_id'])) {
|
||
$result = $result->whereHas('hasOneMember', function ($query) use ($parame) {
|
||
$query->where('uid', $parame['member_id']);
|
||
});
|
||
}
|
||
|
||
if (isset($parame['searchtime']) && $parame['searchtime'] == 2) {
|
||
if ($parame['times']['start'] != '请选择' && $parame['times']['end'] != '请选择') {
|
||
$range = [strtotime($parame['times']['start']), strtotime($parame['times']['end'])];
|
||
|
||
$result = $result->whereBetween('yz_team_dividend_agency.created_at', $range);
|
||
}
|
||
}
|
||
|
||
$result = $result->with(
|
||
['hasOneLevel' => function($query){
|
||
return $query->select(['*']);
|
||
},'hasOneMember' => function($query){
|
||
return $query->select(['*']);
|
||
},'toParent' => function($query){
|
||
return $query->select('uid', 'realname', 'mobile');
|
||
},'hasManyTeamDividend' => function($query){
|
||
return $query->selectRaw('member_id, status, sum(dividend_amount) as total_amount')->groupBy('member_id','status');
|
||
}])->orderBy('id', 'desc');
|
||
|
||
return $result;
|
||
}
|
||
|
||
|
||
public static function getAgencyInfoByUid($uid)
|
||
{
|
||
return self::uniacid()
|
||
->with('hasOneLevel')
|
||
->where('uid', $uid)
|
||
->first();
|
||
}
|
||
|
||
public static function updatedAgentById($data, $id)
|
||
{
|
||
self::uniacid()
|
||
->where('id', $id)
|
||
->update($data);
|
||
|
||
}
|
||
|
||
public static function getAgentByUidId($membeId)
|
||
{
|
||
return self::uniacid()
|
||
->where('uid',$membeId);
|
||
}
|
||
|
||
public static function getFirstAgencyNum($uid)
|
||
{
|
||
return self::uniacid()
|
||
->where('parent_id', $uid)
|
||
->count(DB::raw("distinct(uid)"));
|
||
}
|
||
|
||
public static function getThridAgencyNum($uid)
|
||
{
|
||
return self::uniacid()
|
||
->whereRaw('FIND_IN_SET(?,relation)' , [$uid])
|
||
->count();
|
||
}
|
||
|
||
public static function getFirstAgency($uid)
|
||
{
|
||
return self::uniacid()
|
||
->where('parent_id', $uid)
|
||
->get();
|
||
}
|
||
|
||
/**
|
||
* 统计下级团队人数
|
||
*
|
||
* @return mixed
|
||
*/
|
||
public static function getChildNumByPrantId()
|
||
{
|
||
return self::selectRaw('parent_id, count(parent_id) as total')
|
||
->uniacid()
|
||
->groupBy('parent_id')
|
||
->get();
|
||
}
|
||
|
||
public static function daletedAgency($id)
|
||
{
|
||
return self::where('id', $id)
|
||
->delete();
|
||
}
|
||
public static function getLevelNumAgency($pid, $level)
|
||
{
|
||
if (!$uids = MemberShopInfo::uniacid()->where('parent_id',$pid)->pluck('member_id')->toArray()){
|
||
return 0;
|
||
}
|
||
return self::uniacid()->whereIn('uid',$uids)->where('level', $level)->count(DB::raw("distinct(uid)"));
|
||
// return self::uniacid()
|
||
// ->where('parent_id', $pid)
|
||
// ->where('level', $level)
|
||
// ->count();
|
||
}
|
||
public static function getuid($id)
|
||
{
|
||
return self::select('uid')
|
||
->where('id', $id)
|
||
->first();
|
||
}
|
||
|
||
public static function getMemberAutoWithdrawByUid($uid)
|
||
{
|
||
$set = \Setting::get('plugin.team_dividend');
|
||
|
||
if ($set['withdraw_type']) {
|
||
return self::uniacid()
|
||
->where('uid', $uid)
|
||
->whereHas('hasOneLevel', function ($q) {
|
||
$q->where('auto_withdraw_period','>', 0);
|
||
})
|
||
->with('hasOneLevel')
|
||
->first();
|
||
}
|
||
return self::uniacid()
|
||
->where('uid', $uid)
|
||
->whereHas('hasOneLevel', function ($q) {
|
||
$q->where('auto_withdraw_day' ,'>', 0);
|
||
})
|
||
->with('hasOneLevel')
|
||
->first();
|
||
|
||
}
|
||
public static function getAgencyByMemberId($memberID)
|
||
{
|
||
return self::uniacid()
|
||
->join('yz_team_dividend_level', 'yz_team_dividend_agency.level', '=', 'yz_team_dividend_level.id')
|
||
->where('uid', $memberID);
|
||
}
|
||
|
||
public function agencyLevel()
|
||
{
|
||
return $this->belongsTo('Yunshop\TeamDividend\models\TeamDividendLevelModel', 'level', 'id');
|
||
}
|
||
|
||
public static function getAgentByMemberId($memberID)
|
||
{
|
||
return self::uniacid()
|
||
->with('agencyLevel')
|
||
->where('uid', $memberID);
|
||
}
|
||
|
||
/**
|
||
* @param $agent
|
||
* @param $is_black
|
||
* @param int $type 0-后台/其他,1-爱心值加速池清零设置操作
|
||
* @return bool|void
|
||
* 设置/取消黑名单
|
||
*/
|
||
public function setBlack($agent, $is_black, $type = 0)
|
||
{
|
||
$is_black = $is_black ? 1 : 0;
|
||
if ($agent->is_black == $is_black) {
|
||
return true;
|
||
}
|
||
$agent->is_black = $is_black;
|
||
$agent->black_time = time();
|
||
$agent->save();
|
||
TeamDividendBlackLog::create([
|
||
'uniacid' => \YunShop::app()->uniacid,
|
||
'agent_id' => $agent->id,
|
||
'uid' => $agent->uid,
|
||
'is_black' => $is_black,
|
||
'type' => $type
|
||
]);
|
||
return true;
|
||
}
|
||
|
||
} |