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

namespace App\Modules\Channel\Services;


use App\Modules\Channel\Models\BusinessChannelStat;
use DB;

class BusinessChannelStatService
{

    /**
     * @param $channel_id
     * @param $params [yesterday_register_user_num， current_month_register_user_num，last_month_register_user_num，total_register_user_num，total_send_order_num]
     * @return mixed
     */
    public static function crateUpdate($channel_id, $params) {
        return BusinessChannelStat::crateUpdate($channel_id, $params);
    }

    /**
     * 商务渠道数据统计列表
     * @param $params[]
     *                  channel_id:渠道ID 可选
     *                  channel_name:渠道名称 可选
     *                  search_name: 搜索名称
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  is_enabled: 是否开通 0:未审核； 1:审核通过
     *                  distribution_manages_id: 管理员 可选
     *                  yesterday_register_user_num_sort: 0 1
     *                  current_month_register_user_num_sort: 0 1
     *                  last_month_register_user_num_sort: 0 1
     *                  total_register_user_num_sort: 0 1
     *                  total_send_order_num_sort: 0 1
     *
     * @param string $isAll
     * @return mixed
     */
    public static function getList($params, $isAll = '') {
        return BusinessChannelStat::getList($params, $isAll);
    }

    /**
     * 获取指定渠道列表汇总
     * @param array $channelIds
     * @return mixed
     */
    public static function getBusinessChannelStatSingle($channelIds=[]) {
        return BusinessChannelStat::getBusinessChannelStatSingle($channelIds);
    }

    /**
     *
     * @param $channel_id
     * @return int
     */
    public static function getServiceAccountNum($channel_id){
        $account = DB::table('official_accounts')
            ->join('distribution_channels','official_accounts.distribution_channel_id','=','distribution_channels.id')
            ->select(DB::raw('count(*) as account_num'))
            ->where([
                ['official_accounts.is_auth','=',1],
                ['distribution_channels.id','=',$channel_id]
            ])
            ->first();
        return $account?$account->account_num:0;
    }

    public static function getLoginDays($channel_id,$start_time,$end_time){
        $start_date = date('Y-m-d',strtotime($start_time.' +1 day'));
        $end_date = date('Y-m-d',strtotime($end_time.' +1 day'));
        $company = DB::table('distribution_channels')
            ->select('channel_users.company_id')
            ->where('distribution_channels.id',$channel_id)
            ->join('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
            ->first();
        $login_info = '';
        if($company){
            $company_id = $company->company_id;
            $login_info = DB::table('business_channel_day_stats')
                ->join('distribution_channels','distribution_channels.id','=','business_channel_day_stats.distribution_channel_id')
                ->join('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
                ->select(DB::raw("count(distinct business_channel_day_stats.date) as login_user_num"))
                ->where([
                    ['business_channel_day_stats.is_login_yesterday','=',1],
                    ['business_channel_day_stats.date','>=',$start_date],
                    ['business_channel_day_stats.date','<=',$end_date],
                    ['channel_users.company_id','=',$company_id]
                ])
                ->first();
        }
        /*DB::table('channel_operate_record')
            ->join('distribution_channels','distribution_channels.id','=','channel_operate_record.distribution_channel_id')
            ->join('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
            ->where([
                ['channel_users.company_id','=',$company_id],
                ['distribution_channels.created_at','>=',$start_time],
                ['distribution_channels.created_at','<=',$end_time]
            ])
            ->select(DB::raw("count(distin)"))
            ->first();*/

        if($login_info) {
            return $login_info->login_user_num;
        }
        return $login_info;
    }

    public static function getListNew($params, $isAll = '') {
        return BusinessChannelStat::getListNew($params, $isAll);
    }

    public static function getLastMonthChannelRecharge($channel_id){
        /*$BusinessChannelStats = BusinessChannelStat::where('distribution_channel_id',$channel_id)
            ->whereNotNull('last_month_channels_recharge')
            ->first();*/
        $last_month_start = date('Y-m-01',strtotime('-1 month'));
        $last_month_end = date('Y-m-d H:i:s',strtotime(date('Y-m-01 23:59:59').' -1 day'));
        //if(!$BusinessChannelStats && date('d')){
            $order_sum = DB::table('orders')
                ->where('distribution_channel_id',$channel_id)
                ->where('status','PAID')
                ->whereBetween('created_at',[$last_month_start,$last_month_end])
                ->sum('price');
            return $order_sum?$order_sum:0;
        //}
        //return $BusinessChannelStats->last_month_channels_recharge;
    }

    public static function getByChannelID($channel_id)
    {
        return BusinessChannelStat::getByChannelID($channel_id);
    }

}