Files
spacestation/admin/controllers/autohome/Customer.php
T
xiaoyu e3237532fe area
2025-03-08 16:37:23 +08:00

496 lines
18 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Customer extends HD_Controller
{
private $cfrom = [1 => '空间站', 2 => '车卖场'];
private $cfrom2 = [11 => '厦门', 12 => '海口', 13 => '长沙', 14 => '佛山', 15 => '贵阳'];
//private $channel = [1 => '抖音直播', 2 => '其他'];
private $push_status = ['0' => '未上传', '-1' => '上传失败'];
private $dis_status = [0 => '未分发', 2 => '分发失败'];
private $pushAppKey = '0d14a33984239fd744fa47719de5f916';
private $keySupplyBusinessId = '67';
private $keyOutsidePvareaidId = '303556333';
private $keyExt5 = '10477';
private $channel = array(
'10477' => '初始渠道',
'10489' => '厦门-郑子华',
'10490' => '厦门-魏思雨',
'10491' => '厦门-蔡文彬',
'10492' => '厦门-李怀松',
'10493' => '厦门-哪吒',
'10494' => '厦门-古惟天',
'10495' => '厦门-7',
'10496' => '厦门-8',
'10497' => '厦门-9',
'10498' => '厦门-10',
'10499' => '长沙-1',
'10500' => '长沙-2',
'10501' => '长沙-3',
'10502' => '长沙-4',
'10503' => '长沙-5',
'10504' => '海口-1',
'10505' => '海口-2',
'10506' => '海口-3',
'10507' => '佛山-1',
'10508' => '佛山-2',
'10509' => '佛山-3',
'10510' => '贵阳-1',
'10511' => '贵阳-2',
'10512' => '贵阳-3',
);
public function __construct()
{
parent::__construct();
$this->load->model('autohome/autohome_customer_model');
$this->load->model("area_model");
$this->load->model("biz/biz_model");
$this->load->model("auto/auto_brand_model");
$this->load->model("auto/auto_series_model");
$this->load->helper('order');
$this->load->library('mycurl');
}
public function index()
{
$params = $this->input->get();
list($lists, $count) = $this->lists();
$this->data['province_id'] = intval($params['province_id']);
$this->data['city_id'] = intval($params['city_id']);
$this->data['provinces'] = $this->province_ary();
$this->data['lists'] = $lists;
$this->data['params'] = $params;
$this->data['cfromAry'] = $this->cfrom;
$this->data['channelAry'] = $this->channel;
$this->data['export_button'] = SUPER_ADMIN == $this->role ? 1 : 0;
$this->data['_title'] = '线索列表';
return $this->show_view('autohome/lists', true);
}
public function lists($push = false)
{
$params = $this->input->get();
$where = [];
$page = $params['page'] = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] = $params['size'] ? intval($params['size']) : 30;
if (strlen($params['push_id'])) {
switch ($params['push_id']){
case '0':
$where["push_id"] = 0;
break;
case '-1':
$where["push_id"] = -1;
break;
default:
$where["push_id not in ('0', '-1')"] = null;
}
}
if ($params['mobile']) {
$where["mobile like '{$params['mobile']}'"] = null;
}
//创建时间
if ($params['c_time']) {
$c_time = explode(' ~ ', $params['c_time']);
if ($c_time[0]) {
$where["c_time >="] = $c_time[0] . ' 00:00:00';
}
if ($c_time[1]) {
$where["c_time <="] = $c_time[1] . ' 23:59:59';
}
}
//上传时间
if ($params['p_time']) {
$p_time = explode(' ~ ', $params['p_time']);
if ($p_time[0]) {
$where["p_time >="] = $p_time[0] . ' 00:00:00';
}
if ($p_time[1]) {
$where["p_time <="] = $p_time[1] . ' 23:59:59';
}
}
$province_id = intval($params['province_id']);
$city_id = intval($params['city_id']);
$province_id && $where['province_id'] = $province_id;
$city_id && $where['city_id'] = $city_id;
$params['channel'] && $where['channel'] = $params['channel'];
$params['cfrom_id'] && $where['cfrom'] = $params['cfrom_id'];
$params['cfrom_id2'] && $where['cfrom2'] = $params['cfrom_id2'];
$params['brand_id'] && $where['brand_id'] = $params['brand_id'];
$params['series_id'] && $where['series_id'] = $params['series_id'];
$count = $this->autohome_customer_model->count($where);
$lists = [];
if ($count) {
$rows = $this->autohome_customer_model->select($where, 'id desc', $page, $size);
$city_id_arr = array_filter(array_unique(array_column($rows, 'city_id')));
if ($city_id_arr) {
$str_ids = implode(',', $city_id_arr);
$map_area_city = $this->area_model->map('city_id', '', ["city_id in ({$str_ids})" => null], '', 0, $size, 'city_id,province_name,city_name');
}
$brands = $this->auto_brand_model->get_map_by_ids(array_column($rows, 'brand_id'));
$series = $this->auto_series_model->get_map_by_ids(array_column($rows, 'series_id'));
$map_biz = $this->biz_model->map('biz_name', 'id', ['status' => 1, 'province_id' => 350000], '', 0, 0, 'id,biz_name');
foreach ($rows as $val){
$area = $map_area_city[$val['city_id']][0];
$val['area_title'] = "{$area['province_name']}-{$area['city_name']}";
$val['car_title'] = $brands[$val['brand_id']][0]['name']." ".$series[$val['series_id']][0]['name'];
if($val['cfrom'] == 1){
$cf2 = $this->cfrom2[$val['cfrom2']];
}else{
$cf2 = $map_biz[$val['cfrom2']];
}
$val['cf_title'] = $this->cfrom[$val['cfrom']]." ".$cf2;
$val['channel_id'] = $val['channel'];
$val['channel'] = $this->channel[$val['channel']];
$val['push_status'] = $this->push_status[$val['push_id']] ? $this->push_status[$val['push_id']] : '上传成功';
$val['dis_status'] = $this->dis_status[$val['dis_status']];
$lists[] = $val;
}
}
$this->data['pager'] = array('count' => ceil($count / $params['size']), 'curr' => $params['page'], 'totle' => $count);
return [$lists, $count];
}
public function get()
{
$id = intval($this->input->get('id'));
$info = ['province_id' => 0];
if ($id) {
$info = $this->item_model->get(['id' => $id]);
$info['src_img'] = build_qiniu_image_url($info['img']);
}
$this->data['provinces'] = $this->province_ary();
$this->data['info'] = $info;
$this->data['_title'] = $id ? '编辑' : '新增';
return $this->show_view('/receiver/items/edit', true);
}
public function add()
{
$info = $this->input->post();
$data = [
'title' => $info['title'],
's_time' => $info['s_time'],
'e_time' => $info['e_time'],
'img' => $info['img'],
'province_id' => $info['province_id'],
'descrip' => $info['descrip'],
];
$res = $this->item_model->add($data);
if (!$res) {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
//编辑单条数据
public function edit()
{
$info = $this->input->post();
$row = $this->item_model->get(['id' => $info['id']]);
if (!$row) return $this->show_json(SYS_CODE_FAIL, '数据不存在');
$up_data = [
'title' => $info['title'],
's_time' => $info['s_time'],
'e_time' => $info['e_time'],
'img' => $info['img'],
'province_id' => $info['province_id'],
'descrip' => $info['descrip'],
];
$res = $this->item_model->update($up_data, ['id' => $info['id']]);
if (!$res) {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
public function del()
{
}
public function batch()
{
}
function add_excel()
{
require_once COMMPATH . '/third_party/PHPExcel/IOFactory.php';
$res = $this->upload();
if (!$res['code']) {
return $this->show_json(0, $res['message']);
}
$file = $res['path'];
if ($res['file_ext'] == '.xls') {
$reader = \PHPExcel_IOFactory::createReader('Excel5'); // 读取 excel 文档
} elseif ($res['file_ext'] == '.xlsx') {
$reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档
} else {
return $this->show_json(SYS_CODE_FAIL, '文件无法识别');
}
$PHPExcel = $reader->load($file); // 文档名称
$objWorksheet = $PHPExcel->getActiveSheet();
$rowCnt = $objWorksheet->getHighestRow(); //获取总行数
if ($rowCnt > 800) {
@unlink($file);
return $this->show_json(0, '数据大于800请拆分多个表格导入');
}
$data = array();
for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容
$data[] = array(
'name' => $objWorksheet->getCell('A' . $_row)->getValue(),
'mobile' => $objWorksheet->getCell('B' . $_row)->getValue(),
'cfrom1' => $objWorksheet->getCell('C' . $_row)->getValue(),
'cfrom2' => $objWorksheet->getCell('D' . $_row)->getValue(),
'channel' => $objWorksheet->getCell('E' . $_row)->getValue(),
'province_name' => $objWorksheet->getCell('F' . $_row)->getValue(),
'city_name' => $objWorksheet->getCell('G' . $_row)->getValue(),
'brand_id' => $objWorksheet->getCell('H' . $_row)->getValue(),
'series_id' => $objWorksheet->getCell('I' . $_row)->getValue(),
);
}
$done = $this->add_batch($data);
@unlink($file);
$this->data['load_num'] = count($data);
$this->data['done'] = $done;
return $this->show_json(SYS_CODE_SUCCESS, "成功导入{$done}");
}
public function add_batch($lists=array())
{
$done = 0;
$adds = $map_province = $map_city = $map_county = $map_biz = [];
$cfrom_f = array_flip($this->cfrom);
$cfrom2_f = array_flip($this->cfrom2);
$map_biz = $this->biz_model->map('biz_name', 'id', ['status' => 1, 'province_id' => 350000], '', 0, 0, 'id,biz_name');
$fails = array();
foreach ($lists as $k => $v) {
if (!$v['name']) {
$fails[] = array('data' => $v, 'msg' => 'name is null');
continue;
}
if (!mobile_valid($v['mobile'])) {
$fails[] = array('data' => $v, 'msg' => 'mobile valid fail');
continue;
}
if ($this->autohome_customer_model->count(['mobile' => $v['mobile']])) {
$fails[] = array('data' => $v, 'msg' => 'mobile duplicate');
continue;
}
$cf_id = $cfrom_f[$v['cfrom1']];
if($cf_id == 1){
$cf2_id = $cfrom2_f[$v['cfrom2']];
}else{
$cf2_id = $map_biz[$v['cfrom2']];
}
$province_name = $v['province_name'];
$city_name = $v['city_name'];
if($city_name){
$city = $this->area_model->get(["city_name like '%$city_name'" => null]);
if(!$city){
$city = $this->area_model->get(["county_name like '%$city_name'" => null]);
}
$city_id = $city['city_id'];
$province_id = $city['province_id'];
}
if(!$province_id){
$province = $this->area_model->get(["province_name like '%$province_name'" => null]);
$province_id = $province['province_id'];
}
$temp = array(
'sid' => create_customer_no(),
'name' => $v['name'],
'mobile' => $v['mobile'],
'cfrom' => $cf_id ? $cf_id : 0,
'cfrom2' => $cf2_id ? $cf2_id : 0,
'channel' => $v['channel'] ? $v['channel'] : '',
'brand_id' => $v['brand_id'] ? $v['brand_id'] : '',
'series_id' => $v['series_id'] ? $v['series_id'] : '',
'province_id' => $province_id,
'city_id' => $city_id ? $city_id : 0,
'c_time' => date('Y-m-d H:i:s'),
);
switch($v['city_name']){
case '北京市':
$temp['province_id'] = 110000;
$temp['city_id'] = 110100;
break;
case '天津市':
$temp['province_id'] = 120000;
$temp['city_id'] = 120100;
break;
case '重庆市':
$temp['province_id'] =500000;
$temp['city_id'] = 500100;
break;
case '上海市':
$temp['province_id'] = 310000;
$temp['city_id'] = 310100;
break;
default:
break;
}
$adds[] = $temp;
$done++;
}
$fails && debug_log('[warning]# fails=' . json_encode($fails), __FUNCTION__, $this->log_dir);
if ($adds) {
$ret = $this->autohome_customer_model->add_batch($adds);
if (!$ret) {
debug_log("[error] add_batch fail; " . $this->autohome_customer_model->db->last_query(), __FUNCTION__, $this->log_dir);
$done = 0;
}
}
return $done;
}
private function upload()
{
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/temp/';
$config['allowed_types'] = 'xls|xlsx';
$config['max_size'] = 5120;
$config['file_name'] = 'receiver_clues' . time() . rand(1, 99999);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
return array('code' => SYS_CODE_FAIL, 'message' => $this->upload->display_errors('', ''));
} else {
$data = $this->upload->data();
return array('code' => SYS_CODE_SUCCESS, 'path' => $data['full_path'], 'file_ext' => $data['file_ext']);
}
}
public function export()
{
}
function json_map_cfrom()
{
$pid = $this->input->post('pid');
$map_biz = $this->biz_model->map('biz_name', 'id', ['status' => 1, 'province_id' => 350000, 'type' => 0], '', 0, 0, 'id,biz_name');
if($pid == 1){
$this->data = $this->cfrom2;
}else{
$this->data = array_flip($map_biz);
}
return $this->show_json(SYS_CODE_SUCCESS);
}
public function push_search(){
$url = 'https://autoapi.autohome.com.cn/ggxt/xsyw/api/clues/push';
list($lists, $count) = $this->lists(1);
$succ = $fail = $undo = 0;
if($lists){
foreach ($lists as $v){
if($v['push_id']){
$undo++;
continue;
}
// if($v['brand_id']){
// $brand = $this->auto_brand_model->get(['id' => $v['brand_id']]);
// $brand_id = $brand['ah_id'];
// }
//
// if($v['series_id']){
// $series = $this->auto_series_model->get(['id' => $v['series_id']]);
// $series_id = $series['ah_id'];
// }
$push_data = array(
'pushAppKey' => $this->pushAppKey,
'keySupplyBusinessId' => $this->keySupplyBusinessId,
'keyOutsidePvareaidId' => $this->keyOutsidePvareaidId,
'keyExt5' => $v['channel_id'],
'keyExt12' => addslashes(json_encode(array('flowOrderNo' => $v['sid']))),
'splitCode' => '123',
'keyPrivateFlag' => '0',
'keyTypeId' => '2',
'keyOrderTime' => $v['c_time'],
'keyOrderCityId' => $v['city_id'] ? $v['city_id'] : '110100',
'keyName' => '客户'.$v['id'],
'keyPhone' => $v['mobile'],
'keyPurposeBrandId' => $v['brand_id'] ? $v['brand_id'] : null,
'keyPurposeFactoryId' => null,
'keyCarAudiId' => $v['series_id'] ? $v['series_id'] : null,
'keyCarTypeId' => null,
'keyCardCityId' => $v['city_id'] ? $v['city_id'] : '110100',
'keyNewCarRentSupplierId' => 10
);
$res = $this->mycurl->httpPost($url, $push_data, 'is_json');
if($res){
$res = json_decode($res, true);
if(!$res['status']){
$up = array(
'push_json' => json_encode($push_data),
'push_id' => $res['result']['pushId'],
'p_time' => date('Y-m-d H:i:s'),
);
$succ++;
}else{
$push_data['res'] = $res;
$up = array(
'push_json' => json_encode($push_data),
'push_id' => -1,
'p_time' => date('Y-m-d H:i:s'),
);
$fail++;
}
$this->autohome_customer_model->update($up, array('id' => $v['id']));
}
}
$this->data['succ'] = $succ;
$this->data['fail'] = $fail;
$this->data['undo'] = $undo;
return $this->show_json(SYS_CODE_SUCCESS);
}
}
public function get_dis_status(){
$url = 'https://autoapi.autohome.com.cn/pj-clues-handler/api/queryCluesInfo';
$push_data = array(
'appKey' => $this->pushAppKey,
'keySupplyBusinessId' => $this->keySupplyBusinessId,
'pushIds' => '1728438233720721408',
);
$res = $this->mycurl->httpPost($url, $push_data, 'is_json');
if($res){
print_r(json_encode($push_data));
}
}
}