<?php
/**
 * Created by PhpStorm.
 * User: kang
 * Date: 2021/3/6
 * Time: 15:16
 */

namespace JinDouYun\Model\Finance;

use JinDouYun\Model\MBaseModel;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Dao\Order\DOrder;
use JinDouYun\Dao\Stock\DInventoryOut;
use JinDouYun\Dao\Finance\DAccountType;

class MAccountType extends MBaseModel
{
    
    private $objDAccountType;
    private $enterpriseId;
    private $userCenterId;
    
    public function __construct($enterpriseId, $userCenterId)
    {
        $this->userCenterId = $userCenterId;
        $this->enterpriseId = $enterpriseId;
        parent::__construct($enterpriseId, $userCenterId);
        $this->objDAccountType = new DAccountType('finance');
    }
    
    /**
     * 添加费用类型
     * @param $params
     * @return ResultWrapper
     * @throws Exception
     */
    public function addAccountType($params)
    {
        $dbResult = $this->objDAccountType->get(['name' => $params['name'],'type' => $params['type']]);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAccountType->error(), ErrorCode::$dberror);
        }
        if(!empty($dbResult)){
            return ResultWrapper::fail('该费用类型已存在', ErrorCode::$paramError);
        }
        unset($dbResult);
        $accountTypeId = $this->objDAccountType->insert($params);
        if($accountTypeId === false){
            return ResultWrapper::fail($this->objDAccountType->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($accountTypeId);
    }
    
    /**
     * 获取指定费用类型
     */
    public function getAccountTypeInfo($accountTypeId)
    {
        if( empty($accountTypeId) ){
            return ResultWrapper::fail('接收不到要请求的费用类型', ErrorCode::$paramError);
        }
        $dbResult = $this->objDAccountType-> select($accountTypeId);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAccountType->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
    
    
    /**
     * 编辑费用类型
     *
     * @param int|array $params 修改费用类型的数据
     *
     * @return ResultWrapper
     */
    public function editAccountType($params)
    {
        if( empty($params['id']) ){
            return ResultWrapper::fail('没有指定要修改的费用类型', ErrorCode::$paramError);
        }
        $updateAccountTypeId = $params['id'];
        unset($params['id']);
        $dbResult = $this->objDAccountType->update($params, $updateAccountTypeId);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAccountType->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
    
    /**
     * 删除费用类型
     *
     * @param array $params  要删除费用类型
     *
     * @return ResultWrapper
     */
    public function delAccountType($accountTypeId)
    {
        $updateData = [
            'deleteStatus' => StatusCode::$delete,
        ];
        $dbResult = $this->objDAccountType->update($updateData, $accountTypeId);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAccountType->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
    
    /**
     * 获取所有费用类型
     *
     * @param array $selectParams 过滤条件
     *
     * @return ResultWrapper
     */
    public function getAllAccountType($conditon)
    {
        if( !empty($conditon['type']) ){
            $selectParams['type'] = $conditon['type'];
        }
        $selectParams['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDAccountType->select($selectParams, '*', 'createTime desc');
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAccountType->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

}
