<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/7
 * Time: 上午9:46
 */

namespace App\Modules\Finance\Models;


use Illuminate\Database\Eloquent\Model;

/**
 * 渠道支付通道相关金钱统计
 * Class FinanceMerchantStat
 * @package App\Modules\Finance\Models
 */
class FinanceMerchantStat extends Model
{
    protected $table = 'finance_merchant_stats';
    protected $fillable = ['distribution_channel_id', 'pay_merchant_source', 'enable_withdrawal_amount'];

    /**
     * 获取一条记录
     * @param string $channel_id
     * @param string $pay_merchant_source
     * @return mixed
     */
    public static function getFinanceMerchantStatSingle($channel_id = '', $pay_merchant_source = '') {
        $search_object = self::orderBy('enable_withdrawal_amount','desc');

        if($channel_id) {
            $search_object->where('distribution_channel_id', $channel_id);
        }
        if($pay_merchant_source) {
            $search_object->where('pay_merchant_source', $pay_merchant_source);
        }
        return $search_object->first();
    }

    /**
     * 获取列表
     * @param string $channel_id 可空
     * @param string $pay_merchant_source 可空
     * @return mixed
     */
    public static function getFinanceMerchantStatList($channel_id = '', $pay_merchant_source = '') {
        $search_object = self::orderBy('enable_withdrawal_amount','desc');

        if($channel_id) {
            $search_object->where('distribution_channel_id', $channel_id);
        }
        if($pay_merchant_source) {
            $search_object->where('pay_merchant_source', $pay_merchant_source);
        }
        return $search_object->get();
    }

    /**
     * 获取列表
     * @param string $channel_id 可空
     * @param array $pay_merchant_sources 可空
     * @return mixed
     */
    public static function getFinanceMerchantStatListIgnoreSourceArray($channel_id = '', $pay_merchant_sources = []) {
        $search_object = self::orderBy('enable_withdrawal_amount','asc');

        if($channel_id) {
            $search_object->where('distribution_channel_id', $channel_id);
        }
        if($pay_merchant_sources) {
            $search_object->whereNotIn('pay_merchant_source', $pay_merchant_sources);
        }
        return $search_object->get();
    }

    /**
     * 获取列表
     * @param string $channel_id 可空
     * @param string $pay_merchant_source 可空
     * @return mixed
     */
    public static function getFinanceMerchantStatListIgnoreSource($channel_id = '', $pay_merchant_source = '') {
        $search_object = self::orderBy('enable_withdrawal_amount','desc');

        if($channel_id) {
            $search_object->where('distribution_channel_id', $channel_id);
        }
        if($pay_merchant_source) {
            $search_object->where('pay_merchant_source', '<>',$pay_merchant_source);
        }
        return $search_object->get();
    }

    /**
     * 添加更新渠道支付通道相关金钱统计
     * @param $channel_id
     * @param $pay_merchant_source
     * @param $amount  添加正数，减少负数
     * @return mixed
     */
    public static function createOrUpdate($channel_id, $pay_merchant_source, $amount) {
        $financeMerchantStat = self::where('distribution_channel_id', $channel_id)->where('pay_merchant_source', $pay_merchant_source)->first();
        if(empty($financeMerchantStat)) {
            $financeMerchantStat['distribution_channel_id'] = $channel_id;
            $financeMerchantStat['pay_merchant_source'] = $pay_merchant_source;
            $financeMerchantStat['enable_withdrawal_amount'] = (float)$amount;

            $financeMerchantStat = FinanceMerchantStat::create($financeMerchantStat);
        } else {
            $financeMerchantStat['enable_withdrawal_amount'] = (float)$financeMerchantStat['enable_withdrawal_amount'] + (float)$amount;

            $financeMerchantStat->save();
        }
        return $financeMerchantStat;
    }

    /**
     * 获取除了$pay_merchant_source之外的渠道总额
     * @param string $channel_id
     * @param string $pay_merchant_source
     * @return mixed
     */
    public static function getEnableWithdrawalAmountSumIgnoreSource($channel_id = '', $pay_merchant_source = '') {
        $search_object = self::orderBy('id','desc');
        if($channel_id) {
            $search_object->where('distribution_channel_id', $channel_id);
        }
        if($pay_merchant_source) {
            $search_object->where('pay_merchant_source', '<>', $pay_merchant_source);
        }
        return $search_object->sum('enable_withdrawal_amount');
    }

    /**
     * 获取除了$pay_merchant_source之外的渠道总额
     * @param string $channel_id
     * @param array $pay_merchant_sources
     * @return mixed
     */
    public static function getEnableWithdrawalAmountSumIgnoreSourceArray($channel_id = '', $pay_merchant_sources = []) {
        $search_object = self::orderBy('id','desc');
        if($channel_id) {
            $search_object->where('distribution_channel_id', $channel_id);
        }
        if($pay_merchant_sources) {
            $search_object->whereNotIn('pay_merchant_source', $pay_merchant_sources);
        }
        return $search_object->sum('enable_withdrawal_amount');
    }

    /**
     * 获取支付渠道总额
     * @param string $channel_id
     * @param string $pay_merchant_source
     * @return mixed
     */
    public static function getEnableWithdrawalAmountSum($channel_id = '', $pay_merchant_source = '') {
        $search_object = self::orderBy('id','desc');
        if($channel_id) {
            $search_object->where('distribution_channel_id', $channel_id);
        }
        if($pay_merchant_source) {
            $search_object->where('pay_merchant_source', $pay_merchant_source);
        }
        return $search_object->sum('enable_withdrawal_amount');
    }
}