<?php
|
|
namespace app\common\model\plus\region;
|
|
use app\common\enum\plus\region\RegionLevelEnum;
|
use app\common\enum\user\grade\ChangeTypeEnum;
|
use app\common\model\BaseModel;
|
use app\common\model\plus\region\GradeLog as GradeLogModel;
|
use app\common\model\settings\Region;
|
|
/**
|
* 股东用户模型
|
*/
|
class User extends BaseModel
|
{
|
protected $name = 'region_user';
|
protected $pk = 'user_id';
|
|
/**
|
* 追加字段
|
* @var string[]
|
*/
|
protected $append = ['region'];
|
|
/**
|
* 地区名称
|
* @param $value
|
* @param $data
|
* @return array
|
*/
|
public function getRegionAttr($value, $data)
|
{
|
return [
|
'province' => Region::getNameById($data['province_id']),
|
'city' => Region::getNameById($data['city_id']),
|
'area' => $data['area_id'] == 0 ? '' : Region::getNameById($data['area_id']),
|
];
|
}
|
|
/**
|
* 代理类别
|
* @param $value
|
* @return array
|
*/
|
public function getRegionLevelAttr($value)
|
{
|
return ['text' => RegionLevelEnum::data()[$value]['name'], 'value' => $value];
|
}
|
|
|
/**
|
* 关联会员等级表
|
*/
|
public function grade()
|
{
|
return $this->belongsTo('app\\common\\model\\plus\\region\\Grade', 'grade_id', 'grade_id');
|
}
|
|
/**
|
* 关联会员记录表
|
* @return \think\model\relation\BelongsTo
|
*/
|
public function user()
|
{
|
return $this->belongsTo('app\\common\\model\\user\\User');
|
}
|
|
/**
|
* 关联推荐人表
|
* @return \think\model\relation\BelongsTo
|
*/
|
public function referee()
|
{
|
return $this->belongsTo('app\\common\\model\\user\\User', 'referee_id', 'user_id');
|
}
|
|
/**
|
* 详情
|
*/
|
public static function detail($user_id, $with = ['user'])
|
{
|
return (new static())->with($with)->find($user_id);
|
}
|
|
/**
|
* 是否为股东
|
*/
|
public static function isregionUser($user_id)
|
{
|
$region = self::detail($user_id);
|
return !!$region && !$region['is_delete'];
|
}
|
|
/**
|
* 新增股东用户记录
|
* @param $user_id
|
* @param $data
|
* @return bool
|
*/
|
public static function add($user_id, $data)
|
{
|
$model = static::detail($user_id) ?: new static;
|
if(empty($model['grade_id'])) {
|
$data['grade_id'] = Grade::getDefaultGradeId($user_id);
|
}
|
$model->save(array_merge([
|
'user_id' => $user_id,
|
'is_delete' => 0,
|
'app_id' => $model::$app_id
|
], $data));
|
return true;
|
}
|
|
/**
|
* 发放股东佣金
|
* @param $user_id
|
* @param $money
|
* @return bool
|
*/
|
public static function grantMoney($user_id, $money, $bonus_type = 10)
|
{
|
$bonus_type_arr = ['10' => '代理分红', '20' => '级差奖', '30' => '平级奖'];
|
// 股东详情
|
$model = static::detail($user_id);
|
if (!$model || $model['is_delete']) {
|
return false;
|
}
|
// 累积股东可提现佣金
|
$model->where('user_id', '=', $user_id)->inc('money', $money)->update();
|
// 记录股东资金明细
|
Capital::add([
|
'user_id' => $user_id,
|
'flow_type' => 10,
|
'money' => $money,
|
'bonus_type' => $bonus_type,
|
'describe' => '代理分红结算-' . $bonus_type_arr[$bonus_type],
|
'app_id' => $model['app_id'],
|
]);
|
return true;
|
}
|
|
/**
|
* 批量设置股东等级
|
*/
|
public function upgradeGrade($user, $upgradeGrade)
|
{
|
// 更新会员等级的数据
|
$this->where('user_id', '=', $user['user_id'])
|
->update([
|
'grade_id' => $upgradeGrade['grade_id']
|
]);
|
(new GradeLogModel)->save([
|
'old_grade_id' => $user['grade_id'],
|
'new_grade_id' => $upgradeGrade['grade_id'],
|
'change_type' => ChangeTypeEnum::AUTO_UPGRADE,
|
'user_id' => $user['user_id'],
|
'app_id' => $user['app_id']
|
]);
|
return true;
|
}
|
|
/**
|
* 统计股东数量
|
*/
|
public static function regionCount($grade_id = 0)
|
{
|
$model = new static();
|
if($grade_id > 0) {
|
$model->where('grade_id', '=', $grade_id);
|
}
|
return $model->where('is_delete', '=', 0)
|
->count();
|
}
|
|
}
|