OwnerPriceExpressService.php 10 KB

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