|
@@ -1,6 +1,7 @@
|
|
|
<?php
|
|
|
|
|
|
namespace app\admin\controller;
|
|
|
+use app\admin\model\CustomerModel;
|
|
|
use think\Controller;
|
|
|
use think\File;
|
|
|
use think\Request;
|
|
@@ -402,4 +403,265 @@ class Upload extends Base
|
|
|
//单文件上传
|
|
|
}
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 文件导入
|
|
|
+ */
|
|
|
+ public function fileUpload()
|
|
|
+ {
|
|
|
+ set_time_limit(6000);
|
|
|
+ vendor("PHPExcel.PHPExcel");
|
|
|
+ $objPHPExcel = new \PHPExcel();
|
|
|
+ //获取表单上传文件
|
|
|
+ $file = request()->file('file');
|
|
|
+ $info = $file->validate(['ext' => 'xls'])->move(ROOT_PATH . 'public'); //上传验证后缀名,以及上传之后移动的地址 E:\wamp\www\bick\public
|
|
|
+ if ($info) {
|
|
|
+ $exclePath = $info->getSaveName(); //获取文件名
|
|
|
+ $file_name = ROOT_PATH . 'public' . DS . $exclePath;//上传文件的地址
|
|
|
+ $inputFileType = \PHPExcel_IOFactory::identify($file_name);
|
|
|
+ $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
|
|
|
+ $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
|
|
|
+ $excel_array = $obj_PHPExcel->getSheet(0)->toArray(); //转换为数组格式
|
|
|
+ array_shift($excel_array); //删除第一个数组(标题);
|
|
|
+ $success = [];
|
|
|
+ $error = [];
|
|
|
+
|
|
|
+ $num['error_num'] = 0;
|
|
|
+ $uni = time() . rand(0, 1000);
|
|
|
+// $serialize = serialize($excel_array);
|
|
|
+//
|
|
|
+// Db::name('serialize')->insert(['text'=>$serialize,'uni' => $uni,]);die;
|
|
|
+ foreach ($excel_array as $k => $v) {
|
|
|
+ $status = 1; // 为1标识此行正常
|
|
|
+ // 判断邮件
|
|
|
+ if (!empty($v[2])) {
|
|
|
+ $checkEmail = Db::name('customer')->where('email',$v[2])->where('spread_id',session('uid'))->find();
|
|
|
+ if ($checkEmail) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '此客户邮箱已存在',
|
|
|
+ 'content' => $v[2],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ } else {
|
|
|
+ if (!filter_var($v[2], FILTER_VALIDATE_EMAIL)) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '邮箱格式不正确',
|
|
|
+ 'content' => $v[2],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $customer_chengk = new CustomerModel();
|
|
|
+ if ($customer_chengk->checkCustomer($v[2],$v[1],$v[3])) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '此客户已存在其他状态',
|
|
|
+ 'content' => $v[2],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ }
|
|
|
+ if (!empty($v[2])) {
|
|
|
+ $ckeckWebsiteCompany = Db::name('customer')->where('website','like','%'.$v[3].'%')->where('status',0)->where('email','')->find();
|
|
|
+ if ($ckeckWebsiteCompany) {
|
|
|
+ Db::name('customer')->where('id',$ckeckWebsiteCompany['id'])->update(['spread_id'=>session('uid'),'emial' => $v[2]]);
|
|
|
+ Db::name('customer')->where('emial',$v[2])->setInc('people',1);
|
|
|
+ Db::name('ranking')->where('c_id',$ckeckWebsiteCompany['id'])->update([['admin_id' => session('uid')]]);
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '已存在此公司,网站客户,直接更新,不计入添加次数',
|
|
|
+ 'content' => "已存在此公司,网站客户,直接更新,不计入添加次数",
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 判断网站
|
|
|
+ if (empty($v[3])) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '网站不能为空',
|
|
|
+ 'content' => $v[3],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ } else {
|
|
|
+ $website =explode(chr(10), $v[3]);
|
|
|
+ $v[3] = implode("##",$website);
|
|
|
+ }
|
|
|
+ // 判断评分
|
|
|
+ if ($v[4] == '') {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '评分不能为空',
|
|
|
+ 'content' => $v[4],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ } else {
|
|
|
+ // 判断评分
|
|
|
+ if (!is_numeric($v[4])) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '评分必须为整数',
|
|
|
+ 'content' => $v[4],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断国家
|
|
|
+ $country = Db::name('countries')->where('country', $v[5])->find();
|
|
|
+ if (!$country) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '国家名称不正确',
|
|
|
+ 'content' => $v[5],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ }
|
|
|
+ $v[5] = $country['id'];
|
|
|
+
|
|
|
+ // 判断行业
|
|
|
+ if (empty($v[7])) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '行业不能为空',
|
|
|
+ 'content' => $v[7],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $tapeNanme = explode('&',$v[7]);
|
|
|
+ foreach ($tapeNanme as $vo) {
|
|
|
+ $type = Db::name('type d')
|
|
|
+ ->join('admin a','a.id = d.admin_id')
|
|
|
+ ->where('d.name', $vo)
|
|
|
+ ->where(function ($query) {
|
|
|
+ $query->whereOr('d.admin_id',session('uid'));
|
|
|
+ $query->whereOr('a.groupid','3');
|
|
|
+ $query->whereOr('a.groupid','5');
|
|
|
+ })
|
|
|
+ ->field('d.name,d.id')
|
|
|
+ ->find();
|
|
|
+ if (!$type) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '未设置此行业名称-'.$vo,
|
|
|
+ 'content' => $v[7],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ } else {
|
|
|
+ $typdIds[] = $type['id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $v[7] = ','.implode(',',$typdIds).','; // 改为id
|
|
|
+ unset($typdIds);
|
|
|
+ // 主营产品
|
|
|
+ if (empty($v[8])) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '主营产品不能为空',
|
|
|
+ 'content' => $v[8],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $productName = explode('&',$v[8]);
|
|
|
+ foreach ($productName as $vo) {
|
|
|
+ $produc = Db::name('product d')
|
|
|
+ ->join('admin a','d.admin_id=a.id')
|
|
|
+ ->where('d.product_name', $vo)
|
|
|
+ ->where(function ($query) {
|
|
|
+ $query->whereOr('d.admin_id',session('uid'));
|
|
|
+ $query->whereOr('a.groupid','3');
|
|
|
+ $query->whereOr('a.groupid','5');
|
|
|
+ })
|
|
|
+ ->field('d.product_name,d.id')
|
|
|
+ ->find();
|
|
|
+ if (!$produc) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '未设置此主营产品名称-'.$vo,
|
|
|
+ 'content' => $v[8],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ } else {
|
|
|
+ $producIds[] = $produc['id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $v[8] = ','.implode(',',$producIds).','; // 改为id
|
|
|
+ unset($producIds);
|
|
|
+ $where = [
|
|
|
+ 'email' => $v[2],
|
|
|
+ 'company' => $v[1],
|
|
|
+ 'website' => $v[3],
|
|
|
+ 'spread_id' => session('uid'),
|
|
|
+ ];
|
|
|
+ $isset = Db::name('customer')->where($where)->find();
|
|
|
+ if ($isset) {
|
|
|
+ $error[] = [
|
|
|
+ 'rows' => $k,
|
|
|
+ 'msg' => '此客户已经存在已经自动过滤',
|
|
|
+ 'content' => $v[0],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $status = 0; // 设置为0标识此行有错误
|
|
|
+ }
|
|
|
+ if ($status == 1) {
|
|
|
+ $success[] = [
|
|
|
+ 'name' => $v[0],
|
|
|
+ 'company' => $v[1],
|
|
|
+ 'email' => $v[2],
|
|
|
+ 'website' => $v[3],
|
|
|
+ 'grade' => $v[4],
|
|
|
+ 'country' => $v[5],
|
|
|
+ 'phone' => $v[6],
|
|
|
+ 'type' => $v[7],
|
|
|
+ 'product' => $v[8],
|
|
|
+ 'spread_id' => session('uid'),
|
|
|
+ 'follow_time' => time(),
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ $import_success[] = [
|
|
|
+ 'name' => $v[0],
|
|
|
+ 'email' => $v[2],
|
|
|
+ 'uni' => $uni,
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $num['error_num'] = $num['error_num'] + 1; // 记录失败条数
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var_dump($success);die;
|
|
|
+ $num['success_num'] = count($success);
|
|
|
+ $num['zong'] = count($excel_array);
|
|
|
+ if ($num['success_num'] > 0) {
|
|
|
+ $serializeSuccess = [
|
|
|
+ 'uni' => $uni,
|
|
|
+ 'type' => 2,
|
|
|
+ 'text' => serialize($success),
|
|
|
+ ];
|
|
|
+ Db::name('serialize')->insert($serializeSuccess);
|
|
|
+ die;
|
|
|
+ $customer = new CustomerModel();
|
|
|
+// $customer->add_all($success); // 保存处理所有数据;
|
|
|
+// die;
|
|
|
+// Db::name("import_success")->insertAll($import_success);
|
|
|
+ }
|
|
|
+ if ($num['error_num'] > 0) {
|
|
|
+// Db::name('import_error')->insertAll($error);
|
|
|
+ }
|
|
|
+ return json(['code' => 200, 'uni' => $uni, 'msg' => '完成导入,正在查询状态', 'success_num' => $num['success_num'], 'error_num' => $num['error_num']]);
|
|
|
+ } else {
|
|
|
+ echo $file->getError();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|