ItemController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace console\controllers;
  3. use biz\item\classes\ItemClass;
  4. use biz\item\classes\ItemClassClass;
  5. use biz\item\classes\PtItemClass;
  6. use biz\item\classes\UnitClass;
  7. use yii\console\Controller;
  8. use Yii;
  9. use bizGhs\product\classes\ProductClass;
  10. /**
  11. * 花材初始化 ssh 2021.2.18
  12. */
  13. class ItemController extends Controller
  14. {
  15. //花材初始化 ./yii item/init
  16. public function actionInit()
  17. {
  18. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  19. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  20. $dir = Yii::$app->basePath;
  21. $filename = $dir . '/goods.xls';
  22. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  23. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  24. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  25. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  26. $highestRow = $sheet->getHighestRow();//取得总行数
  27. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  28. $changeData = [];
  29. for ($j = 2; $j <= $highestRow; $j++) {
  30. $str = '';
  31. for ($k = 'A'; $k <= $highestColumn; $k++) {
  32. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  33. }
  34. //explode:函数把字符串分割为数组。
  35. $string = explode("\\", $str);
  36. array_pop($string);
  37. array_push($changeData, $string);
  38. }
  39. if (!empty($changeData)) {
  40. $classList = ItemClassClass::getAllByCondition([], null, '*', 'name');
  41. $unitList = UnitClass::getAllByCondition([], null, '*', 'name');
  42. foreach ($changeData as $key => $item) {
  43. $name = $item[1];
  44. $alias = $item[2];
  45. $className = $item[3];
  46. $classId = isset($classList[$className]['id']) ? $classList[$className]['id'] : 0;
  47. $py = strtolower($item[4]);
  48. $big = $item[5] ?? '';
  49. $big = $big == '枝' ? '支' : $big;
  50. $bigId = isset($unitList[$big]['id']) ? $unitList[$big]['id'] : 0;
  51. $small = $item[6] ?? '';
  52. $small = $small == '枝' ? '支' : $small;
  53. $smallId = isset($unitList[$small]['id']) ? $unitList[$small]['id'] : 0;
  54. $ratio = isset($item[7]) && !empty($item[7]) ? $item[7] : 0;
  55. if(empty($name)){
  56. continue;
  57. }
  58. $data = [
  59. 'name'=>$name,
  60. 'alias'=>$alias,
  61. 'py'=>$py,
  62. 'classId'=>$classId,
  63. 'unitNum'=>$ratio,
  64. 'bigUnit'=>$bigId,
  65. 'smallUnit'=>$smallId,
  66. ];
  67. print_r($data);
  68. ItemClass::add($data);
  69. }
  70. }
  71. }
  72. //命令: ./yii item/sync name,cover 同步name 和cover
  73. //同步item, ptItem更新,同步到 ghsItem, ghsProduct, 同步字段(name,alias,py,cover,ratio,cover等)
  74. public function actionSync($field)
  75. {
  76. $allFiled = ['name','alias','cover','py','ratio','weight','bigUnit','smallUnit','bigUnitId','smallUnitId','stockWarning'];
  77. $fields = explode(",",$field);
  78. $syncFields = array_intersect($allFiled,$fields);
  79. if(empty($syncFields)){
  80. echo "参数不支持";die;
  81. }
  82. $item = PtItemClass::getAllList("id","");
  83. $itemIds = [];
  84. foreach ($item as $v){
  85. $itemIds[] = $v['id'];
  86. }
  87. if(!empty($itemIds)){
  88. foreach ($itemIds as $v){
  89. $itemInfo = PtItemClass::getById($v);
  90. $data =[];
  91. foreach ($syncFields as $f){
  92. $data[$f] = $itemInfo[$f];
  93. }
  94. ProductClass::updateByCondition(['itemId'=>$v],$data);
  95. \bizGhs\item\classes\ItemClass::updateByCondition(['itemId'=>$v],$data);
  96. }
  97. }
  98. echo "done";
  99. }
  100. }