erp/app/Services/Good/GoodService.php

75 lines
2.1 KiB
PHP
Raw Normal View History

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)
{
$goodsType = GoodsType::query()->with("parent_type")->where('id', "=", $typeId)
->first()->toArray();
$params['type_id'] = $typeId;
$params['goods_code'] = static::getChCode($goodsType['id']);
if (!empty($goodsType['parent_type']['id'])) {
$params['goods_code'] .= "|" . static::getChCode($goodsType['parent_type']['id']);
}
$saveData = $params;
$saveData['title'] = $goodsType['name'] . " " . $goodsType['parent_type']['name'] ?? '';
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;
}
public function getChCode($intValue)
{
$ch = range('A', 'Z');
$result = '';
while ($intValue) {
$result = $ch[$intValue % 26] . $result;
$intValue = intval($intValue / 26);
}
return $result;
}
}