You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
crmeb_php/app/common/repositories/system/admin/PartnerRepository.php

191 lines
6.3 KiB

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\common\repositories\system\admin;
//附件
use app\common\dao\system\admin\PartnerDao;
use app\common\model\store\CityArea;
use app\common\model\system\admin\Partner;
use app\common\repositories\BaseRepository;
use app\common\model\user\UserPartner;
use app\common\repositories\system\auth\RoleRepository;
use crmeb\exceptions\AuthException;
use crmeb\services\JwtTokenService;
use FormBuilder\Exception\FormBuilderException;
use FormBuilder\Factory\Elm;
use FormBuilder\Form;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;
use think\facade\Route;
use think\Model;
/**
* Class BaseRepository
* @package common\repositories
* @mixin AdminDao
*/
class PartnerRepository extends BaseRepository
{
public function __construct(PartnerDao $dao)
{
/**
* @var AdminDao
*/
$this->dao = $dao;
}
/**
* @param array $where
* @param $page
* @param $limit
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getList(array $where, $page, $limit)
{
$query = $this->dao->search($where);
$count = $query->count();
$list = $query->page($page, $limit)->select();
return compact('list', 'count');
}
public function create(array $data)
{
return Db::transaction(function () use ($data) {
//生成角色
$info = $this->dao->create($data);
$userPartner = [];
for ($i = 1; $i <= $data['max_role_num']; $i++) {
$name = $i == 1 ? $data['name'] : $data['name'] . '_' . $i;
$userPartner[] = [
'partner_name' => $name,
'partner_id' => $info->id,
];
}
UserPartner::getDB()->insertAll($userPartner);
});
}
/**
* @param int $id
* @return Form
* @throws DataNotFoundException
* @throws DbException
* @throws FormBuilderException
* @throws ModelNotFoundException
* @author xaboy
* @day 2020-04-09
*/
public function getDetail(int $id)
{
try {
$data = $this->dao->get($id)->toArray();
$data['province_id'] = '';
$data['province_name'] = '';
$data['city_id'] = '';
$data['city_name'] = '';
$data['district_id'] = '';
$data['district_name'] = '';
if ($data['is_area'] == 1 && $data['area_id'] > 0) {
$area_data = CityArea::getDB()->where('id', $data['area_id'])->find()->toArray();
$ids = explode("/", $area_data['path']);
if ($area_data['level'] == 1) {
$data['province_id'] = (int)$data['area_id'];
$name_data = CityArea::getDB()->where('id', $data['province_id'])->find()->toArray();
$data['province_name'] = $name_data['name'];
} elseif ($area_data['level'] == 2) {
$data['province_id'] = (int)$ids[1];
$name_data = CityArea::getDB()->where('id', $data['province_id'])->find()->toArray();
$data['province_name'] = $name_data['name'];
$data['city_id'] = (int)$data['area_id'];
$name_data1 = CityArea::getDB()->where('id', $data['city_id'])->find()->toArray();
$data['city_name'] = $name_data1['name'];
} elseif ($area_data['level'] == 3) {
$data['province_id'] = (int)$ids[1];
$name_data = CityArea::getDB()->where('id', $data['province_id'])->find()->toArray();
$data['province_name'] = $name_data['name'];
$data['city_id'] = (int)$ids[2];
$name_data1 = CityArea::getDB()->where('id', $data['city_id'])->find()->toArray();
$data['city_name'] = $name_data1['name'];
$data['district_id'] = (int)$data['area_id'];
$name_data2 = CityArea::getDB()->where('id', $data['district_id'])->find()->toArray();
$data['district_name'] = $name_data2['name'];
}
}
return $data;
} catch (\Exception $e) {
print_r($e->getFile() . '-' . $e->getLine() . "-" . $e->getMessage());
}
}
/**
*
* 更新
* @param int $id id
* @param array $data 数组
* @return int
* @throws DbException
* @author 张先生
* @date 2020-03-26
*/
public function update(int $id, array $data)
{
$old = $this->dao->get($id)->toArray();
if ($old['name'] != $data['name']) {
$list = UserPartner::getDB()->where('partner_id', $id)->select();
foreach ($list as $k => $v) {
$name = str_replace($old['name'], $data['name'], $v['partner_name']);
UserPartner::getDB()->where('id', $v['id'])->update(['partner_name' => $name]);
}
}
return $this->dao->update($id, $data);
}
//删除
public function delete(int $id)
{
UserPartner::getDB()->where('partner_id', $id)->delete();
return $this->dao->delete($id);
}
//检测角色绑定用户
public function existsUser(int $id)
{
return $this->dao->existsUser($id);
}
public function roleExists(int $id)
{
return UserPartner::getDB()->where('id', '=', $id)->where('uid', '>', 0)->find();
}
}