<?php

namespace App\Modules\Trade\Models;

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

class OrderStat extends Model
{
    protected $table = 'order_stats';

    protected $fillable = [
        'distribution_channel_id', 'pay_success_user_num', 'unpaid_num', 'paid_num','total_recharge_amount',
        'yesterday_paid_num','yesterday_unpaid_num','yesterday_recharge_amount','yesterday_ticket_recharge_paid_num','yesterday_ticket_recharge_unpaid_num','yesterday_ticket_recharge_amount','yesterday_year_recharge_paid_num','yesterday_year_recharge_unpaid_num','yesterday_year_recharge_amount',
        'current_month_paid_num','current_month_unpaid_num','current_month_recharge_amount','current_month_ticket_recharge_paid_num','current_month_ticket_recharge_unpaid_num','current_month_ticket_recharge_amount','current_month_year_recharge_paid_num','current_month_year_recharge_unpaid_num','current_month_year_recharge_amount',
        'last_month_paid_num','last_month_unpaid_num','last_month_recharge_amount','last_month_ticket_recharge_paid_num','last_month_ticket_recharge_unpaid_num','last_month_ticket_recharge_amount','last_month_year_recharge_paid_num','last_month_year_recharge_unpaid_num','last_month_year_recharge_amount',
        'send_order_num','register_user_num','ticket_recharge_pay_user_num','year_pay_user_num','force_user_num','promotion_total_uv','promotion_total_pv','official_account_num','official_account_names'];

    //通过分销渠道ID获取
    static function getByChannelId($distribution_channel_id)
    {
        return self::where('distribution_channel_id',$distribution_channel_id)->first();
    }

    //更新创建
    static function add($distribution_channel_id,$params)
    {
        return self::updateOrCreate(compact('distribution_channel_id'),$params);
    }

    //获取渠道列表数据
    static function getList($distribution_channel_id,$phone,$is_all=false)
    {
        $fields = ['order_stats.*','distribution_channels.phone','distribution_channels.nickname'];
        $search_obj = self::select($fields)->leftjoin('distribution_channels','distribution_channels.id','=','order_stats.distribution_channel_id')->orderBy('total_recharge_amount','desc');
        if($distribution_channel_id) $search_obj->where('order_stats.distribution_channel_id',$distribution_channel_id);
        if($phone) $search_obj->where('distribution_channels.phone',$phone);

        if ($is_all)
        {
            return $search_obj->get();
        }else{
            return $search_obj->paginate();
        }

    }

    //获取多个渠道汇总数据
    static function getChannelTotalStat($distribution_channel_ids)
    {
        $fields = [
            DB::raw("
                     sum(total_recharge_amount) total_recharge_amount,
                     sum(unpaid_num) unpaid_num,
                     sum(paid_num) paid_num,
                     sum(ticket_recharge_pay_user_num) ticket_recharge_pay_user_num,
                     sum(promotion_total_uv) promotion_total_uv,
                     sum(force_user_num) force_user_num,
                     sum(year_pay_user_num) year_pay_user_num
                     ")
        ];
        return self::select($fields)->whereIn('distribution_channel_id',$distribution_channel_ids)->first();
    }

    static function getRechargeAmount($distribution_channel_ids){
        return self::select(DB::raw('sum(total_recharge_amount) as total_recharge_amount_sum,sum(current_month_recharge_amount) as current_month_recharge_amount_sum'))
            ->whereIn('distribution_channel_id',$distribution_channel_ids)
            ->first();
    }
}