| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- namespace App\Services;
- use App\Interfaces\UserFilter;
- use App\Logistic;
- use App\OracleBasCustomer;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Str;
- use App\Traits\ServiceAppAop;
- class LogisticService implements UserFilter
- {
- use ServiceAppAop;
- protected $modelClass=Logistic::class;
- /** @var CacheService $cacheService */
- private $cacheService;
- function __construct(){
- $this->instant($this->cacheService,'CacheService');
- }
- public function getSelection($column = ['id','name'], $type = '快递'){
- return $this->cacheService->getOrExecute('LogisticAll_'.implode("",$column).Str::studly($type),function()use($column,$type){
- $query = Logistic::query()->select($column);
- if ($type)$query->where(function ($query)use($type){
- /** @var Builder $query */
- $query->where("type",$type)->orWhere("type","全部");
- });
- return $query->get();
- },config('cache.expirations.rarelyChange'));
- }
- public function firstOrCreate(array $params, array $values = null){
- return $this->cacheService->getOrExecute('LogisticFirstOrCreate'.md5(json_encode($params).json_encode($values)),function()use($params,$values){
- $logistic = Logistic::query();
- if ($values)return $logistic->firstOrCreate($params, $values);
- return $logistic->firstOrCreate($params);
- },config('cache.expirations.commonFrequent'));
- }
- public function getByWmsOrders($orderHeaders){
- $codes = data_get($orderHeaders,'*.userdefine1');
- $codes = array_unique($codes);
- $codes = array_diff($codes,['','*',null]);
- if(!$codes){return [];}
- $logistics = Logistic::query()->whereIn('code',$codes)->get();
- if($logistics->count() < count($codes)){
- $codes = array_diff($codes,data_get($logistics,'*.code'));
- $logistic_list = $this->createLogisticByCarrierIds($codes);
- $logistics = $logistics->concat($logistic_list);
- }
- return $logistics;
- }
- public function createLogisticByCarrierIds($codes){
- if(!$codes){return [];}
- $baseCustomers = OracleBasCustomer::query()
- ->selectRaw('Customer_Type,CustomerID,Descr_C')
- ->where('Customer_Type','CA')
- ->whereIn('CustomerID',$codes)
- ->get();
- $insert_params = [];
- $created_at = Carbon::now()->format('Y-m-d H:i:s');
- foreach ($baseCustomers as $baseCustomer) {
- $insert_params[] = [
- 'code' => $baseCustomer['customerid'],
- 'name' => $baseCustomer['descr_c'],
- 'created_at' => $created_at
- ];
- }
- try {
- if(count($insert_params) > 0){
- $this->insert($insert_params);
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic ' . count($insert_params) . json_encode($insert_params) );
- }
- } catch (\Exception $e) {
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic error' . json_encode($insert_params) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
- } finally {
- return Logistic::query()->whereIn('code',$codes)->get();
- }
- }
- public function find($id)
- {
- return Logistic::query()->find($id);
- }
- public function getLogisticByCodes($codes)
- {
- $collect = collect();
- if(count($codes) == 0) return $collect;
- foreach ($codes as $code) {
- $collect->push($this->getLogisticByCode($code));
- }
- return $collect;
- }
- public function getLogisticByCode($code){
- return Cache::remember("getLogisticByCode_{$code}", config('cache.expirations.rarelyChange'), function()use($code){
- $logistic = Logistic::query()->where('code',$code)->first();
- if($logistic)return $logistic;
- $baseCustomers = app('OracleBasCustomerService')->first(['Customer_Type'=>'CA','CustomerID'=>$code]);
- if(!$baseCustomers)return null;
- try {
- $logistic = Logistic::query()->create(['name' => $baseCustomers['descr_c'], 'code' => $baseCustomers['customerid']]);
- app('LogService')->log(__METHOD__, __FUNCTION__,'创建Logistic SUCCESS'." || ". json_encode($logistic));
- return $logistic;
- } catch (\Exception $e) {
- app('LogService')->log(__METHOD__, __FUNCTION__,'创建Logistic ERROR'." || ". json_encode($logistic)." || ".json_encode($e->getMessage())." || ".json_encode($e->getTraceAsString()));
- return null;
- }
- });
- }
- function getIdArr(?int $userId = null): array
- {
- if (!$userId)$userId = Auth::id();
- return array_column($this->getQuery($userId)->get()->toArray(),"id");
- }
- function getQuery(?int $userId = null): Builder
- {
- if (!$userId)$userId = Auth::id();
- $query = Logistic::query()->select("id");
- if (!app("UserService")->checkAdminIdentity($userId)){
- $query->whereHas("users",function ($query)use($userId){
- $query->where("id",$userId);
- });
- }
- return $query;
- }
- /**
- * 断言快递单号所属快递公司
- *
- * @param string $logisticNumber
- * @param bool $multi
- *
- * @return string|array
- */
- public function assertExpressCompany(string $logisticNumber, bool $multi = false)
- {
- $mapping = [
- "顺丰" => [
- [
- "header" => "SF",
- "length" => 15,
- ],
- [
- "length" => 12,
- "type" => "numeric",
- ],
- ], "圆通" => [
- [
- "header" => "YT",
- "length" => 15,
- ],
- [
- "length" => 13,
- "type" => "numeric",
- "multiHeader" => ["61","71","81"],
- ],
- ], "中通" => [
- [
- "length" => 12,
- "type" => "numeric",
- "multiHeader" => ["54","21"],
- ],
- [
- "length" => 14,
- "type" => "numeric",
- "multiHeader" => ["75","78","73",'74'],
- ],
- ],"韵达" => [
- [
- "length" => 13,
- "type" => "numeric",
- "multiHeader" => ["46","43","42","35"],
- ],
- [
- "length" => 15,
- "type" => "numeric",
- "multiHeader" => ["46","43","31"],
- ],
- ], "京东" => [
- [
- "header" => "JD",
- ],
- ], "邮政" => [
- [
- "length" => 13,
- "type" => "numeric",
- "multiHeader" => ["11","98"],
- ],
- ], "极兔" => [
- [
- "header" => "JT",
- ]
- ], "跨越" => [
- [
- "header" => "KY",
- "length" => 15,
- ],
- [
- "length" => 11,
- "type" => "numeric",
- "multiHeader" => ["80"],
- ],
- ], "申通" => [
- [
- "multiHeader" => ["77"],
- "type" => "numeric",
- "length" => 15,
- ],
- ], "百世" => [
- [
- "multiHeader" => ["55"],
- "type" => "numeric",
- "length" => 15,
- ]
- ], "EMS" => [
- [
- "multiHeader" => ["97"],
- "type" => "numeric",
- "length" => 13,
- ],
- ],
- ];
- $result = $multi ? [] : "";
- foreach ($mapping as $name=>$rules){
- foreach ($rules as $rule){
- if (!$this->matchingNumber($logisticNumber,$rule))continue;
- if ($multi)$result[] = $name;
- else return $name;
- }
- }
- return $result;
- }
- public function matchingNumber(string $logisticNumber, array $rule):bool
- {
- $len = mb_strlen($logisticNumber);
- $match = function (string $key, $val)use($logisticNumber,$len){
- switch ($key){
- case "header":
- return Str::upper(mb_substr($logisticNumber,0,mb_strlen($val))) === $val;
- case "length":
- return $len==$val;
- case "multiHeader":
- foreach ($val as $item)if (mb_substr($logisticNumber,0,mb_strlen($item)) === $item)return true;
- return false;
- case "type":
- try {
- return eval("return is_{$val}('$logisticNumber');");
- }catch (\ParseError $e){
- return false;
- }
- default:
- return false;
- }
- };
- foreach ($rule as $key=>$val)if (!$match($key,$val))return false;
- return true;
- }
- }
|