123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\service\queue;
- use think\console\Input;
- use think\console\Output;
- use think\Db;
- /**
- * Class Jobs
- */
- class ImportTable2Queue
- {
- /**
- * 当前类名
- * @var string
- */
- const URI = self::class;
- /**
- * 执行任务
- * @param Input $input
- * @param Output $output
- * @param array $data
- * @throws \think\exception\PDOException
- */
- public function execute(Input $input, Output $output, array $data = [])
- {
- try {
- set_time_limit(0);
- ini_set ("memory_limit","-1");
- $info = Db::name('system_queue')
- ->where('type',$data['type'])
- ->where('import_name',$data['import_name'])
- ->order('id desc')
- ->limit(1)
- ->find();
- $import_path = $info['local_url'];
- $inputFileType = \PHPExcel_IOFactory::identify($import_path);
- $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
- $worksheetNames = $objReader->listWorksheetNames($import_path);
- #只读取表格数据,忽略里面的各种格式,否则会内存耗尽
- $objReader->setReadDataOnly(TRUE);
- $objReader->setLoadSheetsOnly($worksheetNames[0]);#笔者此处加载第11个sheet
- $objPHPExcels = $objReader->load($import_path);
- $maxCol = $objPHPExcels->getSheet(0)->getHighestColumn();#总列数
- $maxRow = $objPHPExcels->getSheet(0)->getHighestRow();#总行数
- Db::name('system_queue')->where('id',$info['id'])->update(['maxrow'=>($maxRow-1)]);
- $kd_array = Db::name('system_kd')->column('name');
- for ($i = 2; $i <= $maxRow; $i++) {
- $a = [];
- $infos = $objPHPExcels->getSheet(0)->rangeToArray('A' . $i . ':' . $maxCol . $i)[0];#读取一行
- if (gettype($infos[17])=='double'){
- $infos[17] = gmdate("Y-m-d H:i:s", \PHPExcel_Shared_Date::ExcelToPHP($infos[17]));
- }
- $date = date('Y-m-d',strtotime($infos[17]));
- if ($i==2){
- Db::name('system_queue')->where('id',$info['id'])->update(['date'=>$date]);
- $is = Db::name('system_table2')->where('date',$date)->count();
- if ($is){
- Db::name('system_queue')->where('id',$info['id'])->update(
- [
- 'status' => 4, 'end_at' => date('Y-m-d H:i:s'), 'desc' => '表中日期'.$date.'数据已存在,无法导入',
- ]
- );
- die;
- }
- }
- if (!in_array($infos[1],$kd_array)){
- if (!empty($infos[1])){
- array_push($kd_array,$infos[1]);
- Db::name('system_kd')->insert(['name'=>$infos[1]]);
- }
- }
- $a['import_log_id'] = $info['id'];
- $a['name'] = $infos[1];
- $a['dm'] = $infos[5];
- $a['gb_time'] = $infos[17];
- $a['date'] = $date;
- Db::name('system_table2')->insert($a);
- Db::name('system_queue')->where('id',$info['id'])->setInc('deal_count');
- }
- $names = Db::name('system_table2')->where('import_log_id',$info['id'])->where('name','neq','')->group('name')->column('name');
- if ($names){
- $insert_array = array();
- foreach ($names as &$v){
- $b = array();
- $b['import_log_id'] = $info['id'];
- $b['name'] = $v;
- $b['value'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('name',$v)->count();
- $b['date'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('name',$v)->order('id asc')
- ->limit(1)->value('date');
- array_push($insert_array,$b);
- }
- Db::name('system_values2')->insertAll($insert_array);
- }
- $dms = Db::name('system_table2')->where('import_log_id',$info['id'])->whereNotNull('dm')->group('dm')->column('dm');
- if ($dms){
- $insertarray = array();
- foreach ($dms as &$v){
- $c = array();
- $c['import_log_id'] = $info['id'];
- $c['name'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('dm',$v)->value('name');
- $c['dm'] = $v;
- $c['value'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('dm',$v)->count();
- $c['date'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('dm',$v)->order('id asc')->limit(1)->value('date');
- array_push($insertarray,$c);
- }
- Db::name('system_values3')->insertAll($insertarray);
- }
- }catch (\think\exception\ValidateException $e) {
- file_put_contents("error2.txt", $e. "\n" . "\n", FILE_APPEND);
- }
- }
- }
|