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

namespace App\Modules\Channel\Services;


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

class BusinessChannelDayStatService
{


    /**
     * 创建商务渠道日报表数据
     * @param $params [distribution_channel_id, date, register_user_num, send_order_num]
     * @return mixed
     */
    public static function createBusinessChannelDayStat($params) {
        $data = BusinessChannelDayStat::create($params);
        return $data;
    }

    /**
     * 商务渠道数据日统计列表
     * @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 = '') {
        $data = BusinessChannelDayStat::getList($params, $isAll);
        return $data;
    }

    public static function getAutoForceSubUserNum($channel_id,$start,$end){
        return DB::table('force_subscribe_users')
            ->where('distribution_channel_id',$channel_id)
            ->whereBetween('created_at',[$start,$end])
            ->where('official_account_id','<>',1)
            ->where('is_subscribed',1)
            ->count();
    }

    public static function getSelfForceSubUserNum($channel_id,$start,$end){
        return DB::table('force_subscribe_users')
            ->where('distribution_channel_id',$channel_id)
            ->whereBetween('created_at',[$start,$end])
            ->where('official_account_id','=',1)
            ->where('is_subscribed',1)
            ->count();
    }

    public static function getChannelSubscribeNum($channelId,$start,$end){
        $users = DB::table('users')
            ->where('distribution_channel_id',$channelId)
            ->get();
        $user_num = 0;
        foreach ($users as $user){
            $uid = ($user->id)%512;
            $chapter_orders = DB::connect('chapter_order_mysql')
                ->table('chapter_orders'.$uid)
                ->where('uid',$user->id)
                ->whereBetween('created_at',[$start,$end])
                ->first();
            if($chapter_orders){
                $user_num++;
                continue;
            }
            $book_orders = DB::table('book_orders')
                ->where('uid',$user->id)
                ->whereBetween('created_at',[$start,$end])
                ->first();
            if($book_orders){
                $user_num++;
            }
        }
        return $user_num;
    }

     public static function getChannelSubscribeRecharge($channelId,$start,$end){
        $users = DB::table('users')
            ->where('distribution_channel_id',$channelId)
            ->get();
        $fee = 0;
        foreach ($users as $user){
            $uid = ($user->id)%512;
            $chapter_orders = DB::connect('chapter_order_mysql')
                ->table('chapter_orders'.$uid)
                ->where('uid',$user->id)
                ->whereBetween('created_at',[$start,$end])
                ->sum('fee');

            $book_orders = DB::table('book_orders')
                ->where('uid',$user->id)
                ->whereBetween('created_at',[$start,$end])
                ->sum('fee');
            $fee=$fee+$chapter_orders+$book_orders;
        }
        return $fee;
    }

}