<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/2
 * Time: 下午3:56
 */

namespace App\Modules\Finance\Services;


use App\Modules\Finance\Models\FinancialConfig;

class FinancialConfigService
{

    /**
     * 根据渠道ID获取FinancialConfig
     * @param $channelId
     * @return mixed
     */
    public static function getFinancialConfigSingle($channelId) {
        $financialConfig = FinancialConfig::getByDistributionChannel($channelId);
        return $financialConfig;
    }

    /**
     * 判断渠道是否被冻结
     * @param $channelId
     * @return bool true:冻结状态
     */
    public static function isFrozenDistributionChannel($channelId) {
        $financialConfig = self::getFinancialConfigSingle($channelId);

        if(!empty($financialConfig) && $financialConfig['is_frozen'] == -1) {
            return true;
        }
        return false;
    }


    /**
     * 更新账号冻结解冻状态
     * @param $channelId
     * @param $frozen 0:正常状态  -1：冻结账号
     * @param $remark
     */
    public static function updateFrozenDistributionChannel($channelId = '', $frozen = '', $remark = '') {
        if(!$channelId) {
            return;
        }
        if($frozen == -1 || $frozen == 0) {
        } else {
            $frozen = 0;
        }

        $financialConfig = self::getFinancialConfigSingle($channelId);

        if(empty($financialConfig)) {
            $data['distribution_channel_id'] = $channelId;
            $data['is_frozen'] = $frozen;
            $data['remark'] = $remark;
            FinancialConfig::create($data);
        } else {
            $financialConfig['is_frozen'] = $frozen;
            $financialConfig['remark'] = $remark;
            $financialConfig->save();
        }
    }
}