<?php
/**
 * 会员卡管理模块
 * Created by PhpStorm.
 * User: tpl
 * Date: 2019/10/30
 * Time: 13:54
 */
namespace JinDouYun\Controller\Market;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Market\MVipCard;

class ApiVipCard extends BaseController
{
    private $objMVipCard;

    public function __construct($isCheckAcl = false, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMVipCard = new MVipCard($this->onlineEnterpriseId, $this->onlineUserId, true);
    }

    /**
     * 添加,编辑会员卡订单公共数据
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $orderData = [
            'payType'          => isset($params['payType']) ? $params['payType'] : StatusCode::$payType['cashPay'],
            'source'           => isset($params['source']) ? $params['source'] : 0,//订单来源
            'vipCardId'        => isset($params['vipCardId']) ? $params['vipCardId'] : '',//会员卡id
        ];

        foreach ($orderData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $orderData['remark'] = isset($params['remark']) ? $params['remark'] : '';//订单备注
        return $orderData;
    }

    /**
     * 购买会员卡
     */
    public function createVipCardOrder() {
        $orderData = $this->commonFieldFilter();
        $ip = $this->request->get_onlineip();
        $result = $this->objMVipCard->createVipCardOrder($orderData, $ip);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    //会员卡详情
    public function getVipInfo() {
        $vipCardId  = $this->request->param('request_id');

        if ( !$vipCardId ) {
            $this->sendOutput('参数错误', ErrorCode::$paramError );
        }

        $result = $this->objMVipCard->getVipCardInfo($vipCardId);

        if($result->isSuccess()){
            $this->sendOutput($result->getData());
        }else{
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 前台所有会员卡列表
     */
    public function getAllVipCard()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $result = $this->objMVipCard->getAllFrontVipCard($selectParams);

        if($result->isSuccess()){
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
                'received' => $returnData['received']
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 获取会员购买的所有会员卡
     */
    public function getMyVipCards() {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $result = $this->objMVipCard->getMyVipCards($selectParams);

        if($result->isSuccess()){
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
                'received' => $returnData['received']
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}