|
|
|
|
@ -7,6 +7,9 @@ use App\Models\Bed;
|
|
|
|
|
use App\Models\OrderItems;
|
|
|
|
|
use App\Models\Orders;
|
|
|
|
|
use App\Models\Paramedic;
|
|
|
|
|
use App\Models\Recharge;
|
|
|
|
|
use App\Models\Refund;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
|
|
@ -141,4 +144,111 @@ class StatisticsController extends CommonController
|
|
|
|
|
|
|
|
|
|
return response()->json($areas->toArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @OA\Get(
|
|
|
|
|
* path="/manager/statistics/by-duration",
|
|
|
|
|
* summary="V2-获取时间段内的统计或指定明细",
|
|
|
|
|
* description="获取时间段内的统计或指定明细",
|
|
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
|
|
* @OA\Parameter(name="project_id", in="query", @OA\Schema(type="integer"), required=true, description="医院ID"),
|
|
|
|
|
* @OA\Parameter(name="duration", in="query", @OA\Schema(type="string"), required=false, description="统计周期的快捷方式,可选方式:today,this_week,this_month,yesterday,last_week,last_month"),
|
|
|
|
|
* @OA\Parameter(name="from_date", in="query", @OA\Schema(type="string"), required=false, description="开始日期,如果没有shortcut参数,此参数必须"),
|
|
|
|
|
* @OA\Parameter(name="to_date", in="query", @OA\Schema(type="string"), required=false, description="结束日期,如果没有shortcut参数,此参数必须;注意结束日期最多可以比开始日期大31天"),
|
|
|
|
|
* @OA\Parameter(name="detail_type", in="query", @OA\Schema(type="boolean"), required=false, description="明细类型,此参数不传表示仅获取汇总数据,传递时可选项目:checkout(扣款明细),recharge(充值明细),refund(退款明细)"),
|
|
|
|
|
* @OA\Parameter(name="page", in="query", @OA\Schema(type="integer"), required=false, description="当前页码,默认为1"),
|
|
|
|
|
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="integer"), required=false, description="每页数量,默认为10"),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="获取时间段内的统计"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public function byDuration(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$duration = $this->_getDuration();
|
|
|
|
|
$from_date = $duration["from_date"];
|
|
|
|
|
$to_date = $duration["to_date"];
|
|
|
|
|
$from_date_timestamp = strtotime($from_date);
|
|
|
|
|
$to_date_timestamp = strtotime($to_date) + 86400 - 1;
|
|
|
|
|
if (Carbon::parse($from_date)->diffInDays($to_date) >= 31) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
"errorcode" => 90001,
|
|
|
|
|
"errormsg" => "日期间隔不能超过31天"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$model_checkout = OrderItems::whereHas("order", function ($query) use ($request) {
|
|
|
|
|
$query->where("project_id", $request->project_id);
|
|
|
|
|
})->whereNotNull("paid_at")->whereRaw("UNIX_TIMESTAMP(`paid_at`) between {$from_date_timestamp} and {$to_date_timestamp}");
|
|
|
|
|
$model_recharge = Recharge::whereHas("order", function ($query) use ($request) {
|
|
|
|
|
$query->where("project_id", $request->project_id);
|
|
|
|
|
})->whereNotNull("paid_at")->whereRaw("UNIX_TIMESTAMP(`paid_at`) between {$from_date_timestamp} and {$to_date_timestamp}");
|
|
|
|
|
$model_refund = Refund::whereHas("order", function ($query) use ($request) {
|
|
|
|
|
$query->where("project_id", $request->project_id);
|
|
|
|
|
})->whereNotNull("paid_at")->whereRaw("UNIX_TIMESTAMP(`paid_at`) between {$from_date_timestamp} and {$to_date_timestamp}");
|
|
|
|
|
|
|
|
|
|
$page_size = $request->has("page_size") ? (int)$request->page_size : 10;
|
|
|
|
|
switch ($request->detail_type) {
|
|
|
|
|
case "checkout":
|
|
|
|
|
$data = $model_checkout->paginate($page_size);
|
|
|
|
|
return response()->json($data->toArray());
|
|
|
|
|
break;
|
|
|
|
|
case "recharge":
|
|
|
|
|
$data = $model_recharge->paginate($page_size);
|
|
|
|
|
return response()->json($data->toArray());
|
|
|
|
|
break;
|
|
|
|
|
case "refund":
|
|
|
|
|
$data = $model_refund->paginate($page_size);
|
|
|
|
|
return response()->json($data->toArray());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$checkout = $model_checkout->sum("total");
|
|
|
|
|
$refund = $model_refund->sum("money");
|
|
|
|
|
$payment_methods = (new Recharge())->payment_methods;
|
|
|
|
|
$recharge_by_payment = $model_recharge->select("payment", "paid_at", "money", "order_id")->addSelect(DB::raw("sum(`money`) as total"))->groupBy("payment")->get()->keyBy("payment")->toArray();
|
|
|
|
|
$recharge = [];
|
|
|
|
|
$recharge["total"] = collect($recharge_by_payment)->sum("total");
|
|
|
|
|
foreach ($payment_methods as $k => $v) {
|
|
|
|
|
$val = isset($recharge_by_payment[$k]) ? $recharge_by_payment[$k]["total"] : 0;
|
|
|
|
|
$recharge[$k] = $val;
|
|
|
|
|
}
|
|
|
|
|
return response()->json(compact("checkout", "refund", "recharge", "payment_methods"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function _getDuration()
|
|
|
|
|
{
|
|
|
|
|
switch (request()->duration) {
|
|
|
|
|
case "today":
|
|
|
|
|
$from_date = date("Y-m-d");
|
|
|
|
|
$to_date = date("Y-m-d");
|
|
|
|
|
break;
|
|
|
|
|
case "yesterday":
|
|
|
|
|
$from_date = Carbon::today()->sub("1 day")->toDateString();
|
|
|
|
|
$to_date = Carbon::today()->sub("1 day")->toDateString();
|
|
|
|
|
break;
|
|
|
|
|
case "this_week":
|
|
|
|
|
$from_date = Carbon::today()->startOfWeek()->toDateString();
|
|
|
|
|
$to_date = Carbon::today()->endOfWeek()->toDateString();
|
|
|
|
|
break;
|
|
|
|
|
case "last_week":
|
|
|
|
|
$from_date = Carbon::today()->sub("7 day")->startOfWeek()->toDateString();
|
|
|
|
|
$to_date = Carbon::today()->sub("7 day")->endOfWeek()->toDateString();
|
|
|
|
|
break;
|
|
|
|
|
case "this_month":
|
|
|
|
|
$from_date = Carbon::today()->startOfMonth()->toDateString();
|
|
|
|
|
$to_date = Carbon::today()->endOfMonth()->toDateString();
|
|
|
|
|
break;
|
|
|
|
|
case "last_month":
|
|
|
|
|
$from_date = Carbon::today()->sub("1 month")->startOfMonth()->toDateString();
|
|
|
|
|
$to_date = Carbon::today()->sub("1 month")->endOfMonth()->toDateString();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$from_date = request()->from_date;
|
|
|
|
|
$to_date = request()->to_date;
|
|
|
|
|
}
|
|
|
|
|
return compact("from_date", "to_date");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|