| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace App\Services;
- use App\Owner;
- use App\OwnerPriceLogistic;
- use App\OwnerPriceLogisticDetail;
- use App\Services\common\QueryService;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\DB;
- use App\Traits\ServiceAppAop;
- class OwnerPriceLogisticService
- {
- use ServiceAppAop;
- protected $modelClass=OwnerPriceLogistic::class;
- public function paginate($id = null)
- {
- $query = OwnerPriceLogistic::query()->with(["owners","logistics","unit","otherUnit"]);
- if ($id)$query->where("id",$id);
- return $query->paginate(50);
- }
- public function create(array $params)
- {
- $params["operation"] = "C";
- return OwnerPriceLogistic::query()->create($params);
- }
- public function find($id, $withs=[])
- {
- return OwnerPriceLogistic::query()->with($withs)->find($id);
- }
- public function update(array $params, array $values)
- {
- $query = OwnerPriceLogistic::query();
- foreach ($params as $column=>$param){
- $query->where($column,$params);
- }
- return $query->update($values);
- }
- /**
- * 拷贝目标数据
- *
- * @param object|int $model
- * @param array $values
- * @param array|null $owners
- * @param array|null $logistics
- * @param array $items
- * @param bool $isLoadItem
- *
- * @return object|null
- */
- public function copy($model, $values = [], $owners = null, $logistics = null, $items = [], $isLoadItem = true)
- {
- if (is_integer($model))$model = OwnerPriceLogistic::query()->find($model);
- if (!$model)return null;
- $values["operation"] = "U";
- $values["target_id"] = $model->id;
- foreach ($model->getFillable() as $column){
- if (!array_key_exists($column,$values))$values[$column] = $model[$column];
- }
- /** @var OwnerPriceLogistic $copyModel */
- $copyModel = OwnerPriceLogistic::query()->create($values);
- if ($owners===null){
- $query = DB::raw("SELECT * FROM owner_price_logistic_owner WHERE owner_price_logistic_id = {$model->id}");
- $owners = array_column(DB::select($query),"owner_id");
- }
- $copyModel->owners()->sync($owners);
- if ($logistics===null){
- $query = DB::raw("SELECT * FROM owner_price_logistic_logistic WHERE owner_price_logistic_id = {$model->id}");
- $logistics = array_column(DB::select($query),"logistic_id");
- }
- $copyModel->logistics()->sync($logistics);
- $insert = [];
- if ($isLoadItem){
- $model->loadMissing("details");
- /** @var \stdClass $model */
- foreach ($model->details as $item){
- $columns = ["unit_id", "range", "province_id", "city_id", "unit_price", "delivery_fee", "initial_fee", "initial_amount", "rate",];
- if ($items[$item->id] ?? false){
- foreach ($columns as $column){
- if (!array_key_exists($column,$items[$item->id]))$items[$item->id][$column] = $item[$column];
- }
- $obj = $items[$item->id];
- unset($items[$item->id]);
- }else{
- /** @var OwnerPriceLogisticDetail $item */
- $obj = $item->toArray();
- unset($obj["id"]);
- }
- $obj["owner_price_logistic_id"] = $copyModel->id;
- $insert[] = $obj;
- }
- }else{
- foreach ($items as $item){
- $item["owner_price_logistic_id"] = $copyModel->id;
- $insert[] = $item;
- }
- }
- if ($insert)OwnerPriceLogisticDetail::query()->insert($insert);
- return $copyModel;
- }
- /**
- * 审核或恢复目标集
- *
- * @param bool $isAudit
- * @param integer|null|array $ownerId
- * @param integer|null|array $ids
- */
- public function auditOrRecover($isAudit = true, $ownerId = null, $ids = null)
- {
- if (!$ownerId && !$ids)return;
- $result = app(QueryService::class)->priceModelAuditOrRecoverQuery($isAudit,OwnerPriceLogistic::query(),$ownerId,$ids);
- if ($result["delete"])$this->destroy($result["delete"]);
- if ($result["update"])OwnerPriceLogistic::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
- }
- public function destroy($id)
- {
- if (!is_array($id))$id = [$id];
- OwnerPriceLogisticDetail::query()->whereIn("owner_price_logistic_id",$id)->delete();
- DB::table("owner_price_logistic_owner")->whereIn("owner_price_logistic_id",$id)->delete();
- DB::table("owner_price_logistic_logistic")->whereIn("owner_price_logistic_id",$id)->delete();
- return OwnerPriceLogistic::destroy($id);
- }
- public function updateDetail(array $params, array $values)
- {
- $query = OwnerPriceLogisticDetail::query();
- foreach ($params as $column => $param){
- $query->where($column,$param);
- }
- return $query->update($values);
- }
- public function isExistDetail(array $params)
- {
- $query = OwnerPriceLogisticDetail::query();
- foreach ($params as $column => $param){
- $query->where($column,$param);
- }
- return $query->count();
- }
- public function createDetail(array $params)
- {
- return OwnerPriceLogisticDetail::query()->create($params);
- }
- public function destroyDetail($id)
- {
- return OwnerPriceLogisticDetail::destroy($id);
- }
- public function checkRange($range):bool
- {
- $arrRanges = explode(",",$range);
- $len = count($arrRanges);
- if ($len==0)return false; //范围为空 false
- $previous = []; //慢指针数组 不记录第一个值
- foreach ($arrRanges as $index => $value){
- if ($index == $len-1)$preg = "/\d*\-/"; //最后一个范围不允许封闭
- else $preg = "/\d*\-\d*/";
- preg_match($preg, $value, $match);
- if (!$match || $match[0]!=$value)return false;//匹配结果不等于值 false
- $arr = explode("-",$value); //二次切割判断是否连续
- if ($index != 0){
- if ($index == $len-1){
- if ((int)$arr[0] != (int)$previous[$index-1][1])return false; //最后一个范围只需要判断起始值
- }else{
- if ((int)$arr[0] != (int)$previous[$index-1][1] || (int)$arr[1] <= (int)$arr[0])return false; //普通范围起始值必须为上一个结束值 当前结束值必须大于起始值
- }
- }else{
- if ((int)$arr[1] <= (int)$arr[0])return false; //第一个范围只需判断当前结束值大于起始值
- }
- $previous[] = $arr;
- }
- return true;
- }
- /**
- *
- * @param double $amount
- * @param integer $ownerId
- * @param integer $logisticId
- * @param integer $unitId
- * @param integer $provinceId
- * @param integer $cityId
- * @return array
- */
- public function matching(float $amount, int $ownerId, int $logisticId, int $unitId, int $provinceId, int $cityId):array
- {
- $model = OwnerPriceLogistic::query()->with(["details"=>function($query)use($provinceId,$cityId){
- /** @var Builder $query */
- $query->where("province_id",$provinceId)->where("city_id",$cityId);
- }])->where(function ($query)use($unitId){
- /** @var Builder $query */
- $query->where("unit_id",$unitId)->orWhere("other_unit_id",$unitId);
- })->whereHas("owners",function ($query)use($ownerId){
- /** @var Builder $query */
- $query->where("id",$ownerId);
- })->whereHas("logistics",function ($query)use($logisticId){
- /** @var Builder $query */
- $query->where("id",$logisticId);
- })->where(function(Builder $query){
- $query->whereNull("operation")->orWhere("operation","");
- })->first();
- if (!$model || !$model->details)return array(null,null);
- $money = null;
- $GLOBALS["FEE_INFO"]["pick_fee"] = $model->pick_up_price;
- $GLOBALS["FEE_INFO"]["fuel_fee"] = $model->fuel_price;
- $GLOBALS["FEE_INFO"]["info_fee"] = $model->service_price;
- $GLOBALS["FEE_INFO"]["other_fee"] = 0;
- $fee = $model->pick_up_price + $model->fuel_price + $model->service_price; //服务费
- foreach ($model->details as $detail){
- if ($unitId != $detail->unit_id)continue;
- $arr = explode("-",$detail->range);
- if (count($arr) < 2 || !$arr[1]){
- if ($amount >= $arr[0])$money = $this->calculation($amount,$detail,$fee);
- }else{
- if ($amount >= $arr[0] && $amount < $arr[1])$money = $this->calculation($amount,$detail,$fee);
- }
- if ($money)break;
- }
- $taxRate = app("OwnerService")->getTaxRateFee($model, $ownerId);
- $taxFee = $money*($taxRate/100);
- $GLOBALS["FEE_INFO"]["tax_rate"] = $taxRate;
- $GLOBALS["FEE_INFO"]["total_fee"] = $money;
- return array($money,$taxFee);
- }
- private function calculation($amount, $detail, $fee)
- {
- $GLOBALS["FEE_INFO"]["interval"] = $detail->range;
- $GLOBALS["FEE_INFO"]["price"] = $detail->unit_price;
- $GLOBALS["FEE_INFO"]["delivery_fee"] = $detail->delivery_fee;
- $GLOBALS["FEE_INFO"]["initial_fee"] = $detail->initial_fee;
- $GLOBALS["FEE_INFO"]["initial_amount"] = $detail->initial_amount;
- if ($amount < $detail->initial_amount)$amount = $detail->initial_amount; //小于起始数以起始数为准
- $money = $amount * $detail->unit_price;
- if ($money < $detail->initial_fee)$money = $detail->initial_fee; //小于起始计费以起始计费为准
- return $money+$detail->delivery_fee+$fee;
- }
- }
|