<?php

namespace App\Modules\Finance\Models;

use App\Consts\BillConst;
use Illuminate\Database\Eloquent\Model;
use DB;

/**
 * Class Bill 账单
 * @package App\Modules\Finance\Models
 */
class BillPayMerchant extends Model
{
    protected $table = 'bills_by_pay_merchants';
    //recharge_amount:充值金额
    protected $fillable = ['distribution_channel_id', 'distribution_channel_name', 'date', 'recharge_amount', 'tallage', 'settlement_price', 'rate', 'pay_company_id'];

    public static function getOne($channelId, $date)
    {
        return self::where('distribution_channel_id', $channelId)
            ->where('date', $date)
            ->first();
    }

    public static function addPayMerchantsBills($recharge_infos)
    {
        DB::beginTransaction();
        try {
            foreach ($recharge_infos as $recharge_info) {
                $rate = Bill::getRate($recharge_info['distribution_channel_id']);

                // 服务费
                $serviceAmount = 0;
                $channelId     = (int)$recharge_info['distribution_channel_id'];
                if (time() >= strtotime(BillConst::SERVICE_START)) {
                    $serviceAmount = (float)$recharge_info['amount'] * BillConst::SERVICE_RATE;
                    // 保留2位截断小数
                    $serviceAmount = getFloorNum($serviceAmount,2);
                }
                
                // 需要减去服务费
                $recharge_info['amount'] = (float)$recharge_info['amount'] - $serviceAmount;

                $dataBill['distribution_channel_id'] = $recharge_info['distribution_channel_id'];
                $dataBill['date']                    = date("Y-m-d", strtotime(date("Y-m-d") . " -1 day"));
                $dataBill['recharge_amount']         = (float)$recharge_info['amount'];
                $dataBill['settlement_price']        = (float)$recharge_info['amount'] * $rate;
                
                // 保留2位截断小数
                $dataBill['settlement_price'] = getFloorNum($dataBill['settlement_price'],2);
                
                $dataBill['rate']                    = $rate;
                $dataBill['pay_company_id']          = $recharge_info['pay_company_id'];

                //TODO 扣税金额为 0
                $dataBill['tallage'] = 0;

                BillPayMerchant::create($dataBill);
                //更新创建提现信息
                $dataFinancialStatDB = FinancialPayMerchantBalance::where('distribution_channel_id', $recharge_info['distribution_channel_id'])
                    ->where('pay_merchant_company_id', $recharge_info['pay_company_id'])
                    ->first();
                if (empty($dataFinancialStatDB)) {
                    $dataFinancialStat['distribution_channel_id']        = $recharge_info['distribution_channel_id'];
                    $dataFinancialStat['pay_merchant_company_id']        = $recharge_info['pay_company_id'];
                    $dataFinancialStat['accumulative_withdrawal_amount'] = 0;
                    $dataFinancialStat['enable_withdrawal_amount']       = $dataBill['settlement_price'];
                    $dataFinancialStat['latest_withdrawal_amount']       = 0;
                    $dataFinancialStat['withdraw_pending_amount']        = 0;
                    $dataFinancialStat['commission_rate']                = $rate;
                    FinancialPayMerchantBalance::create($dataFinancialStat);
                    //FinancialStat::create($dataFinancialStat);
                } else {
                    \Log::info('addPayMerchantsBills:'.$recharge_info['distribution_channel_id'].' enable_withdrawal_amount:'.$dataFinancialStatDB['enable_withdrawal_amount'].' settlement_price:'.$dataBill['settlement_price']);
                    
                    $dataFinancialStatDB['enable_withdrawal_amount'] = (float)$dataFinancialStatDB['enable_withdrawal_amount'] + (float)$dataBill['settlement_price'];
                    $dataFinancialStatDB['commission_rate']          = $rate;

                    $dataFinancialStatDB->save();
                }

            }
            DB::commit();
        } catch (\Exception $e) {
            \Log::error('add to financial_pay_merchant_balance_failed:' . $e->getMessage());
            DB::rollBack();
        }
    }

}
 