ImportTable2Queue.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $objPHPExcel= \PHPExcel_IOFactory::load($import_path);
  48. $currentSheet=$objPHPExcel->getSheet(0);//当前页
  49. //$maxCol = $objPHPExcel->getSheet(0)->getHighestColumn();#总列数
  50. $maxRow = $objPHPExcel->getSheet(0)->getHighestRow();#总行数
  51. Db::name('system_queue')->where('id',$info['id'])->update(['maxrow'=>($maxRow-1)]);
  52. $kd_array = Db::name('system_kd')->column('name');
  53. //循环从第二行开始,第一行往往是表头
  54. $insert_array = array();
  55. $count = 0;
  56. for($i=2;$i<=$maxRow;$i++)
  57. {
  58. $a = [];
  59. $name =$currentSheet->getCell("B".$i)->getFormattedValue();
  60. $dm=$currentSheet->getCell("F".$i)->getFormattedValue();
  61. $gb_time=$currentSheet->getCell("R".$i)->getFormattedValue();
  62. if (gettype($gb_time)=='double'){
  63. $gb_time = gmdate("Y-m-d H:i:s", \PHPExcel_Shared_Date::ExcelToPHP($gb_time));
  64. }
  65. $date = date('Y-m-d',strtotime($gb_time));
  66. if ($i==2){
  67. Db::name('system_queue')->where('id',$info['id'])->update(['date'=>$date]);
  68. $is = Db::name('system_table2')->where('date',$date)->count();
  69. if ($is){
  70. Db::name('system_queue')->where('id',$info['id'])->update(
  71. [
  72. 'status' => 4, 'end_at' => date('Y-m-d H:i:s'), 'desc' => '表中日期'.$date.'数据已存在,无法导入',
  73. ]
  74. );
  75. die;
  76. }
  77. }
  78. if (!in_array($name,$kd_array)){
  79. if (!empty($name)){
  80. array_push($kd_array,$name);
  81. Db::name('system_kd')->insert(['name'=>$name]);
  82. }
  83. }
  84. $a['import_log_id'] = $info['id'];
  85. $a['name'] = $name;
  86. $a['dm'] = $dm;
  87. $a['gb_time'] = $gb_time;
  88. $a['date'] = $date;
  89. array_push($insert_array,$a);
  90. $count = count($insert_array);
  91. if ($count>='5000'){
  92. Db::name('system_table2')->insertAll($insert_array);
  93. Db::name('system_queue')->where('id',$info['id'])->setInc('deal_count',$count);
  94. $insert_array = array();
  95. $count = 0;
  96. }
  97. }
  98. Db::name('system_table2')->insertAll($insert_array);
  99. Db::name('system_queue')->where('id',$info['id'])->setInc('deal_count',$count);
  100. $names = Db::name('system_table2')
  101. ->where('import_log_id',$info['id'])
  102. ->where('name','neq','')
  103. ->group('name')->column('name');
  104. if ($names){
  105. $insert_array = array();
  106. foreach ($names as &$v){
  107. $b = array();
  108. $b['import_log_id'] = $info['id'];
  109. $b['name'] = $v;
  110. $b['value'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('name',$v)->count();
  111. $b['date'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('name',$v)->order('id asc')
  112. ->limit(1)->value('date');
  113. array_push($insert_array,$b);
  114. }
  115. Db::name('system_values2')->insertAll($insert_array);
  116. }
  117. $dms = Db::name('system_table2')->where('import_log_id',$info['id'])
  118. ->whereNotNull('dm')
  119. ->group('dm')
  120. ->column('dm');
  121. if ($dms){
  122. $insertarray = array();
  123. foreach ($dms as &$v){
  124. $c = array();
  125. $c['import_log_id'] = $info['id'];
  126. $c['name'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('dm',$v)->value('name');
  127. $c['dm'] = $v;
  128. $c['value'] = Db::name('system_table2')->where('import_log_id',$info['id'])->where('dm',$v)->count();
  129. $c['date'] = Db::name('system_queue')->where('id',$info['id'])->value('date');
  130. array_push($insertarray,$c);
  131. }
  132. Db::name('system_values3')->insertAll($insertarray);
  133. }
  134. }catch (\think\exception\ValidateException $e) {
  135. file_put_contents("error2.txt", $e. "\n" . "\n", FILE_APPEND);
  136. }
  137. }
  138. }