<?php
/**
 * 积分商品Controller
 * Created by PhpStorm.
 * User: haoren
 * Date: 2021/03/25
 * Time: 16:00
 */

namespace JinDouYun\Controller\Integral;

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

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Integral\MIntegralGoods;

class ApiIntegralGoods extends BaseController
{
    private $objMIntegralGoods;

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

    /**
     * 积分商品兑换添加
     */
    public function addIntegralGoodsExchange()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'goodsId' => isset($params['goodsId']) ? $params['goodsId'] : '',
            'num' => isset($params['num']) ? $params['num'] : [],
            'address' => isset($params['address']) ? json_encode($params['address']) : [],
        ];
        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }
        $data['shopId'] = $this->shopId ? $this->shopId : 0;
        $data['userCenterId'] = isset($params['userCenterId']) ? $params['userCenterId'] : '';
        if(empty($data['userCenterId'])){
            $data['userCenterId'] = $this->onlineUserId;
        }
        $data['remark'] = isset($params['remark']) ? $params['remark'] : '';

        $result = $this->objMIntegralGoods->addIntegralGoodsExchange($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 积分商品列表
     */
    public function getAllIntegralGoods()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        if(isset($params['search']) && !empty($params['search'])){
            $selectParams['search'] = $params['search'];
        }

        if(!empty($this->shopId)){
            $selectParams['shopId'] = $this->shopId;
        }

        $result = $this->objMIntegralGoods->getAllIntegralGoods($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 积分商品详情
     */
    public function getIntegralGoodsInfo()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMIntegralGoods->getIntegralGoodsInfo($params);

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

    /**
     * 积分商品兑换列表
     */
    public function getAllIntegralGoodsExchange()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        if(isset($params['search']) && !empty($params['search'])){
            $selectParams['search'] = $params['search'];
        }

        if(isset($params['status']) && !empty($params['status'])){
            $selectParams['status'] = $params['status'];
        }

        if(!empty($this->shopId)){
            $selectParams['shopId'] = $this->shopId;
        }

        $selectParams['userCenterId'] = $this->onlineUserId;

        $result = $this->objMIntegralGoods->getAllIntegralGoodsExchange($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 积分商品兑换详情
     */
    public function getIntegralGoodsExchangeInfo()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMIntegralGoods->getIntegralGoodsExchangeInfo($params);

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

}