MobileImport.php 6.2 KB

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