<?php
/**
 * 优惠券
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisCoupon;
use app\ais\model\AisStore;
use app\ais\model\AisCouponUser;
use app\ais\model\AisStoreUnion;
use think\Db;

class Coupon extends Base{

   /**
     * 查询优惠券
     * mode(new|hot|max)
     * @return void
     */
    public function index(){
        $param['mode'] = $this->request->param('mode/s','new');
        $param['page'] = $this->request->param('page/d',1);
        $this->apiSign($param);
        $validate = $this->validate($param,'Coupon.api');
        if(true !== $validate){
            return enjson(403,$validate);
        }
        if($param['mode'] == 'hot'){
            $condition = [['c.member_miniapp_id','=',$this->miniapp_id],['c.is_lock','=',0],['c.types','<=',2],['c.is_end','=',0]];
        }else{
            $condition = [['member_miniapp_id','=',$this->miniapp_id],['is_lock','=',0],['types','<=',2],['is_end','=',0]];
        }
        //查询语句
        $couponSql = AisCoupon::with(['store' => function($query) {
            $query->field('id,name,img');
        }]);
        switch ($param['mode']) {
            case 'hot':
                $sql = $couponSql->alias('c')->where($condition)->join('AisCouponUser u', 'c.id = u.coupon_id')->where(['u.member_miniapp_id' => $this->miniapp_id])
                    ->field('c.*,count(coupon_id) as hot')->group('u.coupon_id')->order('hot desc,c.is_top desc,c.sort desc,c.create_time desc');
                break;
            case 'max':
                $sql = $couponSql->where($condition)->field('*,IF(types > 0,howmuch - howmuch * discount/10,price) as money')->order('money desc,is_top desc,sort desc,create_time desc');
                break;
            default:
                $sql = $couponSql->where($condition)->order('is_top desc,sort desc,create_time desc');
                break;
        }
        $coupon = $sql->page($param['page'],10)->select();
        if ($coupon->isEmpty()) {
            return enjson(204,'没有优惠券');
        }
        //判断已领取
        if ($this->user) {
            $coupon_ids     = array_column($coupon->toArray(),'id');
            $user_coupon    = AisCouponUser::where(['coupon_id' => $coupon_ids,'uid' => $this->user->id,'is_end'=> 0])->field('uid,store_id,coupon_id')->select()->toArray();
            $user_coupon_id = array_unique(array_column($user_coupon,'coupon_id'));
            //给已领取的加状态
            foreach ($coupon as $key => $value) {
                $coupon[$key]->is_get_coupon =  in_array($value['id'],$user_coupon_id) ? 1 : 0;
            }
        }
        return enjson(200,$coupon);
    }

    /**
     * 显示单个优惠券信息
     * @return void
     */
    public function read(){
        $param['id'] = $this->request->param('id');  //优惠券id
        $this->apiSign($param);
        $info = AisCoupon::with(['couponuser' => function($query) {
            $query->field('coupon_id,is_end,create_time');
        },'store' => function($query) {
            $query->field('id,name,address,longitude,latitude,img,telphone');
        }])->where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
        if (empty($info)) {
            return enjson(404,'未找到优惠券');
        }
        $info->is_get_coupon = (empty($info->couponuser) || $info->couponuser->is_end == 1) ? 0 : 1;
        $info->store->img    =  $info->store->img;
        if(!empty($this->lbs)){
            $info->store->distance = getDistance($this->lbs->lng,$this->lbs->lat,$info->store->longitude,$info->store->latitude);
        }
        return enjson(200,$info);
    }


   /**
     * 指定商家的优惠券
     * @return void
     */
    public function store(){
        $param['store_id'] = $this->request->param('store_id');  //商家ID
        $param['page']     = $this->request->param('page/d',1);  //翻页
        $this->apiSign($param);
        $coupon = AisCoupon::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $param['store_id'] ,'is_end' => 0])->where('types','<',3)->order('is_top desc,sort desc,create_time desc')->page($param['page'],10)->select();
        if ($coupon->isEmpty()) {
            return enjson(204);
        }
        return enjson(200,$coupon);
    }
}