| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace App\Services;
- use App\Logistic;
- use App\Owner;
- use App\OwnerPriceExpress;
- use App\OwnerPriceExpressProvince;
- use App\Services\common\QueryService;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\DB;
- use App\Traits\ServiceAppAop;
- class OwnerPriceExpressService
- {
- use ServiceAppAop;
- protected $modelClass=OwnerPriceExpress::class;
- public function paginate($id = null)
- {
- $query = OwnerPriceExpress::query()->with(["owners","logistics"])
- ->orderByDesc("id");
- if ($id)$query->where("id",$id);
- return $query->paginate(50);
- }
- public function find($id, $withs = [])
- {
- return OwnerPriceExpress::query()->with($withs)->find($id);
- }
- /**
- * 拷贝目标数据
- *
- * @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 = OwnerPriceExpress::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 OwnerPriceExpress $copyModel */
- $copyModel = OwnerPriceExpress::query()->create($values);
- if ($owners===null){
- $query = DB::raw("SELECT * FROM owner_price_express_owner WHERE owner_price_express_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_express_logistic WHERE owner_price_express_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 = ["province_id","initial_weight_price","additional_weight_price"];
- 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 OwnerPriceExpressProvince $item */
- $obj = $item->toArray();
- unset($obj["id"]);
- }
- $obj["owner_price_express_id"] = $copyModel->id;
- $insert[] = $obj;
- }
- }else{
- foreach ($items as $item){
- $item["owner_price_express_id"] = $copyModel->id;
- $insert[] = $item;
- }
- }
- if ($insert)OwnerPriceExpressProvince::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,OwnerPriceExpress::query(),$ownerId,$ids);
- if ($result["delete"])$this->destroy($result["delete"]);
- if ($result["update"])OwnerPriceExpress::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
- }
- public function updateDetail(array $params, array $values)
- {
- $query = OwnerPriceExpressProvince::query();
- foreach ($params as $column => $param){
- $query->where($column,$param);
- }
- return $query->update($values);
- }
- public function create(array $params)
- {
- $params["operation"] = "U";
- return OwnerPriceExpress::query()->create($params);
- }
- public function createDetail(array $params)
- {
- return OwnerPriceExpressProvince::query()->create($params);
- }
- public function isExistDetail(array $params)
- {
- $query = OwnerPriceExpressProvince::query();
- foreach ($params as $column => $param){
- $query->where($column,$param);
- }
- return $query->count();
- }
- public function destroyDetail($id)
- {
- return OwnerPriceExpressProvince::destroy($id);
- }
- public function getExistOwnerName($owner_id, $logistic_id, $id=null, $type = 'ownerPriceExpresses') :array
- {
- if (!is_array($owner_id))$owner_id = [$owner_id];
- if (!is_array($logistic_id))$logistic_id = [$logistic_id];
- $owners = Owner::query()->with([$type=>function($query)use($id){
- /** @var Builder $query */
- if ($id)$query->where("id","!=",$id);
- $query->with(["logistics"]);
- }])->whereIn("id",$owner_id)->get();
- $result = [];
- foreach ($owners as $owner){
- $arr = [];
- if ($owner->ownerPriceExpresses){
- foreach ($owner->ownerPriceExpresses as $express){
- if ($express->logistics){
- foreach ($express->logistics as $logistic){
- if (array_search($logistic->id,$logistic_id) !== false)$arr[] = $logistic->name;
- }
- }
- }
- }
- if (count($arr)>0)$result[] = "“".$owner->name."”已绑定:".implode(",",$arr);
- }
- return $result;
- }
- public function getExistLogisticName($logistic_id, $id=null):array
- {
- $logistics = Logistic::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
- if ($id)$query->where("id","!=",$id);
- }])->whereIn("id",$logistic_id)->get();
- $arr = [];
- foreach ($logistics as $logistic){
- if ($logistic->owner_price_expresses_count > 0)$arr[] = $logistic->name;
- }
- return $arr;
- }
- public function destroy($id)
- {
- if (!is_array($id))$id = [$id];
- OwnerPriceExpressProvince::query()->whereIn("owner_price_express_id",$id)->delete();
- $query = "IN (";
- for ($i=0;$i<count($id)-1;$i++)$query .= "{$id[$i]},";
- $query .= "{$id[count($id)-1]})";
- $sql = "SELECT * FROM owner_price_express_owner WHERE owner_price_express_id {$query}";
- $owners = array_column(DB::select(DB::raw($sql)),"owner_id");
- DB::table("owner_price_express_owner")->whereIn("owner_price_express_id",$id)->delete();
- app("OwnerService")->refreshRelevance($owners,2,true);
- DB::table("owner_price_express_logistic")->whereIn("owner_price_express_id",$id)->delete();
- return OwnerPriceExpress::destroy($id);
- }
- public function update(array $params, array $values)
- {
- $query = OwnerPriceExpress::query();
- foreach ($params as $column=>$param){
- $query->where($column,$params);
- }
- return $query->update($values);
- }
- /**
- * 获取绑定承运商
- *
- * @param $owner
- * @return array
- */
- public function getBuildLogistic($owner)
- {
- return app(CacheService::class)->getOrExecute("logistics_owner_".$owner,function ()use($owner){
- $query = DB::raw(<<<sql
- SELECT logistic_id FROM `owner_price_express_owner` e
- LEFT JOIN owner_price_express_logistic l
- ON e.owner_price_express_id = l.owner_price_express_id
- WHERE e.owner_id = ?
- sql
- );
- $logistics = DB::select($query,[$owner]);
- return array_column($logistics,"logistic_id");
- },null);
- }
- /**
- * CODE: -1:未找到计费模型 -2:重量无效
- *
- * @param double $weight
- * @param integer $owner_id
- * @param integer $logistic_id
- * @param integer $province_id
- * @return double
- */
- public function matching($weight, $owner_id, $logistic_id, $province_id)
- {
- if (!$weight)return -2;
- $model = OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){
- /** @var Builder $query */
- $query->where("province_id",$province_id);
- }])->whereHas("owners",function ($query)use($owner_id){
- /** @var Builder $query */
- $query->where("id",$owner_id);
- })->whereHas("logistics",function ($query)use($logistic_id){
- /** @var Builder $query */
- $query->where("id",$logistic_id);
- })->where(function(Builder $query){
- $query->whereNull("operation")->orWhere("operation","");
- })->first();
- if (!$model || count($model->details)<1)return -1;
- if ($weight <= $model->initial_weight)return $model->details[0]->initial_weight_price;
- $weight -= $model->initial_weight;
- return (ceil($weight/$model->additional_weight)*$model->details[0]->additional_weight_price)+$model->details[0]->initial_weight_price;
- }
- }
|