|
|
@@ -1096,7 +1096,7 @@ class PriceModelController extends Controller
|
|
|
$this->gate("客户管理-项目-录入");
|
|
|
$params = request()->input();
|
|
|
$params["logistic_id"] = $params["logistics"];
|
|
|
- $errors = $this->expressValidator($params)->errors();
|
|
|
+ $errors = $this->expressValidator($params,request("id"))->errors();
|
|
|
$exist = [];
|
|
|
foreach ($params["items"] as $index => $item){
|
|
|
if (isset($exist[$item["province_id"]]))$errors["items.".$index.".province_id"] = ["已存在"];
|
|
|
@@ -1104,19 +1104,51 @@ class PriceModelController extends Controller
|
|
|
}
|
|
|
if (count($errors)>0)$this->success(["errors"=>$errors]);
|
|
|
|
|
|
- DB::transaction(function ()use(&$model,$params){
|
|
|
- $model = app('OwnerPriceExpressService')->create([
|
|
|
- "name" => request("name"),
|
|
|
- "initial_weight" => request("initial_weight"),
|
|
|
- "additional_weight" => request("additional_weight"),
|
|
|
- ]);
|
|
|
- foreach ($params["items"] as &$item)$item["owner_price_express_id"] = $model->id;
|
|
|
- OwnerPriceExpressProvince::query()->insert($params["items"]);
|
|
|
- DB::insert(DB::raw("INSERT INTO owner_price_express_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
|
|
|
+ $obj = [
|
|
|
+ "name" => request("name"),
|
|
|
+ "initial_weight" => request("initial_weight"),
|
|
|
+ "additional_weight" => request("additional_weight"),
|
|
|
+ ];
|
|
|
+ if (request("id")){
|
|
|
/** @var OwnerPriceExpress $model */
|
|
|
- $model->logistics()->syncWithoutDetaching(request("logistics"));
|
|
|
- DB::commit();
|
|
|
- });
|
|
|
+ $model = app('OwnerPriceExpressService')->find(request("id"),["details"]);
|
|
|
+ app('OwnerPriceExpressService')->update(["id"=>request("id")],$obj);
|
|
|
+
|
|
|
+ $delete = [];//需要删除子项
|
|
|
+ $update = [["id","province_id","initial_weight_price","additional_weight_price"]];//需要更新子项
|
|
|
+ $insert = [];//需要新增子项
|
|
|
+ foreach ($params["items"] as $item){
|
|
|
+ $obj = [
|
|
|
+ "province_id" => $item["province_id"],
|
|
|
+ "initial_weight_price" => $item["initial_weight_price"],
|
|
|
+ "additional_weight_price" => $item["additional_weight_price"],
|
|
|
+ ];
|
|
|
+ if (isset($item["id"])){
|
|
|
+ $obj["id"] = $item["id"];
|
|
|
+ $update[] = $obj;
|
|
|
+ $delete[] = $item["id"];
|
|
|
+ } else{
|
|
|
+ $obj["owner_price_express_id"] = $model->id;
|
|
|
+ $insert[] = $obj;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $ids = array_column($model->details->toArray(),"id");
|
|
|
+ $delete = array_diff($ids,$delete);
|
|
|
+ if ($delete)OwnerPriceExpressProvince::destroy($delete);
|
|
|
+ if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_express_provinces",$update);
|
|
|
+ if ($insert)OwnerPriceExpressProvince::query()->insert($insert);
|
|
|
+ $model->logistics()->sync(request("logistics"));
|
|
|
+ }else{
|
|
|
+ DB::transaction(function ()use(&$model,$params,$obj){
|
|
|
+ $model = app('OwnerPriceExpressService')->create($obj);
|
|
|
+ foreach ($params["items"] as &$item)$item["owner_price_express_id"] = $model->id;
|
|
|
+ OwnerPriceExpressProvince::query()->insert($params["items"]);
|
|
|
+ DB::insert(DB::raw("INSERT INTO owner_price_express_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
|
|
|
+ /** @var OwnerPriceExpress $model */
|
|
|
+ $model->logistics()->syncWithoutDetaching(request("logistics"));
|
|
|
+ DB::commit();
|
|
|
+ });
|
|
|
+ }
|
|
|
$model->load("details");
|
|
|
$this->success($model);
|
|
|
}
|
|
|
@@ -1127,7 +1159,7 @@ class PriceModelController extends Controller
|
|
|
$params = request()->input();
|
|
|
$params["owner_id"] = [$params["owner_id"]];
|
|
|
$params["logistic_id"] = $params["logistics"];
|
|
|
- $errors = $this->logisticValidator($params)->errors();
|
|
|
+ $errors = $this->logisticValidator($params,request("id"))->errors();
|
|
|
$exist = [];
|
|
|
foreach ($params["items"] as $index => $item){
|
|
|
$key = $item["unit_id"]."-".$item["range"]."-".$item["province_id"]."-".$item["city_id"];
|
|
|
@@ -1135,24 +1167,63 @@ class PriceModelController extends Controller
|
|
|
else $exist[$key] = true;
|
|
|
}
|
|
|
if (count($errors)>0)$this->success(["errors"=>$errors]);
|
|
|
- DB::transaction(function ()use(&$model,$params){
|
|
|
- $model = app("OwnerPriceLogisticService")->create([
|
|
|
- "name" => request('name'),
|
|
|
- "unit_range" => request('unit_range'),
|
|
|
- "unit_id" => request('unit_id'),
|
|
|
- "other_unit_range" => request('other_unit_range'),
|
|
|
- "other_unit_id" => request('other_unit_id'),
|
|
|
- "pick_up_price" => request('pick_up_price'),
|
|
|
- "fuel_price" => request('fuel_price'),
|
|
|
- "service_price" => request('service_price'),
|
|
|
- ]);
|
|
|
- foreach ($params["items"] as &$param)$param["owner_price_logistic_id"] = $model->id;
|
|
|
- OwnerPriceLogisticDetail::query()->insert($params["items"]);
|
|
|
- DB::insert(DB::raw("INSERT INTO owner_price_logistic_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
|
|
|
+
|
|
|
+ $obj = [
|
|
|
+ "name" => request('name'),
|
|
|
+ "unit_range" => request('unit_range'),
|
|
|
+ "unit_id" => request('unit_id'),
|
|
|
+ "other_unit_range" => request('other_unit_range'),
|
|
|
+ "other_unit_id" => request('other_unit_id'),
|
|
|
+ "pick_up_price" => request('pick_up_price'),
|
|
|
+ "fuel_price" => request('fuel_price'),
|
|
|
+ "service_price" => request('service_price'),
|
|
|
+ ];
|
|
|
+ if (request("id")){
|
|
|
+ $model = app("OwnerPriceLogisticService")->find(request("id"),["details"]);
|
|
|
+ app("OwnerPriceLogisticService")->update(["id"=>request("id")],$obj);
|
|
|
+
|
|
|
+ $delete = [];//需要删除子项
|
|
|
+ $update = [["id","unit_id","range","province_id","city_id","unit_price","delivery_fee","initial_fee","initial_amount","rate"]];//需要更新子项
|
|
|
+ $insert = [];//需要新增子项
|
|
|
+ foreach ($params["items"] as $item){
|
|
|
+ $obj = [
|
|
|
+ "unit_id" => $item["unit_id"],
|
|
|
+ "range" => $item["range"],
|
|
|
+ "province_id" => $item["province_id"],
|
|
|
+ "city_id" => $item["city_id"],
|
|
|
+ "unit_price" => $item["unit_price"],
|
|
|
+ "delivery_fee" => $item["delivery_fee"],
|
|
|
+ "initial_fee" => $item["initial_fee"],
|
|
|
+ "initial_amount" => $item["initial_amount"],
|
|
|
+ "rate" => $item["rate"],
|
|
|
+ ];
|
|
|
+ if (isset($item["id"])){
|
|
|
+ $obj["id"] = $item["id"];
|
|
|
+ $update[] = $obj;
|
|
|
+ $delete[] = $item["id"];
|
|
|
+ } else{
|
|
|
+ $obj["owner_price_logistic_id"] = $model->id;
|
|
|
+ $insert[] = $obj;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $ids = array_column($model->details->toArray(),"id");
|
|
|
+ $delete = array_diff($ids,$delete);
|
|
|
+ if ($delete)OwnerPriceLogisticDetail::destroy($delete);
|
|
|
+ if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_logistic_details",$update);
|
|
|
+ if ($insert)OwnerPriceLogisticDetail::query()->insert($insert);
|
|
|
/** @var OwnerPriceLogistic $model */
|
|
|
- $model->logistics()->syncWithoutDetaching(request("logistics"));
|
|
|
- DB::commit();
|
|
|
- });
|
|
|
+ $model->logistics()->sync(request("logistics"));
|
|
|
+ }else{
|
|
|
+ DB::transaction(function ()use(&$model,$params,$obj){
|
|
|
+ $model = app("OwnerPriceLogisticService")->create($obj);
|
|
|
+ foreach ($params["items"] as &$param)$param["owner_price_logistic_id"] = $model->id;
|
|
|
+ OwnerPriceLogisticDetail::query()->insert($params["items"]);
|
|
|
+ DB::insert(DB::raw("INSERT INTO owner_price_logistic_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
|
|
|
+ /** @var OwnerPriceLogistic $model */
|
|
|
+ $model->logistics()->syncWithoutDetaching(request("logistics"));
|
|
|
+ DB::commit();
|
|
|
+ });
|
|
|
+ }
|
|
|
$model->load("details");
|
|
|
$this->success($model);
|
|
|
}
|
|
|
@@ -1160,7 +1231,7 @@ class PriceModelController extends Controller
|
|
|
public function apiStoreDirectLogistic()
|
|
|
{
|
|
|
$this->gate("客户管理-项目-录入");
|
|
|
- $errors = $this->directLogisticValidator(request()->input())->errors();
|
|
|
+ $errors = $this->directLogisticValidator(request()->input(),request("id"))->errors();
|
|
|
$exist = [];
|
|
|
foreach (request("items") as $index=>$item){
|
|
|
if (isset($exist[$item['car_type_id']]))$errors["items.".$index.".car_type_id"] = ["已存在"];
|
|
|
@@ -1169,16 +1240,47 @@ class PriceModelController extends Controller
|
|
|
if (count($errors)>0)$this->success(["errors"=>$errors]);
|
|
|
|
|
|
$items = request()->input("items");
|
|
|
- DB::transaction(function ()use(&$model,$items){
|
|
|
- $model = app("OwnerPriceDirectLogisticService")->create([
|
|
|
- "name" => request("name"),
|
|
|
- "base_km" => request("base_km"),
|
|
|
- ]);
|
|
|
- foreach ($items as &$item)$item["owner_price_direct_logistic_id"] = $model->id;
|
|
|
- OwnerPriceDirectLogisticCar::query()->insert($items);
|
|
|
- DB::insert(DB::raw("INSERT INTO owner_price_direct_logistic_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
|
|
|
- DB::commit();
|
|
|
- });
|
|
|
+ $obj = [
|
|
|
+ "name" => request("name"),
|
|
|
+ "base_km" => request("base_km"),
|
|
|
+ ];
|
|
|
+ if (request("id")){
|
|
|
+ $model = app("OwnerPriceDirectLogisticService")->find(request("id"),["details"]);
|
|
|
+ app("OwnerPriceDirectLogisticService")->update(["id"=>request("id")],$obj);
|
|
|
+
|
|
|
+ $delete = [];//需要删除子项
|
|
|
+ $update = [["id","car_type_id","base_fee","additional_fee"]];//需要更新子项
|
|
|
+ $insert = [];//需要新增子项
|
|
|
+ foreach ($items as $item){
|
|
|
+ $obj = [
|
|
|
+ "car_type_id" => $item["car_type_id"],
|
|
|
+ "base_fee" => $item["base_fee"],
|
|
|
+ "additional_fee" => $item["additional_fee"],
|
|
|
+ ];
|
|
|
+ if (isset($item["id"])){
|
|
|
+ $obj["id"] = $item["id"];
|
|
|
+ $update[] = $obj;
|
|
|
+ $delete[] = $item["id"];
|
|
|
+ } else{
|
|
|
+ $obj["owner_price_direct_logistic_id"] = $model->id;
|
|
|
+ $insert[] = $obj;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $ids = array_column($model->details->toArray(),"id");
|
|
|
+ $delete = array_diff($ids,$delete);
|
|
|
+ if ($delete)OwnerPriceDirectLogisticCar::destroy($delete);
|
|
|
+ if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_direct_logistic_cars",$update);
|
|
|
+ if ($insert)OwnerPriceDirectLogisticCar::query()->insert($insert);
|
|
|
+ }else{
|
|
|
+ DB::transaction(function ()use(&$model,$items,$obj){
|
|
|
+ $model = app("OwnerPriceDirectLogisticService")->create($obj);
|
|
|
+ foreach ($items as &$item)$item["owner_price_direct_logistic_id"] = $model->id;
|
|
|
+ OwnerPriceDirectLogisticCar::query()->insert($items);
|
|
|
+ DB::insert(DB::raw("INSERT INTO owner_price_direct_logistic_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
|
|
|
+ DB::commit();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ /** @var OwnerPriceDirectLogistic $model */
|
|
|
$model->load("details");
|
|
|
$this->success($model);
|
|
|
}
|