Files
2023-06-02 15:16:24 +08:00

674 lines
35 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021.06.23
* Time: 14:08
*/
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
class User extends Wxapp
{
function __construct($inputs, $app_key)
{
parent::__construct($inputs, $app_key);
$this->login_white = array('get_ukey');//登录白名单
$this->majia_white = array('get_ukey', 'get');//超级管理员披上马甲可操作权限
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg = array();//授权微信信息
$this->load->model("biz/biz_model");
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_brand_biz_model');
$this->load->model('items/items_model', 'mdItems');
$this->load->model('items/items_oplogs_model', 'mdItemsOplogs');
$this->load->model('sys/sys_city_model');
$this->load->library('receiver/orders_v2_entity');
}
/**
* 获取ukey
* @return array
* @throws Exception
*/
protected function get_ukey()
{
$mobile_white = ['15359333655', '18350451617', '13860199666'];
$code = $this->input_param('code');
$mobile = $this->input_param('mobile');
$sms_code = $this->input_param('sms_code');
if (!$code || !$mobile || !$sms_code) {
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
//判断验证码
if (!in_array($mobile, $mobile_white) && $this->env != 'd') {//测试环境和测试号码
$mc = &load_cache();
$key = "licheb_login_code_" . $mobile;
$cache_code = $mc->get($key);
if ($sms_code != $cache_code) {
throw new Exception('验证码错误', API_CODE_FAIL);
}
}
$user = $this->app_user_model->get(['mobile' => $mobile, 'status>' => -1]);
if (!$user) {
throw new Exception('用户不存在', API_CODE_FAIL);
}
if (!$user['status']) {
throw new Exception('该账号已停用', API_CODE_FAIL);
}
//判断门店是否存在
if ($user['group_id'] < 4) {
$biz_id = intval($user['biz_id']);
$biz = $this->biz_model->get(['id' => $biz_id, 'status' => 1]);
if (!$biz) {
throw new Exception('门店不存在', API_CODE_FAIL);
}
}
$session = $this->wx_session($code);
//print_r($session);
if (!$session['session_key']) {
throw new Exception('登录失败', API_CODE_FAIL);
}
$uid = $user['id'];
if (!$user['openid']) { //未绑定微信
$upd = [
'openid' => $session['openid']
];
$session['unionid'] && $upd['unionid'] = $session['unionid'];
$ret = $this->app_user_model->update($upd, array('id' => $uid));
if (!$ret) {
debug_log("[error]# code:{$code}; " . $this->app_user_model->db->last_query(), __FUNCTION__, $this->log_dir);
throw new Exception('授权用户信息失败', API_CODE_FAIL);
}
}
$udata = array('uid' => $uid, 'session_key' => $session['session_key']);
$ukey = $this->refresh_login($udata);
return array('ukey' => $ukey);
}
/**
* Notes:用户信息
* Created on: 2022/10/12 14:09
* Created by: dengbw
* @return array
* @throws Exception
*/
protected function get()
{
$uid = $this->session['uid'];
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$user = $this->app_user_model->get(array('id' => $uid));
//获取所属店铺字段
$biz = $this->biz_model->get(['id' => $biz_id, 'status' => 1], 'biz_name,type');
//判断门店是否存在
if (!$biz && $this->session['group_id'] < 4) {
$this->logout();
throw new Exception('门店不存在', API_CODE_FAIL);
}
$group_arr = $this->app_user_model->get_group();
$group_name = $group_arr[$user['group_id']] ? $group_arr[$user['group_id']] : '';
//获取拨打电话
$json = $this->session['jsondata'] ? json_decode($this->session['jsondata'], true) : array();
$tel = $this->session['mobile'];
if ($json && $json['licheb'] && $json['licheb']['tel']) {
$tel = $json['licheb']['tel'];
}
$data = array(
'uid' => $uid,
'uname' => $user['uname'],
'mobile' => $user['mobile'],
'headimg' => $user['headimg'],
'tel' => $tel,
'group_id' => $user['group_id'],
'group_name' => $group_name,
'biz_id' => $biz_id,
'biz_name' => $biz['biz_name'] ? $biz['biz_name'] : '',
'biz_type' => $biz['type'] ? $biz['type'] : '',
'show_sa' => $biz_id == 70 ? true : false,
'new_biz_id' => $this->session['new_biz_id'] ? $this->session['new_biz_id'] : '',
'group_id_type' => $this->session['group_id_type'] ? $this->session['group_id_type'] : '',
);
# 用户可切换的组别
$group_name1 = $group_arr[$user['group_id1']] ? $group_arr[$user['group_id1']] : '';
$group_name_arr = array();
if ($user['group_id'] && $user['group_id1']){
# 渠道角色 再显示下城市名称,门店角色 再显示下门店名称
$where_biz = ["id in ({$user['biz_id']}, {$user['biz_id1']})" => null];
$map_biz = $this->biz_model->map('id', 'biz_name', $where_biz, '', 0, 0, 'id,biz_name');
$where_city = ["city_id in ({$user['city_id']}, {$user['city_id1']})" => null];
$map_city = $this->sys_city_model->map('city_id', 'name', $where_city, '', 0, 0, 'city_id,name');
$group_name_ = '默认角色:'.$group_name;
if ($user['group_id'] == 4){
$group_name_ = $user['city_id'] ? $group_name_."({$map_city[$user['city_id']]})" : $group_name_;
}
else{
$group_name_ = $user['biz_id'] ? $group_name_."({$map_biz[$user['biz_id']]})" : $group_name_;
}
$group_name1_ = '第二角色:'.$group_name1;
if ($user['group_id1'] == 4){
$group_name1_ = $user['city_id1'] ? $group_name1_."({$map_city[$user['city_id1']]})" : $group_name1_;
}
else{
$group_name1_ = $user['biz_id1'] ? $group_name1_."({$map_biz[$user['biz_id1']]})" : $group_name1_;
}
$this->session['group_id_type'] && $group_name_arr[] = array('group_id'=>'group_id', "geoup_name"=>$group_name_);
!$this->session['group_id_type'] && $group_name_arr[] = array('group_id'=>'group_id1', "geoup_name"=>$group_name1_);
}
$data['group_name_arr'] = $group_name_arr;
if ($this->session['group_id_type']){
$data['group_id'] = $user['group_id1'];
$data['group_name'] = $group_name1;
if ($user['group_id1'] != 4){
$biz_id1 = $user['biz_id1'];
$data['biz_id'] = $biz_id1;
$biz1 = $this->biz_model->get(['id' => $biz_id1, 'status' => 1], 'biz_name,type');
$data['biz_name'] = $biz1['biz_name'] ? $biz1['biz_name'] : '';
$data['biz_type'] = $biz1['type'] ? $biz1['type'] : '';
}
}
return $data;
}
/**
* 更新用户信息
* @return array
* @throws Exception
*/
protected function put()
{
$encrypted = $this->input_param('encryptedData');
$iv = $this->input_param('iv');
$intro = $this->input_param('intro');
//获取用户信息
$uid = $this->session['uid'];
$user = $this->app_user_model->get(array('id' => $uid));
$upd = array();
$mobile = '';
//授权
if (!is_null($encrypted)) {
if (!$encrypted || !$iv) {
throw new Exception('授权失败', API_CODE_INVILD_PARAM);
}
$wxdata = $this->wx_data($encrypted, $iv);
if (!$wxdata) {
throw new Exception('授权失败', API_CODE_FAIL);
}
$openid = $wxdata['openId'];
$nickname = $wxdata['nickName'];
$sex = $wxdata['gender'];
$headimg = $wxdata['avatarUrl'];
$unionid = $wxdata['unionId'];
$mobile = $wxdata['phoneNumber'];
if (!$mobile && isset($wxdata['phoneNumber'])) {
throw new Exception('微信未绑定手机号', API_CODE_FAIL);
}
$mobile && !$user['mobile'] && $upd['mobile'] = $mobile;
$nickname && $upd['nickname'] = $nickname;
$headimg && $upd['headimg'] = $headimg;
if ($mobile) {//判断手机号是否重复
if (!mobile_valid($mobile)) {
throw new Exception("请输入正确的手机号", API_CODE_FAIL);
}
$where = array('mobile' => $mobile, 'id <>' => $uid, "status<>" => -1);
$count = $this->app_user_model->count($where);
if ($count > 0) {
throw new Exception("{$mobile}已被绑", API_CODE_FAIL);
}
}
} else {//编辑其他信息
$userInfo = $this->input_param('userInfo');
$userInfo['nickName'] && $upd['nickname'] = $userInfo['nickName'];
$userInfo['avatarUrl'] && $upd['headimg'] = $userInfo['avatarUrl'];
if ($intro) {
if (mb_strlen($intro, 'utf8') > 20) {
throw new Exception('介绍自己20个字够啦', API_CODE_FAIL);
}
$upd['signature'] = $intro;
}
}
if ($upd) {
$ret = $this->app_user_model->update($upd, array('id' => $uid));
if (!$ret) {
debug_log("[error]# " . $this->app_user_model->db->last_query(), __FUNCTION__, $this->log_dir);
throw new Exception('请求失败', API_CODE_FAIL);
}
}
$data = array(
'mobile' => $mobile,
'nickname' => $upd['nickname'] ? $upd['nickname'] : $user['nickname'],
'headimg' => $upd['headimg'] ? $upd['headimg'] : $user['headimg'],
);
return $data;
}
/**
* 统计数据
*/
protected function get_cal()
{
$uid = $this->session['uid'];
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$group_id = $this->session['group_id'];
$this->load->model('receiver/receiver_customers_model', 'customers_model');
$this->load->model('receiver/order/receiver_orders_model', 'orders_model');
$this->load->model('receiver/order/receiver_orders_v2_model');
$this->load->model('receiver/order/receiver_order_signs_model', 'order_signs_model');
$this->load->model('receiver/receiver_customers_visit_data_model', 'mdCustomerVisitData');
$where = ['status>' => -1, 'is_top' => 1, 'biz_id' => $biz_id, 'cs_biz_id<>' => -1, 'status!=' => 2];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$gz_count = $this->customers_model->count($where);
$where = ['status' => 0, 'biz_id' => $biz_id];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$sign_count = $this->orders_model->count($where);
$where = ['status' => 1, 'biz_id' => $biz_id];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$loan_count = $this->orders_model->count($where);
//客户代办事项
if ($group_id == 1) {
$customer_op_list = [];
} else {
//未派单客户
$where = ['admin_id' => 0, 'biz_id' => $biz_id, 'cs_biz_id<>' => -1, 'status>=' => 0];
if ($group_id == 4 && $biz_id != 1) {
$where['brand_id!='] = 3; //渠道经理过滤
}
$unuse_count = $this->customers_model->count($where);
$defeat_count = $this->customers_model->count(['biz_id' => $biz_id, 'cs_biz_id<>' => -1, 'if_defeat' => 1, 'status>' => -1]);
$customer_op_list = [
['title' => '待分配客户(人)', 'icon' => 'icon-daifenpei', 'total' => $unuse_count, 'page' => '/pages/customer/allot/index'],
['title' => '战败申请(人)', 'icon' => 'icon-statistics-custom-5', 'total' => $defeat_count, 'page' => '/pages/customer/optDefeat/index'],
];
}
$where = ['status>=' => 0, 'payway' => 0, 'id>=' => Orders_v2_entity::V2_START_ID,
'brand_id>' => 0, 'biz_id' => $biz_id, 'status!=' => 2,
'id not in (select o_id from lc_receiver_order_status where pid_status=1 and status=2)' => null];
$group_id == 1 && $where['sale_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$fq_total = $this->receiver_orders_v2_model->count($where);
$where = ['status>=' => 0, 'id>=' => Orders_v2_entity::V2_START_ID,
'brand_id>' => 0, 'biz_id' => $biz_id, 'status!=' => 2,
'id not in (select o_id from lc_receiver_order_status where pid_status=2 and status=1)' => null];
$group_id == 1 && $where['sale_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$pc_total = $this->receiver_orders_v2_model->count($where);
$where = [
'status>=' => 0, 'id>=' => Orders_v2_entity::V2_START_ID,
'brand_id>' => 0, 'biz_id' => $biz_id, 'status!=' => 2,
'id not in (select o_id from lc_receiver_order_status where pid_status=3 and status=1)' => null,
'id in (select o_id from lc_receiver_order_status where pid_status=0 and status=2)' => null];
$group_id == 1 && $where['sale_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$kp_total = $this->receiver_orders_v2_model->count($where);
$where = [
'status>=' => 0, 'id>=' => Orders_v2_entity::V2_START_ID,
'brand_id>' => 0, 'biz_id' => $biz_id, 'status!=' => 2,
'id in (select o_id from lc_receiver_order_status where pid_status=3 and status=1)' => null,
'id not in (select o_id from lc_receiver_order_status where pid_status=4 and status=2)' => null
];
$group_id == 1 && $where['sale_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$ck_total = $this->receiver_orders_v2_model->count($where);
$deallist = [
['title' => '分期办理 ', 'icon' => 'icon-banfenqi', 'total' => $fq_total, 'page' => '/pages/order/filterList/index2?type=fq&title=分期办理'],
['title' => '车辆匹配', 'icon' => 'icon-cheliangfenpei', 'total' => $pc_total, 'page' => '/pages/order/filterList/index2?type=pc&title=车辆分配'],
['title' => '发票开具', 'icon' => 'icon-kaipiao1', 'total' => $kp_total, 'page' => '/pages/order/filterList/index2?type=kp&title=发票开具'],
['title' => '交付确认', 'icon' => 'icon-jiaofu', 'total' => $ck_total, 'page' => '/pages/order/filterList/index2?type=jf&title=交付确认'],
];
$where_v = ['a.biz_id' => $biz_id, 'a.cs_biz_id<>' => -1, 'a.status in(0,1)' => null, 'b.t_day' => date('Y-m-d')];
$where_c = ['biz_id' => $biz_id, 'cs_biz_id<>' => -1, 'status in(0,1)' => null];
$group_id == 4 && $where_c['brand_id!='] = 3;
if ($group_id == 1) {
$where_v['b.sales_id'] = $uid;
$where_c['admin_id'] = $uid;
}
$h_num = $this->customers_model->count(array_merge($where_c, ['level' => 'H']));
$h_num_1 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'H', 'b.status<>' => 2]));
$h_num_2 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'H', 'b.status' => 2]));
$a_num = $this->customers_model->count(array_merge($where_c, ['level' => 'A']));
$a_num_1 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'A', 'b.status<>' => 2]));
$a_num_2 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'A', 'b.status' => 2]));
$b_num = $this->customers_model->count(array_merge($where_c, ['level' => 'B']));
$b_num_1 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'B', 'b.status<>' => 2]));
$b_num_2 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'B', 'b.status' => 2]));
$c_num = $this->customers_model->count(array_merge($where_c, ['level' => 'C']));
$c_num_1 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'C', 'b.status<>' => 2]));
$c_num_2 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'C', 'b.status' => 2]));
$levelSt = [
['title' => 'H级客户', 'list' => [['title' => '总数 >', 'num' => $h_num, 'url' => '/pages/customer/filterList/index?level=H&status_tp=1&title=H级客户']
, ['title' => '今日需跟进 >', 'num' => $h_num_1, 'url' => '/pages/customer/filterList/index?level=H&status=1&visit=1&title=H级今日需跟进']
, ['title' => '今日已跟进 >', 'num' => $h_num_2, 'url' => '/pages/customer/filterList/index?level=H&status=2&visit=1&title=H级今日已跟进']]],
['title' => 'A级客户', 'list' => [['title' => '总数 >', 'num' => $a_num, 'url' => '/pages/customer/filterList/index?level=A&status_tp=1&title=A级客户']
, ['title' => '今日需跟进 >', 'num' => $a_num_1, 'url' => '/pages/customer/filterList/index?level=A&status=1&visit=1&title=A级今日需跟进']
, ['title' => '今日已跟进 >', 'num' => $a_num_2, 'url' => '/pages/customer/filterList/index?level=A&status=2&visit=1&title=A级今日已跟进']]],
['title' => 'B级客户', 'list' => [['title' => '总数 >', 'num' => $b_num, 'url' => '/pages/customer/filterList/index?level=B&status_tp=1&title=B级客户']
, ['title' => '今日需跟进 >', 'num' => $b_num_1, 'url' => '/pages/customer/filterList/index?level=B&status=1&visit=1&title=B级今日需跟进']
, ['title' => '今日已跟进 >', 'num' => $b_num_2, 'url' => '/pages/customer/filterList/index?level=B&status=2&visit=1&title=B级今日已跟进']]],
['title' => 'C级客户', 'list' => [['title' => '总数 >', 'num' => $c_num, 'url' => '/pages/customer/filterList/index?level=C&status_tp=1&title=C级客户']
, ['title' => '今日需跟进 >', 'num' => $c_num_1, 'url' => '/pages/customer/filterList/index?level=C&status=1&visit=1&title=C级今日需跟进']
, ['title' => '今日已跟进 >', 'num' => $c_num_2, 'url' => '/pages/customer/filterList/index?level=C&status=2&visit=1&title=C级今日已跟进']]],
];
$wl_num = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['b.status<>' => 2]));
//数据统计
$str_uids = $str_userids = '';
if ($group_id == 1) {
$str_uids = $uid;
$str_userids = $this->session['userid'];
} else {
$res_user = $this->app_user_model->select(['biz_id' => $biz_id, 'group_id <' => 4, 'status>=' => 0], 'id asc', 0, 0, 'id,userid');
foreach ($res_user as $k => $v) {
$str_uids = $str_uids ? $str_uids . ',' . $v['id'] : $v['id'];
$v['userid'] && $str_userids = $str_userids ? $str_userids . "','" . $v['userid'] : $v['userid'];
}
}
!$str_uids && $str_uids = '-1';
!$str_userids && $str_userids = '-1';
$this->load->model('receiver/receiver_customer_oplogs_model', 'mdCustomerOpLogs');
$s_today = date('Y-m-d') . ' 00:00:00';
$e_today = date('Y-m-d') . ' 23:59:59';
$s_month = date('Y-m-01', strtotime(date("Y-m-d"))) . ' 00:00:00';
$e_month = date('Y-m-d', strtotime("$s_month +1 month -1 day")) . ' 23:59:59';
//线索
$where_today_xs = ['biz_id' => $biz_id, 'cs_biz_id<>' => -1, 'status>=' => 0, 'c_time>=' => strtotime($s_today), 'c_time<=' => strtotime($e_today)];
$where_month_xs = ['biz_id' => $biz_id, 'cs_biz_id<>' => -1, 'status>=' => 0, 'c_time>=' => strtotime($s_month), 'c_time<=' => strtotime($e_month)];
//企微
$where_today_qy = ['change_type' => 'add_external_contact', 'c_time>=' => strtotime($s_today), 'c_time<=' => strtotime($e_today)];
$where_month_qy = ['change_type' => 'add_external_contact', 'c_time>=' => strtotime($s_month), 'c_time<=' => strtotime($e_month)];
//到店
$where_today_dd = ['biz_id' => $biz_id, 'status>=' => 0, 'cs_biz_id<>' => -1,
"id in(select customer_id from lc_receiver_customer_oplogs where type=4 and uid in({$str_uids}) and c_time>=" . strtotime($s_today) . " and c_time<=" . strtotime($e_today) . ")" => null];
$where_month_dd = ['biz_id' => $biz_id, 'status>=' => 0, 'cs_biz_id<>' => -1,
"id in(select customer_id from lc_receiver_customer_oplogs where type=4 and uid in({$str_uids}) and c_time>=" . strtotime($s_month) . " and c_time<=" . strtotime($e_month) . ")" => null];
//订单
$where_today_order = ['status>=' => 0, 'order_time>=' => $s_today, 'order_time<=' => $e_today];
$where_month_order = ['status>=' => 0, 'order_time>=' => $s_month, 'order_time<=' => $e_month];
//战败
$where_today_defeat = ['biz_id' => $biz_id, 'status>=' => 0, 'cs_biz_id<>' => -1,
"id in(select customer_id from lc_receiver_customer_oplogs where type=7 and uid in({$str_uids}) and c_time>=" . strtotime($s_today) . " and c_time<=" . strtotime($e_today) . ")" => null];
$where_month_defeat = ['biz_id' => $biz_id, 'status>=' => 0, 'cs_biz_id<>' => -1,
"id in(select customer_id from lc_receiver_customer_oplogs where type=7 and uid in({$str_uids}) and c_time>=" . strtotime($s_month) . " and c_time<=" . strtotime($e_month) . ")" => null];
//退订
$where_today_refund = ['status' => 2, 'refund_time>=' => $s_today, 'refund_time<=' => $e_today];
$where_month_refund = ['status' => 2, 'refund_time>=' => $s_month, 'refund_time<=' => $e_month];
if ($group_id == 1) {
$where_today_xs['admin_id'] = $uid;
$where_month_xs['admin_id'] = $uid;
$where_today_order['admin_id'] = $uid;
$where_month_order['admin_id'] = $uid;
$where_today_refund['admin_id'] = $uid;
$where_month_refund['admin_id'] = $uid;
} else if ($group_id == 2 || $group_id == 3) {//店长/老板
$where_today_order["(biz_id = {$biz_id} OR admin_id = {$uid})"] = null;
$where_month_order["(biz_id = {$biz_id} OR admin_id = {$uid})"] = null;
$where_today_refund["(biz_id = {$biz_id} OR admin_id = {$uid})"] = null;
$where_month_refund["(biz_id = {$biz_id} OR admin_id = {$uid})"] = null;
} else if ($group_id == 4) {
$where_today_order['biz_id'] = $biz_id;
$where_month_order['biz_id'] = $biz_id;
$where_today_refund['biz_id'] = $biz_id;
$where_month_refund['biz_id'] = $biz_id;
if ($biz_id != 1) {//渠道经理过滤
$where_today_order['brand_id<>'] = 3;
$where_month_order['brand_id<>'] = 3;
$where_today_refund['brand_id<>'] = 3;
$where_month_refund['brand_id<>'] = 3;
}
}
$today_qy = $month_qy = 0;
if ($str_userids) {
$where_today_qy["userid in('{$str_userids}')"] = null;
$where_month_qy["userid in('{$str_userids}')"] = null;
$re_biz = $this->biz_model->get(['id' => $biz_id]);
if ($re_biz['type'] == 5) {//异业店
$this->load->model('app/app_different_qy_log_model', 'mdDifferentQyLog');
$today_qy = $this->mdDifferentQyLog->count($where_today_qy, 'distinct(external_userid)');
$month_qy = $this->mdDifferentQyLog->count($where_month_qy, 'distinct(external_userid)');
} else {
$this->load->model('app/app_lichene_qy_log_model', 'mdWechatqyLog');
$today_qy = $this->mdWechatqyLog->count($where_today_qy, 'distinct(external_userid)');
$month_qy = $this->mdWechatqyLog->count($where_month_qy, 'distinct(external_userid)');
}
}
$statistics = [
['today' => ['title' => '今日', 'value' => $this->customers_model->count($where_today_xs), 'url' => '/pages/customer/filterList/index?status=3&visit=3&title=线索']
, 'month' => ['title' => '本月线索', 'value' => $this->customers_model->count($where_month_xs)]],
['today' => ['title' => '今日', 'value' => $today_qy, 'url' => '/pages/customer/filterList/index?status=4&visit=4&title=企v']
, 'month' => ['title' => '本月企v', 'value' => $month_qy]],
['today' => ['title' => '今日', 'value' => $this->customers_model->count($where_today_dd), 'url' => '/pages/customer/filterList/index?status=5&visit=5&title=到店']
, 'month' => ['title' => '本月到店', 'value' => $this->customers_model->count($where_month_dd)]],
['today' => ['title' => '今日', 'value' => $this->receiver_orders_v2_model->count($where_today_order), 'url' => '/pages/order/filterList/index2?status=6&visit=6&title=订单']
, 'month' => ['title' => '本月订单', 'value' => $this->receiver_orders_v2_model->count($where_month_order)]],
['today' => ['title' => '今日', 'value' => $this->customers_model->count($where_today_defeat), 'url' => '/pages/customer/filterList/index?status=7&visit=7&title=战败']
, 'month' => ['title' => '本月战败', 'value' => $this->customers_model->count($where_month_defeat)]],
['today' => ['title' => '今日', 'value' => $this->receiver_orders_v2_model->count($where_today_refund), 'url' => '/pages/order/filterList/index2?status=8&visit=8&title=退订']
, 'month' => ['title' => '本月退订', 'value' => $this->receiver_orders_v2_model->count($where_month_refund)]]
];
$data = [
'wl_count' => ['title' => '待跟进客户(人)', 'num' => $wl_num, 'url' => '/pages/customer/filterList/index?status=1&visit=1&title=待跟进客户'],
'gz_count' => ['title' => '特别关注客户(人)', 'num' => $gz_count, 'url' => '/pages/customer/filterList/index?istop=1&title=特别关注客户'],
'sign_count' => $sign_count,
'loan_count' => $loan_count,
'deallist' => $deallist,
'customer_op_list' => $customer_op_list,
'levelSt' => $levelSt,
'where_today_qy' => $where_today_qy,
'statistics' => $statistics,
'str_uids' => $str_uids
];
return $data;
}
/**
* 更新联系手机号
* @return array
* @throws Hd_Exception
*/
protected function put_tel()
{
$tel = $this->input_param('tel');
if (!mobile_valid($tel)) {
throw new Hd_Exception('确认一下手机号是否正确', API_CODE_INVILD_PARAM);
}
$uid = $this->session['uid'];
$user = $this->app_user_model->get(array('id' => $uid));
$json = $user['jsondata'] ? json_decode($user['jsondata'], true) : array();
$json['licheb']['tel'] = $tel;
$upd = array('jsondata' => json_encode($json, JSON_UNESCAPED_UNICODE));
$ret = $this->app_user_model->update($upd, array('id' => $uid));
if (!$ret) {
throw new Hd_Exception('更新失败', API_CODE_FAIL);
}
$data = array('tel' => $tel);
return $data;
}
/**
* 获取门店信息
* @return array
*/
protected function get_bizs()
{
$city_id = $this->input_param('city_id');
$lists = [];
$biz_id_arr = explode(',', $this->session['biz_id']);
$fileds = 'id,biz_name,jsondata';
if ($this->session['biz_id'] && $biz_id_arr) {
$city_id && $o_where = ['city_id' => $city_id, 'type<>' => 4];
$bizs = $this->biz_model->get_by_id_arr($biz_id_arr, $o_where, $fileds);
} else {
$typeAry = $this->biz_model->type_ary();
$type_ids = implode(',',array_keys($typeAry));
$bizs = $this->biz_model->select(['status' => 1, 'city_id' => $city_id, "type in ($type_ids)" => null, 'type<>' => 4], 'id desc', '', '', $fileds);
}
if ($bizs) {
foreach ($bizs as $key => $val) {
$auto_brands = [];
$b_biz_rows = $this->auto_brand_biz_model->select(['biz_id'=>$val['id']],'','','','brand_id');
$brand_ids = implode(',', array_column($b_biz_rows,'brand_id'));
if ($b_biz_rows && $brand_ids) {
$where = [
"id in ($brand_ids)" => null,
"id !=" => 3 //过滤狸车品牌
];
$brands = $this->auto_brand_model->select($where, '', 0, 0, 'id,name');
foreach ($brands as $key2 => $val2) {
$res_items = $this->mdItems->select(['brand_id' => $val2['id'], 'status<>' => 0, 'biz_id' => $val['id']
, 'bill_time' => '0000-00-00 00:00:00'], 'in_time asc', 0, 0, 'id,in_time,c_time');
$str_ids = $res_items ? implode(',', array_column($res_items, 'id')) : '';
$bg_color = '#999';
$name2 = '';
if ($str_ids) {
$bg_color = '#fff';
$in_time = $res_items[0]['in_time'] != '0000-00-00 00:00:00' ? strtotime($res_items[0]['in_time']) : $res_items[0]['c_time'];
$re_logs = $this->mdItemsOplogs->select(["item_id in({$str_ids})" => null, 'type' => 2, 'biz_id_to' => $val['id']
], 'com_time asc', 1, 1, 'item_id,com_time');//找出调拨最早
if ($re_logs && $re_logs[0]['item_id']) {
$this->load->model('items/items_transfer_model', 'mdTransfer');
$re_tran = $this->mdTransfer->get(['item_id' => $re_logs[0]['item_id'], 'status' => 1]);
if ($re_tran) {
$in_time = '';
$name2 = '(在途)';
} else {
$in_time = strtotime($re_logs[0]['com_time']);
}
}
if ($in_time) {//计算库存天数
$days = round((time() - $in_time) / 3600 / 24);
$days > 0 && $name2 = '(' . $days . '天)';
}
}
$auto_brands[] = ['name' => $val2['name'] . $name2, 'bg_color' => $bg_color];
}
}
$lists[] = [
'id' => $val['id'],
'name' => $val['biz_name'],
'auto_brands' => $auto_brands
];
}
}
$data = [
'list' => $lists,
];
return $data;
}
//获取门店管理员
protected function get_admins()
{
$biz_id = $this->input_param('biz_id');
$page = $this->input_param('page');
$size = $this->input_param('size');
!$page && $page = 1;
!$size && $size = 10;
if (!$biz_id) {
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
}
$where = [
'status' => 1,
'biz_id' => $biz_id
];
$lists = [];
$count = $this->app_user_model->count($where);
if ($count) {
$rows = $this->app_user_model->select($where, 'id desc', $page, $size, 'id,uname');
$lists = $rows;
}
$tabs = $bizs = [];
$re_biz = $this->biz_model->get(['id' => $biz_id, 'status' => 1]);
if ($re_biz['type'] == 1) {//品牌店
$tabs = [['id' => 1, 'name' => '本店'], ['id' => 2, 'name' => '其它门店']];
$bizs = $this->biz_model->select(['type' => 1, 'status' => 1], 'id desc', 0, 0, 'id, biz_name as name');
}
$data = [
'list' => $lists,
'total' => $count,
'tabs' => $tabs,
'bizs' => $bizs,
];
return $data;
}
/**
* 重新设置用户session里的biz_id
*/
protected function put_resetbiz()
{
$ukey = $this->input_param('ukey');
$biz_id = $this->input_param('biz_id');
if (!$biz_id) {
throw new Hd_Exception('参数错误', API_CODE_INVILD_PARAM);
}
$this->session['new_biz_id'] = $biz_id;
$this->app_redis->save($this->redis_login . $ukey, json_encode($this->session, JSON_UNESCAPED_UNICODE), 30 * 24 * 3600);
throw new Exception('保存成功', API_CODE_SUCCESS);
}
/**
* 切换角色:重新设置用户session里的group_id
*/
protected function put_resetgroupid()
{
$ukey = $this->input_param('ukey');
$group_id_type = $this->input_param('group_id');
if (!$group_id_type) {
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
$uid = $this->session['uid'];
$user = $this->app_user_model->get(array('id' => $uid));
if ($group_id_type == 'group_id1'){
$this->session['group_id'] = $user['group_id1'];
$this->session['biz_id'] = $user['biz_id1'];
$this->session['city_id'] = $user['city_id1'];
$this->session['group_id_type'] = $group_id_type;
$this->session['new_biz_id'] = intval($user['biz_id1']);
}
else{
$this->session['group_id'] = $user['group_id'];
$this->session['biz_id'] = $user['biz_id'];
$this->session['city_id'] = $user['city_id'];
$this->session['group_id_type'] = '';
$this->session['new_biz_id'] = '';
}
$this->app_redis->save($this->redis_login . $ukey, json_encode($this->session, JSON_UNESCAPED_UNICODE), 30 * 24 * 3600);
throw new Exception('保存成功', API_CODE_SUCCESS);
}
}