<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/17
 * Time: 下午4:34
 */

namespace App\Modules\Finance\Models;


use Illuminate\Database\Eloquent\Model;

/**
 * Class CommissionRate 结算佣金
 * @package App\Modules\Finance\Models
 */
class CommissionRate extends Model
{
    protected $table = 'commission_rates';

    protected $fillable = ['distribution_channel_id', 'begin_amount', 'end_amount', 'rate'];


    /**
     * 根据渠道ID获取列表
     * @param $distribution_channel_id
     * @return mixed
     */
    static function getListByDistributionChannel($distribution_channel_id)
    {
        $result = self::where('distribution_channel_id', $distribution_channel_id)
            ->orderBy('begin_amount', 'asc')
            ->orderBy('end_amount', 'asc')
            ->orderBy('rate', 'asc')
            ->get();
        return $result;
    }







    /**
     * 申请提现
     * @param $data
     * @return bool
     */
    static function apply($data)
    {

        DB::beginTransaction();
        $cash = DB::table('withdraw_cash')->insert($data);
        $stat = DistributionChannelStat::decrDistributionChannelWithdrawAmount($data['distribution_channel_id'],$data['amount']);

        if($cash && $stat)
        {
            DB::commit();
            return $cash;
        }else{
            DB::rollBack();
            return false;
        }

    }
}