<?php
namespace app\appapi\controller\v1;

use app\appapi\ApiController;
use think\facade\Db;


class Bonus extends ApiController
{
    public function check(){
        $bonus_sn = input('bonus_sn');

        $result = Db::name('bonus')
        ->alias('b')
        ->leftjoin('bonus_type bt','b.bonus_type_id=bt.type_id')
        ->where('bonus_sn',$bonus_sn)
        ->find();

        if(!$result){
            return $this->Error('優惠券不存在');
        }
        
        if($result['remain_number']==0){
            return $this->Error('優惠券已用完');        
        }

        if($result['use_start_date']!=0 && $result['use_start_date']>time()){
            return $this->Error('優惠券未開始使用');        
        }

        if($result['use_end_date']!=0 && $result['use_end_date']<time()){
            return $this->Error('優惠券已過期');        
        }

        $rtn = [
            'type_money' => $result['type_money'],
            'type_name' => $result['type_name'],
            'bonus_number' => $result['bonus_number'],
            'remain_number' => $result['remain_number'],
        ];
        return $this->Success($rtn);
    
    }
}