155 lines
4.5 KiB
PHP
155 lines
4.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models\Server;
|
|
|
|
use App\Http\Response\ResponseJson;
|
|
use App\Models\ProfileCourtship;
|
|
use App\Models\User;
|
|
use App\Models\Wechat;
|
|
use App\Models\CrmUser;
|
|
use App\Traits\CanPreview;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Traits\CanReward;
|
|
use App\Traits\Canlike;
|
|
use App\Traits\Canlog;
|
|
use App\Models\Server\TouristOrder;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class MerchantUser extends Model
|
|
{
|
|
use CanReward;
|
|
use Canlike;
|
|
use Canlog;
|
|
use CanPreview;
|
|
use ResponseJson;
|
|
|
|
protected $guarded = [];
|
|
public const USERINFO = ['pic', 'nickname', 'mobile', 'sex'];
|
|
public const PROFIILEINFO = ['birthday', 'province', 'city','state', 'belief','stature', 'weight','resident_province', 'resident_city', 'degree', 'industry', 'industry_sub', 'income','introduction', 'interest_hobby'];
|
|
/**
|
|
* 小程序用户
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function mpUser()
|
|
{
|
|
return $this->hasOne(User::class, 'id', 'user_id');
|
|
}
|
|
|
|
public function profile(){
|
|
return $this->hasOne(ProfileCourtship::class, 'user_id', 'user_id');
|
|
}
|
|
|
|
public function singleUser(){
|
|
return $this->hasMany(IdentityAuthorization::class, 'm_user_id', 'id');
|
|
}
|
|
|
|
public function orders() {
|
|
return $this->hasMany(TouristOrder::class, 'account_id', 'id');
|
|
}
|
|
|
|
public function crmUser() {
|
|
return $this->hasMany(CrmUser::class, 'user_id', 'user_id');
|
|
}
|
|
|
|
public function addEarning($m_id,$type, $amount, $order_id)
|
|
{
|
|
try{
|
|
\DB::beginTransaction();
|
|
//获取账号
|
|
$account = $this->getMEarningAccount($m_id);
|
|
//增加收益记录
|
|
switch($type){
|
|
case 'transfer':
|
|
$earning = MEarning::create([
|
|
'm_id'=>$m_id,
|
|
'm_user_id'=>$this->id,
|
|
'm_order_id'=>$order_id,
|
|
'type'=>'transfer',
|
|
'sharer'=>null,
|
|
'ratio'=>1,
|
|
'sub_ratio'=>1,
|
|
'value'=>$amount,
|
|
'status'=>'finished',
|
|
]);
|
|
break;
|
|
default:
|
|
\DB::rollback();
|
|
return false;
|
|
}
|
|
//修改余额
|
|
$account->increment('balance', $amount);
|
|
$account->increment('total_value', $amount);
|
|
\DB::commit();
|
|
return $earning;
|
|
}catch(\Exception $e){
|
|
\DB::rollback();
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getMEarningAccount($m_id)
|
|
{
|
|
$m_user_id = $this->id;
|
|
$account = MEarningAccount::firstOrCreate(['m_id'=> $m_id, 'm_user_id'=>$m_user_id]);
|
|
return $account;
|
|
}
|
|
public function answers(){
|
|
return $this->hasMany(ReportAnswer::class,'m_user_id','id')->orderBy('question_id','asc');
|
|
}
|
|
|
|
//当前用户是否vip
|
|
public function isVip(){
|
|
$is_vip = 0;
|
|
if($this->unionid){
|
|
$wechat = Wechat::where('unionid',$this->unionid)->first();
|
|
if($wechat && $wechat->user_id){
|
|
$user = User::find($wechat->user_id);
|
|
if($user && $user->rank_id > 0){
|
|
$is_vip = 1;
|
|
return $is_vip;
|
|
}
|
|
}
|
|
}
|
|
if($this->user_id){
|
|
$user = User::find($this->user_id);
|
|
if($user && $user->rank_id > 0) {
|
|
$is_vip = 1;
|
|
return $is_vip;
|
|
}
|
|
}
|
|
return $is_vip;
|
|
}
|
|
|
|
public function mUserProfile()
|
|
{
|
|
return $this->hasOne(MerchantUserProfile::class, 'user_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* 除福恋平台外的其他平台用户资料完善情况
|
|
*/
|
|
public function isCompletedProfile()
|
|
{
|
|
$result = true;
|
|
foreach (self::PROFIILEINFO as $info) {
|
|
if (empty($this->mUserProfile->$info)) {
|
|
Log::info("用户:".$this->id.", 未完善资料 $info");
|
|
$result = false;
|
|
break;
|
|
}
|
|
}
|
|
unset($this->mUserProfile);
|
|
if (empty($result)) return $result;
|
|
foreach (self::USERINFO as $info) {
|
|
if (empty($this->$info)) {
|
|
Log::info("用户:".$this->id.", 未完善资料 $info");
|
|
$result = false;
|
|
break;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
}
|