<?php
/**
 * 优惠券
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/12/23
 * Time: 9:22
 */

namespace JinDouYun\Controller\Market;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Market\MCoupon;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

class ApiCoupon extends BaseController
{
    private $objMCoupon;

    /**
     * ApiCoupon constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @throws \Exception
     */
    public function __construct($isCheckAcl = false, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMCoupon = new MCoupon($this->onlineUserId, $this->onlineEnterpriseId);
    }


    /**
     * 可领取列表
     * @throws \Exception
     */
    public function couponList()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'grantType' => isset($paramsData['grantType']) ? $paramsData['grantType'] : '',//发放方式(10主动领取 20定向发放 30注册领取 40在线支付赠送)
        ];

        foreach ($params as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        isset($paramsData['payAmount']) && $params['payAmount'] = $paramsData['payAmount'];
        $result = $this->objMCoupon->couponList($params);
        if ($result->isSuccess()) {
            $vipCoupon = $this->objMCoupon->vipCoupon();
            if (!$vipCoupon->isSuccess()) {
                parent::sendOutput($vipCoupon->getData(), $vipCoupon->getErrorCode());
            }
            $allCoupon = array_merge($result->getData(), $vipCoupon->getData());
            $pageData = [
                'pageIndex' => 0,
                'pageSize'  => 10,
                'pageTotal' => count($allCoupon),
            ];
            parent::sendOutput($allCoupon, 0, $pageData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 领取优惠券(单个) (如果vipCardId!=0,则此优惠券是会员卡优惠券使用couponId = 如果vipCardId)
     * @throws \Exception
     */
    public function receive()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'couponId' => isset($paramsData['couponId']) ? $paramsData['couponId'] : '',
        ];
        foreach ($params as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $params['vipCardId'] = isset($paramsData['vipCardId']) ? $paramsData['vipCardId'] : 0;
        $result = $this->objMCoupon->receive($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 会员卡一键领取优惠券
     * @throws \Exception
     */
    public function oneKeyReceive()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'couponId'  => isset($paramsData['couponId']) ? $paramsData['couponId'] : '',//id有对个逗号分隔
            'vipCardId' => isset($paramsData['vipCardId']) ? $paramsData['vipCardId'] : '',//一个id
        ];

        foreach ($params as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMCoupon->oneKeyReceive($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 优惠券盒子
     * @throws \Exception
     */
    public function selectAll()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'type' => isset($paramsData['type']) ? $paramsData['type'] : '',
        ];
        foreach ($params as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $pageParams = pageToOffset(isset($paramsData['page']) ? $paramsData['page'] : 1, isset($paramsData['pageSize']) ? $paramsData['pageSize'] : 10);
        $params['limit'] = $pageParams['limit'];
        $params['offset'] = $pageParams['offset'];
        $result = $this->objMCoupon->selectAll($params);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => isset($paramsData['page']) ? $paramsData['page'] : 1,
                'pageSize'  => isset($paramsData['pageSize']) ? $paramsData['pageSize'] : 10,
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
}