123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?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\exception\HttpResponseException;
- use think\Db;
- /**
- * Class Jobs
- */
- class ImportTable1Queue
- {
- /**
- * 当前类名
- * @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'];
- $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
- $objExcel = $objReader->load($import_path);
- $maxRow = $objExcel->getSheet(0)->getHighestRow();#总行数
- Db::name('system_queue')->where('id',$info['id'])->update(['maxrow'=>($maxRow-1)]);
- $list = $objExcel->getActiveSheet()->toArray();
- $dates = array_filter(array_unique(array_column($list,'0')));
- if (count($dates)>2){
- Db::name('system_queue')->where('id',$info['id'])->update(
- [
- 'status' => 4, 'end_at' => date('Y-m-d H:i:s'), 'desc' => '表中存在多个日期,导入失败',
- ]
- );
- die();
- }
- $datee = date('Y-m-d',strtotime($dates[1]));
- Db::name('system_queue')->where('id',$info['id'])->update(['date'=>$datee]);
- $infos = Db::name('system_values')->where('date',$datee)->count();
- if ($infos){
- Db::name('system_queue')->where('id',$info['id'])->update(
- [
- 'status' => 4, 'end_at' => date('Y-m-d H:i:s'), 'desc' => '表中日期'.$datee.'数据已存在,无法导入',
- ]
- );
- die();
- }
- $array = array();
- $kd_array = Db::name('system_kd')->column('name');
- foreach ($list as $k=>$v){
- if ($k==0){
- for ($i=9;$i<count($v);$i++){
- $name = mb_substr($v[$i],0,2,"utf-8"); //截取前两个汉字
- $array[$i] = $name;
- if (!in_array($name,$kd_array)){
- if (!empty($name)){
- array_push($kd_array,$name);
- Db::name('system_kd')->insert(['name'=>$name]);
- }
- }
- }
- }else{
- if (!$v[0]){
- continue;
- }
- $date = date('Y-m-d',strtotime($v[0]));
- $insert_array = array();
- $site_id = Db::name('system_site')->where('code',$v[6])->value('id');
- if (!$site_id){
- $site_id = Db::name('system_site')->insertGetId(
- [
- 'code'=>$v[6],
- 'name'=>$v[7]
- ]
- );
- }
- for ($i=8;$i<count($v);$i++){
- $a = array();
- $a['site_id'] = $site_id;
- $a['date'] = $date;
- $a['import_log_id'] = $info['id'];
- $a['name'] = $i==8 ? "总签收" : $array[$i];
- $a['value'] = $v[$i];
- array_push($insert_array,$a);
- }
- Db::name('system_values')->insertAll($insert_array);
- }
- }
- Db::name('system_values')->where('name','eq','')->delete();
- $sql = "SELECT site_id FROM system_values WHERE import_log_id=".$info['id']." GROUP BY site_id";
- $deal_count = count(Db::query($sql));
- Db::name('system_queue')->where('id',$info['id'])->update(['deal_count'=>$deal_count]);
- }catch (\think\exception\ValidateException $e) {
- file_put_contents("error2.txt", $e. "\n" . "\n", FILE_APPEND);
- }
- }
- }
|