<?php
/**
 * Created by PhpStorm.
 * User: Gss
 * Date: 2021/4/6 0006
 * Time: 12:21
 */

namespace JinDouYun\Controller\Merchant;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Merchant\MMerchantSettlement;
use Mall\Framework\Core\ErrorCode;

class MerchantSettlement extends BaseController
{
    private $objMMerchantSettlement;

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

    /**
     * 添加和编辑结算记录公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter()
    {

        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $merchantSettlementData = [
            'orderId'               => getArrayItem($params,'orderId',''),
            'orderNo'               => getArrayItem($params, 'orderNo',''),
            'goodsId'              => isset($params['goodsId']) ? $params['goodsId'] : '',
            'goodsName'              => isset($params['goodsName']) ? $params['goodsName'] : '',
            'goodsPrice'             => isset($params['goodsPrice']) ? $params['goodsPrice'] : '',
            'goodsMoney'             => getArrayItem($params,'goodsMoney',0.00),
            'merchantId'            => getArrayItem($params,'merchantId',''),
            'merchantName'          => getArrayItem($params,'merchantId','')
        ];

        foreach ($merchantSettlementData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $merchantSettlementData['createTime'] = time();
        $merchantSettlementData['updateTime'] = time();
        return $merchantSettlementData;
    }

    /*
     * 新增结算记录
     * */
    public function addMerchantSettlement()
    {
        $merchantSettlementData = $this->commonFieldFilter();

        $result = $this->objMMerchantSettlement->addMerchantSettlement($merchantSettlementData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /*
     * 结算记录状态
     * */
    public function updateMerchantSettlement()
    {
        $params = $this->request->getRawJson();
        if( empty($params['orderId'])){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

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

    /*
     * 获取所有结算记录
     * */
    public function getAllMerchantSettlement()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $params['limit'] = $pageParams['limit'];
        $params['offset'] = $pageParams['offset'];
//        $params['operatorName'] = getArrayItem($params,'operatorName','');
        $params['MerchantId'] = getArrayItem($params,'MerchantId','');
        $params['goodName'] = getArrayItem($params,'goodName','');
        $params['settlementStatus'] = getArrayItem($params,'settlementStatus','');
        $params['start'] = getArrayItem($params,'start','');
        $params['end'] = getArrayItem($params,'end','');

        $returnData = $this->objMMerchantSettlement->getAllMerchantSettlement($params);

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