ItemController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace console\controllers;
  3. use biz\item\classes\ItemClass;
  4. use biz\item\classes\ItemClassClass;
  5. use biz\item\classes\UnitClass;
  6. use yii\console\Controller;
  7. use Yii;
  8. /**
  9. * 花材初始化 shish 2021.2.18
  10. */
  11. class ItemController extends Controller
  12. {
  13. //花材初始化 ./yii item/init
  14. public function actionInit()
  15. {
  16. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  17. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  18. $dir = Yii::$app->basePath;
  19. $filename = $dir . '/goods.xls';
  20. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  21. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  22. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  23. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  24. $highestRow = $sheet->getHighestRow();//取得总行数
  25. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  26. $changeData = [];
  27. for ($j = 2; $j <= $highestRow; $j++) {
  28. $str = '';
  29. for ($k = 'A'; $k <= $highestColumn; $k++) {
  30. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  31. }
  32. //explode:函数把字符串分割为数组。
  33. $string = explode("\\", $str);
  34. array_pop($string);
  35. array_push($changeData, $string);
  36. }
  37. if (!empty($changeData)) {
  38. $classList = ItemClassClass::getAllByCondition([], null, '*', 'name');
  39. $unitList = UnitClass::getAllByCondition([], null, '*', 'name');
  40. foreach ($changeData as $key => $item) {
  41. $name = $item[1];
  42. $alias = $item[2];
  43. $className = $item[3];
  44. $classId = isset($classList[$className]['id']) ? $classList[$className]['id'] : 0;
  45. $py = strtolower($item[4]);
  46. $big = $item[5] ?? '';
  47. $big = $big == '枝' ? '支' : $big;
  48. $bigId = isset($unitList[$big]['id']) ? $unitList[$big]['id'] : 0;
  49. $small = $item[6] ?? '';
  50. $small = $small == '枝' ? '支' : $small;
  51. $smallId = isset($unitList[$small]['id']) ? $unitList[$small]['id'] : 0;
  52. $ratio = isset($item[7]) && !empty($item[7]) ? $item[7] : 0;
  53. if(empty($name)){
  54. continue;
  55. }
  56. $data = [
  57. 'name'=>$name,
  58. 'alias'=>$alias,
  59. 'py'=>$py,
  60. 'classId'=>$classId,
  61. 'unitNum'=>$ratio,
  62. 'bigUnit'=>$bigId,
  63. 'smallUnit'=>$smallId,
  64. ];
  65. print_r($data);
  66. ItemClass::add($data);
  67. }
  68. }
  69. }
  70. }