Files
liche/admin/controllers/receiver/Owners.php
T
2022-09-06 17:44:34 +08:00

545 lines
23 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Notes:车主
* Created on: 2022/6/15 17:15
* Created by: dengbw
*/
class Owners extends HD_Controller
{
private $searchTpAry = array('mobile' => '车主电话', 'name' => '车主姓名', 'car_num' => '车牌号');
private $searchTimeAry = array('c_time' => '创建时间', 'order_time' => '购车时间', 'ck_time' => '交付时间', 'u_time' => '最后操作时间');
public function __construct()
{
parent::__construct();
$this->load->model('receiver/receiver_owners_model', 'mdOwners');
$this->load->model('receiver/receiver_owners_oplogs_model', 'mdOwnersOplogs');
$this->load->model('receiver/receiver_owners_tag_model', 'mdOwnersTag');
$this->load->model('receiver/receiver_owners_tagdata_model', 'mdOwnersTagdata');
$this->load->model("biz/biz_model", 'mdBiz');
$this->load->model('receiver/order/receiver_orders_v2_model', 'mdOrders');
$this->load->model('app/licheb/app_licheb_users_model', 'mdLichebUsers');
$this->load->model('receiver/receiver_xz_model', 'mdXz');
$this->load->model('auto/auto_brand_model', 'mdBrand');
$this->load->model('auto/auto_series_model', 'mdSeries');
$this->load->model('auto/auto_attr_model', 'mdAttr');
$this->load->model('area_model', 'mdArea');
}
public function index()
{
return $this->lists();
}
public function lists()
{
$params = $this->input->get();
$this->data = $this->dataSelect($params);
return $this->show_view('receiver/owners/lists', true);
}
private function dataSelect($params)
{
$page = $params['page'] = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] = $params['size'] ? intval($params['size']) : 20;
!$params['city_id_admin'] && $params['city_id_admin'] = '';
!$params['county_id_admin'] && $params['county_id_admin'] = '';
!$params['biz_id_admin'] && $params['biz_id_admin'] = '';
!$params['admin_id'] && $params['admin_id'] = '';
!$params['city_id'] && $params['city_id'] = '';
!$params['county_id'] && $params['county_id'] = '';
!$params['sex'] && $params['sex'] = '';
!$params['brand_id'] && $params['brand_id'] = '';
!$params['s_id'] && $params['s_id'] = '';
!$params['v_id'] && $params['v_id'] = '';
if (strlen($params['status'])) {
$where["a.status"] = $params['status'];
} else {
$where["a.status>=0"] = null;
}
if ($params['title']) {
$where["a.{$params['search_tp']} like '%{$params['title']}%'"] = null;
}
!$params['search_tp'] && $params['search_tp'] = 'mobile';
if ($params['c_time']) {
$c_time = explode(' ~ ', $params['c_time']);
$c_time[0] && $where["a.c_time >="] = strtotime($c_time[0] . ' 00:00:00');
$c_time[1] && $where["a.c_time <="] = strtotime($c_time[1] . ' 23:59:59');
}
if ($params['order_time']) {
$order_time = explode(' ~ ', $params['order_time']);
$order_time[0] && $where["a.order_time >="] = $order_time[0] . ' 00:00:00';
$order_time[1] && $where["a.order_time <="] = $order_time[1] . ' 23:59:59';
}
if ($params['ck_time']) {
$ck_time = explode(' ~ ', $params['ck_time']);
$ck_time[0] && $where["a.ck_time >="] = $ck_time[0] . ' 00:00:00';
$ck_time[1] && $where["a.ck_time <="] = $ck_time[1] . ' 23:59:59';
}
if ($params['u_time']) {
$u_time = explode(' ~ ', $params['u_time']);
$u_time[0] && $where["a.u_time >="] = $u_time[0] . ' 00:00:00';
$u_time[1] && $where["a.u_time <="] = $u_time[1] . ' 23:59:59';
}
if (strlen($params['wxqy'])) {
$where["a.wxqy"] = $params['wxqy'];
} else {
$params['wxqy'] = '';
}
if ($params['city_id']) {
$where["a.city_id"] = $params['city_id'];
}
if ($params['county_id']) {
$where["a.county_id"] = $params['county_id'];
}
if ($params['sex']) {
$where["a.sex"] = $params['sex'];
}
if ($params['brand_id']) {
$where["a.brand_id"] = $params['brand_id'];
}
if ($params['s_id']) {
$where["a.s_id"] = $params['s_id'];
}
if ($params['v_id']) {
$where["a.v_id"] = $params['v_id'];
}
//销售员筛选
if ($params['admin_id']) {
$where['b.admin_id'] = $params['admin_id'];
} else if ($params['biz_id_admin']) {
$where['b.biz_id'] = $params['biz_id_admin'];
} else if ($params['county_id_admin']) {
$where["b.biz_id in(select id from lc_biz where county_id={$params['county_id_admin']})"] = null;
} else if ($params['city_id_admin']) {
$where["b.biz_id in(select id from lc_biz where city_id={$params['city_id_admin']})"] = null;
}
$lists = [];
$wxqyAry = $this->mdOwners->wxqyAry();
$statusAry = $this->mdOwners->statusAry();
$count = $this->db->select('a.id')
->from('lc_receiver_owners as a')
->join('lc_receiver_orders_v2 as b', "b.id=a.o_id", 'left')
->where($where)
->count_all_results();
if ($count) {
$offset = ($page - 1) * $size;
$res = $this->db->select('a.*,b.admin_id,b.biz_id')
->from('lc_receiver_owners as a')
->join('lc_receiver_orders_v2 as b', "b.id=a.o_id", 'left')
->where($where)
->order_by('a.id Desc')
->limit($size, $offset)
->get()->result_array();
$str_brand_ids = implode(',', array_unique(array_column($res, 'brand_id')));
$map_brands = $this->mdBrand->map('id', 'name', ["id in({$str_brand_ids})" => null]);
$str_s_ids = implode(',', array_unique(array_column($res, 's_id')));
$map_series = $this->mdSeries->map('id', 'name', ["id in({$str_s_ids})" => null]);
$str_v_ids = implode(',', array_unique(array_column($res, 'v_id')));
$map_attrs = $this->mdAttr->map('id', 'title', ["id in({$str_v_ids})" => null]);
$str_county_ids = implode(',', array_unique(array_column($res, 'county_id')));
$map_countys = $this->mdArea->map('county_id', 'city_name,county_name', ["county_id in({$str_county_ids})" => null]);
$str_biz_ids = implode(',', array_unique(array_column($res, 'biz_id')));
$map_bizs = $this->mdBiz->map('id', 'biz_name', ["id in({$str_biz_ids})" => null]);
$str_admin_ids = implode(',', array_unique(array_column($res, 'admin_id')));
$map_admins = $this->mdLichebUsers->map('id', 'uname', ["id in({$str_admin_ids})" => null]);
foreach ($res as $key => $val) {
$mobile_sub = $val['mobile'];
$sex = $car_name = $city_name = '';
$map_brands[$val['brand_id']] && $car_name = $map_brands[$val['brand_id']];
$map_series[$val['s_id']] && $car_name = $car_name ? $car_name . '-' . $map_series[$val['s_id']] : $map_series[$val['s_id']];
$map_attrs[$val['v_id']] && $car_name = $car_name ? $car_name . '-' . $map_attrs[$val['v_id']] : $map_attrs[$val['v_id']];
$map_countys[$val['county_id']] && $city_name = $map_countys[$val['county_id']]['city_name'] . '-' . $map_countys[$val['county_id']]['county_name'];
if (!$city_name && $val['city_id']) {
$re_are = $this->mdArea->get(['city_id' => $val['city_id']]);
$re_are && $city_name = $re_are['city_name'];
}
$val['sex'] && $sex = $val['sex'] == 1 ? '男' : '女';
$biz_name = $map_bizs[$val['biz_id']] ? $map_bizs[$val['biz_id']] : '';
$admin_name = $map_admins[$val['admin_id']] ? $map_admins[$val['admin_id']] : '';
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => $val['mobile'],
'mobile_sub' => $mobile_sub,
'sex' => $sex,
'age' => $this->getAgeByBirth($val['birth_day']),
'car_name' => $car_name,
'city_name' => $city_name,
'biz_name' => $biz_name,
'admin_name' => $admin_name,
'wxqy' => $wxqyAry[$val['wxqy']],
'car_num' => $val['car_num'],
'order_time' => $val['order_time'] != '0000-00-00 00:00:00' ? $val['order_time'] : '',
'ck_time' => $val['ck_time'] != '0000-00-00 00:00:00' ? $val['ck_time'] : '',
'u_time' => $val['u_time'] != '0000-00-00 00:00:00' ? $val['u_time'] : '',
'status_name' => $statusAry[$val['status']]
];
}
}
$show_info['searchTpAry'] = $this->searchTpAry;
$show_info['wxqyAry'] = $wxqyAry;
$show_info['statusAry'] = $statusAry;
$data['show_info'] = $show_info;
$data['lists'] = $lists;
$data['pager'] = array('count' => ceil($count / $size), 'curr' => $page, 'totle' => $count);
$data['searchTimeAry'] = $this->searchTimeAry;
$data['params'] = $params;
$data['_title'] = '车主列表';
return $data;
}
public function get()
{
$id = $this->input->get('id');
$row = $this->mdOwners->get(array('id' => $id));
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '车主不存在!');
}
//用户信息
// if (SUPER_ADMIN == $this->role || 35 == $this->role) {
// $mobile_sub = $row['mobile'];
// } else {
// $mobile_sub = $row['mobile'] ? substr_replace($row['mobile'], '*****', 0, 5) : '';
// }
$mobile_sub = $row['mobile'];
$info = $row;
$info['mobile'] = $mobile_sub;
$info['edit_type'] = 0;
$info['order_time'] = $row['order_time'] != '0000-00-00 00:00:00' ? $row['order_time'] : '';
$info['ck_time'] = $row['ck_time'] != '0000-00-00 00:00:00' ? $row['ck_time'] : '';
$info['c_time'] = date('Y-m-d H:i:s', $row['c_time']);
$info['birth_day'] = $row['birth_day'] != '0000-00-00' ? $row['birth_day'] : '';
$info['tag'] = $this->get_tag($row['id']);
$biz_name = $admin_name = '';
if ($row['o_id']) {
$re_o = $this->mdOrders->get(array('id' => $row['o_id']), 'biz_id,admin_id');
if ($re_o['biz_id']) {
$re_b = $this->mdBiz->get(array('id' => $re_o['biz_id']), 'biz_name');
$re_b && $biz_name = $re_b['biz_name'];
}
if ($re_o['admin_id']) {
$re_u = $this->mdLichebUsers->get(array('id' => $re_o['admin_id']), 'uname');
$re_u && $admin_name = $re_u['uname'];
}
}
$info['biz_name'] = $biz_name;
$info['admin_name'] = $admin_name;
//操作日志
$rows_log = $this->mdOwnersOplogs->select(['own_id' => $id], 'id desc', 0, 0);
$logs = [];
if ($rows_log) {
foreach ($rows_log as $key => $value) {
$rec_text = $rec_url = '';
if ($value['type'] == 2) {
$rec_row = $this->mdXz->get(['id' => $value['log']], 'rec_url,duration');
if ($rec_row['duration']) {
$rec_row['rec_url'] && $rec_url = $rec_row['rec_url'];
!$rec_row['rec_url'] && $rec_text = '录音暂未生成';
} else {
$rec_text = '未接通';
}
}
$imgs = [];
if ($value['imgs']) {
$json_imgs = json_decode($value['imgs'], true);
foreach ($json_imgs as $key1 => $value1) {
$imgs[] = build_qiniu_image_url($value1);
}
}
$logs[] = array(
'uname' => $value['uname'],
'log' => $value['log'],
'imgs' => $imgs,
'rec_url' => $rec_url,
'rec_text' => $rec_text,
'type_name' => $this->mdOwnersOplogs->typeAry($value['type']),
'c_time' => date('Y-m-d H:i', $value['c_time'])
);
}
}
$show_info['wxqyAry'] = $this->mdOwners->wxqyAry();
$show_info['statusAry'] = $this->mdOwners->statusAry();
$this->data['show_info'] = $show_info;
$this->data['info'] = $info;
$this->data['logs'] = $logs;
$this->data['_title'] = '车主户详情';
return $this->show_view('receiver/owners/get', true);
}
public function add()
{
// TODO: Implement add() method.
}
/**
* 新增日志
* @return bool
*/
function add_log()
{
$params = $this->input->post();
if (!$params['id']) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
if (!$params['log']) {
return $this->show_json(SYS_CODE_FAIL, '请输入内容!');
}
$addData = array(
'own_id' => $params['id'],
'uid' => $this->uid,
'uname' => $this->username,
'type' => intval($params['type']),
'log' => $params['log'],
'c_time' => time()
);
$id = $this->mdOwnersOplogs->add($addData);
if ($id) {
return $this->show_json(SYS_CODE_SUCCESS, '添加成功');
} else {
return $this->show_json(SYS_CODE_FAIL, '添加失败');
}
}
/**
* 编辑
* @return bool
*/
public function edit()
{
$info = $this->input->post('info');
if (!$info) {
return $this->show_json(SYS_CODE_FAIL, '非法参数!');
}
$row = $this->mdOwners->get(array('id' => $info['id']));
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '车主信息不存在!');
}
$msg = '修改成功';
$code = SYS_CODE_SUCCESS;
if ($info['edit_type'] == 1) {//更新状态
if ($row['status'] == $info['status']) {
return $this->show_json(SYS_CODE_FAIL, '未修改状态!');
}
$statuAry = $this->mdOwners->statusAry();
$status_name = $statuAry[$row['status']];
$status_name_up = $statuAry[$info['status']];
$log = '更新状态(' . $status_name . ')为(' . $status_name_up . ')';
$up_data['status'] = $info['status'];
$ret = $this->mdOwners->update($up_data, array('id' => $info['id']));
if (!$ret) {
$code = SYS_CODE_FAIL;
$msg = '修改状态失败';
} else {
$msg = '修改状态成功';
$this->addLog(array('own_id' => $info['id'], 'type' => 0, 'log' => $log));
}
} else if ($info['edit_type'] == 2) {
//更新车主信息
$ret = $this->mdOwners->update(['name' => $info['name'], 'mobile' => $info['mobile'], 'birth_day' => $info['birth_day'], 'sex' => $info['sex'],
'brand_id' => $info['brand_id'], 's_id' => $info['s_id'], 'v_id' => $info['v_id'], 'car_num' => $info['car_num'],
'order_time' => $info['order_time'], 'ck_time' => $info['ck_time'], 'wxqy' => $info['wxqy']
, 'city_id' => $info['city_id'], 'county_id' => $info['county_id']], ['id' => $info['id']]);
if (!$ret) {
$code = SYS_CODE_FAIL;
$msg = '修改车主信息失败';
} else {
$msg = '修改车主信息成功';
$this->addLog(array('own_id' => $info['id'], 'type' => 0, 'log' => '更新车主信息'));
}
} else if ($info['edit_type'] == 3) {
$id = $info['id'];
//客户标签
if (!$info['tag']) {
return $this->show_json(SYS_CODE_FAIL, '客户画像不存在!');
}
$add_tag = [];
//查找已加入标签
$res_td = $this->mdOwnersTagdata->select(['o_id' => $id], 'id desc', 0, 0, 't_id');
$tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : '';
foreach ($info['tag'] as $key => $val) {
foreach ($val['list'] as $key2 => $val2) {
if ($val['type'] == 'checkbox') {
if ($val2['checked'] == 'true') {
if (!$tag_data || !in_array($val2['id'], $tag_data)) {//未加标签,新增
$add_tag[] = ['o_id' => $id, 't_id' => $val2['id'], 'c_time' => time()];
}
} else {
if ($tag_data && in_array($val2['id'], $tag_data)) {//删除标签
$this->mdOwnersTagdata->delete(['o_id' => $id, 't_id' => $val2['id']]);
}
}
} else {
if ($val['value'] == $val2['id']) {
if (!$tag_data || !in_array($val2['id'], $tag_data)) {//未加标签,新增
$add_tag[] = ['o_id' => $id, 't_id' => $val2['id'], 'c_time' => time()];
}
} else {
if ($tag_data && in_array($val2['id'], $tag_data)) {//删除标签
$this->mdOwnersTagdata->delete(['o_id' => $id, 't_id' => $val2['id']]);
}
}
}
}
}
if ($add_tag && count($add_tag)) {
$this->mdOwnersTagdata->add_batch($add_tag);
}
$this->addLog(array('own_id' => $info['id'], 'type' => 0, 'log' => '修改车主画像'));
}
return $this->show_json($code, $msg);
}
public function del()
{
}
public function batch()
{
// TODO: Implement batch() method.
}
public function export()
{
$params = $this->input->get();
$params['page'] = 1;
$params['size'] = 10000;
$data = $indexs = array();
$res = $this->dataSelect($params);
$fileName = $res['_title'];
foreach ($res['lists'] as $key => $value) {
$temp['name'] = $value['name'];
$temp['mobile'] = $value['mobile'];
$temp['car_name'] = $value['car_name'];
$temp['city_name'] = $value['city_name'];
$temp['car_num'] = $value['car_num'];
$temp['sex'] = $value['sex'];
$temp['age'] = $value['age'];
$temp['wxqy'] = $value['wxqy'];
$temp['status_name'] = $value['status_name'];
$temp['order_time'] = $value['order_time'];
$temp['ck_time'] = $value['ck_time'];
$temp['city_name'] = $value['city_name'];
$temp['biz_name'] = $value['biz_name'];
$temp['admin_name'] = $value['admin_name'];
$temp['u_time'] = $value['u_time'];
$data[] = $value;
}
$indexs = [
'name' => '车主',
'mobile' => '手机号',
'car_name' => '车辆',
'city_name' => '所在城市',
'car_num' => '车牌号',
"sex" => "性别",
"age" => "年龄",
"wxqy" => "企微好友",
"status_name" => "状态",
"order_time" => "购车时间",
"ck_time" => "交付时间",
"biz_name" => "所属门店",
"admin_name" => "销售员",
"u_time" => "最后操作时间",
];
array_unshift($data, $indexs);
$this->load->library('excel');
$this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis'));
}
/**
* Notes:增加日志
* Created on: 2021/7/23 10:48
* Created by: dengbw
* @param array $ary
* @return mixed
*/
private function addLog($ary = array())
{
$id = 0;
if ($ary['log']) {
$addData = array(
'own_id' => $ary['own_id'],
'uid' => $this->uid,
'uname' => $this->username,
'type' => intval($ary['type']),
'log' => $ary['log'],
'c_time' => time()
);
$id = $this->mdOwnersOplogs->add($addData);
}
return $id;
}
/**
* Notes:车主标签
* Created on: 2022/6/16 14:47
* Created by: dengbw
* @param $id
* @return array
*/
private function get_tag($id)
{
$show = $res_td = [];
$res = $this->mdOwnersTag->select(['status' => 1, 'pid' => 0], 'sort desc,id desc', 0, 0, 'id,name,type');
if ($res) {
$id && $res_td = $this->mdOwnersTagdata->select(['o_id' => $id], 'id desc', 0, 0, 't_id');//查找用户选择
$tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : '';
foreach ($res as $key => $val) {
$list = [];
$value = '';
$res2 = $this->mdOwnersTag->select(['status' => 1, 'pid' => $val['id']], 'sort desc,id desc', 0, 0, 'id,name');
foreach ($res2 as $key2 => $val2) {
//检查是否选中标签
$setValue = ['id' => $val2['id'], 'name' => $val2['name']];
if ($val['type'] == 'checkbox') {
$setValue['checked'] = $tag_data && in_array($val2['id'], $tag_data) ? true : false;
} else {
if ($tag_data && in_array($val2['id'], $tag_data)) {
$value = $val2['id'];
}
}
$list[] = $setValue;
}
$show[] = ['id' => $val['id'], 'name' => $val['name'], 'type' => $val['type'], 'value' => $value, 'list' => $list];
}
}
return $show;
}
/**
* Notes:根据生日计算年龄,年龄的格式是:2018-01-22
* Created on: 2022/6/16 14:47
* Created by: dengbw
* @param $birthday
* @return false|int|string
*/
private function getAgeByBirth($birthday)
{
$birth_year = date('Y', strtotime($birthday));
$birth_month = date('m', strtotime($birthday));
$birth_day = date('d', strtotime($birthday));
if (!$birthday || $birthday == '0000-00-00') {
return '';
}
if (empty($birth_year) || empty($birth_month) || empty($birth_day)) {
return '';
}
$current_year = date('Y', time());
$current_month = date('m', time());
$current_day = date('d', time());
if ($birth_year >= $current_year) {
return 0;
}
$age = $current_year - $birth_year - 1;
if ($current_month > $birth_month) {
return $age + 1;
} else if ($current_month == $birth_month && $current_day >= $birth_day) {
return $age + 1;
} else {
return $age;
}
}
}