<?php
|
|
namespace app\common\model\branch;
|
|
use app\common\model\BaseModel;
|
use app\common\model\user\User as UserModel;
|
use app\common\model\branch\Branch as BranchModel;
|
use app\common\model\settings\Region;
|
|
/**
|
* 连盟成员模型
|
*/
|
class Member extends BaseModel
|
{
|
protected $name = 'branch_member';
|
protected $pk = 'user_id';
|
|
/**
|
* 追加字段
|
* @var string[]
|
*/
|
protected $append = ['region'];
|
|
/**
|
* 关联所属分会表
|
*/
|
public function branch()
|
{
|
return $this->belongsTo('app\\common\\model\\branch\\Branch', 'branch_id', 'branch_id');
|
}
|
|
/**
|
* 关联所属分会表
|
*/
|
public function supplier()
|
{
|
return $this->belongsTo('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id');
|
}
|
|
/**
|
* 关联会员记录表
|
* @return \think\model\relation\BelongsTo
|
*/
|
public function user()
|
{
|
return $this->belongsTo('app\\common\\model\\user\\User');
|
}
|
|
/**
|
* 关联职务
|
*/
|
public function position()
|
{
|
return $this->belongsTo('app\\common\\model\\branch\\Position', 'position_id', 'position_id');
|
}
|
|
/**
|
* 籍贯
|
* @param $value
|
* @param $data
|
* @return array
|
*/
|
public function getRegionAttr($value, $data)
|
{
|
if (!isset($data['province_id'])) {
|
return false;
|
}
|
return [
|
'province' => Region::getNameById($data['province_id']),
|
'city' => Region::getNameById($data['city_id']),
|
'region' => $data['region_id'] == 0 ? '' : Region::getNameById($data['region_id']),
|
];
|
}
|
|
/**
|
* 详情
|
*/
|
public static function detail($user_id, $with = ['user'])
|
{
|
return (new static())->with($with)->find($user_id);
|
}
|
|
/**
|
* 是否已经是分会成员
|
*/
|
public static function isMember($user_id)
|
{
|
$member = self::detail($user_id);
|
return !!$member && !$member['is_delete'];
|
}
|
|
/**
|
* 统计成员数量
|
*/
|
public static function memberCount($branch_id = 0)
|
{
|
$model = new static();
|
if($branch_id > 0) {
|
$model->where('branch_id', '=', $branch_id);
|
}
|
return $model->where('is_delete', '=', 0)
|
->count();
|
}
|
|
/**
|
* 新增记录
|
*/
|
public function add($data)
|
{
|
$data['app_id'] = self::$app_id;
|
$this->save($data);
|
// 更新分会人数
|
BranchModel::incTotal($data['branch_id']);
|
// 检查真实姓名和电话有没有更改
|
$user = UserModel::detail($data['user_id']);
|
$userData = [];
|
if ($user['real_name'] != $data['real_name']) {
|
$userData['real_name'] = $data['real_name'];
|
}
|
if ($user['mobile'] != $data['mobile']) {
|
$userData['mobile'] = $data['mobile'];
|
}
|
if ($userData) {
|
$user->save($userData);
|
}
|
return true;
|
}
|
|
/**
|
* 获取会员数据 (可指定某天)
|
*/
|
public function getMemberTotal($startDate = null, $endDate = null, $branch_id = 0)
|
{
|
$model = $this;
|
|
!is_null($startDate) && $model = $model->where('create_time', '>=', strtotime($startDate));
|
|
if (is_null($endDate)) {
|
!is_null($startDate) && $model = $model->where('create_time', '<', strtotime($startDate) + 86400);
|
} else {
|
$model = $model->where('create_time', '<', strtotime($endDate) + 86400);
|
}
|
|
if ($branch_id > 0) {
|
$model = $model->where('branch_id', '=', $branch_id);
|
}
|
|
$model = $model->where('is_delete', '=', 0);
|
return $model->count();
|
}
|
|
public function memberGroupNum($date, $branch_id)
|
{
|
$field = 'count(*) as total,from_unixtime(create_time,\'%m-%d\') as `day`';
|
if ($date == 'year') {
|
$field = 'count(*) as total,from_unixtime(create_time,\'%m\') as `day`';
|
}
|
$model = $this->field($field)
|
->where('is_delete', '=', 0)
|
->when($date, function ($query, $date) {
|
getModelTime($query, $date, 'create_time');
|
})->when($branch_id, function ($query, $branch_id) {
|
$query->where('branch_id', $branch_id);
|
});
|
return $model->order('create_time ASC')->group('day')->select();
|
}
|
|
}
|