<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 礼物接口
 */
class Gift extends Api
{
    protected $noNeedLogin = ['getGiftList','getGiftType'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        $this->giftModel = new \app\common\model\Gift();
        $this->gifttypeModel = new \app\common\model\GiftType();
    }

    /**
     * 获取礼物列表
     */
    public function getGiftList() {
        $type = $this->request->request("type"); // 礼物类型:0=活动,1=常规,2=人气,3=浪漫,4=豪华
        $page = $this->request->request('page',1); // 分页
        $pageNum = $this->request->request('pageNum',100); // 分页
        // 分页搜索构建
        $pageStart = ($page-1)*$pageNum;

        // 获取基本信息
        $where = [];
        $type != '' && $where["type"] = $type;
//        $giftList = $this->giftModel->where($where)->limit($pageStart,$pageNum)->select();
        $giftList = $this->giftModel->where($where)->order("sort","asc")->select();
        $this->success("获取成功!",$giftList);
    }

    /**
     * 获取礼物类型
     */
    public function getGiftType() {
        // 获取基本信息
        $where = [];
        $where["is_show"] = 1;
        $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
        $this->success("获取成功!",$giftList);
    }

    /**
     * 获取我的背包礼物
     */
    public function getMyBackGift() {

        $userid = $this->request->request("user_id", $this->auth->id);
        $page = $this->request->request('page',1); // 分页
        $pageNum = $this->request->request('pageNum',10); // 分页
        // 分页搜索构建
        $pageStart = ($page-1)*$pageNum;
        $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
            ->where(["user_id"=>$userid,"is_use"=>0])
            ->limit($pageStart,$pageNum)
            ->group("name")
            ->select();
        $this->success("获取成功!",$list);
    }


    /**
     * 获取我的礼物墙
     */
    public function getMyGiftWall() {
        $user_id = $this->request->request("user_id", 0);

        $userid = $user_id ? $user_id : $this->auth->id;
        $list = \app\common\model\GiftUserParty::alias('a')->join("hx_gift g", "g.id = a.gift_id", "inner")->field("gift_id,g.name,g.image,sum(number) as number")
            ->where(["user_to_id" => $userid, 'g.type' => ['<>', 6]])
            ->group("gift_id")
            ->order('g.value desc')
            ->select();
        $this->success("获取成功!", $list);
    }
}