<?php
/**
 * 资金转账管理模块
 * Created by PhpStorm.
 * User: tpl
 * Date: 2019/10/30
 * Time: 13:54
 */
namespace JinDouYun\Controller\Finance;

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

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Finance\MAccountTransfer;
use JinDouYun\Cache\FinanceCache;

class AccountTransfer extends BaseController
{
    private $objMAccountTransfer;
    private $objFinanceCache;
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMAccountTransfer = new MAccountTransfer($this->onlineEnterpriseId, $this->onlineUserId);
        $this->objFinanceCache =  new FinanceCache();
    }

    /**
     * 添加和编辑资金转账管理公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter(){

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

        $accountTransferData = [
            'operatorId' => $this->onlineEnterpriseId,
            'operatorName' => isset($params['operatorName']) ? $params['operatorName'] : '',
            'auditStatus' => StatusCode::$auditStatus['auditing'],
            'shopId' => isset($params['shopId']) ? $params['shopId'] : '',
            'shopName' => isset($params['shopName']) ? $params['shopName'] : '',
            'records' => isset($params['records']) ? $params['records'] : '',
        ];

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

        if(isset($accountTransferData['records']['outAccountNumber']) && $accountTransferData['records']['inAccountNumber'] && $accountTransferData['records']['outAccountNumber'] == $accountTransferData['records']['inAccountNumber']){
            $this->sendOutput('出入账户不能是同一个', ErrorCode::$paramError );
        }


        $accountTransferData['tempSave'] = true;
        if(isset($params['tempSave']) && $params['tempSave'] == false) {
            unset($accountTransferData['tempSave']);
            $totalMoney = 0;
            foreach ($accountTransferData['records'] as $record) {
                $totalMoney += $record['money'];
            }

            $accountTransferData['totalMoney'] = $totalMoney;
            $accountTransferData['records'] = json_encode($accountTransferData['records']);
            $accountTransferData['createTime'] = time();
            $accountTransferData['updateTime'] = time();
        }
        return $accountTransferData;
    }

    /**
     * 添加资金转账
     */
    public function addAccountTransfer()
    {
        $accountTransferData = $this->commonFieldFilter();

        if(isset($accountTransferData['tempSave'])) {
            $this->objFinanceCache->saveAccountTransfer($this->onlineEnterpriseId, $this->onlineUserId, $accountTransferData);
            parent::sendOutput('暂存成功');
        }else {
            $result = $this->objMAccountTransfer ->addAccountTransfer($accountTransferData);
            if($result->isSuccess()){
                //删除暂存数据
                $this->objFinanceCache->delAccountTransfer($this->onlineEnterpriseId, $this->onlineUserId);
                parent::sendOutput($result->getData());
            }else{
                parent::sendOutput($result->getData(), $result->getErrorCode());
            }
        }

    }

    /**
     * 获取暂存信息
     */
    public function getTempAccountTransferData() {
        $result = $this->objFinanceCache->getAccountTransfer($this->onlineEnterpriseId, $this->onlineUserId);
        $this->sendOutput($result);
    }

    /**
     * 获取指定资金转账信息
     */
    public function getAccountTransferInfo()
    {
        $accountTransferId  = $this->request->param('request_id');

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

        $result = $this->objMAccountTransfer->getAccountTransferInfo($accountTransferId);

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

    /**
     * 编辑资金转账
     */
    public function editAccountTransfer()
    {
        $accountTransferId  = $this->request->param('request_id');
        if(empty($accountTransferId)){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $accountTransferData = $this->commonFieldFilter();
        $accountTransferData['id'] = $accountTransferId;
        unset($accountTransferData['createTime']);

        $result = $this->objMAccountTransfer->editAccountTransfer($accountTransferData);
        if($result->isSuccess()){
            //删除暂存数据
            $this->objFinanceCache->delAccountTransfer($this->onlineEnterpriseId, $this->onlineUserId);
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }


    /**
     * 资金转账审核
     */
    public function updateAccountTransferStatus()
    {
        $accountTransferId  = $this->request->param('request_id');
        if(empty($accountTransferId)){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        $result = $this->objMAccountTransfer->updateAccountTransferStatus($accountTransferId);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }


    /**
     * 后台所有资金转账列表
     */
    public function getAllAccountTransfer()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $selectParams['no'] = isset($params['no']) ? $params['no'] : '';
        $selectParams['start'] = isset($params['start']) ? $params['start'] : '';
        $selectParams['end'] = isset($params['end']) ? $params['end'] : '';
        $selectParams['outAccountId'] = isset($params['outAccountId']) ? $params['outAccountId'] : '';
        $selectParams['inAccountId'] = isset($params['inAccountId']) ? $params['inAccountId'] : '';

        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $result = $this->objMAccountTransfer->getAllAccountTransfer($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());
        }
    }
}