<?php

namespace App\Modules\Finance\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Bill 账单
 * @package App\Modules\Finance\Models
 */
class OfficialAccountBills extends Model
{
    protected $table = 'official_account_bills';

    protected $fillable = ['distribution_channel_id', 'appid', 'date', 'nickname', 'recharge_amount', 'channel_recharge_amount'];

    /**
     * 获取服务号的充值信息
     * @param $distribution_channel_id
     * @param $start_time
     * @param $end_time
     * @param $isAll
     * @return mixed
     */
    static function getOfficialAccountBills($distribution_channel_id, $start_time, $end_time, $isAll)
    {
        $search_object = self::orderBy('official_account_bills.date', 'desc')->where('official_account_bills.distribution_channel_id', $distribution_channel_id)
            ->join('official_accounts', 'official_account_bills.appid', '=', 'official_accounts.appid')
            ->leftjoin('send_order_breakeven_stats', function ($join) {
                $join->on('official_accounts.id', '=', 'send_order_breakeven_stats.official_account_id')->on('official_account_bills.date', '=', 'send_order_breakeven_stats.date');
            });

        $search_object->select('official_account_bills.distribution_channel_id', 'official_account_bills.appid',
            'official_account_bills.date', 'official_account_bills.nickname',
            'official_account_bills.recharge_amount', 'official_account_bills.channel_recharge_amount',
            'send_order_breakeven_stats.recharge_amount_in_one_month as recharge_amount_in_30_days',
            'send_order_breakeven_stats.recharge_amount_in_two_months as recharge_amount_in_60_days',
            'send_order_breakeven_stats.recharge_amount_in_three_months as recharge_amount_in_90_days',
            'send_order_breakeven_stats.force_user_num as new_fans_num');

        if ($start_time) {
            $search_object->where('official_account_bills.date', '>=', $start_time);
        }

        if ($end_time) {
            $search_object->where('official_account_bills.date', '<=', $end_time);
        }

        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }
}
 