<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/26
 * Time: 下午3:24
 */

namespace App\Modules\Channel\Models;


use Illuminate\Database\Eloquent\Model;

/**
 * 商务渠道数据日统计
 * Class BusinessChannelDayStat
 * @package App\Modules\Channel\Models
 */
class BusinessChannelDayStat extends Model
{
    protected  $table = 'business_channel_day_stats';
    protected  $fillable = ['distribution_channel_id', 'date', 'register_user_num', 'send_order_num','is_login_yesterday',
        'whole_site_uv','whole_site_pv','auto_force_sub_num','self_force_sub_num','subscribe_user_num','subscribe_amount'];


    /**
     * 商务渠道数据日统计列表
     * @param $params[]
     *                  channel_id:渠道ID 可选
     *                  channel_name:渠道名称 可选
     *                  search_name: 搜索名称
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  start_dateA:统计开始时间 可选
     *                  end_dateA:统计结束时间 可选
     *                  is_enabled: 是否开通 0:未审核； 1:审核通过
     *                  distribution_manages_id: 管理员 可选
     * @param $isAll
     * @return mixed
     */
    public static function getList($params, $isAll = '') {
        $search_object = Channel::select([
            'distribution_channels.id',
            'distribution_channels.name',
            'distribution_channels.pay_merchant_id',
            'distribution_channels.phone',
            'distribution_channels.nickname',
            'distribution_channels.person_in_charge_name',
            'distribution_channels.latest_login_time',
            'distribution_channels.remark',
            'distribution_channels.latest_login_ip',
            'distribution_channels.password',
            'distribution_channels.register_ip',
            'distribution_channels.is_enabled',
            'distribution_channels.distribution_manages_id',
            'distribution_channels.created_at',
            'distribution_manages.account as distribution_manages_account',
            'distribution_manages.number as distribution_manages_number',
            'distribution_manages.nickname as distribution_manages_nickname',
            'business_channel_day_stats.date as business_channel_day_stats_date',
            'business_channel_day_stats.register_user_num as business_channel_day_stats_register_user_num',
            'business_channel_day_stats.send_order_num as business_channel_day_stats_send_order_num',
        ])
            ->leftjoin('distribution_manages','distribution_manages.id','=','distribution_channels.distribution_manages_id')
            ->leftjoin('business_channel_day_stats','business_channel_day_stats.distribution_channel_id','=','distribution_channels.id')
            ->orderBy('distribution_channels.id','asc');

        $search_object->where(function ($query) use($params) {
            $query->where('distribution_manages.role', 'business')
                ->orWhereNull('distribution_manages.role');
        });


        if(isset($params['channel_id']) && $params['channel_id']) $search_object->where('distribution_channels.id', $params['channel_id']);
        if(isset($params['channel_name']) && $params['channel_name']) $search_object->where('distribution_channels.name','like', "%".$params['channel_name']."%");

        if(isset($params['search_name']) && $params['search_name']) {
            $search_object->where(function ($query) use($params) {
                $query->where('distribution_channels.name','like', "%".$params['search_name']."%")
                    ->orWhere('distribution_channels.nickname', 'like', "%".$params['search_name']."%")
                    ->orWhere('distribution_channels.person_in_charge_name', 'like', "%".$params['search_name']."%");
            });
        }

        if(isset($params['start_date']) && $params['start_date']) $search_object->where('distribution_channels.created_at','>=', $params['start_date']);
        if(isset($params['end_date']) && $params['end_date']) $search_object->where('distribution_channels.created_at','<=', $params['end_date']);

        if(isset($params['start_dateA']) && $params['start_dateA']) $search_object->where('business_channel_day_stats.date','>=', $params['start_date']);
        if(isset($params['end_dateA']) && $params['end_dateA']) $search_object->where('business_channel_day_stats.date','<=', $params['end_date']);

        if(isset($params['is_enabled']) && is_numeric($params['is_enabled'])) {
            $search_object->where('distribution_channels.is_enabled', $params['is_enabled']);
        }

        if(isset($params['distribution_manages_id']) && is_numeric($params['distribution_manages_id'])) {
            $search_object->where('distribution_channels.distribution_manages_id', $params['distribution_manages_id']);
        }

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

        } else {

            return $search_object->paginate();
        }
    }
}