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

class FinanceType extends BaseController
{
    private $objMFinanceType;

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

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

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

        $financeTypeData = [
            'enterpriseId' => $this->onlineEnterpriseId,
            'link' => isset($params['link']) ? $params['link'] : '',
            'name' => isset($params['name']) ? $params['name'] : '',
            'isDefault' => isset($params['isDefault']) ? $params['isDefault'] : '',
            'enableStatus' => isset($params['enableStatus']) ? $params['enableStatus'] : '',
        ];

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

    /**
     * 添加财务类型
     */
    public function addFinanceType()
    {
        $financeTypeData = $this->commonFieldFilter();

        $result = $this->objMFinanceType ->addFinanceType($financeTypeData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * 获取指定财务类型信息
     */
    public function getFinanceTypeInfo()
    {
        $financeTypeId  = $this->request->param('request_id');

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

        $result = $this->objMFinanceType->getFinanceTypeInfo($financeTypeId);

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

    /**
     * 编辑财务类型
     */
    public function editFinanceType()
    {
        $financeTypeId  = $this->request->param('request_id');
        if(empty($financeTypeId)){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $financeTypeData = $this->commonFieldFilter();
        $financeTypeData['id'] = $financeTypeId;
        unset($financeTypeData['createTime']);

        $result = $this->objMFinanceType->editFinanceType($financeTypeData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * 删除财务类型
     */
    public function delFinanceType()
    {
        $financeTypeId  = $this->request->param('request_id');
        if(!$financeTypeId){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        $result = $this->objMFinanceType->delFinanceType($financeTypeId);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

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

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

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

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


    /**
     * 后台所有财务类型列表
     */
    public function getAllFinanceType()
    {
        $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'];
        $result = $this->objMFinanceType->getAllFinanceType($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());
        }
    }


    /**
     * 后台所有财务类型列表 (不分页)
     */
    public function getAllFinanceTypeNoPage()
    {
        $link  = $this->request->param('request_id');
        $selectParams = [];
        if($link) {
            $selectParams['link'] = $link;
        }

        $result = $this->objMFinanceType->getAllFinanceTypeNoPage($selectParams);
        if($result->isSuccess()){
            $returnData = $result->getData();
            parent::sendOutput($returnData);
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}