ImportTable1Queue.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\exception\HttpResponseException;
  18. use think\Db;
  19. /**
  20. * Class Jobs
  21. */
  22. class ImportTable1Queue
  23. {
  24. /**
  25. * 当前类名
  26. * @var string
  27. */
  28. const URI = self::class;
  29. /**
  30. * 执行任务
  31. * @param Input $input
  32. * @param Output $output
  33. * @param array $data
  34. * @throws \think\exception\PDOException
  35. */
  36. public function execute(Input $input, Output $output, array $data = [])
  37. {
  38. try {
  39. set_time_limit(0);
  40. ini_set ("memory_limit","-1");
  41. $info = Db::name('system_queue')
  42. ->where('type',$data['type'])
  43. ->where('import_name',$data['import_name'])
  44. ->order('id desc')
  45. ->limit(1)
  46. ->find();
  47. $import_path = $info['local_url'];
  48. $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
  49. $objExcel = $objReader->load($import_path);
  50. $maxRow = $objExcel->getSheet(0)->getHighestRow();#总行数
  51. Db::name('system_queue')->where('id',$info['id'])->update(['maxrow'=>($maxRow-1)]);
  52. $list = $objExcel->getActiveSheet()->toArray();
  53. $dates = array_filter(array_unique(array_column($list,'0')));
  54. if (count($dates)>2){
  55. Db::name('system_queue')->where('id',$info['id'])->update(
  56. [
  57. 'status' => 4, 'end_at' => date('Y-m-d H:i:s'), 'desc' => '表中存在多个日期,导入失败',
  58. ]
  59. );
  60. die();
  61. }
  62. $datee = date('Y-m-d',strtotime($dates[1]));
  63. Db::name('system_queue')->where('id',$info['id'])->update(['date'=>$datee]);
  64. $infos = Db::name('system_values')->where('date',$datee)->count();
  65. if ($infos){
  66. Db::name('system_queue')->where('id',$info['id'])->update(
  67. [
  68. 'status' => 4, 'end_at' => date('Y-m-d H:i:s'), 'desc' => '表中日期'.$datee.'数据已存在,无法导入',
  69. ]
  70. );
  71. die();
  72. }
  73. $array = array();
  74. $kd_array = Db::name('system_kd')->column('name');
  75. foreach ($list as $k=>$v){
  76. if ($k==0){
  77. for ($i=9;$i<count($v);$i++){
  78. $name = mb_substr($v[$i],0,2,"utf-8"); //截取前两个汉字
  79. $array[$i] = $name;
  80. if (!in_array($name,$kd_array)){
  81. if (!empty($name)){
  82. array_push($kd_array,$name);
  83. Db::name('system_kd')->insert(['name'=>$name]);
  84. }
  85. }
  86. }
  87. }else{
  88. if (!$v[0]){
  89. continue;
  90. }
  91. $date = date('Y-m-d',strtotime($v[0]));
  92. $insert_array = array();
  93. $site_id = Db::name('system_site')->where('code',$v[6])->value('id');
  94. if (!$site_id){
  95. $site_id = Db::name('system_site')->insertGetId(
  96. [
  97. 'code'=>$v[6],
  98. 'name'=>$v[7]
  99. ]
  100. );
  101. }
  102. for ($i=8;$i<count($v);$i++){
  103. $a = array();
  104. $a['site_id'] = $site_id;
  105. $a['date'] = $date;
  106. $a['import_log_id'] = $info['id'];
  107. $a['name'] = $i==8 ? "总签收" : $array[$i];
  108. $a['value'] = $v[$i];
  109. array_push($insert_array,$a);
  110. }
  111. Db::name('system_values')->insertAll($insert_array);
  112. }
  113. }
  114. Db::name('system_values')->where('name','eq','')->delete();
  115. $sql = "SELECT site_id FROM system_values WHERE import_log_id=".$info['id']." GROUP BY site_id";
  116. $deal_count = count(Db::query($sql));
  117. Db::name('system_queue')->where('id',$info['id'])->update(['deal_count'=>$deal_count]);
  118. }catch (\think\exception\ValidateException $e) {
  119. file_put_contents("error2.txt", $e. "\n" . "\n", FILE_APPEND);
  120. }
  121. }
  122. }