OwnerPriceExpressService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace App\Services;
  3. use App\Logistic;
  4. use App\Owner;
  5. use App\OwnerPriceExpress;
  6. use App\OwnerPriceExpressProvince;
  7. use App\Services\common\QueryService;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\DB;
  11. use App\Traits\ServiceAppAop;
  12. class OwnerPriceExpressService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=OwnerPriceExpress::class;
  16. public function paginate($id = null)
  17. {
  18. $query = OwnerPriceExpress::query()->with(["owners","logistics"])
  19. ->orderByDesc("id");
  20. if ($id)$query->where("id",$id);
  21. return $query->paginate(50);
  22. }
  23. public function find($id, $withs = [])
  24. {
  25. return OwnerPriceExpress::query()->with($withs)->find($id);
  26. }
  27. /**
  28. * 拷贝目标数据
  29. *
  30. * @param object|int $model
  31. * @param array $values
  32. * @param array|null $owners
  33. * @param array|null $logistics
  34. * @param array $items
  35. * @param bool $isLoadItem
  36. *
  37. * @return object|null
  38. */
  39. public function copy($model, $values = [], $owners = null, $logistics = null, $items = [], $isLoadItem = true)
  40. {
  41. if (is_integer($model))$model = OwnerPriceExpress::query()->find($model);
  42. if (!$model)return null;
  43. $values["operation"] = "U";
  44. $values["target_id"] = $model->id;
  45. foreach ($model->getFillable() as $column){
  46. if (!array_key_exists($column,$values))$values[$column] = $model[$column];
  47. }
  48. /** @var OwnerPriceExpress $copyModel */
  49. $copyModel = OwnerPriceExpress::query()->create($values);
  50. if ($owners===null){
  51. $query = DB::raw("SELECT * FROM owner_price_express_owner WHERE owner_price_express_id = {$model->id}");
  52. $owners = array_column(DB::select($query),"owner_id");
  53. }
  54. $copyModel->owners()->sync($owners);
  55. if ($logistics===null){
  56. $query = DB::raw("SELECT * FROM owner_price_express_logistic WHERE owner_price_express_id = {$model->id}");
  57. $logistics = array_column(DB::select($query),"logistic_id");
  58. }
  59. $copyModel->logistics()->sync($logistics);
  60. $insert = [];
  61. if ($isLoadItem){
  62. $model->loadMissing("details");
  63. /** @var \stdClass $model */
  64. foreach ($model->details as $item){
  65. $columns = ["province_id","initial_weight_price","additional_weight_price"];
  66. if ($items[$item->id] ?? false){
  67. foreach ($columns as $column){
  68. if (!array_key_exists($column,$items[$item->id]))$items[$item->id][$column] = $item[$column];
  69. }
  70. $obj = $items[$item->id];
  71. unset($items[$item->id]);
  72. }else{
  73. /** @var OwnerPriceExpressProvince $item */
  74. $obj = $item->toArray();
  75. unset($obj["id"]);
  76. }
  77. $obj["owner_price_express_id"] = $copyModel->id;
  78. $obj["initial_weight_price"] = json_encode($obj["initial_weight_price"]);
  79. $obj["additional_weight_price"] = json_encode($obj["additional_weight_price"]);
  80. $insert[] = $obj;
  81. }
  82. }else{
  83. foreach ($items as $item){
  84. $item["owner_price_express_id"] = $copyModel->id;
  85. $item["initial_weight_price"] = json_encode($item["initial_weight_price"]);
  86. $item["additional_weight_price"] = json_encode($item["additional_weight_price"]);
  87. $insert[] = $item;
  88. }
  89. }
  90. if ($insert)OwnerPriceExpressProvince::query()->insert($insert);
  91. return $copyModel;
  92. }
  93. /**
  94. * 审核或恢复目标集
  95. *
  96. * @param bool $isAudit
  97. * @param integer|null|array $ownerId
  98. * @param integer|null|array $ids
  99. */
  100. public function auditOrRecover($isAudit = true, $ownerId = null, $ids = null)
  101. {
  102. if (!$ownerId && !$ids)return;
  103. $result = app(QueryService::class)->priceModelAuditOrRecoverQuery($isAudit,OwnerPriceExpress::query(),$ownerId,$ids);
  104. if ($result["delete"])$this->destroy($result["delete"]);
  105. if ($result["update"])OwnerPriceExpress::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
  106. if (!is_array($ownerId))$ownerId = [$ownerId];
  107. foreach ($ownerId as $owner)Cache::tags("expressFeeOwner:".$owner)->flush();
  108. }
  109. public function updateDetail(array $params, array $values)
  110. {
  111. $query = OwnerPriceExpressProvince::query();
  112. foreach ($params as $column => $param){
  113. $query->where($column,$param);
  114. }
  115. return $query->update($values);
  116. }
  117. public function create(array $params)
  118. {
  119. $params["operation"] = "C";
  120. return OwnerPriceExpress::query()->create($params);
  121. }
  122. public function createDetail(array $params)
  123. {
  124. return OwnerPriceExpressProvince::query()->create($params);
  125. }
  126. public function isExistDetail(array $params)
  127. {
  128. $query = OwnerPriceExpressProvince::query();
  129. foreach ($params as $column => $param){
  130. $query->where($column,$param);
  131. }
  132. return $query->count();
  133. }
  134. public function destroyDetail($id)
  135. {
  136. return OwnerPriceExpressProvince::destroy($id);
  137. }
  138. public function getExistOwnerName($owner_id, $logistic_id, $id=null, $type = 'ownerPriceExpresses') :array
  139. {
  140. if (!is_array($owner_id))$owner_id = [$owner_id];
  141. if (!is_array($logistic_id))$logistic_id = [$logistic_id];
  142. $owners = Owner::query()->with([$type=>function($query)use($id){
  143. /** @var Builder $query */
  144. if ($id)$query->where("id","!=",$id);
  145. $query->with(["logistics"]);
  146. }])->whereIn("id",$owner_id)->get();
  147. $result = [];
  148. foreach ($owners as $owner){
  149. $arr = [];
  150. if ($owner->ownerPriceExpresses){
  151. foreach ($owner->ownerPriceExpresses as $express){
  152. if ($express->logistics){
  153. foreach ($express->logistics as $logistic){
  154. if (array_search($logistic->id,$logistic_id) !== false)$arr[] = $logistic->name;
  155. }
  156. }
  157. }
  158. }
  159. if (count($arr)>0)$result[] = "“".$owner->name."”已绑定:".implode(",",$arr);
  160. }
  161. return $result;
  162. }
  163. public function getExistLogisticName($logistic_id, $id=null):array
  164. {
  165. $logistics = Logistic::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
  166. if ($id)$query->where("id","!=",$id);
  167. }])->whereIn("id",$logistic_id)->get();
  168. $arr = [];
  169. foreach ($logistics as $logistic){
  170. if ($logistic->owner_price_expresses_count > 0)$arr[] = $logistic->name;
  171. }
  172. return $arr;
  173. }
  174. public function destroy($id)
  175. {
  176. if (!$id)return 0;
  177. if (!is_array($id))$id = [$id];
  178. OwnerPriceExpressProvince::query()->whereIn("owner_price_express_id",$id)->delete();
  179. DB::table("owner_price_express_owner")->whereIn("owner_price_express_id",$id)->delete();
  180. DB::table("owner_price_express_logistic")->whereIn("owner_price_express_id",$id)->delete();
  181. return OwnerPriceExpress::destroy($id);
  182. }
  183. public function update(array $params, array $values)
  184. {
  185. $query = OwnerPriceExpress::query();
  186. foreach ($params as $column=>$param){
  187. $query->where($column,$params);
  188. }
  189. return $query->update($values);
  190. }
  191. /**
  192. * 获取绑定承运商
  193. *
  194. * @param $owner
  195. * @return array
  196. */
  197. public function getBuildLogistic($owner)
  198. {
  199. return app(CacheService::class)->getOrExecute("logistics_owner_".$owner,function ()use($owner){
  200. $query = DB::raw(<<<sql
  201. SELECT logistic_id FROM `owner_price_express_owner` e
  202. LEFT JOIN owner_price_express_logistic l
  203. ON e.owner_price_express_id = l.owner_price_express_id
  204. WHERE e.owner_id = ?
  205. sql
  206. );
  207. $logistics = DB::select($query,[$owner]);
  208. return array_column($logistics,"logistic_id");
  209. },null);
  210. }
  211. /**
  212. * @param integer $owner
  213. * @param integer $logistic
  214. * @param integer $province
  215. *
  216. * @return OwnerPriceExpress|\stdClass|null
  217. */
  218. public function getOwnerPriceExpress($owner,$logistic,$province)
  219. {
  220. return Cache::tags("expressFeeOwner:".$owner)->remember("expressFee:".$owner."_".$logistic."_".$province,config("cache.expirations.rarelyChange"),
  221. function ()use($owner,$logistic,$province){
  222. return OwnerPriceExpress::query()->with(["details"=>function($query)use($province){
  223. /** @var Builder $query */
  224. $query->where("province_id",$province);
  225. }])->whereHas("owners",function ($query)use($owner){
  226. /** @var Builder $query */
  227. $query->where("id",$owner);
  228. })->whereHas("logistics",function ($query)use($logistic){
  229. /** @var Builder $query */
  230. $query->where("id",$logistic);
  231. })->where(function(Builder $query){
  232. $query->whereNull("operation")->orWhere("operation","");
  233. })->first();
  234. });
  235. }
  236. /**
  237. *
  238. * @param double $weight
  239. * @param integer $ownerId
  240. * @param integer $logisticId
  241. * @param integer $provinceId
  242. * @param string $month
  243. *
  244. * @return array
  245. */
  246. public function matching(float $weight, int $ownerId, int $logisticId, int $provinceId, string $month):array
  247. {
  248. if (!$weight)return array(null,null);
  249. $model = $this->getOwnerPriceExpress($ownerId,$logisticId,$provinceId);
  250. if (!$model || count($model->details)<1)return array(null,null);
  251. $to1 = $to2 = 0;
  252. if ($model->amount_interval){
  253. $total = app("OrderService")->getOrderQuantity($ownerId,false,$month)+1;//获取该货主月C端单量
  254. for ($i=count($model->amount_interval);$i<0;$i--)if ($total>=$model->amount_interval[$i]){$to1 = $i;break;}
  255. if (isset($to1) && isset($model->weight_interval[$to1])){
  256. for ($i=count($model->weight_interval[$to1]);$i<0;$i--){
  257. if ($weight>=$model->weight_interval[$to1][$i]){$to2 = $i;break;}
  258. }
  259. }
  260. }
  261. $initPrice = $model->details[0]->initial_weight_price[$to1][$to2];
  262. $additionalPrice = $model->details[0]->additional_weight_price[$to1][$to2];
  263. if ($weight>$model->initial_weight){
  264. $weight -= $model->initial_weight;
  265. $amount = ceil($weight/$model->additional_weight);
  266. $GLOBALS["FEE_INFO"]["additional_weight_amount"] = $amount; //续重量向上取整 例:不足1为1
  267. $money = ($amount*$additionalPrice)+$initPrice;
  268. }else $money = $initPrice;
  269. $GLOBALS["FEE_INFO"]["initial_weight"] = $model->initial_weight ?: 0;
  270. $GLOBALS["FEE_INFO"]["additional_weight"] = $model->additional_weight ?: 0;
  271. $GLOBALS["FEE_INFO"]["initial_weight_price"] = $initPrice ?: 0;
  272. $GLOBALS["FEE_INFO"]["additional_weight_price"] = $additionalPrice ?:0;
  273. $taxRate = app("OwnerService")->getTaxRateFee($model, $ownerId);//获取税率
  274. $taxFee = $money*($taxRate/100);
  275. $GLOBALS["FEE_INFO"]["tax_rate"] = $taxRate;
  276. return array($money,$taxFee);
  277. }
  278. }