<?php

namespace app\api\controller;

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

/**
 * 商品类接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取零售商品详情
     * @return void
     */
    public function getGoodsDetails(){

        $gid = input('gid',0);

        $res = Db::name("unishop_product")->where('id',$gid)->whereNull("deletetime")->find();

        if(empty($res)){
            $this->error("商品已售空或已下架");
        }

        //判断是否存在优惠券
        $time = time();

        $coupon = Db::name("unishop_coupon")->field('id,title,value,least')->where('switch',1)->where("starttime",'<=',$time)->where("endtime",">=",$time)->whereNull("deletetime")->limit(1)->select();

        if(!empty($coupon)){
            $res['coupon'] = $coupon;
        }

        //查询商品评论
        $evaluate = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar,comment,rate')->where("product_id",$gid)->whereNull("deletetime")->limit(2)->select();
        if(!empty($evaluate)){

            $res['evaluate'] =  $evaluate;
            $res['evaluate_count'] = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar')->where("product_id",$gid)->whereNull("deletetime")->count();

        }

        //计算平均分
        $rate = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar')->where("product_id",$gid)->whereNull("deletetime")->avg('rate');
        $res['level'] =  $rate;

        $this->success("获取零售商品详情",$res);
    }

    /**
     * 批发商品详情
     * @return void
     */
    public function getWholesaleGoodsDetails(){

        $gid = input('gid',0);

        $res = Db::name("unishop_wholesale_goods")->where('goods_id',$gid)->find();

        if(empty($res)){
            $this->error("商品已售空或已下架");
        }

        //判断是否存在优惠券
        $time = time();

        $coupon = Db::name("unishop_coupon")->field('id,title,value,least')->where('switch',1)->where("starttime",'<=',$time)->where("endtime",">=",$time)->whereNull("deletetime")->limit(1)->select();

        if(!empty($coupon)){
            $res['coupon'] = $coupon;
        }

        //查询商品评论
        $evaluate = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar,comment,rate')->where("product_id",$gid)->whereNull("deletetime")->limit(2)->select();
        if(!empty($evaluate)){

            $res['evaluate'] =  $evaluate;
            $res['evaluate_count'] = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar')->where("product_id",$gid)->whereNull("deletetime")->count();

        }

        //计算平均分
        $rate = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar')->where("product_id",$gid)->whereNull("deletetime")->avg('rate');
        $res['level'] =  $rate;

        $this->success("批发商品详情",$res);
    }

}
