MobileImport.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\common\service;
  3. use app\admin\library\Auth;
  4. use app\admin\model\Admin;
  5. use app\common\model\Mobile;
  6. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  7. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  8. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  9. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  10. use think\Db;
  11. use think\Exception;
  12. use think\exception\PDOException;
  13. class MobileImport{
  14. public static function import($file,$admin_id,$type=1){
  15. if (!$file) {
  16. throw_user(__('Parameter %s can not be empty', 'file'));
  17. }
  18. $file=parse_url($file,PHP_URL_PATH);
  19. $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  20. if (!is_file($filePath)) {
  21. throw_user(__('No results were found'));
  22. }
  23. //实例化reader
  24. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  25. if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  26. throw_user(__('Unknown data format'));
  27. }
  28. if ($ext === 'csv') {
  29. $file = fopen($filePath, 'r');
  30. $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  31. $fp = fopen($filePath, "w");
  32. $n = 0;
  33. while ($line = fgets($file)) {
  34. $line = rtrim($line, "\n\r\0");
  35. $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  36. if ($encoding != 'utf-8') {
  37. $line = mb_convert_encoding($line, 'utf-8', $encoding);
  38. }
  39. if ($n == 0 || preg_match('/^".*"$/', $line)) {
  40. fwrite($fp, $line . "\n");
  41. } else {
  42. fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
  43. }
  44. $n++;
  45. }
  46. fclose($file) || fclose($fp);
  47. $reader = new Csv();
  48. } elseif ($ext === 'xls') {
  49. $reader = new Xls();
  50. } else {
  51. $reader = new Xlsx();
  52. }
  53. //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
  54. $importHeadType = 'comment';
  55. $table = (new Mobile)->getTable();
  56. $database = \think\Config::get('database.database');
  57. $fieldArr = [];
  58. $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
  59. foreach ($list as $k => $v) {
  60. if ($importHeadType == 'comment') {
  61. $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
  62. } else {
  63. $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
  64. }
  65. }
  66. $infoArr=[
  67. '免流APP'=>'free_app',
  68. '套餐信息'=>'describe',
  69. '注意事项'=>'content',
  70. '每年流量(G'=>'flow_year',
  71. '费用'=>'fee',
  72. '首月免月租'=>'first_month_free',
  73. '前多少名免单'=>'flow_free_limit',
  74. ];
  75. //加载文件
  76. $insert = [];
  77. try {
  78. if (!$PHPExcel = $reader->load($filePath)) {
  79. throw_user(__('Unknown data format'));
  80. }
  81. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  82. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  83. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  84. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  85. $fields = [];
  86. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  87. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  88. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  89. $fields[] = $val;
  90. }
  91. }
  92. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  93. $values = [];
  94. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  95. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  96. $values[] = is_null($val) ? '' : $val;
  97. }
  98. $row = [];
  99. $row_info = [];
  100. $temp = array_combine($fields, $values);
  101. foreach ($temp as $k => $v) {
  102. if (isset($fieldArr[$k]) && $k !== '') {
  103. $row[$fieldArr[$k]] = $v;
  104. }
  105. if(isset($infoArr[$k]) && $k!==''){
  106. $row_info[$infoArr[$k]]=$v;
  107. }
  108. }
  109. if(Mobile::withTrashed()->where('no',$row['no'])->find()){
  110. continue;
  111. }
  112. Db::startTrans();
  113. $row['proxy_id']=Admin::where('nickname',$row['proxy_id'])->value('id');
  114. $row['admin_id']=$admin_id;
  115. $row['type']=$type;
  116. $mobile=Mobile::create($row);
  117. if(!$mobile){
  118. Db::rollback();
  119. continue;
  120. }
  121. $info=$mobile->info()->save($row_info);
  122. if(!$info){
  123. Db::rollback();
  124. continue;
  125. }
  126. Db::commit();
  127. }
  128. } catch (Exception $exception) {
  129. throw_user($exception->getMessage());
  130. }
  131. }
  132. }