<?php
/**
 * Created by PhpStorm.
 * User: Gss
 * Date: 2021/4/8 0008
 * Time: 19:33
 */

namespace JinDouYun\Controller\Finance;

use JinDouYun\Model\Finance\MRefund;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;

class Refund extends BaseController
{
    private $objMRefund;

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

    /**
     * 添加和编辑退款单管理公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter()
    {

        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $receivedData = [
            'currentAccountName' => isset($params['currentAccountName']) ? $params['currentAccountName'] : '',
            'financeType'        => isset($params['financeType']) ? $params['financeType'] : '',
            'financeTypeId'      => isset($params['financeTypeId']) ? $params['financeTypeId'] : '',
            'shopId'             => isset($params['shopId']) ? $params['shopId'] : '',
            'shopName'           => isset($params['shopName']) ? $params['shopName'] : '',
            'refundTime'        => isset($params['refundTime']) ? $params['refundTime'] : '',
            'operatorId'         => $this->onlineUserId,
            'accountList'        => isset($params['accountList']) ? $params['accountList'] : [],
        ];

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

        $receivedData['money'] = 0;
        foreach ($receivedData['accountList'] as $account) {
            if(!empty($account)) {
                foreach ($account as $k =>$v) {
                    if(in_array($k, ['accountId','accountName','money']) && empty($v)) {
                        $this->sendOutput('请输入账户'.$k, ErrorCode::$paramError);
                    }
                }
                $receivedData['money'] = bcadd($receivedData['money'], $account['money'], 2);
            }
        }
        $receivedData['unitId'] = isset($params['unitId']) ? $params['unitId'] : 0;
        $receivedData['unitName'] = isset($params['unitName']) ? $params['unitName'] : '匿名来往单位';
        $receivedData['originId'] = isset($params['originId']) ? $params['originId'] : '';
        $receivedData['originNo'] = isset($params['originNo']) ? $params['originNo'] : '';
        $receivedData['type'] = isset($params['type']) ? $params['type'] : StatusCode::$standard;
        $receivedData['sourceNo'] = isset($params['sourceNo']) ? $params['sourceNo'] : '';
        $receivedData['payType'] = isset($params['payType']) ? $params['payType'] : '';
        $receivedData['auditStatus'] = StatusCode::$auditStatus['auditing'];
        $receivedData['createTime'] = isset($params['createTime']) ? $params['createTime'] : '';
        $receivedData['updateTime'] = time();

        return $receivedData;
    }

    /**
     * 添加退款单
     */
    public function addRefund()
    {
        $receivedData = $this->commonFieldFilter();
        $receivedData['createTime'] = time();
        $result = $this->objMRefund->addRefund($receivedData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 修改收款单
     */
    public function editRefund()
    {
        $refundId  = $this->request->param('request_id');
        if(empty($refundId)){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $refundData = $this->commonFieldFilter();
        $refundData['id'] = $refundId;
        $result = $this->objMRefund->editRefund($refundData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 获取指定退款单信息
     * @throws \Exception
     */
    public function getRefundInfo()
    {
        $params = $this->request->getRawJson();
        if (empty($params['id']) || empty($params['createTime'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $result = $this->objMRefund->getRefundInfo($params);

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

    /**
     * 退款单审核
     */
    public function updateRefundStatus()
    {
        $params = $this->request->getRawJson();
        if (empty($params['id']) || empty($params['createTime'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

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

    /**
     * 后台所有收款单列表
     */
    public function getAllRefund()
    {
        $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'];
        $selectParams['no'] = getArrayItem($params,'no','');
        $selectParams['financeTypeId'] = getArrayItem($params, 'financeTypeId','');
        $selectParams['start'] = getArrayItem($params, 'start','');
        $selectParams['end'] = getArrayItem($params, 'end','');
        $selectParams['auditStatus'] = getArrayItem($params, 'auditStatus','');
        $selectParams['unitId'] = getArrayItem($params, 'unitId','');
        $selectParams['unitName'] = getArrayItem($params, 'unitName','');
        $selectParams['type'] = getArrayItem($params, 'type','');
        $result = $this->objMRefund->getAllRefund($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(), $result->getErrorCode());
        }
    }

}