<?php

namespace app\api\controller;

use app\api\lists\UserCouponLists;
use app\api\logic\UserCouponLogic;
use app\api\validate\UserCouponValidate;

class UserCouponController extends BaseApiController
{
    public array $notNeedLogin = ['categoryCouponLists'];
    /**
     * 我的优惠卷列表
     * @return \think\response\Json
     */
    public function lists()
    {
        $params = (new UserCouponValidate())->goCheck('voucher',[
            'user_id'=>$this->userId
        ]);
        $result = UserCouponLogic::userCouponList($params);
        if (false === $result) {
            return $this->fail(UserCouponLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * 一键领取优惠卷
     * @return \think\response\Json
     */
    public function add()
    {
        $params = (new UserCouponValidate())->post()->goCheck('add',[
            'user_id'=>$this->userId
        ]);
        $result = UserCouponLogic::add($params);
        if(!empty($result)){
            return $this->fail(implode(',',$result));
        }else{
            return $this->success('领取成功', [], 1, 1);
        }
    }

    /**
     * 商品可领取优惠券列表
     * @return \think\response\Json
     */
    public function categoryCouponLists()
    {
        $params = (new UserCouponValidate())->goCheck('category',[
            'user_id'=>$this->userId
        ]);
        $result = UserCouponLogic::categoryCouponLists($params);
        if (false === $result) {
            return $this->fail(UserCouponLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * 订单可用优惠券
     * @return \think\response\Json
     */
    public function categoryWithAmountLists()
    {
        $params = (new UserCouponValidate())->goCheck('categoryWithAmountList',[
            'user_id'=>$this->userId
        ]);
        $result = UserCouponLogic::categoryWithAmountLists($params);
        if (false === $result) {
            return $this->fail(UserCouponLogic::getError());
        }
        return $this->data($result);
    }
}