ImportTable2beifenQueue.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\service\queue;
  15. use think\console\Input;
  16. use think\console\Output;
  17. use think\Db;
  18. /**
  19. * Class Jobs
  20. */
  21. class ImportTable2Queue
  22. {
  23. /**
  24. * 当前类名
  25. * @var string
  26. */
  27. const URI = self::class;
  28. /**
  29. * 执行任务
  30. * @param Input $input
  31. * @param Output $output
  32. * @param array $data
  33. * @throws \think\exception\PDOException
  34. */
  35. public function execute(Input $input, Output $output, array $data = [])
  36. {
  37. try {
  38. set_time_limit(0);
  39. ini_set ("memory_limit","-1");
  40. $info = Db::name('system_queue')
  41. ->where('type',$data['type'])
  42. ->where('import_name',$data['import_name'])
  43. ->order('id desc')
  44. ->limit(1)
  45. ->find();
  46. $import_path = $info['local_url'];
  47. $inputFileType = \PHPExcel_IOFactory::identify($import_path);
  48. $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
  49. $worksheetNames = $objReader->listWorksheetNames($import_path);
  50. #只读取表格数据,忽略里面的各种格式,否则会内存耗尽
  51. $objReader->setReadDataOnly(TRUE);
  52. $objReader->setLoadSheetsOnly($worksheetNames[0]);#笔者此处加载第11个sheet
  53. $objPHPExcels = $objReader->load($import_path);
  54. $maxCol = $objPHPExcels->getSheet(0)->getHighestColumn();#总列数
  55. $maxRow = $objPHPExcels->getSheet(0)->getHighestRow();#总行数
  56. Db::name('system_queue')->where('id',$info['id'])->update(['maxrow'=>($maxRow-1)]);
  57. $kd_array = Db::name('system_kd')->column('name');
  58. for ($i = 2; $i <= $maxRow; $i++) {
  59. $a = [];
  60. $infos = $objPHPExcels->getSheet(0)->rangeToArray('A' . $i . ':' . $maxCol . $i)[0];#读取一行
  61. if (gettype($infos[17])=='double'){
  62. $infos[17] = gmdate("Y-m-d H:i:s", \PHPExcel_Shared_Date::ExcelToPHP($infos[17]));
  63. }
  64. $date = date('Y-m-d',strtotime($infos[17]));
  65. if ($i==2){
  66. Db::name('system_queue')->where('id',$info['id'])->update(['date'=>$date]);
  67. $is = Db::name('system_table2')->where('date',$date)->count();
  68. if ($is){
  69. Db::name('system_queue')->where('id',$info['id'])->update(
  70. [
  71. 'status' => 4, 'end_at' => date('Y-m-d H:i:s'), 'desc' => '表中日期'.$date.'数据已存在,无法导入',
  72. ]
  73. );
  74. die;
  75. }
  76. }
  77. if (!in_array($infos[1],$kd_array)){
  78. if (!empty($infos[1])){
  79. array_push($kd_array,$infos[1]);
  80. Db::name('system_kd')->insert(['name'=>$infos[1]]);
  81. }
  82. }
  83. $a['import_log_id'] = $info['id'];
  84. $a['name'] = $infos[1];
  85. $a['dm'] = $infos[5];
  86. $a['gb_time'] = $infos[17];
  87. $a['date'] = $date;
  88. Db::name('system_table2')->insert($a);
  89. Db::name('system_queue')->where('id',$info['id'])->setInc('deal_count');
  90. }
  91. $names = Db::name('system_table2')->where('import_log_id',$info['id'])->where('name','neq','')->group('name')->column('name');
  92. if ($names){
  93. $insert_array = array();
  94. foreach ($names as &$v){
  95. $b = array();
  96. $b['import_log_id'] = $info['id'];
  97. $b['name'] = $v;
  98. $b['value'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('name',$v)->count();
  99. $b['date'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('name',$v)->order('id asc')
  100. ->limit(1)->value('date');
  101. array_push($insert_array,$b);
  102. }
  103. Db::name('system_values2')->insertAll($insert_array);
  104. }
  105. $dms = Db::name('system_table2')->where('import_log_id',$info['id'])->whereNotNull('dm')->group('dm')->column('dm');
  106. if ($dms){
  107. $insertarray = array();
  108. foreach ($dms as &$v){
  109. $c = array();
  110. $c['import_log_id'] = $info['id'];
  111. $c['name'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('dm',$v)->value('name');
  112. $c['dm'] = $v;
  113. $c['value'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('dm',$v)->count();
  114. $c['date'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('dm',$v)->order('id asc')->limit(1)->value('date');
  115. array_push($insertarray,$c);
  116. }
  117. Db::name('system_values3')->insertAll($insertarray);
  118. }
  119. }catch (\think\exception\ValidateException $e) {
  120. file_put_contents("error2.txt", $e. "\n" . "\n", FILE_APPEND);
  121. }
  122. }
  123. }