<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use app\common\model\score\Log;
use app\common\model\score\Event;

/**
 * 积分接口
 */
class Score  extends Api
{
    /**
     * @var \app\common\model\score\Log
     */
    protected $model = null;
    protected $noNeedRight = ['*'];
//    protected $noNeedLogin = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new Log;


    }





    /**
     * @ApiTitle( 积分项列表)
     * @ApiSummary(积分项列表)
     * @ApiMethod(GET)
     * @ApiParams(name = "keywords", type = "string",required=false,description = "搜索关键字")
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "way", type = "string",required=false,description = "free=自由申请,direct=直接奖励  默认自由申请")
     * @ApiReturn({
     *
     *})
     */
    public function index()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $page  =  $this->request->param('page/d', 0); //页数
        $limit =  $this->request->param('limit/d', 0); //条数
        $keywords =  $this->request->param('keywords/s', ''); //搜索关键字
        $way =  $this->request->param('way/s', 'free'); //搜索关键字

        try{
            //当前申请状态
            $res = Event::allList($page, $limit,$keywords,$way,[]);
//             var_dump($this->model->getLastSql());
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }




    /**
     * @ApiTitle( 我的积分申请列表)
     * @ApiSummary(我的积分申请列表)
     * @ApiMethod(GET)
     * @ApiParams(name = "keywords", type = "string",required=false,description = "搜索关键字")
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "status", type = "string",required=false,description = "审核状态:0=待审核,1=审核通过,2=审核不通过   可多值逗号拼接")
     * @ApiParams(name = "time", type = "string",required=false,description = "复合时间查询:today今天,week本周,month本月,year本年,yesterday昨天,last year上一年,last week上周，last month上個月，lately7最近7天 ， lately30最近30天，按开始时间区间查传值格式：Y/m/d H:M:S-Y/m/d H:M:S")
     * @ApiReturn({
     *
     *})
     */
    public function log()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $page  =  $this->request->param('page/d', 0); //页数
        $limit =  $this->request->param('limit/d', 0); //条数
        $keywords =  $this->request->param('keywords/s', ''); //搜索关键字
        $params  =  [];
        $params["status"] =  $this->request->param('status/s', ''); //搜索关键字
        $params["time"] =  $this->request->param('time/s', ''); //搜索关键字
        $params["user_id"] = $user_id;
        try{
            //当前申请状态
            $res = Log::allList($page, $limit,$keywords,$params);
//             var_dump($this->model->getLastSql());
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }




    /**
     * @ApiTitle( 月度积分排行榜及个人月度当前积分累计)
     * @ApiSummary(月度积分排行榜及个人月度当前积分累计)
     * @ApiMethod(GET)
     * @ApiParams(name = "time", type = "string",required=false,description = "要查询的月份的时间戳")
     * @ApiReturn({
     *
     *})
     */
    public function month()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $time =  $this->request->param('time/d', null); //搜索关键字

        try{
            //当前申请状态
            $res = Log::statistics($user_id,$time);
//             var_dump($this->model->getLastSql());
        }catch (\Exception $e){

            $this->error($e->getMessage().$e->getFile().$e->getLine());
        }
        $this->success('查询成功', $res);
    }





    /**
     * @ApiTitle( 积分申请)
     * @ApiSummary(积分申请)
     * @ApiMethod(POST)
     * @ApiParams(name = "score_event_id", type = "string",required=true,description = "积分事件id")
     * @ApiParams(name = "images", type = "string",required=true,description = "多图逗号拼接，附件只要相对路径，不要全路径")
     * @ApiParams(name = "desc", type = "string",required=false,description = "备注")
     * @ApiReturn({
     *
     *})
     */
    public function add()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $params=[];
        $params["user_id"] =  $user_id; //页数
        $params["score_event_id"] =  $this->request->post('score_event_id/d', ''); //条数
        $params["images"] =  $this->request->post('images/s', ''); //搜索关键字
        $params["desc"] =  $this->request->post('desc/s', ''); //搜索关键字
        //全局锁
        $this->setUrlLock("only");
        try{
            if(!$params["score_event_id"]) throw new \Exception(__('积分项不存在'));
            //当前申请状态
            $res = $this->model->add($params,true,true);
//             var_dump($this->model->getLastSql());
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('添加成功', $res);
    }




}