<?php
/**
 *
 * @file:UserStatisticsController.php
 * @Date: 2023/6/20
 * @Time: 09:29
 */


namespace Modules\Statistic\Http\Controllers;

use Catch\Base\CatchController;
use Catch\Exceptions\FailedException;
use Illuminate\Http\Request;

use Modules\Common\Services\Statistic\OptimizerDayUser;
use Modules\Statistic\Services\UserStatisticsService;
use Modules\User\Http\Controllers\UserTrait;

class UserStatisticsController extends CatchController
{

    use  UserTrait;
    /**
     *  获取当日数据
     * name: todayData
     * date 2023/06/20 09:45
     */
    public function todayData(Request $request)
    {
        $userContext = $this->getUserContext(null);
        $type = 1;
        if( $userContext['loginUserRoles']->contains('company')){
            $type = 2;
        }else if ($userContext['loginUserRoles']->contains('optimizer')){
            $type = 3;
        }
        $miniProgramId = $request->input('miniprogram_id',0);
        $uid = $this->getLoginUserId();
        return UserStatisticsService::getTodayData($uid,$type,$miniProgramId);
    }

    /**
     *  历史统计
     * name: stats
     * @param Request $request
     * date 2023/06/25 14:24
     */
    public function stats(Request $request)
    {
        $miniProgramId = $request->input('miniprogram_id',0);
        $userContext = $this->getUserContext(null);
        $type = 1;
        if( $userContext['loginUserRoles']->contains('company')){
            $type = 2;
        }else if ($userContext['loginUserRoles']->contains('optimizer')){
            $type = 3;
        }
        $uid = $this->getLoginUserId();
        return UserStatisticsService::historyStats($uid,$type,$miniProgramId);
    }

    /**
     *   列表查询
     * name: list
     * @param Request $request
     * date 2023/06/25 14:24
     */
    public function list(Request $request){
        $param = $request->all();
        $param = $this->getlistParams($param);
       return UserStatisticsService::list($param);
    }

    public function listStatistics(Request $request){
        $param = $request->all();
        $param = $this->getlistParams($param);
        return UserStatisticsService::listStatistics($param);
    }

    private function getlistParams($param)
    {
        $userContext = $this->getUserContext(null);
        if (getProp($param, 'puser_id')) {
            $param['puser_id'] = $param['puser_id'];
        }
        if (getProp($param, 'user_id')) {
            $param['user_id'] = $param['user_id'];
        }
        // 公司账户处理
        if ($userContext['loginUserRoles']->contains('company')) {
            $param['puser_id'] = $userContext['operateUser']->id;
        }

        // 投手处理
        if ($userContext['loginUserRoles']->contains('optimizer')) {
            $param['user_id'] = $userContext['operateUser']->id;
            $param['puser_id'] = $userContext['operateUser']->pid;
        }
        if (!getProp($param,'end_at')){
            $param['end_at'] = date('Y-m-d');
        }else{
            $param['end_at'] = date('Y-m-d',strtotime($param['end_at']));
        }
        if (!getProp($param,'start_at')){
            // 默认最近7天
            $param['start_at'] = date('Y-m-d',strtotime($param['end_at'])-86400*7);
        }else{
            $param['start_at'] = date('Y-m-d',strtotime($param['start_at']));
        }
        return $param;
    }


}
