Files
liche/admin/controllers/app/licheb/Member.php
T
2022-04-08 15:36:44 +08:00

445 lines
16 KiB
PHP
Executable File

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Member extends HD_Controller
{
private $groups = [1 => '销售', 2 => '店长', 3 => '老板', 4 => '渠道经理'];
public function __construct()
{
parent::__construct();
$this->load->model('app/licheb/App_licheb_users_model', 'userM');
$this->load->model('app/licheb/App_licheb_channel_biz_model', 'mdChannelBiz');
$this->load->model("biz/biz_model");
}
//首页信息
public function index()
{
$this->lists();
}
//数据列表
public function lists($where = array())
{
$params = $this->input->get();
$page = $params['page'];
$size = $params['size'];
!$page && $page = 1;
!$size && $size = 20;
if ($params['name']) {
$where['uname like "%' . $params['name'] . '%"'] = null;
$this->data['name'] = $params['name'];
}
if ($params['mobile']) {
$where['mobile'] = $params['mobile'];
$this->data['mobile'] = $params['mobile'];
}
if ($params['group_id']) {
$where['group_id'] = $params['group_id'];
$this->data['group_id'] = $params['group_id'];
}
if ($params['biz_id']) {
$where['biz_id'] = $params['biz_id'];
} else {
$params['city_id'] = '';
$params['county_id'] = '';
$params['biz_id'] = '';
}
$count = $this->userM->count($where);
$lists = $this->userM->select($where, 'id desc', $page, $size);
foreach ($lists as $key => $val) {
$biz_name = '';
if ($val['group_id'] == 4) {
$biz_name = ' <a href="javascript:void(0)" data-open="/app/licheb/channel/lists_biz?uid=' . $val['id'] . '">门店列表</a>';
} else if ($val['biz_id']) {
$re_biz = $this->biz_model->get(['id' => $val['biz_id'], 'status' => 1], 'biz_name');
$re_biz && $biz_name = $re_biz['biz_name'];
}
$lists[$key]['biz_name'] = $biz_name;
$lists[$key]['group_name'] = $this->groups[$val['group_id']];
if (SUPER_ADMIN == $this->role) {//超级管理员才允许操作用户的披上超级马甲
$majia = array();
if ($val['jsondata']) {
$json = json_decode($val['jsondata'], true);
$majia = $json['majia'] ? $json['majia'] : array();
}
$lists[$key]['umajia'] = $majia;
}
}
$this->data['app_id'] = 2;
$this->data['params'] = $params;
$this->data['lists'] = $lists;
$this->data['groupAry'] = $this->groups;
$this->data['_title'] = '用户列表';
$this->data['pager'] = array('count' => ceil($count / $size), 'curr' => $page, 'totle' => $count);
return $this->show_view('/app/licheb/member/lists', true);
}
//展示单条数据
public function get()
{
$this->load->model("sys/sys_city_model", 'mdSysCity');
$id = $this->input->get('id');
$row = $this->userM->get(['id' => $id]);
$selectedBrands = [];
if ($id) {
$info = array(
'id' => $row['id'],
'uname' => $row['uname'],
'mobile' => $row['mobile'],
'group_id' => $row['group_id'],
'biz_id' => $row['biz_id'],
'city_id' => $row['city_id'],
);
if ($row['group_id'] == 4) {
$this->load->model('app/licheb/App_licheb_channel_biz_model', 'mdChannelBiz');
$res_biz = $this->mdChannelBiz->select(['uid' => $id], 'id desc', 0, 0, 'biz_id');
$res_biz && $biz_id_arr = array_unique(array_column($res_biz, 'biz_id'));
if ($res_biz) {
$biz_id_arr = array_unique(array_column($res_biz, 'biz_id'));
$ids = implode(',', $biz_id_arr);
$selectedBrands = $this->biz_model->select(["id in ($ids)" => null, 'status' => 1], '', 0, 0, 'id as biz_id,biz_name as name');
}
} else if ($row['biz_id']) {
$re_biz = $this->biz_model->get(["id" => $row['biz_id'], 'status' => 1], 'id ,biz_name');
if ($re_biz) {
$selectedBrands[] = ['biz_id' => $re_biz['id'], 'name' => $re_biz['biz_name']];
}
}
$action = '/app/licheb/member/edit';
$_title = '编辑用户';
} else {
$info = array('biz_id' => '', 'group_id' => '', 'city_id' => 0);
$action = '/app/licheb/member/add';
$_title = '添加用户';
}
$show_info['cityList'] = $this->mdSysCity->select(['status' => 1], 'id desc', 0, 0, 'city_id,name');
$this->data['selectedBrands'] = $selectedBrands;
$this->data['show_info'] = $show_info;
$this->data['info'] = $info;
$this->data['groups'] = $this->groups;
$this->data['action'] = $action;
$this->data['_title'] = $_title;
return $this->show_view('app/licheb/member/get', true);
}
//添加单条数据
public function add()
{
$info = $this->input->post('info');
$uname = $info['uname'];
$mobile = $info['mobile'];
$group_id = $info['group_id'];
$bizs = $info['bizs'];
$city_id = $info['city_id'];
$biz_id_arr = array_column($bizs, 'biz_id');
if (!mobile_valid($mobile)) {
return $this->show_json(SYS_CODE_FAIL, '输入正确手机号');
}
if (!$uname) {
return $this->show_json(SYS_CODE_FAIL, '请输入姓名');
}
$exist = $this->userM->get(array('mobile' => $mobile));
if ($exist) {
return $this->show_json(SYS_CODE_FAIL, '手机号的用户存在');
}
$add = array(
'mobile' => $mobile,
'uname' => $uname,
'city_id' => $city_id,
'status' => 1,
'c_time' => time(),
);
$group_id && $add['group_id'] = $group_id;
$biz_id_arr && $add['biz_id'] = implode(',', $biz_id_arr);
$id = $this->userM->add($add);
if ($id && $group_id == 4 && $biz_id_arr) {//加狸车宝渠道门店
$this->channel_biz(["uid" => $id, 'biz_id_arr' => $biz_id_arr]);
}
return $this->show_json(SYS_CODE_SUCCESS, '操作成功', '/app/licheb/member');
}
//编辑单条数据
public function edit()
{
$info = $this->input->post('info');
$id = $info['id'];
$uname = $info['uname'];
$nickname = $info['nickname'];
$mobile = $info['mobile'];
$group_id = $info['group_id'];
$bizs = $info['bizs'];
$city_id = $info['city_id'];
$biz_id_arr = is_array($bizs) ? array_column($bizs, 'biz_id') : '';
if (!mobile_valid($mobile)) {
return $this->show_json(SYS_CODE_FAIL, '输入正确手机号');
}
$exist = $this->userM->get(array('mobile' => $mobile, 'id <>' => $id));
if ($exist) {
return $this->show_json(SYS_CODE_FAIL, '手机号的用户存在');
}
$upd = array(
'mobile' => $mobile,
'city_id' => $city_id,
);
$upd['group_id'] = $group_id ? $group_id : 0;
$upd['biz_id'] = $biz_id_arr ? implode(',', $biz_id_arr) : 0;
$nickname && $upd['nickname'] = $nickname;
$uname && $upd['uname'] = $uname;
$this->userM->update($upd, array('id' => $id));
if ($id && $group_id == 4) {//加狸车宝渠道门店
$this->channel_biz(["uid" => $id, 'biz_id_arr' => $biz_id_arr, 'edit' => 1]);
}
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
//删除单条数据
public function del()
{
}
//批量操作(默认修改状态)
public function batch()
{
$field = $this->input->post('field');
if ($field === 'ifcheck') {
$status = intval($this->input->post('value'));
$ids = $this->input->post('id');
$ids = explode(',', $ids);
if (!$ids || count($ids) < 1) {
$this->show_json(0, '请选择要操作的选项');
}
$where = array('id in(' . implode(',', $ids) . ')' => null);
} else {
$id = intval($this->input->post('id'));
$status = intval($this->input->post('value'));
if (!$id) {
$this->show_json(0, '参数错误');
}
$where['id'] = $id;
}
$data['status'] = $status;
$this->userM->update($data, $where);
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
//导出数据列表
public function export()
{
}
function json_lists()
{
$biz_id = $this->input->post('biz_id');
$status = $this->input->post('status');
$page = $this->input->post('page');
$size = $this->input->post('size');
$type = intval($this->input->post('type'));
$if_mobile = intval($this->input->post('if_mobile'));
$where = array();
if (strlen($status) > 0) {
$where['status'] = $status;
} else {
$whre['status > -1'] = null;
}
$biz_id && $where['biz_id'] = $biz_id;
$total = $this->userM->count($where);
$lists = $uids = array();
if ($total) {
$select = $if_mobile ? 'id,uname,mobile' : 'id,uname';
$rows = $this->userM->select($where, 'id desc', $page, $size, $select);
foreach ($rows as $v) {
$uids[$v['id']] = $v['id'];
$title = $if_mobile ? $v['uname'] . "({$v['mobile']})" : $v['uname'];
$lists[] = array(
'id' => $v['id'],
'title' => $title,
);
}
}
if ($type == 1) {//查找渠道经理
$where2 = ['status' => 1, 'group_id' => 4];
$where2["id in(select uid from lc_app_licheb_channel_biz where biz_id = {$biz_id})"] = null;
$total2 = $this->userM->count($where2);
if ($total2) {
$rows2 = $this->userM->select($where2, 'id desc', $page, $size, 'id,uname,mobile');
foreach ($rows2 as $v) {
if (!$uids[$v['id']]) {
$title = $if_mobile ? $v['uname'] . "({$v['mobile']}渠道)" : $v['uname'] . '(渠道)';
$lists[] = array(
'id' => $v['id'],
'title' => $title,
);
}
}
}
}
$this->data = array('total' => $total, 'list' => $lists, 'list2' => $rows2);
return $this->show_json(SYS_CODE_SUCCESS);
}
/**
* Notes:加狸车宝渠道门店
* Created on: 2021/11/12 14:54
* Created by: dengbw
* @param array $json
*/
function channel_biz($json = [])
{
$biz_ids = $json['biz_id_arr'] ? implode(',', $json['biz_id_arr']) : 0;
if ($json['edit'] == 1) {//编辑删除未添加的
$where_d['uid'] = $json['uid'];
$biz_ids && $where_d["biz_id not in({$biz_ids})"] = null;
$this->mdChannelBiz->delete($where_d);
}
if (!$biz_ids) {
return;
}
$res_b = $this->biz_model->select(["id in({$biz_ids})" => null], '', 0, 0, 'id as biz_id');
foreach ($res_b as $key => $value) {
$where = ["uid" => $json['uid'], 'biz_id' => $value['biz_id']];
$re_cb = $this->mdChannelBiz->get($where);
if (!$re_cb) {
$where['c_time'] = time();
$this->mdChannelBiz->add($where);
}
}
}
/**
* Notes:获取门店渠道经理
* Created on: 2021/11/15 11:31
* Created by: dengbw
* @return bool
*/
function get_channel_uid()
{
$biz_id = $this->input->get('biz_id');
if (!$biz_id) {
return $this->show_json(SYS_CODE_FAIL, '参数错误');
}
$res_b = $this->mdChannelBiz->select(["biz_id" => $biz_id], '', 0, 0, 'uid');
$this->data = $res_b ? array_column($res_b, 'uid') : [];
return $this->show_json(SYS_CODE_SUCCESS);
}
/**
* Notes:保存渠道管理员
* Created on: 2021/11/15 15:54
* Created by: dengbw
* @return bool
*/
function edit_channel_uid()
{
$biz_id = $this->input->post('biz_id');
$channel_uid = $this->input->post('channel_uid');
if (!$biz_id) {
return $this->show_json(SYS_CODE_FAIL, '参数错误');
}
$re_biz = $this->biz_model->get(['id' => $biz_id]);
if (!$re_biz) {
return $this->show_json(SYS_CODE_FAIL, '店铺不存在!');
}
$where_del['biz_id'] = $biz_id;
if ($channel_uid) {
$channel_uid_str = implode(',', $channel_uid);
$where_del["uid not in({$channel_uid_str})"] = null;
}
$res_b = $this->mdChannelBiz->select($where_del, '', 0, 0, 'uid'); //临时
$this->mdChannelBiz->delete($where_del);
//临时
foreach ($res_b as $key => $value) {
$res_b2 = $this->mdChannelBiz->select(['uid' => $value['uid']], '', 0, 0, 'biz_id');
$biz_id_up = $res_b2 ? array_unique(array_column($res_b2, 'biz_id')) : 0;
$biz_id_up && $biz_id_up = implode(',', $biz_id_up);
$this->userM->update(['biz_id' => $biz_id_up], ['id' => $value['uid']]);
}
//临时
if ($channel_uid) {
foreach ($channel_uid as $key => $value) {
$uid = $value;
$where = ["uid" => $uid, 'biz_id' => $biz_id];
$re_cb = $this->mdChannelBiz->get($where);
if (!$re_cb) {
$where['c_time'] = time();
$this->mdChannelBiz->add($where);
//临时
$res_b2 = $this->mdChannelBiz->select(['uid' => $uid], '', 0, 0, 'biz_id');
$biz_id_up = $res_b2 ? array_unique(array_column($res_b2, 'biz_id')) : 0;
$biz_id_up && $biz_id_up = implode(',', $biz_id_up);
$this->userM->update(['biz_id' => $biz_id_up], ['id' => $uid]);
//临时
}
}
}
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
/**
* Notes:获取门店管理员
* Created on: 2021/11/15 17:33
* Created by: dengbw
* @return bool
*/
function get_users_group()
{
$group_id = $this->input->get('group_id');
$biz_id = $this->input->get('biz_id');
$admin_name = $this->input->get('admin_name');
if (!$group_id || !$biz_id) {
return $this->show_json(SYS_CODE_FAIL, '参数错误');
}
$where = ["group_id" => $group_id, 'status >' => -1];
$admin_name && $where['uname like "%' . $admin_name . '%"'] = null;
$res_u = $this->userM->select($where, '', 0, 0, 'id,uname as name');
$res_u2 = $this->userM->select(array_merge($where, ['biz_id' => $biz_id]), '', 0, 0, 'id');
$this->data['admins'] = $res_u ? $res_u : [];
$this->data['admin_id'] = $res_u2 ? array_column($res_u2, 'id') : [];
return $this->show_json(SYS_CODE_SUCCESS);
}
/**
* Notes:保存门店管理员
* Created on: 2021/11/15 17:33
* Created by: dengbw
* @return bool
*/
function edit_users_group()
{
$biz_manage = $this->input->post('biz_manage');
if (!$biz_manage['biz_id']) {
return $this->show_json(SYS_CODE_FAIL, '参数错误');
}
if (!$biz_manage['admin_name']) {
$where_del = ['biz_id' => $biz_manage['biz_id'], 'group_id' => $biz_manage['group_id']];
if ($biz_manage['admin_id']) {
$admin_id_str = implode(',', $biz_manage['admin_id']);
$where_del["id not in({$admin_id_str})"] = null;
}
$this->userM->update(["biz_id" => 0], $where_del);
}
foreach ($biz_manage['admin_id'] as $key => $value) {
$this->userM->update(["biz_id" => $biz_manage['biz_id']], ['id' => $value]);
}
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
}