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

namespace App\Modules\Finance\Models;


use Illuminate\Database\Eloquent\Model;

/**
 * 清算汇总表
 * Class LiquidatedStat
 * @package App\Modules\Finance\Models
 */
class LiquidatedStat extends Model
{
    protected $table = 'liquidated_stats';
    protected $fillable = ['amount', 'account_balance_amount', 'account_balance_amount_day', 'pay_merchant_source'];

    /**
     * 添加更新清算汇总
     * @param $amount 添加正数，减少负数
     * @param $pay_merchant_source
     * @return mixed
     */
    public static function createOrUpdate($pay_merchant_source, $amount) {
        $liquidatedStat = self::where('pay_merchant_source', $pay_merchant_source)->first();

        if($pay_merchant_source == config('common.tonglianpay') || $pay_merchant_source == config('common.officialpay')) {
            //官方，通联，每天自动结算，不用清算数据
            $amount = 0;
        }

        if(empty($liquidatedStat)) {
            $liquidatedStat['amount'] = $amount;
            $liquidatedStat['pay_merchant_source'] = $pay_merchant_source;

            $liquidatedStat = LiquidatedStat::create($liquidatedStat);
        } else {
            $liquidatedStat['amount'] = (float)$liquidatedStat['amount'] + (float)$amount;

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

    /**
     * 更新支付渠道余额
     * @param $pay_merchant_source
     * @param $amount
     * @return mixed
     */
    public static function updateAccountBalanceAmount($pay_merchant_source, $amount) {
        $liquidatedStat = self::where('pay_merchant_source', $pay_merchant_source)->first();
        print_r('$liquidatedStat:'.json_encode($liquidatedStat));
        if(!empty($liquidatedStat)) {

            $hh = date('H');

            $liquidatedStat->account_balance_amount_day = (float)$amount;
            $liquidatedStat->account_balance_amount = (float)$amount;
            print_r('account_balance_amount_day:'.$liquidatedStat->account_balance_amount);
            $liquidatedStat->save();
            print_r('pay_source:'.$pay_merchant_source);
            $res = self::where('pay_merchant_source', $pay_merchant_source)->update(['account_balance_amount_day'=>$amount,'account_balance_amount'=>$amount]);
            print_r('---:'.$res);
        }
        return $liquidatedStat;
    }
    /**
     * 更新支付渠道余额当天
     * @param $pay_merchant_source
     * @param $amount
     * @return mixed
     */
    public static function updateAccountBalanceAmountDay($pay_merchant_source, $amount) {
        $liquidatedStat = self::where('pay_merchant_source', $pay_merchant_source)->first();
        if(!empty($liquidatedStat)) {
            $liquidatedStat['account_balance_amount_day'] = $liquidatedStat['account_balance_amount_day'] + (float)$amount;
            $liquidatedStat->save();
        }
        return $liquidatedStat;
    }

    /**
     * 清算汇总列表
     * @return mixed
     */
    public static function getLiquidatedStatList() {
        $search_object = LiquidatedStat::select([
            'liquidated_stats.id',
            'liquidated_stats.amount',
            'liquidated_stats.account_balance_amount',
            'liquidated_stats.pay_merchant_source'
        ]);
        return $search_object->get();
    }

    /**
     * 根据支付通道获取
     * @param $pay_merchant_source
     * @return mixed
     */
    public static function getLiquidatedStatSingleBySource($pay_merchant_source) {
        $liquidatedStat = self::where('pay_merchant_source', $pay_merchant_source)->first();
        return $liquidatedStat;
    }
}