作者 何书鹏
1 个管道 的构建 失败 耗费 28 秒

用户归属组织功能开发

... ... @@ -106,16 +106,22 @@ class Study extends Backend
$this->request->get(["filter"=>json_encode($filter),'op'=>json_encode($op)]);
}
//所属组织
$organization_id = $this->getOrganizationId();
if($organization_id > 0){
$basic_where['user.organization_id'] = $organization_id;
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams(['third_exam.id','study.examname']);
$total = $this->model
->with(['user','study'])
->with(['user','study','organization'])
->where($where)
->where($basic_where)
->order($sort, $order)
->count();
$list = $this->model
->with(['user','study'])
->with(['user','study','organization'])
->where($where)
->where($basic_where)
->order($sort, $order)
... ... @@ -123,11 +129,6 @@ class Study extends Backend
->select();
foreach ($list as $k => &$v) {
$v['is_pass'] = $v['score'] >= $v['study']['pass_mark'] ? '1' : '2';
$v['studys'] = Db::name('third_study')
->alias('a')
->join('study b','b.id = a.study_id')
->where('a.third_id',$v['id'])
->column('b.examname');
}
$result = array("total" => $total, "rows" => $list);
... ...
... ... @@ -49,16 +49,26 @@ class ThirdStudy extends Backend
{
return $this->selectpage();
}
//所属组织
$basic_where = [];
$organization_id = $this->getOrganizationId();
if($organization_id > 0){
$basic_where['user.organization_id'] = $organization_id;
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams(['id','user.username','study.examname','user.work_address']);
$total = $this->model
->with(['user','study'])
->with(['user','study','organization'])
->where($where)
->where($basic_where)
->order($sort, $order)
->count();
$list = $this->model
->with(['user','study'])
->with(['user','study','organization'])
->where($where)
->where($basic_where)
->order($sort, $order)
->limit($offset, $limit)
->select();
... ...
... ... @@ -3,7 +3,10 @@
namespace app\admin\controller\auth;
use app\admin\model\AuthGroup;
use app\admin\model\ThirdExam;
use app\admin\model\ThirdStudy;
use app\common\controller\Backend;
use app\common\model\User;
use fast\Tree;
use think\Db;
use think\Exception;
... ... @@ -112,8 +115,21 @@ class Group extends Backend
$rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
$params['rules'] = implode(',', $rules);
if ($params) {
$this->model->create($params);
$this->success();
Db::startTrans();
try {
$result = $this->model->create($params);
//所属组织
$organization_id = $parentmodel->organization_id?:($params['is_organization']=='1'?$result->id:0);
$result->save([
'organization_id' => $organization_id,
'is_organization' => $organization_id ? '1' : '0',
]);
Db::commit();
$this->success();
}catch (Exception $e){
Db::rollback();
$this->error($e->getMessage());
}
}
$this->error();
}
... ... @@ -159,15 +175,36 @@ class Group extends Backend
// 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
$rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
$params['rules'] = implode(',', $rules);
//所属组织
$params['organization_id'] = $parentmodel->organization_id?:($params['is_organization']=='1'?$row->id:0);
$params['is_organization'] = $parentmodel->organization_id?'0':$params['is_organization'];
if ($params) {
Db::startTrans();
try {
//用户所属组织变更
$UserModel = new User;
$ThirdStudyModel = new ThirdStudy;
$ThirdExamModel = new ThirdExam;
if($row->organization_id && $params['organization_id'] && $params['organization_id'] != $row->organization_id){
$UserModel->where('organization_id',$row->organization_id)->update(['organization_id'=>$params['organization_id']]);
$ThirdStudyModel->where('organization_id',$row->organization_id)->update(['organization_id'=>$params['organization_id']]);
$ThirdExamModel->where('organization_id',$row->organization_id)->update(['organization_id'=>$params['organization_id']]);
}
$row->save($params);
$children_auth_groups = model("AuthGroup")->all(['id'=>['in',implode(',',(Tree::instance()->getChildrenIds($row->id)))]]);
$childparams = [];
foreach ($children_auth_groups as $key=>$children_auth_group) {
$childparams[$key]['id'] = $children_auth_group->id;
$childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
//下级所属组织
$childparams[$key]['organization_id'] = $params['organization_id'];
$childparams[$key]['is_organization'] = '0';
//用户所属组织变更
if($children_auth_group->organization_id && $params['organization_id'] && $params['organization_id'] != $children_auth_group->organization_id){
$UserModel->where('organization_id',$children_auth_group->organization_id)->update(['organization_id'=>$params['organization_id']]);
$ThirdStudyModel->where('organization_id',$children_auth_group->organization_id)->update(['organization_id'=>$params['organization_id']]);
$ThirdExamModel->where('organization_id',$children_auth_group->organization_id)->update(['organization_id'=>$params['organization_id']]);
}
}
model("AuthGroup")->saveAll($childparams);
Db::commit();
... ...
... ... @@ -67,15 +67,24 @@ class User extends Backend
$this->request->get(["filter"=>json_encode($filter),'op'=>json_encode($op)]);
}
//所属组织
$basic_where = [];
$organization_id = $this->getOrganizationId();
if($organization_id > 0){
$basic_where['user.organization_id'] = $organization_id;
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams('user.id,user.username,user.nickname,user.password,user.card,user.work_address,user.studynum');
$total = $this->model
->with('group')
->with(['organization'])
->where($where)
->where($basic_where)
->order($sort, $order)
->count();
$list = $this->model
->with('group')
->with(['organization'])
->where($where)
->where($basic_where)
->order($sort, $order)
->limit($offset, $limit)
->select();
... ... @@ -132,6 +141,8 @@ class User extends Backend
}
}
// 所属组织
$params['organization_id'] = $this->getOrganizationId() ?: 0;
$result = false;
Db::startTrans();
try {
... ... @@ -153,6 +164,7 @@ class User extends Backend
'third_id' => $third_id,
'study_id' => $study['study_id'],
'periodtime' => strtotime($study['periodtime']),
'organization_id' => $params['organization_id'], //所属组织
];
$classes_ids = Db::name('classes')->where('study_id',$study['study_id'])->where('is_major',1)->column('id');
foreach ($classes_ids as $class_id){
... ... @@ -259,6 +271,7 @@ class User extends Backend
'third_id' => $third_id,
'study_id' => $study['study_id'],
'periodtime' => strtotime($study['periodtime']),
'organization_id' => $row->organization_id, //所属组织
];
}else{
// 修改到期时间
... ... @@ -462,7 +475,7 @@ class User extends Backend
}
$row['examname'] = !empty($row['examname']) ? $row['examname'] : '';
if(!isset($user_study_exist[$row['username'].'-'.$row['examname']])){
if ($user = UserModel::where('username',$row['username'])->field('id')->find()) {
if ($user = UserModel::where('username',$row['username'])->field('id,organization_id')->find()) {
// 添加报考科目信息
$study = Db::name('study')->where('examname',$row['examname'])->field('id')->find();
$third_study = Db::name('third_study')->where('study_id',$study['id'])->where('third_id',$user['id'])->find();
... ... @@ -471,6 +484,7 @@ class User extends Backend
'third_id' => $user['id'],
'study_id' => $study['id'],
'periodtime' => intval(($row['periodtime'] - 25569) * 3600 * 24) - 28800, //转换成1970年以来的秒数
'organization_id' => $user['organization_id'], //所属组织
];
$classes_ids = Db::name('classes')->where('study_id',$study['id'])->where('is_major',1)->column('id');
foreach ($classes_ids as $class_id){
... ... @@ -492,6 +506,7 @@ class User extends Backend
if(!isset($user_exist[$row['username']])){
// 调整后的数据
unset($row['examname'],$row['periodtime']);
$row['organization_id'] = $this->getOrganizationId() ?: 0; //所属组织
$insert[] = $row;
$user_exist[$row['username']] = 1;
}
... ... @@ -532,20 +547,21 @@ class User extends Backend
}
// 添加报考科目
foreach ($insert_origin as $v){
$third_id = UserModel::where('username',$v['username'])->value('id');
$user = UserModel::where('username',$v['username'])->field('id,organization_id')->find();
// 添加报考科目信息
$study = Db::name('study')->where('examname',$v['examname'])->field('id')->find();
$third_study = Db::name('third_study')->where('study_id',$study['id'])->where('third_id',$third_id)->find();
$third_study = Db::name('third_study')->where('study_id',$study['id'])->where('third_id',$user['id'])->find();
if($study && !$third_study){
$third_study_data[] = [
'third_id' => $third_id,
'third_id' => $user['id'],
'study_id' => $study['id'],
'periodtime' => intval(($v['periodtime'] - 25569) * 3600 * 24) - 28800, //转换成1970年以来的秒数
'organization_id' => $user['organization_id'], //所属组织
];
$classes_ids = Db::name('classes')->where('study_id',$study['id'])->where('is_major',1)->column('id');
foreach ($classes_ids as $class_id){
$study_class_data[] = [
'third_id' => $third_id,
'third_id' => $user['id'],
'study_id' => $study['id'],
'class_id' => $class_id,
'playtime' => 0,
... ...
... ... @@ -18,4 +18,9 @@ class ThirdExam extends Model
{
return $this->belongsTo('Study','study_id','id',[],'LEFT')->setEagerlyType(0);
}
public function organization()
{
return $this->belongsTo('AuthGroup', 'organization_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
}
\ No newline at end of file
... ...
... ... @@ -55,4 +55,9 @@ class ThirdStudy extends Model
{
return $this->belongsTo('Study', 'study_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function organization()
{
return $this->belongsTo('AuthGroup', 'organization_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
}
... ...
... ... @@ -106,4 +106,8 @@ class User extends Model
return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function organization()
{
return $this->belongsTo('AuthGroup', 'organization_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
}
... ...
... ... @@ -14,6 +14,12 @@
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('是否组织')}:</label>
<div class="col-xs-12 col-sm-8">
{:build_radios('row[is_organization]', ['0'=>__('否'), '1'=>__('是')])}
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Permission')}:</label>
<div class="col-xs-12 col-sm-8">
<span class="text-muted"><input type="checkbox" name="" id="checkall" /> <label for="checkall"><small>{:__('Check all')}</small></label></span>
... ...
... ... @@ -14,6 +14,12 @@
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('是否组织')}:</label>
<div class="col-xs-12 col-sm-8">
{:build_radios('row[is_organization]', ['0'=>__('否'), '1'=>__('是')], $row['is_organization'])}
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Permission')}:</label>
<div class="col-xs-12 col-sm-8">
<span class="text-muted"><input type="checkbox" name="" id="checkall" /> <label for="checkall"><small>{:__('Check all')}</small></label></span>
... ...
... ... @@ -148,7 +148,7 @@ class Exam extends Api
$exam['exam_num'] = 1;
$exam['createtime'] = time();
$exam['updatetime'] = time();
$exam['organization_id'] = $this->auth->organization_id; //所属组织
$info['third_exam_id'] = Db::name('third_exam')->insertGetId($exam);
}else{
$exam_times = Db::name('third_exam')
... ... @@ -177,6 +177,7 @@ class Exam extends Api
$exam['exam_num'] = $third_exam['exam_num'] + 1;
$exam['createtime'] = time();
$exam['updatetime'] = time();
$exam['organization_id'] = $this->auth->organization_id; //所属组织
$info['third_exam_id'] =Db::name('third_exam')->insertGetId($exam);
}
... ...
... ... @@ -524,4 +524,15 @@ class Backend extends Controller
//刷新Token
$this->request->token();
}
/**
* 获取组织ID
*/
public function getOrganizationId(){
return db('auth_group_access')
->alias('aga')
->join('__' . strtoupper('auth_group') . '__ ag', 'aga.group_id = ag.id', 'LEFT')
->where("aga.uid='{$this->auth->id}' and ag.status='normal' and ag.organization_id>0")
->value('ag.organization_id');
}
}
... ...
... ... @@ -49,6 +49,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function (
{field: 'id', title: 'ID'},
{field: 'pid', title: __('Parent')},
{field: 'name', title: __('Name'), align: 'left'},
{field: 'is_organization', title: __('是否组织'), searchList: {'0' : '否','1' : '是'}, formatter: Table.api.formatter.normal},
{field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
{
field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
... ...
... ... @@ -113,6 +113,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
{checkbox: true},
{field: 'user.id', title: __('Id'), sortable: true},
// {field: 'group.name', title: __('Group')},
{field: 'organization.name', title: __('所属组织')},
{field: 'user.username', title: __('用户名'), operate:'LIKE'},
{field: 'user.nickname', title: __('真实姓名'), operate:'LIKE'},
{field: 'study.id', title: __('报考科目'), searchList: Config.studyList,formatter: Table.api.formatter.flag},
... ...
... ... @@ -25,6 +25,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'organization.name', title: __('所属组织')},
{field: 'user.username', title: __('Third.username')},
{field: 'user.work_address', title: __('Third.work_address')},
{field: 'study.examname', title: __('Study.examname')},
... ...
... ... @@ -46,6 +46,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
{checkbox: true},
{field: 'id', title: __('Id'), sortable: true},
// {field: 'group.name', title: __('Group')},
{field: 'organization.name', title: __('所属组织')},
{field: 'username', title: __('用户名'), operate:'LIKE'},
{field: 'nickname', title: __('真实姓名'), operate:'LIKE'},
{field: 'password', title: __('密码'), operate:false},
... ...