MobileImport.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. $fieldArr=[
  67. "手机号"=>'no',
  68. "卡名称"=>'name',
  69. "省份"=>'province',
  70. "归属地"=>'city',
  71. "运营商"=>'network',
  72. "卡品牌"=>'brand',
  73. "原价"=>'amount_original',
  74. "底价"=>'amount_di',
  75. "售价"=>'amount_base',
  76. "预存话费"=>'amount_charge',
  77. "备注(仅我可见)"=>'remark',
  78. "号码状态"=>'status',
  79. "供应商"=>'proxy_id',
  80. ];
  81. //dd($fieldArr);
  82. $infoArr=[
  83. '免流APP'=>'free_app',
  84. '资费套餐'=>'describe',
  85. '注意事项'=>'content',
  86. '每年流量(G'=>'flow_year',
  87. '费用'=>'fee',
  88. '首月免月租'=>'first_month_free',
  89. '前多少名免单'=>'flow_free_limit',
  90. '简介'=>'describe',
  91. ];
  92. //加载文件
  93. $insert = [];
  94. try {
  95. if (!$PHPExcel = $reader->load($filePath)) {
  96. throw_user(__('Unknown data format'));
  97. }
  98. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  99. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  100. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  101. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  102. $fields = [];
  103. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  104. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  105. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  106. $fields[] = preg_replace("/\s+/",'',$val);
  107. }
  108. }
  109. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  110. $values = [];
  111. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  112. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  113. $values[] = is_null($val) ? '' : $val;
  114. }
  115. $row = [];
  116. $row_info = [];
  117. $temp = array_combine($fields, $values);
  118. foreach ($temp as $k => $v) {
  119. if (isset($fieldArr[$k]) && $k !== '') {
  120. $row[$fieldArr[$k]] = $v;
  121. }
  122. if(isset($infoArr[$k]) && $k!==''){
  123. $row_info[$infoArr[$k]]=$v;
  124. }
  125. }
  126. if(!$row['no'] || Mobile::withTrashed()->where('no',$row['no'])->find()){
  127. continue;
  128. }
  129. Db::startTrans();
  130. if($type==1) {
  131. $row['proxy_id'] = Admin::where('nickname', $row['proxy_id'])->value('id');
  132. if (!$row['proxy_id']) {
  133. Db::rollback();
  134. continue;
  135. }
  136. }
  137. $row['admin_id']=$admin_id;
  138. $row['type']=$type;
  139. $row['status']=0;
  140. $mobile=(new Mobile);
  141. $mobile->allowField(true)->save($row);
  142. if(!$mobile){
  143. Db::rollback();
  144. continue;
  145. }
  146. $info=$mobile->info()->save($row_info);
  147. if(!$info){
  148. Db::rollback();
  149. continue;
  150. }
  151. Db::commit();
  152. }
  153. } catch (Exception $exception) {
  154. throw $exception;
  155. throw_user($exception->getMessage());
  156. }
  157. }
  158. }