From 629479309040a4f691ba6e8550d9bc54cf4fa2ed Mon Sep 17 00:00:00 2001 From: weizong song Date: Tue, 13 Apr 2021 15:56:42 +0800 Subject: [PATCH] up --- .../Controllers/Manager/OrdersController.php | 2 +- .../Manager/StatisticsController.php | 110 ++++++++++++++++++ app/Models/Recharge.php | 21 ++-- config/database.php | 2 +- routes/web.php | 1 + 5 files changed, 121 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Manager/OrdersController.php b/app/Http/Controllers/Manager/OrdersController.php index e328b3a..c2794c4 100644 --- a/app/Http/Controllers/Manager/OrdersController.php +++ b/app/Http/Controllers/Manager/OrdersController.php @@ -892,7 +892,7 @@ class OrdersController extends CommonController * @OA\POST( * path="/manager/checkout-order/{id}", * summary="V2-订单结算", - * description="交互流程如下:初次请求带上just_check参数,将返回to_recharge_total或to_refund_total值;如果to_recharge_total大于0表示需要充值,充值完成之后,去除just_check参数再次提交;如果to_refund_total大于0表示需要退款,根据返回的退款方式操作后,去除just_check参数再次提交", + * description="交互流程如下:初次请求带上just_check参数,将返回to_recharge_total和to_refund_total值;仅在to_recharge_total和to_refund_total都为0的情况下才可以结算成功;如果to_recharge_total大于0表示需要充值,充值完成之后,剔除just_check参数再次提交;如果to_refund_total大于0表示需要退款,根据返回的退款方式操作后,剔除just_check参数再次提交", * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="订单id"), * @OA\Parameter(name="to_date", in="query", @OA\Schema(type="date"), required=true, description="结算到的日子"), diff --git a/app/Http/Controllers/Manager/StatisticsController.php b/app/Http/Controllers/Manager/StatisticsController.php index 1943b38..b1c01a6 100644 --- a/app/Http/Controllers/Manager/StatisticsController.php +++ b/app/Http/Controllers/Manager/StatisticsController.php @@ -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"); + } } diff --git a/app/Models/Recharge.php b/app/Models/Recharge.php index ef890b0..9fbf80a 100755 --- a/app/Models/Recharge.php +++ b/app/Models/Recharge.php @@ -9,23 +9,18 @@ class Recharge extends SoftDeletesModel { protected $table = "recharge"; protected $appends = ["payment_name"]; + public $payment_methods = [ + "cash" => "现金", + "weixin" => "微信", + "alipay" => "支付宝", + "pos" => "POS机" + ]; public function getPaymentNameAttribute() { $payment_name = $this->payment; - switch ($this->payment) { - case "weixin": - $payment_name = "微信"; - break; - case "alipay": - $payment_name = "支付宝"; - break; - case "cash": - $payment_name = "现金"; - break; - case "pos": - $payment_name = "POS机"; - break; + if (isset($this->payment_methods[$payment_name])) { + $payment_name = $this->payment_methods[$payment_name]; } return $payment_name; } diff --git a/config/database.php b/config/database.php index b42d9b3..aeabec6 100644 --- a/config/database.php +++ b/config/database.php @@ -56,7 +56,7 @@ return [ 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, - 'strict' => true, + 'strict' => false, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), diff --git a/routes/web.php b/routes/web.php index 11474a8..17a9fdf 100644 --- a/routes/web.php +++ b/routes/web.php @@ -173,6 +173,7 @@ Route::group(["namespace" => "Manager", "prefix" => "manager"], function () { Route::get('statistics/overview', 'StatisticsController@overview'); Route::get('statistics/beds', 'StatisticsController@beds'); + Route::get('statistics/by-duration', 'StatisticsController@byDuration'); }); });