332 lines
14 KiB
PHP
332 lines
14 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
require_once APPPATH . 'controllers/api/BaseController.php';
|
|
|
|
/**
|
|
* Notes:私域直播_分组管理
|
|
* Created on: 2022/11/24 17:15
|
|
* Created by: dengbw
|
|
*/
|
|
class Groups extends BaseController
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->load->model('market/Market_sylive_groups_model', 'mdSyliveGroups');
|
|
$this->load->model('market/Market_sylive_groups_user_model', 'mdSyliveGroupsUser');
|
|
$this->load->model('market/Market_sylive_user_model', 'mdSyliveUser');
|
|
}
|
|
|
|
/**
|
|
* Notes:获取分组
|
|
* Created on: 2022/9/16 11:11
|
|
* Created by: dengbw
|
|
*/
|
|
public function index_get()
|
|
{
|
|
$activityId = intval($this->input_param('activityId'));
|
|
$unGroupsType = $this->input_param('unGroupsType');
|
|
$where['status>='] = 0;
|
|
$activityId && $where['activityId'] = $activityId;
|
|
$unGroupsType && $where['groupsLevel<>'] = $unGroupsType;
|
|
$sort_order = 'sortNumber asc,groupsId desc';
|
|
$fileds = 'activityId,groupsId,parentId,sortNumber,userFrom,groupsName,ifBiz,statisticsType,originalBizId';
|
|
$res = $this->mdSyliveGroups->select($where, $sort_order, 0, 0, $fileds);
|
|
foreach ($res as $k => $v) {
|
|
$res[$k]['activityId'] = intval($v['activityId']);
|
|
$res[$k]['groupsId'] = intval($v['groupsId']);
|
|
$res[$k]['userFrom'] = intval($v['userFrom']);
|
|
$res[$k]['parentId'] = intval($v['parentId']);
|
|
$res[$k]['sortNumber'] = intval($v['sortNumber']);
|
|
$res[$k]['ifBiz'] = intval($v['ifBiz']);
|
|
$res[$k]['statisticsType'] = $v['statisticsType'] ? intval($v['statisticsType']) : '';
|
|
$res[$k]['originalBizId'] = $v['originalBizId'] ? intval($v['originalBizId']) : '';
|
|
}
|
|
$this->return_response_list($res);
|
|
}
|
|
|
|
/**
|
|
* Notes:查找上级分组
|
|
* Created on: 2022/10/24 15:24
|
|
* Created by: dengbw
|
|
*/
|
|
public function parent_get()
|
|
{
|
|
$parentId = intval($this->input_param('parentId'));
|
|
$sort_order = 'sortNumber asc,groupsId desc';
|
|
$where['status>='] = 0;
|
|
$where['parentId'] = $parentId;
|
|
$res = $this->mdSyliveGroups->select($where, $sort_order, 0, 0, 'groupsId,groupsName');
|
|
$this->return_response_list($res);
|
|
}
|
|
|
|
/**
|
|
* Notes:添加分组
|
|
* Created on: 2022/9/19 16:43
|
|
* Created by: dengbw
|
|
*/
|
|
public function index_post()
|
|
{
|
|
$activityId = intval($this->input_param('activityId'));
|
|
$parentId = intval($this->input_param('parentId'));
|
|
$groupsName = $this->input_param('groupsName');
|
|
$userFrom = intval($this->input_param('userFrom'));
|
|
$sortNumber = intval($this->input_param('sortNumber'));
|
|
$ifBiz = intval($this->input_param('ifBiz'));
|
|
$statisticsType = intval($this->input_param('statisticsType'));
|
|
$originalBizId = intval($this->input_param('originalBizId'));
|
|
if (!$groupsName) {
|
|
$this->return_json('请输入分组名称');
|
|
}
|
|
$groupsLevel = 0;
|
|
if ($parentId) {//判断上级分组
|
|
$re_org = $this->mdSyliveGroups->get(['groupsId' => $parentId]);
|
|
if ($re_org['ifBiz']) {
|
|
$userFrom = $re_org['userFrom'] == 0 ? '门店' : '团队';
|
|
$this->return_json("选择的上级分组{$userFrom}不能保存,请重新选择");
|
|
}
|
|
$re_org['groupsLevel'] == 3 && $ifBiz = 1;//如果上级等级3级,当前分组设为门店
|
|
!$ifBiz && $groupsLevel = $re_org['groupsLevel'] + 1;
|
|
} else {
|
|
$re_org = $this->mdSyliveGroups->get(['parentId' => $parentId]);
|
|
if ($re_org) {
|
|
$this->return_json('请选择上级分组');
|
|
}
|
|
}
|
|
$originalBizId = $ifBiz ? $originalBizId : 0;
|
|
$addDate = ['activityId' => $activityId, 'parentId' => $parentId, 'originalBizId' => $originalBizId, 'groupsName' => $groupsName
|
|
, 'groupsLevel' => $groupsLevel, 'statisticsType' => $statisticsType, 'userFrom' => $userFrom, 'ifBiz' => $ifBiz
|
|
, 'sortNumber' => $sortNumber, 'createTime' => date('Y-m-d H:i:s')];
|
|
$groupsId = $this->mdSyliveGroups->add($addDate);
|
|
if (!$groupsId) {
|
|
$this->return_json('添加分组失败');
|
|
}
|
|
if ($originalBizId) {
|
|
$levelAry = $this->getLevelAry($groupsId);
|
|
$res = $this->mdSyliveUser->select(['bizId' => $originalBizId, 'status>=' => 0], 'userId asc', 0, 0
|
|
, 'userId,organizationId,bizId');
|
|
$addUser = [];
|
|
foreach ($res as $v) {
|
|
$re_user = $this->mdSyliveGroupsUser->get(['activityId' => $activityId, 'userId' => $v['userId'], 'status>=' => 0]);
|
|
if (!$re_user) {
|
|
$type = $v['bizId'] == $v['organizationId'] ? 1 : 0;
|
|
$value = ['userId' => $v['userId'], 'type' => $type, 'activityId' => $activityId
|
|
, 'groupsId' => $groupsId, 'userFrom' => $userFrom, 'createTime' => date('Y-m-d H:i:s')];
|
|
$data = array_merge($value, $levelAry);
|
|
$addUser[] = $data;
|
|
}
|
|
}
|
|
$addUser && $this->mdSyliveGroupsUser->add_batch($addUser);
|
|
}
|
|
$this->return_response();
|
|
}
|
|
|
|
/**
|
|
* Notes:修改分组
|
|
* Created on: 2022/9/19 17:29
|
|
* Created by: dengbw
|
|
*/
|
|
public function index_put()
|
|
{
|
|
$groupsId = intval($this->input_param('groupsId'));
|
|
$parentId = intval($this->input_param('parentId'));
|
|
$groupsName = $this->input_param('groupsName');
|
|
$userFrom = intval($this->input_param('userFrom'));
|
|
$sortNumber = intval($this->input_param('sortNumber'));
|
|
$ifBiz = intval($this->input_param('ifBiz'));
|
|
$statisticsType = intval($this->input_param('statisticsType'));
|
|
$originalBizId = intval($this->input_param('originalBizId'));
|
|
if (!$groupsId) {
|
|
$this->return_json('参数错误');
|
|
}
|
|
if (!$groupsName) {
|
|
$this->return_json('请输入分组名称');
|
|
}
|
|
$re_gro = $this->mdSyliveGroups->get(['groupsId' => $groupsId]);
|
|
$activityId = intval($re_gro['activityId']);
|
|
if ($re_gro && $re_gro['parentId'] != $parentId) {
|
|
$re_user = $this->mdSyliveGroupsUser->get(['groupsId' => $groupsId, 'status>=' => 0, 'activityId' => $activityId]);
|
|
if ($re_user) {
|
|
$this->return_json('此分组已有用户,不能修改上级分组');
|
|
}
|
|
}
|
|
$groupsLevel = 0;
|
|
if ($parentId) {//判断上级分组
|
|
$re_org = $this->mdSyliveGroups->get(['groupsId' => $parentId]);
|
|
if ($re_org['ifBiz']) {
|
|
$userFrom = $re_org['userFrom'] == 0 ? '门店' : '团队';
|
|
$this->return_json("选择的上级分组{$userFrom}不能保存,请重新选择");
|
|
}
|
|
$re_org['groupsLevel'] == 3 && $ifBiz = 1;//如果上级等级3级,当前分组设为门店
|
|
!$ifBiz && $groupsLevel = $re_org['groupsLevel'] + 1;
|
|
} else {
|
|
$re_org = $this->mdSyliveGroups->get(['parentId' => $parentId, 'activityId' => $activityId]);
|
|
if ($re_org && $groupsId != $re_org['groupsId']) {
|
|
$this->return_json('请选择上级分组');
|
|
}
|
|
}
|
|
$originalBizId = $ifBiz ? $originalBizId : 0;
|
|
$upDate = ['parentId' => $parentId, 'originalBizId' => $originalBizId, 'groupsName' => $groupsName, 'groupsLevel' => $groupsLevel,
|
|
'statisticsType' => $statisticsType, 'userFrom' => $userFrom, 'ifBiz' => $ifBiz, 'sortNumber' => $sortNumber];
|
|
$this->mdSyliveGroups->update($upDate, ['groupsId' => $groupsId]);
|
|
if (!$re_gro['originalBizId'] && $originalBizId) {//首次绑定同步用户
|
|
$levelAry = $this->getLevelAry($groupsId);
|
|
$res = $this->mdSyliveUser->select(['bizId' => $originalBizId, 'status>=' => 0], 'userId asc', 0, 0
|
|
, 'userId,organizationId,bizId');
|
|
$addUser = [];
|
|
foreach ($res as $v) {
|
|
$re_user = $this->mdSyliveGroupsUser->get(['activityId' => $activityId, 'userId' => $v['userId'], 'status>=' => 0]);
|
|
if (!$re_user) {
|
|
$type = $v['bizId'] == $v['organizationId'] ? 1 : 0;
|
|
$value = ['userId' => $v['userId'], 'type' => $type, 'activityId' => $activityId
|
|
, 'groupsId' => $groupsId, 'userFrom' => $userFrom, 'createTime' => date('Y-m-d H:i:s')];
|
|
$data = array_merge($value, $levelAry);
|
|
$addUser[] = $data;
|
|
}
|
|
}
|
|
$addUser && $this->mdSyliveGroupsUser->add_batch($addUser);
|
|
}
|
|
$this->return_response();
|
|
}
|
|
|
|
/**
|
|
* Notes:删除分组
|
|
* Created on: 2022/9/19 11:08
|
|
* Created by: dengbw
|
|
*/
|
|
public function index_delete()
|
|
{
|
|
$groupsId = intval($this->input_param('id'));
|
|
if (!$groupsId) {
|
|
$this->return_json('参数错误');
|
|
}
|
|
$ret = $this->mdSyliveGroups->update(['status' => -1], ['groupsId' => $groupsId]);
|
|
if ($ret) {//删除分组用户
|
|
$this->mdSyliveGroupsUser->update(['status' => -1], ['groupsId' => $groupsId]);
|
|
}
|
|
$this->return_response();
|
|
}
|
|
|
|
/**
|
|
* Notes:导出分组所有顾问
|
|
* Created on: 2023/5/6 14:56
|
|
* Created by: dengbw
|
|
*/
|
|
public function consultant_export_get()
|
|
{
|
|
$activityId = $this->inputs['activityId'];
|
|
if (!$activityId) {
|
|
$this->return_json('参数错误');
|
|
}
|
|
$where = ["activityId" => $activityId, "status" => 0];
|
|
$count = $this->mdSyliveGroupsUser->count($where);
|
|
if ($count > 10000) {
|
|
$this->return_json('导出失败,每次导出不能超出10000条数据');
|
|
}
|
|
if ($count == 0) {
|
|
$this->return_json('导出失败,无数据');
|
|
}
|
|
$levels = [];
|
|
$re_gro = $this->mdSyliveGroups->get(['activityId' => $activityId, 'parentId' => 0, 'status>=' => 0]);
|
|
if ($re_gro['statisticsType']) {
|
|
$this->load->model('market/Market_sys_dictionary_data_model', 'mdSysDictionaryData');
|
|
$res_dit = $this->mdSysDictionaryData->select(['status>=' => 0, 'dictId' => $re_gro['statisticsType']], 'sortNumber asc,dictDataId desc'
|
|
, 0, 0, 'dictDataCode,dictDataName');
|
|
foreach ($res_dit as $k => $v) {
|
|
$groupsLevel = intval($v['dictDataCode']);
|
|
$re_gro = $this->mdSyliveGroups->get(['activityId' => $activityId, 'groupsLevel' => $groupsLevel, 'status>=' => 0]);
|
|
if ($re_gro) {
|
|
$levels[] = ['label' => $v['dictDataName'], 'prop' => "levelId{$groupsLevel}"];
|
|
}
|
|
}
|
|
}
|
|
$res = $this->mdSyliveGroupsUser->select($where, "levelId1 asc,bizId asc,groupsUserId asc", 0, 0
|
|
, 'userId,type,userFrom,levelId1,levelId2,levelId3,bizId');
|
|
$list = $userIds = $groupsIds = [];
|
|
foreach ($res as $v) {
|
|
if ($v['levelId1'] && !in_array($v['levelId1'], $groupsIds)) {
|
|
$groupsIds[] = $v['levelId1'];
|
|
}
|
|
if ($v['levelId2'] && !in_array($v['levelId2'], $groupsIds)) {
|
|
$groupsIds[] = $v['levelId2'];
|
|
}
|
|
if ($v['levelId3'] && !in_array($v['levelId3'], $groupsIds)) {
|
|
$groupsIds[] = $v['levelId3'];
|
|
}
|
|
if ($v['bizId'] && !in_array($v['bizId'], $groupsIds)) {
|
|
$groupsIds[] = $v['bizId'];
|
|
}
|
|
if ($v['userId'] && !in_array($v['userId'], $userIds)) {
|
|
$userIds[] = $v['userId'];
|
|
}
|
|
}
|
|
$userIds = $userIds ? implode(",", $userIds) : -1;
|
|
$map_user = $this->mdSyliveUser->map('userId', 'uname,nickname,mobile', ["userId in({$userIds})" => null]);
|
|
$groupsIds = $groupsIds ? implode(',', $groupsIds) : -1;
|
|
$map_groups = $this->mdSyliveGroups->map('groupsId', 'groupsName', ["groupsId in({$groupsIds})" => null, 'activityId' => $activityId]);
|
|
foreach ($res as $v) {
|
|
$uname = $nickname = $mobile = '';
|
|
$user = $map_user[$v['userId']];
|
|
if ($user) {
|
|
$uname = $user['uname'];
|
|
$nickname = $user['nickname'];
|
|
$mobile = $user['mobile'];
|
|
}
|
|
if ($v['type'] == 0) {
|
|
$roleName = '销售';
|
|
} else if ($v['type'] == 1) {
|
|
$roleName = $v['userFrom'] == 0 ? '店长' : '团长';
|
|
} else {
|
|
$roleName = '管理员';
|
|
}
|
|
$list1 = ['uname' => $uname, 'nickname' => $nickname, 'mobile' => $mobile, 'roleName' => $roleName];
|
|
$list2 = [];
|
|
foreach ($levels as $k2 => $v2) {
|
|
$levelId = $v[$v2['prop']];
|
|
$list2[$v2['prop']] = $map_groups[$levelId] ? $map_groups[$levelId] : '';
|
|
}
|
|
$item = count($list2) ? array_merge($list1, $list2) : $list1;
|
|
$item['stores'] = $map_groups[$v['bizId']] ? $map_groups[$v['bizId']] : '';
|
|
$list[] = $item;
|
|
}
|
|
$columns1 = ['姓名', '微信昵称', '手机号', '角色'];
|
|
$columns2 = [];
|
|
foreach ($levels as $k => $v) {
|
|
$columns2[] = $v['label'];
|
|
}
|
|
$columns3 = count($columns2) ? array_merge($columns1, $columns2) : $columns1;
|
|
$columns = array_merge($columns3, ['门店']);
|
|
$this->return_response_list(['list' => $list, 'columns' => $columns]);
|
|
}
|
|
|
|
/**
|
|
* Notes:获取分组等级ID
|
|
* Created on: 2022/11/30 14:30
|
|
* Created by: dengbw
|
|
* @param $groupsId
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
private function getLevelAry($groupsId, $data = [])
|
|
{
|
|
$re = $this->mdSyliveGroups->get(['groupsId' => $groupsId], 'groupsId,parentId,groupsLevel,ifBiz');
|
|
if (!$re) {
|
|
return $data;
|
|
} else {
|
|
if ($re['groupsLevel']) {//分类id
|
|
$levelId = "levelId" . $re['groupsLevel'];
|
|
$data[$levelId] = $re['groupsId'];
|
|
}
|
|
if ($re['ifBiz']) {//门店id
|
|
$data['bizId'] = $re['groupsId'];
|
|
}
|
|
if ($re['parentId']) {
|
|
return $this->getLevelAry($re['parentId'], $data);
|
|
} else {
|
|
return $data;
|
|
}
|
|
}
|
|
}
|
|
|
|
} |