<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/21
 * Time: 10:42
 */

namespace App\Modules\User\Models;

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

class RegisterUserDayStat extends Model
{
    protected $table = 'promotion_day_city_user_stats';

    protected $fillable =
        ['id', 'date', 'country', 'province', 'city', 'register_user_num', 'recharge_in_one_day',
            'arpu_in_one_day', 'created_at', 'updated_at'
        ];

    static function getRecord($date, $country, $province, $city)
    {
        return self::where('date', $date)->where('country', $country)
            ->where('province', $province)
            ->where('city', $city)->first();
    }

    static function addItem($data)
    {
        return self::create($data);
    }

    static function getWeekTotalInfo($begin_date, $end_date)
    {
        $search_obj = self::where('date', '>=', $begin_date)->where('date', '<=', $end_date);

        $search_obj->select(
            'country',
            'province',
            DB::raw('sum(register_user_num) sum_register_user_num'),
            DB::raw('sum(recharge_in_one_day) sum_recharge_in_one_day'),
            DB::raw('sum(recharge_in_one_day)/sum(register_user_num) sum_arup')
        );
        $search_obj->orderBy('sum_recharge_in_one_day', 'desc');
        $search_obj->groupBy('country')->groupBy('province');
        return $search_obj->get();
    }

}