basePath; $filename = $dir . '/goods.xls'; $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型 $excelReader = \PHPExcel_IOFactory::createReader($fileType); $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径 $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表 $highestRow = $sheet->getHighestRow();//取得总行数 $highestColumn = $sheet->getHighestColumn(); //取得总列数 $changeData = []; for ($j = 2; $j <= $highestRow; $j++) { $str = ''; for ($k = 'A'; $k <= $highestColumn; $k++) { $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\'; } //explode:函数把字符串分割为数组。 $string = explode("\\", $str); array_pop($string); array_push($changeData, $string); } if (!empty($changeData)) { $classList = ItemClassClass::getAllByCondition([], null, '*', 'name'); $unitList = UnitClass::getAllByCondition([], null, '*', 'name'); foreach ($changeData as $key => $item) { $name = $item[1]; $alias = $item[2]; $className = $item[3]; $classId = isset($classList[$className]['id']) ? $classList[$className]['id'] : 0; $py = strtolower($item[4]); $big = $item[5] ?? ''; $big = $big == '枝' ? '支' : $big; $bigId = isset($unitList[$big]['id']) ? $unitList[$big]['id'] : 0; $small = $item[6] ?? ''; $small = $small == '枝' ? '支' : $small; $smallId = isset($unitList[$small]['id']) ? $unitList[$small]['id'] : 0; $ratio = isset($item[7]) && !empty($item[7]) ? $item[7] : 0; if (empty($name)) { continue; } $data = [ 'name' => $name, 'alias' => $alias, 'py' => $py, 'classId' => $classId, 'unitNum' => $ratio, 'bigUnit' => $bigId, 'smallUnit' => $smallId, ]; print_r($data); ItemClass::add($data); } } } //命令: ./yii item/sync name,cover 同步name 和cover //同步item, ptItem更新,同步到 ghsItem, ghsProduct, 同步字段(name,alias,py,cover,ratio,cover等) public function actionSync($field) { $allFiled = ['name', 'alias', 'cover', 'py', 'ratio', 'weight', 'bigUnit', 'smallUnit', 'bigUnitId', 'smallUnitId', 'stockWarning']; $fields = explode(",", $field); $syncFields = array_intersect($allFiled, $fields); if (empty($syncFields)) { echo "参数不支持"; die; } $item = PtItemClass::getAllList("id", ""); $itemIds = []; foreach ($item as $v) { $itemIds[] = $v['id']; } if (!empty($itemIds)) { foreach ($itemIds as $v) { $itemInfo = PtItemClass::getById($v); $data = []; foreach ($syncFields as $f) { $data[$f] = $itemInfo[$f]; } ProductClass::updateByCondition(['itemId' => $v], $data); \bizGhs\item\classes\ItemClass::updateByCondition(['itemId' => $v], $data); } } echo "done"; } //平台花材初始化导入 linqh 2021-05-08 //命令: ./yii item/init-pt-item public function actionInitPtItem($clear = false) { $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/"); require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口 $dir = Yii::$app->basePath; $filename = $dir . '/item.xls'; $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型 $excelReader = \PHPExcel_IOFactory::createReader($fileType); $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径 $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表 $highestRow = $sheet->getHighestRow();//取得总行数 $highestColumn = $sheet->getHighestColumn(); //取得总列数 $classData = []; $itemData = []; $classIdMap = []; for ($j = 2; $j <= $highestRow; $j++) { $str = ''; for ($k = 'A'; $k <= $highestColumn; $k++) { $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\'; } //explode:函数把字符串分割为数组。 $string = explode("\\", $str); array_pop($string); $itemName = $string[0] ?? ''; //花材名称 $className = $string[1] ?? ''; //分类名称 $weight = $string[2] ?? 0; //重量 kg $bigUnit = $string[3] ?? ''; //大单位名称 $smallUnit = $string[4] ?? ''; //小单位名称 $ratio = $string[5] ?? 0; //比例 $cost = $string[6] ?? 0;//成本价 $addPrice = $string[7] ?? 0; //加价 $stockWarning = $string[8] ?? 0; $alias = $string[9] ?? ''; $cover = $string[10] ?? ''; $inTurn = $string[11] ?? 0; if ($itemName) { $py = stringUtil::py($itemName); $item = [ 'name' => $itemName, 'alias' => $alias, 'py' => $py, 'cover' => $cover, 'classId' => 0, 'ratio' => $ratio, 'weight' => $weight, 'stockWarning' => $stockWarning, 'cost' => $cost, 'addPrice' => $addPrice, 'bigUnit' => $bigUnit, 'smallUnit' => $smallUnit, 'inTurn' => $inTurn, ]; $itemData[$className][] = $item; $classData[] = $className; } } $classData = array_unique($classData); //写入class if ($clear) { //清空数据 Yii::$app->db->createCommand()->truncateTable('xhPtItemClass')->execute(); Yii::$app->db->createCommand()->truncateTable('xhPtItem')->execute(); } foreach ($classData as $v) { $addClass = PtItemClassClass::add(['name' => $v, 'inTurn' => 0, 'group' => 0]); $classIdMap[$v] = $addClass['id']; } $insertItemData = []; foreach ($itemData as $k => $v) { $classId = $classIdMap[$k] ?? 0; if ($classId) { foreach ($v as $i) { $i['classId'] = $classId; $insertItemData[] = $i; } } } if (!empty($insertItemData)) { PtItemClass::batchAdd($insertItemData); } echo "done"; } // ./yii item/generate 1 public function actionGenerate($id) { util::stop('停用'); $shop = ShopClass::getById($id, true); if (empty($shop)) { util::stop('没有找到门店'); } $sjId = $shop->sjId; ItemService::initItem($sjId, $id); } }