ImportDataFile.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\Db;
  5. use think\Exception;
  6. use tools\Phptree;
  7. class ImportDataFile extends Auth
  8. {
  9. public function importexcel(){
  10. return $this->fetch();
  11. }
  12. public function kq(){
  13. set_time_limit(0);
  14. ini_set("memory_limit", -1);
  15. ob_flush();//清空缓存
  16. flush();//刷新缓存
  17. if(request()->file()) {
  18. $file = request()->file('file');
  19. // 移动到框架应用根目录/uploads/ 目录下
  20. $info = $file->validate([ 'size'=>500*1024*1024,'ext'=>'xls,xlsx' ])
  21. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
  22. if(!$info){
  23. exit('文件上传失败');
  24. }
  25. $img = './uploads/temp/' . $info->getSaveName();
  26. $filePath = str_replace('\\', '/', $img);
  27. $excelArray = ExcelUtil::read($filePath,
  28. ['val','val1','val2','val3','val4','val5','val6','val7','val8','val9','val10','val11','val13','val12']
  29. );
  30. // unset($excelArray[0]);
  31. if(empty($excelArray)){
  32. exit('文件内容为空');
  33. }
  34. $succ = [];
  35. foreach ($excelArray as $k => $v) {
  36. $name = $v['val'];
  37. $t = $v['val5'];
  38. $date = mb_substr($t,0,-11);
  39. $data = [
  40. 'name'=>$name,
  41. 'st'=>$date,
  42. 'day'=>date('Y-m-d',strtotime($date))
  43. ];
  44. $ret = Db::name('shuaka')->insert($data);
  45. if($ret){
  46. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  47. }else{
  48. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  49. }
  50. }
  51. }else{
  52. exit('请上传文件');
  53. }
  54. }
  55. public function sava(){
  56. $lists = Db::name('shuaka')
  57. ->group('name,day')
  58. ->order('id asc')
  59. ->select();
  60. foreach ($lists as $k=>$v){
  61. $count = Db::name('shuaka')
  62. ->where('name',$v['name'])
  63. ->where('day',$v['day'])
  64. ->count();
  65. $st = Db::name('shuaka')
  66. ->where('name',$v['name'])
  67. ->where('day',$v['day'])
  68. ->order('st asc')
  69. ->value('st');
  70. $et = Db::name('shuaka')
  71. ->where('name',$v['name'])
  72. ->where('day',$v['day'])
  73. ->order('st desc')
  74. ->value('st');
  75. $data = [
  76. 'name'=>$v['name'],
  77. 'st'=>$st,
  78. 'et'=>$count>1?$et:'',
  79. ];
  80. $res = Db::name('shuaka_log')->insertGetId($data);
  81. if($res){
  82. echo $res.'<pre/>';
  83. }
  84. }
  85. }
  86. }