Files
liche/market/controllers/api/institution/OrganizationUser.php
T
2022-12-22 14:10:39 +08:00

307 lines
12 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . 'controllers/api/BaseController.php';
/**
* Notes:私域直播_机构用户管理
* Created on: 2022/9/19 17:15
* Created by: dengbw
*/
class OrganizationUser extends BaseController
{
public function __construct()
{
parent::__construct();
$this->load->model('market/Market_sylive_user_model', 'mdSyliveUser');
$this->load->model('market/Market_sylive_organization_model', 'mdSyliveOrganization');
}
/**
* Notes:用户管理列表
* Created on: 2022/9/20 14:48
* Created by: dengbw
*/
public function page_get()
{
$page = $this->input_param('page');
$limit = $this->input_param('limit');
$uname = $this->input_param('uname');
$nickname = $this->input_param('nickname');
$sort = $this->input_param('sort');
$order = $this->input_param('order');
$organizationId = intval($this->input_param('organizationId'));
!$page && $page = 1;
!$limit && $limit = 10;
$sort_order = 'userId desc';
if ($sort && $order) {
$sort_order = $sort . ' ' . $order;
}
$list = [];
$where['status>='] = 0;
$organizationId && $where['organizationId'] = $organizationId;
$uname && $where['uname'] = $uname;
$nickname && $where['nickname'] = $nickname;
$this->load->model('market/Market_sylive_organization_model', 'mdSyliveOrganization');
$organizationType = 0;
$re_org = $this->mdSyliveOrganization->get(['organizationId' => $organizationId]);
$re_org && $organizationType = $re_org['organizationType'];
$roleName = $this->mdSyliveUser->roleAry($organizationType);
$count = $this->mdSyliveUser->count($where);
if ($count) {
$res = $this->mdSyliveUser->select($where, $sort_order, $page, $limit);
foreach ($res as $v) {
$status = intval($v['status']);
$createTime = $v['createTime'] != '0000-00-00 00:00:00' ? $v['createTime'] : '';
$list[] = [
'userId' => $v['userId'], 'uname' => $v['uname'], 'nickname' => $v['nickname'], 'mobile' => $v['mobile']
, 'roleName' => $roleName, 'organizationId' => $organizationId, 'status' => $status, 'createTime' => $createTime];
}
}
$date = ['list' => $list, 'count' => $count];
$this->return_response_list($date);
}
/**
* Notes:获取机构用户
* Created on: 2022/11/29 11:08
* Created by: dengbw
*/
public function list_post()
{
$this->load->model('market/Market_sylive_groups_user_model', 'mdSyliveGroupsUser');
$this->load->model('market/Market_sylive_groups_model', 'mdSyliveGroups');
$activityId = intval($this->input_param('activityId'));
$organizationIds = $this->input_param('organizationIds');
$keyword = trim($this->input_param('keyword'));
$where = ['status>=' => 0, 'organizationId>' => 0];
if ($keyword) {
$this->load->model('market/Market_sylive_activity_model', 'mdSyliveActivity');
$where["(uname LIKE '%{$keyword}%' OR mobile LIKE '%{$keyword}%')"] = null;
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re['organizationId']) {//找当前顶级机构用户
$where["topOrgId"] = $re['organizationId'];
}
} else if ($organizationIds) {
$organizationIds = implode(',', $organizationIds);
$where["organizationId in({$organizationIds}) or bizId in({$organizationIds})"] = null;
}
$res = $this->mdSyliveUser->select($where, 'userId desc', 0, 0, 'userId,uname,mobile,organizationId,bizId');
if ($keyword && $res) {
$organizationIds = [];
foreach ($res as $v) {
$v['bizId'] && $organizationIds[] = $v['bizId'];
$v['organizationId'] && $organizationIds[] = $v['organizationId'];
}
$organizationIds && $organizationIds = implode(',', $organizationIds);
}
$map_organization = [];
if ($organizationIds) {
$map_organization = $this->mdSyliveOrganization->map('organizationId', 'organizationName,organizationType'
, ["organizationId in({$organizationIds})" => null]);
}
$list = [];
foreach ($res as $v) {
$uname = $v['uname'] ? $v['uname'] : '-';
$organization = $map_organization[$v['bizId']];
if (!$organization) {
$organization = $map_organization[$v['organizationId']];
}
$organizationName = $organization['organizationName'] ? $organization['organizationName'] : '-';
$type = 2;
if ($v['bizId']) {
$type = $v['bizId'] == $v['organizationId'] ? 1 : 0;
}
$groups = 0;
$re_gro_use = $this->mdSyliveGroupsUser->get(['activityId' => $activityId, 'userId' => $v['userId']
, 'status>=' => 0], 'groupsId,bizId');
if ($re_gro_use) {
$groups = 1;
$groupsId = $re_gro_use['bizId'] ? $re_gro_use['bizId'] : $re_gro_use['groupsId'];
$re_gro = $this->mdSyliveGroups->get(['activityId' => $activityId, 'groupsId' => $groupsId
, 'status>=' => 0], 'groupsName');
$re_gro['groupsName'] && $organizationName .= "(分组-{$re_gro['groupsName']})";
}
$list[] = ['userId' => $v['userId'], 'uname' => $uname, 'mobile' => $v['mobile'], 'type' => $type
, 'groups' => $groups, 'organizationName' => $organizationName];
}
$this->return_response_list($list);
}
/**
* Notes:添加用户
* Created on: 2022/9/21 16:46
* Created by: dengbw
*/
public function index_post()
{
$mobile = $this->input_param('mobile');
$organizationId = intval($this->input_param('organizationId'));
$uname = $this->input_param('uname');
if (!$mobile) {
$this->return_json('请输入手机号');
}
if (!$organizationId) {
$this->return_json('请选择所属机构');
}
if (!$uname) {
$this->return_json('请输入姓名');
}
$topOrgId = $this->getTopOrgId($organizationId);
$re = $this->mdSyliveUser->get(['mobile' => $mobile, 'topOrgId' => $topOrgId]);
if ($re && $re['status'] != -1) {
if ($re['teamId'] || $re['organizationId']) {
$this->return_json('手机号已存在当前机构');
}
}
$bizId = 0;
$re_org = $this->mdSyliveOrganization->get(['organizationId' => $organizationId]);
if ($re_org) {
if ($re_org['organizationType'] == 4 && $re_org['parentId']) {
$bizId = $re_org['parentId'];
} else if ($re_org['organizationType'] == 3) {
$bizId = $re_org['organizationId'];
}
}
$addDate = ['topOrgId' => $topOrgId, 'organizationId' => $organizationId, 'uname' => $uname,
'bizId' => $bizId, 'status' => 0, 'teamId' => 0];
if ($re['userId']) {
$this->mdSyliveUser->update($addDate, ['userId' => $re['userId']]);
$this->return_response([], '绑定用户成功');
} else {
$addDate['mobile'] = $mobile;
$addDate['createTime'] = date('Y-m-d H:i:s');
$id = $this->mdSyliveUser->add($addDate);
if (!$id) {
$this->return_json('添加用户失败');
}
$this->return_response();
}
}
/**
* Notes:修改用户
* Created on: 2022/9/21 14:48
* Created by: dengbw
*/
public function index_put()
{
$userId = intval($this->input_param('userId'));
$organizationId = intval($this->input_param('organizationId'));
$uname = $this->input_param('uname');
if (!$userId) {
$this->return_json('参数错误');
}
if (!$organizationId) {
$this->return_json('请选择所属机构');
}
if (!$uname) {
$this->return_json('请输入姓名');
}
$re = $this->mdSyliveUser->get(['userId' => $userId]);
if (!$re) {
$this->return_json('无此用户');
}
$topOrgId = $re['topOrgId'];
if ($re['organizationId'] != $organizationId) {
$topOrgId = $this->getTopOrgId($organizationId);
if ($re['topOrgId'] && $re['topOrgId'] != $topOrgId) {
$this->return_json('不可修改顶级机构');
}
}
$bizId = 0;
$re_org = $this->mdSyliveOrganization->get(['organizationId' => $organizationId]);
if ($re_org) {
if ($re_org['organizationType'] == 4 && $re_org['parentId']) {
$bizId = $re_org['parentId'];
} else if ($re_org['organizationType'] == 3) {
$bizId = $re_org['organizationId'];
}
}
$upDate = ['topOrgId' => $topOrgId, 'organizationId' => $organizationId, 'bizId' => $bizId, 'teamId' => 0, 'uname' => $uname];
$this->mdSyliveUser->update($upDate, ['userId' => $userId]);
$this->return_response();
}
/**
* Notes:修改状态
* Created on: 2022/9/21 16:10
* Created by: dengbw
*/
public function status_put()
{
$userId = $this->input_param('userId');
$status = $this->input_param('status');
if (!$userId) {
$this->return_json('参数错误');
}
$this->mdSyliveUser->update(['status' => $status], ['userId' => $userId]);
$this->return_response();
}
/**
* Notes:删除用户
* Created on: 2022/9/21 16:10
* Created by: dengbw
* @param null $userId
*/
public function index_delete($userId = null)
{
if (!$userId) {
$this->return_json('参数错误');
}
$this->mdSyliveUser->update(['status' => -1], ['userId' => $userId]);
$this->return_response();
}
/**
* Notes:栓验字段
* Created on: 2022/9/21 15:52
* Created by: dengbw
*/
public function existence_get()
{
$field = $this->input_param('field');
$value = $this->input_param('value');
$organizationId = intval($this->input_param('organizationId'));
if ($organizationId) {
$topOrgId = $this->getTopOrgId($organizationId);
$where = [$field => $value, 'topOrgId' => $topOrgId, 'status>=' => 0];
$re = $this->mdSyliveUser->get($where);
if ($re) {
if ($field == 'mobile') {
if ($re['teamId'] || $re['organizationId']) {
$this->return_json('已存在', 0);
}
} else {
$this->return_json('已存在', 0);
}
}
}
$this->return_json('不存在', 1);
}
/**
* Notes:获取顶级机构id
* Created on: 2022/12/8 14:39
* Created by: dengbw
* @param $organizationId
* @param $topOrgId
* @return mixed
*/
private function getTopOrgId($organizationId, $topOrgId = 0)
{
$re = $this->mdSyliveOrganization->get(['organizationId' => $organizationId], 'organizationId,parentId');
if (!$re) {
return $topOrgId;
} else {
$topOrgId = $re['organizationId'];
if ($re['parentId']) {
return $this->getTopOrgId($re['parentId'], $topOrgId);
} else {
return $topOrgId;
}
}
}
}