2024-08-12 19:36:42 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Good;
|
|
|
|
|
|
|
|
|
|
use App\Events\BatchStockUpdateEvent;
|
|
|
|
|
use App\Models\BusinessOrderItem;
|
|
|
|
|
use App\Models\CombinationGood;
|
|
|
|
|
use App\Models\DailyStockRecord;
|
|
|
|
|
use App\Models\Goods;
|
|
|
|
|
use App\Models\GoodsSku;
|
|
|
|
|
use App\Models\GoodsType;
|
|
|
|
|
use App\Utils\DateTimeUtils;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Predis\Command\Redis\SUBSCRIBE;
|
|
|
|
|
|
|
|
|
|
class GoodService
|
|
|
|
|
{
|
|
|
|
|
public function saveDefaultGoodsByGoodType($typeId)
|
|
|
|
|
{
|
2024-08-14 17:02:31 +08:00
|
|
|
$goodsType = GoodsType::query()->with("parentType")->where('id', "=", $typeId)
|
2024-08-12 19:36:42 +08:00
|
|
|
->first()->toArray();
|
|
|
|
|
$params['type_id'] = $typeId;
|
2024-08-14 17:02:31 +08:00
|
|
|
$parentName = "";
|
2024-08-12 19:36:42 +08:00
|
|
|
$params['goods_code'] = static::getChCode($goodsType['id']);
|
|
|
|
|
if (!empty($goodsType['parent_type']['id'])) {
|
2024-08-14 17:02:31 +08:00
|
|
|
$params['goods_code'] .= "Z". static::getChCode($goodsType['parent_type']['id']);
|
|
|
|
|
$parentName = $goodsType['parent_type']['name'] ?? '';
|
2024-08-12 19:36:42 +08:00
|
|
|
}
|
|
|
|
|
$saveData = $params;
|
2024-08-14 17:02:31 +08:00
|
|
|
$saveData['title'] = $goodsType['name'] . " " .$parentName;
|
2024-08-12 19:36:42 +08:00
|
|
|
return Goods::query()->firstOrCreate($params, $saveData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:02:31 +08:00
|
|
|
/**
|
|
|
|
|
* 获取非z的字符串
|
|
|
|
|
* @param $intValue
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2024-08-12 19:36:42 +08:00
|
|
|
public function getChCode($intValue)
|
|
|
|
|
{
|
2024-08-14 17:02:31 +08:00
|
|
|
$ch = range('A', 'Y');
|
2024-08-12 19:36:42 +08:00
|
|
|
$result = '';
|
|
|
|
|
while ($intValue) {
|
2024-08-14 17:02:31 +08:00
|
|
|
$result = $ch[$intValue % 25] . $result;
|
|
|
|
|
$intValue = intval($intValue / 25);
|
2024-08-12 19:36:42 +08:00
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|