<?php
|
|
namespace app\common\model\branch;
|
|
use app\common\model\BaseModel;
|
use app\shop\model\auth\User as AuthUserModel;
|
use app\shop\model\auth\UserRole as UserRoleModel;
|
use app\common\model\shop\Role as RoleModel;
|
use app\common\enum\branch\BranchTypeEnum;
|
use app\common\model\settings\Region;
|
use app\common\model\branch\PointsLog as PointsLogModel;
|
|
/**
|
* 商家分会模型
|
*/
|
class Branch extends BaseModel
|
{
|
protected $name = 'branch';
|
protected $pk = 'branch_id';
|
|
/**
|
* 追加字段
|
* @var string[]
|
*/
|
protected $append = ['region', 'parent_branch'];
|
|
/**
|
* 关联应用表
|
*/
|
public function app()
|
{
|
return $this->belongsTo('app\\common\\model\\app\\App', 'app_id', 'app_id');
|
}
|
|
/**
|
* 关联logo
|
*/
|
public function logo()
|
{
|
return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'logo_id');
|
}
|
|
/**
|
* 关联超管
|
*/
|
public function superUser()
|
{
|
return $this->hasOne('app\\common\\model\\branch\\User', 'branch_id', 'branch_id')
|
->where('is_super','=', 1);
|
}
|
|
/**
|
* 地区名称
|
* @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']),
|
];
|
}
|
|
/**
|
* 所属连盟名称
|
* @param $value
|
* @param $data
|
* @return array
|
*/
|
public function getParentBranchAttr($value, $data)
|
{
|
if (!empty($data['parent_branch_id'])) {
|
return Branch::getBranchNameById($data['parent_branch_id']);
|
}
|
return '';
|
}
|
|
/**
|
* 级别
|
* @param $value
|
* @return array
|
*/
|
public function getBranchTypeAttr($value)
|
{
|
return ['text' => BranchTypeEnum::data()[$value]['name'], 'value' => $value];
|
}
|
|
/**
|
* 详情
|
*/
|
public static function detail($branch_id, $with = [])
|
{
|
return (new static())->with($with)->find($branch_id);
|
}
|
|
/**
|
* 更新分会人数
|
*/
|
public static function incTotal($branch_id, $inc = 1)
|
{
|
$res = self::detail($branch_id);
|
if ($res) {
|
$total = $res['member_num'] + $inc;
|
$res->save(['member_num' => $total]);
|
}
|
}
|
|
/**
|
* 累积分会的可用积分
|
*/
|
public function setIncPoints($points, $describe, $decPoints = 0)
|
{
|
// 新增积分变动明细
|
PointsLogModel::add([
|
'branch_id' => $this['branch_id'],
|
'value' => $points,
|
'describe' => $describe,
|
'app_id' => $this['app_id'],
|
]);
|
|
// 更新用户可用积分
|
$data['points'] = ($this['points'] + $points + $decPoints <= 0) ? 0 : $this['points'] + $points + $decPoints;
|
$this->where('branch_id', '=', $this['branch_id'])->update($data);
|
|
return true;
|
}
|
|
/**
|
* 累积分会结算金额 (批量)
|
*/
|
public function onBatchIncBranchMoney($data)
|
{
|
foreach ($data as $branchId => $branchMoney) {
|
$this->where(['branch_id' => $branchId])
|
->inc('total_money', $branchMoney)
|
->inc('money', $branchMoney)
|
->update();
|
}
|
return true;
|
}
|
|
//获取名称获取准确的分会
|
public static function getBranchIdByName($name)
|
{
|
$model = new static();
|
$branch = $model->where('name', '=', $name)
|
->where('is_delete', '=', 0)
|
->find();
|
return empty($branch) ? '-1' : $branch["branch_id"];
|
}
|
|
// 根据id获取名称
|
public static function getBranchNameById($branch_id)
|
{
|
$model = new static();
|
$name = $model->where('branch_id', '=', $branch_id)
|
->where('is_delete', '=', 0)
|
->value('name');
|
return $name;
|
}
|
|
/**
|
* 分会独立收款是否开启
|
*/
|
public static function getIndependentOpen($branch_id)
|
{
|
$model = new static();
|
$data = $model->where('branch_id', '=', $branch_id)
|
->where('is_delete', '=', 0)
|
->find();
|
return empty($data['is_independent']) ? 0 : $data['is_independent'];
|
}
|
|
/**
|
* 验证分会名称是否重复
|
*/
|
public static function checkExist($name)
|
{
|
return !!static::withoutGlobalScope()
|
->where('name', '=', $name)
|
->value('branch_id');
|
}
|
|
/**
|
* 获取分会总数
|
*/
|
public function getBranchTotal($branch_id = null) {
|
$model = $this;
|
if ($branch_id) {
|
$model = $model->where('parent_branch_id', '=', $branch_id);
|
}
|
return $model->where('is_delete', '=', 0)->count();
|
}
|
|
}
|