<?php

namespace app\api\controller;

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

/**
 * 优惠券
 */
class Coupon extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取优惠券详情
     * @return void
     */
    public function getCouponDetails(){

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

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

        if(empty($res)){
            $this->error("优惠券已下架");
        }
        if(!empty($res)){

            $res['starttime'] = date('Y-m-d H:i:s',$res['starttime']);
            $res['endtime'] = date('Y-m-d H:i:s',$res['endtime']);

        }

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

    /**
     * 获取优惠券列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponList(){

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

        $coupon = Db::name("unishop_coupon")->where('switch',1)->where("starttime",'<=',$time)->where("endtime",">=",$time)->whereNull("deletetime")->order('weigh desc')->select();

        if(!empty($coupon)){
            foreach ($coupon as $key=>$value){
                $coupon[$key]['starttime'] = date('Y-m-d H:i:s',$value['starttime']);
                $coupon[$key]['endtime'] = date('Y-m-d H:i:s',$value['endtime']);
            }
        }

        $this->success("优惠券列表",$coupon);
    }

}
