weizong song 5 years ago
parent 1ed6f18b04
commit 8c4adb1035

@ -55,7 +55,7 @@ class OrdersController extends CommonController
$product->min_price = (new ProductItems())->where("product_id", $product->id)->min("price");
$max_price = (new ProductItems())->where("product_id", $product->id)->max("price");
$max_paramedic_price = (new ProductParamedicLevel())->where("product_id", $product->id)->max("price");
$max_factor_total = DB::select(DB::raw("select * from (select sum(max_price) as max_total,product_id from (select max(price) as max_price,product.id as product_id from product inner join factor on factor.product_id = product.id inner join factor_items on factor.id = factor_items.factor_id group by factor_items.factor_id) as a group by a.product_id) as b left join product on product.id = b.product_id where product.id=".$product->id));
$max_factor_total = DB::select(DB::raw("select * from (select sum(max_price) as max_total,product_id from (select max(price) as max_price,product.id as product_id from product inner join factor on factor.product_id = product.id inner join factor_items on factor.id = factor_items.factor_id group by factor_items.factor_id) as a group by a.product_id) as b left join product on product.id = b.product_id where product.id=" . $product->id));
$product->max_price = ($max_price + $max_paramedic_price + $max_factor_total[0]->max_total);
return response()->json($product->toArray());
@ -126,19 +126,51 @@ class OrdersController extends CommonController
public function getAvailableParamedics(Request $request)
{
//todo:更精准的筛选器
DB::enableQueryLog();
$bed = (new Bed())->find($request->bed_id);
$date = $request->start_date ?? date("Y-m-d");
$model = new Paramedic();
$model = $model->where("project_id", $bed->project_id);
$model = $model->where("status", 1);
$model = $model->with(["levelInProject"]);
$model = $model->select("id", "name", "avatar", "sex", "birthday", "hometown", "paramedic_level_id");
//性别筛选:如果是女患者,选择女的护工
if ($request->sex == "女") {
$model = $model->where("sex", "女");
}
//todo:护工所在病区筛选
//一对N的数量排序
$model->withCount(["orders as ongoing_orders_count" => function ($query) use ($date) {
//todo:与日期及自动结单关联
$query->where("status", Orders::STATUS_ONGOING);
}]);
$model = $model->orderBy("ongoing_orders_count");
//同房间的排序
$model->withCount(["orders as ongoing_orders_count_in_same_room" => function ($query) use ($date, $bed) {
//todo:与日期及自动结单关联
$query->where("status", Orders::STATUS_ONGOING)->whereHas("room", function ($query) use ($bed) {
$query->whereRaw("`room`.`id` = {$bed->room_id}");
});
}]);
$model = $model->orderBy("ongoing_orders_count_in_same_room", "desc");
//todo:同病区其他房间的病床排序
$page_size = 3;
$paramedics = $model->limit($page_size)->select("id", "name", "avatar", "sex", "birthday", "hometown")->get();
$paramedics = $model->limit($page_size)->get();
//价格运算
$factors = (new Orders())->requestFactorsToOrderFactors();
foreach ($paramedics as $paramedic) {
$product_item = ProductItems::where("patient_quantity", "<=", $paramedic->ongoing_orders_count + 1)->orderBy("patient_quantity", "desc")->first();
$price = $paramedic->levelInProject->price + $product_item->price;
foreach ($factors as $factor) {
$price += $factor["price"];
}
$paramedic->price = $price;
}
$result = [];
$result[$date] = $paramedics->toArray();
return response()->json($result);
@ -350,15 +382,15 @@ class OrdersController extends CommonController
$product = Product::find($request->product_id);
if ($request->paramedic_id) {
$paramedic = Paramedic::withCount(["orders"=>function($query) use ($request) {
$paramedic = Paramedic::withCount(["orders" => function ($query) use ($request) {
$query->where("status", Orders::STATUS_ONGOING);
//todo:计算自动结单对当前所选日期的订单数的影响
}])->find($request->paramedic_id);
$product_paramedic_level = (new ProductParamedicLevel())->where("paramedic_level_id",$paramedic->paramedic_level_id)->first();
$product_paramedic_level = (new ProductParamedicLevel())->where("paramedic_level_id", $paramedic->paramedic_level_id)->first();
$product_item = (new ProductItems())->where("patient_quantity", "<=", $paramedic->orders_count + 1)->orderBy("patient_quantity", "desc")->first();
} else {
$product_paramedic_level = (new ProductParamedicLevel())->where("product_id", $request->product_id)->where("price", 0)->first();
$product_item = (new ProductItems())->where("product_id", $request->product_id)->where("patient_quantity",1)->first();
$product_item = (new ProductItems())->where("product_id", $request->product_id)->where("patient_quantity", 1)->first();
}
$price = $product_item->price + $product_paramedic_level->price;

@ -44,6 +44,10 @@ class Paramedic extends SoftDeletesModel
return $this->belongsTo(ParamedicLevel::class, "paramedic_level_id", "id");
}
public function levelInProject() {
return $this->hasOneThrough(ProductParamedicLevel::class, ParamedicLevel::class,"id","paramedic_level_id","paramedic_level_id","id");
}
public function orders()
{
return $this->hasMany(Orders::class);

Loading…
Cancel
Save