<?php
|
|
namespace app\api\model\branch;
|
|
use app\common\exception\BaseException;
|
use app\common\model\branch\Branch as BranchModel;
|
use app\common\model\branch\User as BranchUserModel;
|
|
/**
|
* 分会模型
|
*/
|
class Branch extends BranchModel
|
{
|
/**
|
* 隐藏字段
|
* @var array
|
*/
|
protected $hidden = [
|
'is_delete',
|
'app_id',
|
'update_time'
|
];
|
|
/**
|
* 获取列表数据
|
*/
|
public function getList($params, $branch_id)
|
{
|
$model = $this;
|
if (isset($params['keyword']) && $params['keyword']) {
|
$model = $model->where('name', 'like', '%' . $params['keyword'] . '%');
|
}
|
if(isset($params['branch_ids'])&&$params['branch_ids']){
|
$model = $model->where('branch_id', 'in', $params['branch_ids']);
|
}
|
if(isset($params['is_takeout']) && $params['is_takeout'] > -1){
|
$model = $model->where('is_takeout', '=', $params['is_takeout']);
|
}
|
// 查询列表数据
|
return $model->with(['superUser'])
|
->where('parent_branch_id', '=', $branch_id)
|
->where('is_delete', '=', '0')
|
->order(['create_time' => 'desc'])
|
->paginate($params);
|
}
|
|
/**
|
* 添加
|
*/
|
public function add($data, $branch_id)
|
{
|
// 开启事务
|
$this->startTrans();
|
try {
|
if (BranchModel::checkExist($data['name'])) {
|
$this->error = '分会名称已存在,请更换';
|
return false;
|
}
|
if (BranchUserModel::checkExist($data['user_name'])) {
|
$this->error = '登录账号已存在,请更换';
|
return false;
|
}
|
if (BranchUserModel::isManager($data['user_id'])) {
|
$this->error = '绑定的用户已经是管理员,请更换';
|
return false;
|
}
|
// 添加分会
|
$data['app_id'] = self::$app_id;
|
$data['parent_branch_id'] = $branch_id;
|
$this->save($data);
|
// 添加登录用户
|
$user_model = new BranchUserModel();
|
$user_model->save([
|
'user_id' => $data['user_id'],
|
'user_name' => $data['user_name'],
|
'password' => salt_hash($data['password']),
|
'real_name' => $data['link_name'],
|
'branch_id' => $this['branch_id'],
|
'is_super' => 1,
|
'app_id' => self::$app_id,
|
]);
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
/**
|
* 修改
|
*/
|
public function edit($data)
|
{
|
// 开启事务
|
$this->startTrans();
|
try {
|
if ($this['superUser'] && $data['user_name'] != $this['superUser']['user_name'] && BranchUserModel::checkExist($data['user_name'])) {
|
$this->error = '登录账号已存在';
|
return false;
|
}
|
|
$user = BranchUserModel::detail(['user_id' => $data['user_id']]);
|
if ($user && $user['branch_id'] != $this['branch_id']) {
|
$this->error = '绑定的用户已经是管理员,请更换';
|
return false;
|
}
|
|
// 修改分会
|
$this->save([
|
'name' => $data['name'],
|
'link_name' => $data['link_name'],
|
'link_phone' => $data['link_phone'],
|
'province_id' => $data['province_id'],
|
'city_id' => $data['city_id'],
|
'region_id' => $data['region_id'],
|
'user_id' => $data['user_id'],
|
]);
|
|
// 修改登录用户
|
$user_model = $this['superUser'];
|
$user_data = [
|
'user_id' => $data['user_id'],
|
'user_name' => $data['user_name']
|
];
|
if (isset($data['password']) && !empty($data['password'])) {
|
$user_data['password'] = salt_hash($data['password']);
|
}
|
$user_model->save($user_data);
|
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
/**
|
* 软删除
|
*/
|
public function setDelete()
|
{
|
return $this->save(['is_delete' => 1]);
|
}
|
|
}
|