<?php

namespace App\Http\Controllers\DataCenter;

use App\Http\Controllers\Controller;
use App\Http\Enum\StaticTypeEnum;
use App\Http\Resources\DailySalesReportResource;
use App\Models\BusinessOrderItem;
use App\Models\DailySalesReport;
use App\Models\GoodsSku;
use App\Services\Statistic\SaleDataService;
use App\Utils\FormatUtils;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class DataCenterController extends Controller
{
    public function salesReport(Request $request)
    {
        $sort = $request->get('sort', 'sales_num');
        $order = $request->get('sortOrder', 'DESC');
        if (empty($request->get('date'))) {
            $request->offsetSet('date', date('Y-m-d'));
        }

        $dailySalesReports = DailySalesReport::query()
            ->filter()
            ->orderBy($sort, $order)
            ->paginate($request->get('per_page'));

        foreach ($dailySalesReports as &$dailySalesReport) {
            $dailySalesReport['stock'] = $dailySalesReport['inventory'] + $dailySalesReport['arrived_today_num'];
            $dailySalesReport['goal_rate'] = FormatUtils::getPercent($dailySalesReport['goal_rate'], 1);
            $dailySalesReport['S1_rate'] = FormatUtils::getPercent($dailySalesReport['S1_rate'], 1);
            $dailySalesReport['S2_rate'] = FormatUtils::getPercent($dailySalesReport['S2_rate'], 1);
            $dailySalesReport['S3_rate'] = FormatUtils::getPercent($dailySalesReport['S3_rate'], 1);
            $dailySalesReport['S4_rate'] = FormatUtils::getPercent($dailySalesReport['S4_rate'], 1);
            $dailySalesReport['S5_rate'] = FormatUtils::getPercent($dailySalesReport['S5_rate'], 1);
            $dailySalesReport['S6_rate'] = FormatUtils::getPercent($dailySalesReport['S6_rate'], 1);
            $dailySalesReport['S7_rate'] = FormatUtils::getPercent($dailySalesReport['S7_rate'], 1);
        }

        return DailySalesReportResource::collection($dailySalesReports);
    }

    /**
     * 销售报表
     * @param Request $request
     * @return void
     */
    public function saleStatistics(Request $request)
    {
        //获取所有参数
        $allParams = $request->all();
        //进行校验验证
        $validator = Validator::make($allParams, [
            'type' => 'required|integer', //1表示今日
            'start_day' => 'sometimes|date_format:Y-m-d',
            'end_day' => 'sometimes|date_format:Y-m-d',
            'start_time' => 'sometimes|date_format:Y-m-d H:i:s',
            'end_time' => 'sometimes|date_format:Y-m-d H:i:s'
        ]);

        if ($validator->fails()) {
            //校验失败返回异常
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
            return response($this->res, $this->res['httpCode']);
        }
        return $this->success(SaleDataService::saleStatistics($request));
    }

    /**
     * spu 维度的数据统计
     * @param Request $request
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|mixed
     */
    public function spuSaleStatistics(Request $request)
    {
        //进行校验验证
        $validator = Validator::make($request->all(), [
            'type' => 'required|integer', //1表示今日
            'start_day' => 'sometimes|date_format:Y-m-d',
            'end_day' => 'sometimes|date_format:Y-m-d',
            'start_time' => 'sometimes|date_format:Y-m-d H:i:s',
            'end_time' => 'sometimes|date_format:Y-m-d H:i:s'
        ]);
        if ($validator->fails()) {
            //校验失败返回异常
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
            return response($this->res, $this->res['httpCode']);
        }
        return $this->success(SaleDataService::spuSaleStatistics($request));
    }

    public function gmvStatistics(Request $request)
    {
        //进行校验验证
        $validator = Validator::make($request->all(), [
            'type' => 'required|integer', //1表示今日
            'start_day' => 'required_unless:type,1|date_format:Y-m-d',
            'end_day' => 'required_unless:type,1|date_format:Y-m-d',
            "interval" => 'sometimes|integer|max:120|min:1',
            'start_time' => 'sometimes|date_format:Y-m-d H:i:s',
            'end_time' => 'sometimes|date_format:Y-m-d H:i:s',
            'sku_id' => 'sometimes|integer'
        ]);
        if ($validator->fails()) {
            //校验失败返回异常
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
            return response($this->res, $this->res['httpCode']);
        }
        return $this->success(SaleDataService::gmvStatistics($request));
    }

    public function lossRecordStatistics(Request $request)
    {
        //进行校验验证
        $validator = Validator::make($request->all(), [
            'start_time' => 'sometimes|date_format:Y-m-d H:i:s',
            'end_time' => 'sometimes|date_format:Y-m-d H:i:s',
            'sku_id' => 'sometimes|integer',
        ]);
        if ($validator->fails()) {
            //校验失败返回异常
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
            return response($this->res, $this->res['httpCode']);
        }
        return $this->success(SaleDataService::lossRecordStatistics($request));
    }
}