OwnerPriceExpressService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. $query = "IN (";
  180. for ($i=0;$i<count($id)-1;$i++)$query .= "{$id[$i]},";
  181. $query .= "{$id[count($id)-1]})";
  182. $sql = "SELECT * FROM owner_price_express_owner WHERE owner_price_express_id {$query}";
  183. $owners = array_column(DB::select(DB::raw($sql)),"owner_id");
  184. DB::table("owner_price_express_owner")->whereIn("owner_price_express_id",$id)->delete();
  185. app("OwnerService")->refreshRelevance($owners,2,true);
  186. DB::table("owner_price_express_logistic")->whereIn("owner_price_express_id",$id)->delete();
  187. return OwnerPriceExpress::destroy($id);
  188. }
  189. public function update(array $params, array $values)
  190. {
  191. $query = OwnerPriceExpress::query();
  192. foreach ($params as $column=>$param){
  193. $query->where($column,$params);
  194. }
  195. return $query->update($values);
  196. }
  197. /**
  198. * 获取绑定承运商
  199. *
  200. * @param $owner
  201. * @return array
  202. */
  203. public function getBuildLogistic($owner)
  204. {
  205. return app(CacheService::class)->getOrExecute("logistics_owner_".$owner,function ()use($owner){
  206. $query = DB::raw(<<<sql
  207. SELECT logistic_id FROM `owner_price_express_owner` e
  208. LEFT JOIN owner_price_express_logistic l
  209. ON e.owner_price_express_id = l.owner_price_express_id
  210. WHERE e.owner_id = ?
  211. sql
  212. );
  213. $logistics = DB::select($query,[$owner]);
  214. return array_column($logistics,"logistic_id");
  215. },null);
  216. }
  217. /**
  218. * @param integer $owner
  219. * @param integer $logistic
  220. * @param integer $province
  221. *
  222. * @return OwnerPriceExpress|\stdClass|null
  223. */
  224. public function getOwnerPriceExpress($owner,$logistic,$province)
  225. {
  226. return Cache::tags("expressFeeOwner:".$owner)->remember("expressFee:".$owner.$logistic.$province,config("cache.expirations.rarelyChange"),
  227. function ()use($owner,$logistic,$province){
  228. return OwnerPriceExpress::query()->with(["details"=>function($query)use($province){
  229. /** @var Builder $query */
  230. $query->where("province_id",$province);
  231. }])->whereHas("owners",function ($query)use($owner){
  232. /** @var Builder $query */
  233. $query->where("id",$owner);
  234. })->whereHas("logistics",function ($query)use($logistic){
  235. /** @var Builder $query */
  236. $query->where("id",$logistic);
  237. })->where(function(Builder $query){
  238. $query->whereNull("operation")->orWhere("operation","");
  239. })->first();
  240. });
  241. }
  242. /**
  243. *
  244. * @param double $weight
  245. * @param integer $owner_id
  246. * @param integer $logistic_id
  247. * @param integer $province_id
  248. * @return array
  249. */
  250. public function matching($weight, $owner_id, $logistic_id, $province_id)
  251. {
  252. if (!$weight)return array(null,null);
  253. $model = $this->getOwnerPriceExpress($owner_id,$logistic_id,$province_id);
  254. if (!$model || count($model->details)<1)return array(null,null);
  255. if ($model->amount_interval){
  256. $total = app("OrderService")->getOrderQuantity($owner_id)+1;//获取该货主本月C端单量
  257. for ($i=count($model->amount_interval);$i<0;$i--){
  258. if ($total>=$model->amount_interval[$i]){$to1 = $i;break;}
  259. }
  260. if (isset($to1) && isset($model->weight_interval[$to1])){
  261. for ($i=count($model->weight_interval[$to1]);$i<0;$i--){
  262. if ($weight>=$model->weight_interval[$to1][$i]){$to2 = $i;break;}
  263. }
  264. }
  265. }
  266. if (!isset($to1))$to1 = 0;
  267. if (!isset($to2))$to2 = 0;
  268. $initPrice = $model->details[0]->initial_weight_price[$to1][$to2];
  269. $additionalPrice = $model->details[0]->additional_weight_price[$to1][$to2];
  270. if ($weight <= $model->initial_weight)$money = $initPrice;
  271. else{
  272. $weight -= $model->initial_weight;
  273. $money = (ceil($weight/$model->additional_weight)*$additionalPrice)+$initPrice;
  274. }
  275. if ($model->tax_rate_id && $model->taxRate){
  276. $taxFee = $money*($model->taxRate->value/100);
  277. }else{
  278. /** @var Owner|\stdClass $owner */
  279. $owner = Owner::query()->with("taxRate")
  280. ->whereHas("taxRate")->find($owner_id);
  281. if ($owner)$taxFee = $money*($owner->taxRate->value/100);
  282. else $taxFee = null;
  283. }
  284. return array($money,$taxFee);
  285. }
  286. }