OwnerPriceLogisticService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerPriceLogistic;
  4. use App\OwnerPriceLogisticDetail;
  5. use App\Services\common\QueryService;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Support\Facades\DB;
  8. use App\Traits\ServiceAppAop;
  9. class OwnerPriceLogisticService
  10. {
  11. use ServiceAppAop;
  12. protected $modelClass=OwnerPriceLogistic::class;
  13. public function paginate($id = null)
  14. {
  15. $query = OwnerPriceLogistic::query()->with(["owners","logistics","unit","otherUnit"]);
  16. if ($id)$query->where("id",$id);
  17. return $query->paginate(50);
  18. }
  19. public function create(array $params)
  20. {
  21. $params["operation"] = "C";
  22. return OwnerPriceLogistic::query()->create($params);
  23. }
  24. public function find($id, $withs=[])
  25. {
  26. return OwnerPriceLogistic::query()->with($withs)->find($id);
  27. }
  28. public function update(array $params, array $values)
  29. {
  30. $query = OwnerPriceLogistic::query();
  31. foreach ($params as $column=>$param){
  32. $query->where($column,$params);
  33. }
  34. return $query->update($values);
  35. }
  36. /**
  37. * 拷贝目标数据
  38. *
  39. * @param object|int $model
  40. * @param array $values
  41. * @param array|null $owners
  42. * @param array|null $logistics
  43. * @param array $items
  44. * @param bool $isLoadItem
  45. *
  46. * @return object|null
  47. */
  48. public function copy($model, $values = [], $owners = null, $logistics = null, $items = [], $isLoadItem = true)
  49. {
  50. if (is_integer($model))$model = OwnerPriceLogistic::query()->find($model);
  51. if (!$model)return null;
  52. $values["operation"] = "U";
  53. $values["target_id"] = $model->id;
  54. foreach ($model->getFillable() as $column){
  55. if (!array_key_exists($column,$values))$values[$column] = $model[$column];
  56. }
  57. /** @var OwnerPriceLogistic $copyModel */
  58. $copyModel = OwnerPriceLogistic::query()->create($values);
  59. if ($owners===null){
  60. $query = DB::raw("SELECT * FROM owner_price_logistic_owner WHERE owner_price_logistic_id = {$model->id}");
  61. $owners = array_column(DB::select($query),"owner_id");
  62. }
  63. $copyModel->owners()->sync($owners);
  64. if ($logistics===null){
  65. $query = DB::raw("SELECT * FROM owner_price_logistic_logistic WHERE owner_price_logistic_id = {$model->id}");
  66. $logistics = array_column(DB::select($query),"logistic_id");
  67. }
  68. $copyModel->logistics()->sync($logistics);
  69. $insert = [];
  70. if ($isLoadItem){
  71. $model->loadMissing("details");
  72. /** @var \stdClass $model */
  73. foreach ($model->details as $item){
  74. $columns = ["unit_id", "range", "province_id", "city_id", "unit_price", "delivery_fee", "initial_fee", "initial_amount", "rate",];
  75. if ($items[$item->id] ?? false){
  76. foreach ($columns as $column){
  77. if (!array_key_exists($column,$items[$item->id]))$items[$item->id][$column] = $item[$column];
  78. }
  79. $obj = $items[$item->id];
  80. unset($items[$item->id]);
  81. }else{
  82. /** @var OwnerPriceLogisticDetail $item */
  83. $obj = $item->toArray();
  84. unset($obj["id"]);
  85. }
  86. $obj["owner_price_logistic_id"] = $copyModel->id;
  87. $insert[] = $obj;
  88. }
  89. }else{
  90. foreach ($items as $item){
  91. $item["owner_price_logistic_id"] = $copyModel->id;
  92. $insert[] = $item;
  93. }
  94. }
  95. if ($insert)OwnerPriceLogisticDetail::query()->insert($insert);
  96. return $copyModel;
  97. }
  98. /**
  99. * 审核或恢复目标集
  100. *
  101. * @param bool $isAudit
  102. * @param integer|null|array $ownerId
  103. * @param integer|null|array $ids
  104. */
  105. public function auditOrRecover($isAudit = true, $ownerId = null, $ids = null)
  106. {
  107. if (!$ownerId && !$ids)return;
  108. $result = app(QueryService::class)->priceModelAuditOrRecoverQuery($isAudit,OwnerPriceLogistic::query(),$ownerId,$ids);
  109. if ($result["delete"])$this->destroy($result["delete"]);
  110. if ($result["update"])OwnerPriceLogistic::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
  111. }
  112. public function destroy($id)
  113. {
  114. if (!is_array($id))$id = [$id];
  115. OwnerPriceLogisticDetail::query()->whereIn("owner_price_logistic_id",$id)->delete();
  116. $query = "IN (";
  117. for ($i=0;$i<count($id)-1;$i++)$query .= "{$id[$i]},";
  118. $query .= "{$id[count($id)-1]})";
  119. $sql = "SELECT * FROM owner_price_logistic_owner WHERE owner_price_logistic_id {$query}";
  120. $owners = array_column(DB::select(DB::raw($sql)),"owner_id");
  121. DB::table("owner_price_logistic_owner")->whereIn("owner_price_logistic_id",$id)->delete();
  122. app("OwnerService")->refreshRelevance($owners,3,true);
  123. DB::table("owner_price_logistic_logistic")->whereIn("owner_price_logistic_id",$id)->delete();
  124. return OwnerPriceLogistic::destroy($id);
  125. }
  126. public function updateDetail(array $params, array $values)
  127. {
  128. $query = OwnerPriceLogisticDetail::query();
  129. foreach ($params as $column => $param){
  130. $query->where($column,$param);
  131. }
  132. return $query->update($values);
  133. }
  134. public function isExistDetail(array $params)
  135. {
  136. $query = OwnerPriceLogisticDetail::query();
  137. foreach ($params as $column => $param){
  138. $query->where($column,$param);
  139. }
  140. return $query->count();
  141. }
  142. public function createDetail(array $params)
  143. {
  144. return OwnerPriceLogisticDetail::query()->create($params);
  145. }
  146. public function destroyDetail($id)
  147. {
  148. return OwnerPriceLogisticDetail::destroy($id);
  149. }
  150. public function checkRange($range):bool
  151. {
  152. $arrRanges = explode(",",$range);
  153. $len = count($arrRanges);
  154. if ($len==0)return false; //范围为空 false
  155. $previous = []; //慢指针数组 不记录第一个值
  156. foreach ($arrRanges as $index => $value){
  157. if ($index == $len-1)$preg = "/\d*\-/"; //最后一个范围不允许封闭
  158. else $preg = "/\d*\-\d*/";
  159. preg_match($preg, $value, $match);
  160. if (!$match || $match[0]!=$value)return false;//匹配结果不等于值 false
  161. $arr = explode("-",$value); //二次切割判断是否连续
  162. if ($index != 0){
  163. if ($index == $len-1){
  164. if ((int)$arr[0] != (int)$previous[$index-1][1])return false; //最后一个范围只需要判断起始值
  165. }else{
  166. if ((int)$arr[0] != (int)$previous[$index-1][1] || (int)$arr[1] <= (int)$arr[0])return false; //普通范围起始值必须为上一个结束值 当前结束值必须大于起始值
  167. }
  168. }else{
  169. if ((int)$arr[1] <= (int)$arr[0])return false; //第一个范围只需判断当前结束值大于起始值
  170. }
  171. $previous[] = $arr;
  172. }
  173. return true;
  174. }
  175. /**
  176. * CODE: -1:未找到计费模型
  177. *
  178. * @param double $amount
  179. * @param integer $owner_id
  180. * @param integer $logistic_id
  181. * @param integer $unit_id
  182. * @param integer $province_id
  183. * @param integer $city_id
  184. * @return double
  185. */
  186. public function matching($amount, $owner_id, $logistic_id, $unit_id, $province_id, $city_id)
  187. {
  188. $model = OwnerPriceLogistic::query()->with(["details"=>function($query)use($province_id,$city_id){
  189. /** @var Builder $query */
  190. $query->where("province_id",$province_id)->where("city_id",$city_id);
  191. }])->where(function ($query)use($unit_id){
  192. /** @var Builder $query */
  193. $query->where("unit_id",$unit_id)->orWhere("other_unit_id",$unit_id);
  194. })->whereHas("owners",function ($query)use($owner_id){
  195. /** @var Builder $query */
  196. $query->where("id",$owner_id);
  197. })->whereHas("logistics",function ($query)use($logistic_id){
  198. /** @var Builder $query */
  199. $query->where("id",$logistic_id);
  200. })->where(function(Builder $query){
  201. $query->whereNull("operation")->orWhere("operation","");
  202. })->first();
  203. if (!$model || !$model->details)return -1;
  204. $fee = $model->pick_up_price + $model->fuel_price + $model->service_price; //服务费
  205. foreach ($model->details as $detail){
  206. if ($unit_id != $detail->unit_id)continue;
  207. $arr = explode("-",$detail->range);
  208. if (count($arr) < 2 || !$arr[1]){
  209. if ($amount >= $arr[0])return $this->calculation($amount,$detail,$fee);
  210. }else{
  211. if ($amount >= $arr[0] && $amount < $arr[1])return $this->calculation($amount,$detail,$fee);
  212. }
  213. }
  214. return -1;
  215. }
  216. private function calculation($amount, $detail, $fee)
  217. {
  218. if ($amount < $detail->initial_amount)$amount = $detail->initial_amount; //小于起始数以起始数为准
  219. $money = $amount * $detail->unit_price;
  220. if ($money < $detail->initial_fee)$money = $detail->initial_fee; //小于起始计费以起始计费为准
  221. return $money+$detail->delivery_fee+$fee;
  222. }
  223. }