<?php
/**
 * 支付方式设置
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/10/31
 * Time: 15:46
 */

namespace JinDouYun\Controller\System;

use Mall\Framework\Core\ErrorCode;
use JinDouYun\Controller\BaseController;

use JinDouYun\Model\System\MPaymentSetting;
use Mall\Framework\Core\StatusCode;

class PaymentSetting extends BaseController
{
    private $objMPaymentSetting;

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

    /**
     * 添加,编辑支付方式公共数据
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'title'         => isset($params['title']) ? $params['title'] : '',
            'defaultStatus' => isset($params['defaultStatus']) ? $params['defaultStatus'] : '',
            'enableStatus'  => isset($params['enableStatus']) ? $params['enableStatus'] : '',
        ];

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

        return $data;
    }

    /**
     * 获取指定支付方式详情
     * @throws \Exception
     */
    public function getPaymentInfoById()
    {
        $id = $this->request->param('request_id');

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

        $result = $this->objMPaymentSetting->getPaymentInfoById($id);

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

    /**
     * 获取支付方式列表
     * @throws \Exception
     */
    public function getAllPayment()
    {
        $page = $this->request->param('page') ?: 1;
        $pageSize = $this->request->param('pageSize') ?: 10;

        $offset = ($page - 1) * $pageSize;

        $selectParams = [
            'limit'  => $pageSize,
            'offset' => $offset,
        ];

        $orderData = $this->objMPaymentSetting->getAllPayment($selectParams);

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

    /**
     * 更新支付方式,启用/禁用
     * @throws \Exception
     */
    public function updateEnableStatus()
    {
        $params['id'] = $this->request->param('request_id');
        $params['enableStatus'] = $this->request->param('enableStatus');

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

        $result = $this->objMPaymentSetting->updateEnableStatus($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 更新支付方式的默认状态
     * @throws \Exception
     */
    public function updateDefaultStatus()
    {
        $params['id'] = $this->request->param('request_id');
        $params['defaultStatus'] = $this->request->param('defaultStatus');

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

        $result = $this->objMPaymentSetting->updateDefaultStatus($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 编辑支付方式
     */
    public function savePaySetting(){
        $id = $this->request->param('request_id');
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'id'            => $id,
            'paymentData'   => isset($params['paymentData']) ? json_encode($params['paymentData']) : [],
            'enableStatus'  => isset($params['enableStatus']) ? $params['enableStatus'] : StatusCode::$delete,
            'defaultStatus' => isset($params['defaultStatus']) ? $params['defaultStatus'] : StatusCode::$delete,
            'enterpriseId'  => $this->onlineEnterpriseId,
        ];
        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }

        $modelResult = $this->objMPaymentSetting->savePaySetting($data);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());

    }
}