<?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\StatusCode;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Finance\MAccount;

class Account extends BaseController
{
    private $objMAccount;

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

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

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

        $accountData = [
            'enterpriseId' => $this->onlineEnterpriseId,
            'type' => getArrayItem($params, 'type', 0),
            'name' => isset($params['name']) ? $params['name'] : '',
            'accountNumber' => isset($params['accountNumber']) ? $params['accountNumber'] : '',
            'beginMoney' => isset($params['beginMoney']) ? intval($params['beginMoney']) : 0,
            'shopId' => isset($params['shopId']) ? $params['shopId'] : '',
            'shopName' => isset($params['shopName']) ? $params['shopName'] : '',
            'enableStatus' => isset($params['enableStatus']) ? $params['enableStatus'] : '',
            'isDefault' => isset($params['isDefault']) ? $params['isDefault'] : StatusCode::$delete,
        ];

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

        $accountData['bankAccount'] = getArrayItem($params, 'bankAccount');
        $accountData['bankName'] = getArrayItem($params, 'bankName');
        $accountData['remark']= isset($params['remark']) ? $params['remark'] : '';
        $accountData['money']= $accountData['beginMoney'];
        $accountData['createTime'] = time();
        $accountData['updateTime'] = time();

        // 如果是新行卡类型需要设置开户行名称
        if( $accountData['type'] == StatusCode::$financeAccountType['bank'] && empty($params['bankName'])){
            $this->sendOutput('bankName参数错误', ErrorCode::$paramError );
        }


        return $accountData;
    }

    /**
     * 添加账户
     */
    public function addAccount()
    {
        $accountData = $this->commonFieldFilter();

        $result = $this->objMAccount ->addAccount($accountData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * 获取指定账户信息
     */
    public function getAccountInfo()
    {
        $accountId  = $this->request->param('request_id');

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

        $result = $this->objMAccount->getAccountInfo($accountId);

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

    /**
     * 编辑账户
     */
    public function editAccount()
    {
        $accountId  = $this->request->param('request_id');
        if(empty($accountId)){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $accountData = $this->commonFieldFilter();
        $accountData['id'] = $accountId;
        unset($accountData['createTime']);
        unset($accountData['beginMoney']);
        unset($accountData['money']);
        $result = $this->objMAccount->editAccount($accountData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * 删除账户
     */
    public function delAccount()
    {
        $accountId  = $this->request->param('request_id');
        if(!$accountId){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        $result = $this->objMAccount->delAccount($accountId);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 账户启用和禁用
     */
    public function updateAccountStatus()
    {
        $params = $this->request->getRawJson();
        if( empty($params['id']) &&  empty($params['enableStatus'])){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

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

    /**
     * 账户默认
     */
    public function updateAccountDefaultStatus()
    {
        $params = $this->request->getRawJson();
        if( empty($params['id']) &&  empty($params['isDefault'])){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

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

    /**
     * 后台所有账户列表
     */
    public function getAllAccount()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }
        $includeMaster = true;
        if(isset($params['shopId']) && !empty($params['shopId'])) {
            $selectParams['shopId'] = $params['shopId'];
            
        }
        if(!empty($params['includeType']) && $params['includeType']==2){
            $includeMaster = false;
        }
        if(isset($params['enableStatus']) && !empty($params['enableStatus'])) {
            $selectParams['enableStatus'] = $params['enableStatus'];
        }
        if(isset($params['type']) && !empty($params['type'])) {
            $selectParams['type'] = $params['type'];
        }
        if(isset($params['start']) && !empty($params['start'])) {
            $selectParams['start'] = $params['start'];
        }
        if(isset($params['end']) && !empty($params['end'])) {
            $selectParams['end'] = $params['end'];
        }
        if(isset($params['isMem']) && !empty($params['isMem'])){
            $selectParams['isMem'] = $params['isMem'];
        }


        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $result = $this->objMAccount->getAllAccount($selectParams,$includeMaster);

        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());
        }
    }


    /**
     * 财务今日报表
     */
    public function getTodayStatistics()
    {
        $params['start'] = $this->request->param('start');
        $params['end'] = $this->request->param('end');
        $result = $this->objMAccount->getTodayStatistics($params);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
}