with("parentType")->where('id', "=", $typeId) ->first()->toArray(); $params['type_id'] = $typeId; $parentName = ""; $params['goods_code'] = static::getChCode($goodsType['id']); if (!empty($goodsType['parent_type']['id'])) { $params['goods_code'] .= "Z". static::getChCode($goodsType['parent_type']['id']); $parentName = $goodsType['parent_type']['name'] ?? ''; } $saveData = $params; $saveData['title'] = $goodsType['name'] . " " .$parentName; return Goods::query()->firstOrCreate($params, $saveData); } public function getTypeFormatName($typeId) { $goodsType = GoodsType::query()->with("parentType")->where('id', "=", $typeId) ->first()->toArray(); $parentName = ""; if (!empty($goodsType['parent_type']['name'])) { $parentName = $goodsType['parent_type']['name'] ?? ''; } return $goodsType['name'] . " " .$parentName; } public function getRandomCode() { $time = time(); return substr($time, -7) . static::randomString(3); } public static function randomString($len = 32) { $chars = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]; // 将数组打乱 shuffle($chars); $charsLen = count($chars) - 1; $output = ""; for ($i = 0; $i < $len; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } /** * 获取非z的字符串 * @param $intValue * @return string */ public function getChCode($intValue) { $ch = range('A', 'Y'); $result = ''; while ($intValue) { $result = $ch[$intValue % 25] . $result; $intValue = intval($intValue / 25); } return $result; } }