LogisticService.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace App\Services;
  3. use App\Interfaces\UserFilter;
  4. use App\Logistic;
  5. use App\OracleBasCustomer;
  6. use Carbon\Carbon;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Str;
  11. use App\Traits\ServiceAppAop;
  12. class LogisticService implements UserFilter
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=Logistic::class;
  16. /** @var CacheService $cacheService */
  17. private $cacheService;
  18. function __construct(){
  19. $this->instant($this->cacheService,'CacheService');
  20. }
  21. public function getSelection($column = ['id','name'], $type = '快递'){
  22. return $this->cacheService->getOrExecute('LogisticAll_'.implode("",$column).Str::studly($type),function()use($column,$type){
  23. $query = Logistic::query()->select($column);
  24. if ($type)$query->where(function ($query)use($type){
  25. /** @var Builder $query */
  26. $query->where("type",$type)->orWhere("type","全部");
  27. });
  28. return $query->get();
  29. },config('cache.expirations.rarelyChange'));
  30. }
  31. public function firstOrCreate(array $params, array $values = null){
  32. return $this->cacheService->getOrExecute('LogisticFirstOrCreate'.md5(json_encode($params).json_encode($values)),function()use($params,$values){
  33. $logistic = Logistic::query();
  34. if ($values)return $logistic->firstOrCreate($params, $values);
  35. return $logistic->firstOrCreate($params);
  36. },config('cache.expirations.commonFrequent'));
  37. }
  38. public function getByWmsOrders($orderHeaders){
  39. $codes = data_get($orderHeaders,'*.userdefine1');
  40. $codes = array_unique($codes);
  41. $codes = array_diff($codes,['','*',null]);
  42. if(!$codes){return [];}
  43. $logistics = Logistic::query()->whereIn('code',$codes)->get();
  44. if($logistics->count() < count($codes)){
  45. $codes = array_diff($codes,data_get($logistics,'*.code'));
  46. $logistic_list = $this->createLogisticByCarrierIds($codes);
  47. $logistics = $logistics->concat($logistic_list);
  48. }
  49. return $logistics;
  50. }
  51. public function createLogisticByCarrierIds($codes){
  52. if(!$codes){return [];}
  53. $baseCustomers = OracleBasCustomer::query()
  54. ->selectRaw('Customer_Type,CustomerID,Descr_C')
  55. ->where('Customer_Type','CA')
  56. ->whereIn('CustomerID',$codes)
  57. ->get();
  58. $insert_params = [];
  59. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  60. foreach ($baseCustomers as $baseCustomer) {
  61. $insert_params[] = [
  62. 'code' => $baseCustomer['customerid'],
  63. 'name' => $baseCustomer['descr_c'],
  64. 'created_at' => $created_at
  65. ];
  66. }
  67. try {
  68. if(count($insert_params) > 0){
  69. $this->insert($insert_params);
  70. LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic ' . count($insert_params) . json_encode($insert_params) );
  71. }
  72. } catch (\Exception $e) {
  73. LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic error' . json_encode($insert_params) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
  74. } finally {
  75. return Logistic::query()->whereIn('code',$codes)->get();
  76. }
  77. }
  78. public function find($id)
  79. {
  80. return Logistic::query()->find($id);
  81. }
  82. public function getLogisticByCodes($codes)
  83. {
  84. $collect = collect();
  85. if(count($codes) == 0) return $collect;
  86. foreach ($codes as $code) {
  87. $collect->push($this->getLogisticByCode($code));
  88. }
  89. return $collect;
  90. }
  91. public function getLogisticByCode($code){
  92. return Cache::remember("getLogisticByCode_{$code}", config('cache.expirations.rarelyChange'), function()use($code){
  93. $logistic = Logistic::query()->where('code',$code)->first();
  94. if($logistic)return $logistic;
  95. $baseCustomers = app('OracleBasCustomerService')->first(['Customer_Type'=>'CA','CustomerID'=>$code]);
  96. if(!$baseCustomers)return null;
  97. try {
  98. $logistic = Logistic::query()->create(['name' => $baseCustomers['descr_c'], 'code' => $baseCustomers['customerid']]);
  99. app('LogService')->log(__METHOD__, __FUNCTION__,'创建Logistic SUCCESS'." || ". json_encode($logistic));
  100. return $logistic;
  101. } catch (\Exception $e) {
  102. app('LogService')->log(__METHOD__, __FUNCTION__,'创建Logistic ERROR'." || ". json_encode($logistic)." || ".json_encode($e->getMessage())." || ".json_encode($e->getTraceAsString()));
  103. return null;
  104. }
  105. });
  106. }
  107. function getIdArr(?int $userId = null): array
  108. {
  109. if (!$userId)$userId = Auth::id();
  110. return array_column($this->getQuery($userId)->get()->toArray(),"id");
  111. }
  112. function getCodeArr(?int $userId): array
  113. {
  114. return [];
  115. }
  116. function getQuery(?int $userId = null, $column = "id"): Builder
  117. {
  118. if (!$userId)$userId = Auth::id();
  119. $query = Logistic::query()->select($column);
  120. if (!app("UserService")->checkAdminIdentity($userId)){
  121. $query->whereHas("users",function ($query)use($userId){
  122. $query->where("users.id",$userId);
  123. });
  124. }
  125. return $query;
  126. }
  127. /**
  128. * 断言快递单号所属快递公司
  129. *
  130. * @param string $logisticNumber
  131. * @param bool $multi
  132. *
  133. * @return string|array
  134. */
  135. public function assertExpressCompany(string $logisticNumber, bool $multi = false)
  136. {
  137. $mapping = [
  138. "顺丰" => [
  139. [
  140. "header" => "SF",
  141. "length" => 15,
  142. ],
  143. [
  144. "length" => 12,
  145. "type" => "numeric",
  146. ],
  147. ], "圆通" => [
  148. [
  149. "header" => "YT",
  150. "length" => 15,
  151. ],
  152. [
  153. "length" => 13,
  154. "type" => "numeric",
  155. "multiHeader" => ["61","71","81"],
  156. ],
  157. ], "中通" => [
  158. [
  159. "length" => 12,
  160. "type" => "numeric",
  161. "multiHeader" => ["54","21"],
  162. ],
  163. [
  164. "length" => 14,
  165. "type" => "numeric",
  166. "multiHeader" => ["75","78","73",'74'],
  167. ],
  168. ],"韵达" => [
  169. [
  170. "length" => 13,
  171. "type" => "numeric",
  172. "multiHeader" => ["46","43","42","35","120",'318','530','570'],
  173. ],
  174. [
  175. "length" => 15,
  176. "type" => "numeric",
  177. "multiHeader" => ["46","43","31"],
  178. ],
  179. ], "京东" => [
  180. [
  181. "header" => "JD",
  182. ],
  183. ], "邮政" => [
  184. [
  185. "length" => 13,
  186. "type" => "numeric",
  187. "multiHeader" => ["11","98"],
  188. ],
  189. [
  190. "length" => 13,
  191. "header" => "KH",
  192. ],
  193. ], "极兔" => [
  194. [
  195. "header" => "JT",
  196. ]
  197. ], "跨越" => [
  198. [
  199. "header" => "KY",
  200. "length" => 15,
  201. ],
  202. [
  203. "length" => 11,
  204. "type" => "numeric",
  205. "multiHeader" => ["80"],
  206. ],
  207. ], "申通" => [
  208. [
  209. "multiHeader" => ["77"],
  210. "type" => "numeric",
  211. "length" => 15,
  212. ],
  213. [
  214. "multiHeader" => ["660"],
  215. "type" => "numeric",
  216. "length" => 13,
  217. ],
  218. ], "百世" => [
  219. [
  220. "multiHeader" => ["55"],
  221. "type" => "numeric",
  222. "length" => 15,
  223. ]
  224. ], "EMS" => [
  225. [
  226. "multiHeader" => ["97","10","95","96","99"],
  227. "type" => "numeric",
  228. "length" => 13,
  229. ],
  230. ], "德邦" => [
  231. [
  232. "header" => "DPK",
  233. "length" => 15,
  234. ],
  235. ]
  236. ];
  237. $result = $multi ? [] : "";
  238. foreach ($mapping as $name=>$rules){
  239. foreach ($rules as $rule){
  240. if (!$this->matchingNumber($logisticNumber,$rule))continue;
  241. if ($multi)$result[] = $name;
  242. else return $name;
  243. }
  244. }
  245. return $result;
  246. }
  247. public function matchingNumber(string $logisticNumber, array $rule):bool
  248. {
  249. $len = mb_strlen($logisticNumber);
  250. $match = function (string $key, $val)use($logisticNumber,$len){
  251. switch ($key){
  252. case "header":
  253. return Str::upper(mb_substr($logisticNumber,0,mb_strlen($val))) === $val;
  254. case "length":
  255. return $len==$val;
  256. case "multiHeader":
  257. foreach ($val as $item)if (mb_substr($logisticNumber,0,mb_strlen($item)) === $item)return true;
  258. return false;
  259. case "type":
  260. try {
  261. return eval("return is_{$val}('$logisticNumber');");
  262. }catch (\ParseError $e){
  263. return false;
  264. }
  265. default:
  266. return false;
  267. }
  268. };
  269. foreach ($rule as $key=>$val)if (!$match($key,$val))return false;
  270. return true;
  271. }
  272. }