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

namespace App\Http\Controllers\Manage\Channel;

use App\Http\Controllers\Manage\Channel\Transformers\BusinessChannelDayStatTransformer;
use App\Http\Controllers\Manage\Channel\Transformers\BusinessChannelStatTransformer;
use App\Http\Controllers\Manage\Channel\Transformers\BusinessCompanyStatTransformer;
use App\Http\Controllers\Manage\Channel\Transformers\BusinessStatDataTransformer;
use App\Http\Controllers\Manage\Finance\BaseController;
use App\Modules\Channel\Models\BusinessChannelStat;
use App\Modules\Channel\Services\BusinessChannelDayStatService;
use App\Modules\Channel\Services\BusinessChannelStatService;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Channel\Services\CompanyService;
use App\Modules\Manage\Services\ManageService;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\Trade\Services\OrderDayStatService;
use App\Modules\Trade\Services\OrderService;
use App\Modules\Trade\Services\OrderStatService;
use App\Modules\User\Services\UserService;
use DB;
use Illuminate\Http\Request;

class BusinessChannelController extends BaseController
{

    /**
     * @apiVersion 1.0.0
     * @apiDescription 商务渠道数据统计每日列表
     * @api {GET} channel/getBusinessChannelDayList 商务渠道数据统计每日列表
     * @apiGroup channel
     * @apiName channel/getBusinessChannelDayList
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_enabled] 0:待审核； 1:已审核.(可不传)
     * @apiParam   {Number}  [distribution_manages_id] 管理员ID.(可不传)
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccess {String}  person_in_charge_name 负责人.
     * @apiSuccess {String}  create_time 注册时间
     * @apiSuccess {Number}  distribution_manages_id 管理员ID
     * @apiSuccess {String}  distribution_manages_account 管理员
     * @apiSuccess {String}  distribution_manages_number 管理员
     * @apiSuccess {String}  distribution_manages_nickname 管理员昵称
     * @apiSuccess {String}  business_channel_day_stats_date 统计时间
     * @apiSuccess {String}  business_channel_day_stats_register_user_num 注册用户数
     * @apiSuccess {String}  business_channel_day_stats_send_order_num 派单数
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *             {
     *                  "id": 1,
     *                   "phone": "",
     *                   "name": "121",
     *                   "pay_merchant_id": 1,
     *                   "nickname": "是说",
     *                   "latest_login_time": "",
     *                   "latest_login_ip": "",
     *                   "is_enabled": 1,
     *                   "register_ip": "",
     *                   "remark": "",
     *                   "person_in_charge_name": "波哥帅",
     *                   "create_time": "2017-11-20 18:34:17",
     *                   "distribution_manages_id": 0,
     *                   "distribution_manages_account": "zsy_finance",
     *                   "distribution_manages_number": "",
     *                   "distribution_manages_nickname": "3333",
     *                   "business_channel_day_stats_date": "2017-12-25",
     *                   "business_channel_day_stats_register_user_num": 0,
     *                   "business_channel_day_stats_send_order_num": 0
     *              }
     *          ]
     *     }
     */
    public function getBusinessChannelDayList(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';

        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $distribution_manages_id = $request->has('distribution_manages_id') ? $request->input('distribution_manages_id') : '';
        $isEnabled = $request->has('is_enabled') ? $request->input('is_enabled') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';

        //商务，只获取到当前商务下的渠道列表
        if ($this->getLoginUserRole() == "business") {
            $distribution_manages_id = $this->getLoginUserId();
        }
//        dd($this->getLoginUserId());

        $params = [
            'channel_id' => $distribution_channel_id,
            'channel_name' => $distribution_channel_name,
            'start_date' => $start_time,
            'end_date' => $end_time,
            'distribution_manages_id' => $distribution_manages_id,
            'is_enabled' => $isEnabled,
            'search_name' => $search_name,
        ];

        $result = BusinessChannelDayStatService::getList($params);

        return response()->pagination(new BusinessChannelDayStatTransformer(), $result);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获商务渠道数据统计列表
     * @api {GET} channel/getBusinessChannelList 商务渠道数据统计列表
     * @apiGroup channel
     * @apiName channel/getBusinessChannelList
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_enabled] 0:待审核； 1:已审核.(可不传)
     * @apiParam   {Number}  [distribution_manages_id] 管理员ID.(可不传)
     * @apiParam   {Number}  [yesterday_register_user_num_sort] 昨日注册用户数排序  0:升序，1:降序.(可不传)
     * @apiParam   {Number}  [current_month_register_user_num_sort] 当月注册用户数排序  0:升序，1:降序.(可不传)
     * @apiParam   {Number}  [last_month_register_user_num_sort] 上月注册用户数排序  0:升序，1:降序.(可不传)
     * @apiParam   {Number}  [total_register_user_num_sort] 总注册用户数排序  0:升序，1:降序.(可不传)
     * @apiParam   {Number}  [total_send_order_num_sort] 总派单数  0:升序，1:降序.(可不传)
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccess {String}  person_in_charge_name 负责人.
     * @apiSuccess {String}  create_time 注册时间
     * @apiSuccess {Number}  distribution_manages_id 管理员ID
     * @apiSuccess {String}  distribution_manages_account 管理员
     * @apiSuccess {String}  distribution_manages_number 管理员
     * @apiSuccess {String}  distribution_manages_nickname 管理员昵称
     * @apiSuccess {String}  business_channel_stats_today_register_user_num 今日注册用户数
     * @apiSuccess {String}  business_channel_stats_yesterday_register_user_num 昨日注册用户数
     * @apiSuccess {String}  business_channel_stats_current_month_register_user_num 当月注册用户数
     * @apiSuccess {String}  business_channel_stats_last_month_register_user_num 上月注册用户数
     * @apiSuccess {String}  business_channel_stats_total_register_user_num 总注册用户数
     * @apiSuccess {String}  business_channel_stats_total_send_order_num 总派单数
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *             {
     *                  "id": 1,
     *                   "phone": "",
     *                   "name": "121",
     *                   "pay_merchant_id": 1,
     *                   "nickname": "是说",
     *                   "latest_login_time": "",
     *                   "latest_login_ip": "",
     *                   "is_enabled": 1,
     *                   "register_ip": "",
     *                   "remark": "",
     *                   "person_in_charge_name": "波哥帅",
     *                   "create_time": "2017-11-20 18:34:17",
     *                   "distribution_manages_id": 0,
     *                   "distribution_manages_account": "zsy_finance",
     *                   "distribution_manages_number": "",
     *                   "distribution_manages_nickname": "3333",
     *                   "business_channel_stats_today_register_user_num": 4,
     *                   "business_channel_stats_yesterday_register_user_num": 0,
     *                   "business_channel_stats_current_month_register_user_num": 0,
     *                   "business_channel_stats_last_month_register_user_num": 0,
     *                   "business_channel_stats_total_register_user_num": 0,
     *                   "business_channel_stats_total_send_order_num": 0
     *              }
     *          ]
     *     }
     */
    public function getBusinessChannelList(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';

        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $distribution_manages_id = $request->has('distribution_manages_id') ? $request->input('distribution_manages_id') : '';
        $isEnabled = $request->has('is_enabled') ? $request->input('is_enabled') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';

        //商务，只获取到当前商务下的渠道列表
        if ($this->getLoginUserRole() == "business") {
            $distribution_manages_id = $this->getLoginUserId();
        }

        $yesterday_register_user_num_sort = $request->has('yesterday_register_user_num_sort') ? $request->input('yesterday_register_user_num_sort') : '';
        $current_month_register_user_num_sort = $request->has('current_month_register_user_num_sort') ? $request->input('current_month_register_user_num_sort') : '';
        $last_month_register_user_num_sort = $request->has('last_month_register_user_num_sort') ? $request->input('last_month_register_user_num_sort') : '';
        $total_register_user_num_sort = $request->has('total_register_user_num_sort') ? $request->input('total_register_user_num_sort') : '';
        $total_send_order_num_sort = $request->has('total_send_order_num_sort') ? $request->input('total_send_order_num_sort') : '';

        $params = [
            'channel_id' => $distribution_channel_id,
            'channel_name' => $distribution_channel_name,
            'start_date' => $start_time,
            'end_date' => $end_time,
            'distribution_manages_id' => $distribution_manages_id,
            'is_enabled' => $isEnabled,
            'search_name' => $search_name,
            'yesterday_register_user_num_sort' => $yesterday_register_user_num_sort,
            'current_month_register_user_num_sort' => $current_month_register_user_num_sort,
            'last_month_register_user_num_sort' => $last_month_register_user_num_sort,
            'total_register_user_num_sort' => $total_register_user_num_sort,
            'total_send_order_num_sort' => $total_send_order_num_sort,
        ];

        $result = BusinessChannelStatService::getList($params);
        $today = date('Y-m-d');

        if ($result) {
            foreach ($result as $item) {
                $item['business_channel_stats_today_register_user_num'] = UserService::getChannelDayTotal($item['id'], $today);
                $item['business_channel_stats_total_register_user_num'] = UserService::getChannelTotal($item['id']);
            }
        }
        return response()->pagination(new BusinessChannelStatTransformer(), $result);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获商务所有渠道数据统计汇总
     * @api {GET} channel/getBusinessChannelSingle 获商务所有渠道数据统计汇总
     * @apiGroup channel
     * @apiName channel/getBusinessChannelSingle
     * @apiParam   {Number}  [distribution_manages_id] 管理员ID.(可不传)
     * @apiSuccess {String}  today_register_user_num 今日注册用户数
     * @apiSuccess {String}  yesterday_register_user_num 昨日注册用户数
     * @apiSuccess {String}  current_month_register_user_num 当月注册用户数
     * @apiSuccess {String}  last_month_register_user_num 上月注册用户数
     * @apiSuccess {String}  stats_total_register_user_num 总注册用户数
     * @apiSuccess {String}  stats_total_send_order_num 总派单数
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *                   "today_register_user_num": 4,
     *                   "yesterday_register_user_num": 0,
     *                   "current_month_register_user_num": 0,
     *                   "last_month_register_user_num": 0,
     *                   "total_register_user_num": 0,
     *                   "total_send_order_num": 0
     *          }
     *     }
     */
    public function getBusinessChannelSingle(Request $request)
    {

        $distribution_manages_id = $request->has('distribution_manages_id') ? $request->input('distribution_manages_id') : '';
        //商务，只获取到当前商务下的渠道列表
        if ($this->getLoginUserRole() == "business") {
            $distribution_manages_id = $this->getLoginUserId();
        }

        if ($this->getLoginUserRole() == "admin") {
            $distribution_manages_id = '';
        }

        $channelIds = ChannelService::getChannelIdList($distribution_manages_id);

        $yesterStartDay = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 00:00:00";
        $yesterEndDay = date('Y-m-d', strtotime(date("Y-m-d"))) . " 00:00:00";
        $toDayEndDay = date('Y-m-d', strtotime(date("Y-m-d"))) . " 23:59:59";
        $last_monthStartDay = date('Y-m', strtotime(date("Y-m") . " -1 month")) . "-01 00:00:00";
        $current_monthStartDay = date('Y-m', strtotime(date("Y-m"))) . "-01  00:00:00";

        $lastMonthStart = date('Y-m-01 00:00:00',strtotime('-1 month'));
        $lastMonthEnd = date('Y-m-d 23:59:59',strtotime(date('Y-m-01').'-1 day'));
        $today_register_user_num = UserService::getChannelsDayToDayTotal($channelIds, $yesterEndDay, $toDayEndDay);

        $currentMonthStart = date('Y-m-01 00:00:00');
//        $yesterday_register_user_num = UserService::getChannelsDayToDayTotal($channelIds, $yesterStartDay, $yesterEndDay);
//        $current_month_register_user_num = UserService::getChannelsDayToDayTotal($channelIds, $current_monthStartDay, $yesterEndDay);
//        $last_month_register_user_num = UserService::getChannelsDayToDayTotal($channelIds, $last_monthStartDay, $current_monthStartDay);
//        $total_register_user_num = UserService::getChannelsDayToDayTotal($channelIds);
//        $total_send_order_num = SendOrderService::getTotalPromotionCountByChannelIds($channelIds);

        $yesterday_register_user_num = BusinessChannelStat::whereIn('distribution_channel_id', $channelIds)
//            ->where('created_at', '>=', $yesterStartDay)
//            ->where('created_at', '<', $yesterEndDay)
            ->sum('yesterday_register_user_num');
        $current_month_register_user_num = BusinessChannelStat::whereIn('distribution_channel_id', $channelIds)
//            ->where('created_at', '>=', $current_monthStartDay)
//            ->where('created_at', '<', $yesterEndDay)
            ->sum('current_month_register_user_num');
        $last_month_register_user_num = BusinessChannelStat::whereIn('distribution_channel_id', $channelIds)
//            ->where('created_at', '>=', $last_monthStartDay)
//            ->where('created_at', '<', $current_monthStartDay)
            ->sum('last_month_register_user_num');
        $total_register_user_num = BusinessChannelStat::whereIn('distribution_channel_id', $channelIds)
            ->sum('total_register_user_num');
        $total_send_order_num = BusinessChannelStat::whereIn('distribution_channel_id', $channelIds)
            ->sum('total_send_order_num');
        $yesterday_new_companies_num = BusinessChannelStat::getNewAddedCompaniesNum($yesterStartDay,$yesterEndDay,$channelIds);
        $yesterday_new_channel_num = BusinessChannelStat::getNewwAddedChannelNum($yesterStartDay,$yesterEndDay,$channelIds);

        $last_month_new_companies_num = BusinessChannelStat::getNewAddedCompaniesNum($currentMonthStart,date('Y-m-d H:i:s'),$channelIds);
        $last_month_new_channel_num = BusinessChannelStat::getNewwAddedChannelNum($currentMonthStart,date('Y-m-d H:i:s'),$channelIds);
        $recharge_data = OrderStatService::getRechargeAmount($channelIds);
        $manage_company_total_charge_amount = $recharge_data->total_recharge_amount_sum;

        $month_begin_time = date('Y-m-01 00:00:00');
        $manage_company_total_charge_amount_current_month = $recharge_data->current_month_recharge_amount_sum;//OrderDayStatService::getRechargeByChannels($channelIds,$time_range=['begin_time' => $month_begin_time, 'end_time' => $yesterEndDay]);
        //$manage_company_total_charge_amount_current_month = OrderService::getAmount($manage_order_params_current_month);

        return response()->success(compact(
            'today_register_user_num',
            'yesterday_register_user_num',
            'current_month_register_user_num',
            'last_month_register_user_num',
            'total_register_user_num',
            'total_send_order_num',
            'last_month_new_companies_num',
            'last_month_new_channel_num',
            'yesterday_new_companies_num',
            'yesterday_new_channel_num',
            'manage_company_total_charge_amount',
            'manage_company_total_charge_amount_current_month'
        ));
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获商务数据统计列表
     * @api {GET} channel/getBusinessDataList 商务数据统计列表
     * @apiGroup channel
     * @apiName channel/getBusinessDataList
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_enabled]: 0:不可用； 1:可用
     *
     * @apiSuccess {Number}  distribution_manages_id 管理员ID
     * @apiSuccess {String}  distribution_manages_account 管理员
     * @apiSuccess {String}  distribution_manages_number 管理员
     * @apiSuccess {String}  distribution_manages_nickname 管理员昵称
     * @apiSuccess {String}  today_register_user_num 今日注册用户数
     * @apiSuccess {String}  yesterday_register_user_num 昨日注册用户数
     * @apiSuccess {String}  current_month_register_user_num 当月注册用户数
     * @apiSuccess {String}  last_month_register_user_num 上月注册用户数
     * @apiSuccess {String}  total_register_user_num 总注册用户数
     * @apiSuccess {String}  total_send_order_num 总派单数
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *             {
     *                   "distribution_manages_id": 0,
     *                   "distribution_manages_account": "zsy_finance",
     *                   "distribution_manages_nickname": "3333",
     *                   "distribution_manages_latest_login_time": "",
     *                   "distribution_manages_latest_login_ip": "",
     *                   "distribution_manages_is_enabled": 1,
     *                   "distribution_manages_remark": "",
     *                   "distribution_manages_register_ip": "",
     *                   "distribution_manages_create_time": "",
     *                   "distribution_manages_update_time": "",
     *                   "distribution_manages_number": "",
     *                   "today_register_user_num": 4,
     *                   "yesterday_register_user_num": 0,
     *                   "current_month_register_user_num": 0,
     *                   "last_month_register_user_num": 0,
     *                   "total_register_user_num": 0,
     *                   "total_send_order_num": 0
     *              }
     *          ]
     *     }
     */
    public function getBusinessDataList(Request $request)
    {

        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $isEnabled = $request->has('is_enabled') ? $request->input('is_enabled') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';

        $params = [
            'start_date' => $start_time,
            'end_date' => $end_time,
            'is_enabled' => $isEnabled,
            'search_name' => $search_name,
        ];

        $business = ManageService::getBusinessManageList($params);

        $today = date('Y-m-d');
        $todayEnd = $today . " 23:59:59";

        if ($business) {
            foreach ($business as $item) {
                $channelIds = ChannelService::getChannelIdList($item['id']);
                $business_channel_stats = BusinessChannelStatService::getBusinessChannelStatSingle($channelIds);

                $item['yesterday_register_user_num'] = $business_channel_stats->yesterday_register_user_num;
                $item['current_month_register_user_num'] = $business_channel_stats->current_month_register_user_num;
                $item['last_month_register_user_num'] = $business_channel_stats->last_month_register_user_num;
                $item['total_register_user_num'] = $business_channel_stats->total_register_user_num;
                $item['total_send_order_num'] = $business_channel_stats->total_send_order_num;

                $item['current_month_new_channels_recharge'] = $business_channel_stats->current_month_new_channels_recharge_sum;
                $item['current_month_channels_recharge'] = $business_channel_stats->current_month_channels_recharge_sum;
                $item['last_month_channels_recharge_sum'] = $business_channel_stats->last_month_channels_recharge_sum;
                $item['last_month_new_channels_recharge_sum'] = $business_channel_stats->last_month_new_channels_recharge_sum;

                $item['today_register_user_num'] = UserService::getChannelsDayToDayTotal($channelIds, $today, $todayEnd);
                $item['current_month_companies_num'] = BusinessChannelStat::getNewAddedCompaniesNum(date('Y-m-01 00:00:00', time()), date('Y-m-d H:i:s'), $channelIds);
                $item['current_month_channel_num'] = BusinessChannelStat::getNewwAddedChannelNum(date('Y-m-01 00:00:00', time()), date('Y-m-d H:i:s'), $channelIds);
                $item['last_month_companies_num'] = BusinessChannelStat::getNewAddedCompaniesNum(date('Y-m-01',strtotime('-1 month')),date('Y-m-d H:i:s',strtotime(date('Y-m-01 23:59:59').' -1 day')),$channelIds);
                $item['last_month_channel_num'] = BusinessChannelStat::getNewwAddedChannelNum(date('Y-m-01',strtotime('-1 month')),date('Y-m-d H:i:s',strtotime(date('Y-m-01 23:59:59').' -1 day')),$channelIds);
            }
        }
        return response()->pagination(new BusinessStatDataTransformer(), $business);
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获商务渠道数据统计列表
     * @api {GET} channel/getBusinessChannelList2 商务渠道数据统计列表
     * @apiGroup channel
     * @apiName channel/getBusinessChannelList2
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_enabled] 0:待审核； 1:已审核.(可不传)
     * @apiParam   {Number}  [distribution_manages_id] 管理员ID.(可不传)
     * @apiParam   {Number}  [yesterday_register_user_num_sort] 昨日注册用户数排序  0:升序，1:降序.(可不传)
     * @apiParam   {Number}  [current_month_register_user_num_sort] 当月注册用户数排序  0:升序，1:降序.(可不传)
     * @apiParam   {Number}  [last_month_register_user_num_sort] 上月注册用户数排序  0:升序，1:降序.(可不传)
     * @apiParam   {Number}  [total_register_user_num_sort] 总注册用户数排序  0:升序，1:降序.(可不传)
     * @apiParam   {Number}  [total_send_order_num_sort] 总派单数  0:升序，1:降序.(可不传)
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccess {String}  person_in_charge_name 负责人.
     * @apiSuccess {String}  create_time 注册时间
     * company_name--公司名
     * business_channel_stats_today_register_user_num  今日注册数
     * business_channel_stats_yesterday_register_user_num 昨日注册用户数
     * business_channel_stats_current_month_register_user_num 当月注册用户数
     * business_channel_stats_last_month_register_user_num 上月注册用户数
     * business_channel_stats_total_register_user_num 总注册用户数
     * business_channel_stats_total_send_order_num 总派单数
     * business_channel_stats_service_account_sum 服务号数
     * business_channel_stats_last_week_actual_send_orders_sum 上周实际派单
     * business_channel_stats_current_week_actual_send_orders_sum 本周实际派单
     * business_channel_stats_yesterday_create_orders_sum 昨日创建派单
     * business_channel_stats_last_week_login_day_sum 上周登录天数
     * business_channel_stats_current_week_login_days_sum 本周登录天数
     * business_channel_stats_is_yesterday_login 昨日是否登录
     *
     * @apiSuccess {Number}  distribution_manages_id 管理员ID
     * @apiSuccess {String}  distribution_manages_account 管理员
     * @apiSuccess {String}  distribution_manages_number 管理员
     * @apiSuccess {String}  distribution_manages_nickname 管理员昵称
     * @apiSuccess {String}  business_channel_stats_today_register_user_num 今日注册用户数
     * @apiSuccess {String}  business_channel_stats_yesterday_register_user_num 昨日注册用户数
     * @apiSuccess {String}  business_channel_stats_current_month_register_user_num 当月注册用户数
     * @apiSuccess {String}  business_channel_stats_last_month_register_user_num 上月注册用户数
     * @apiSuccess {String}  business_channel_stats_total_register_user_num 总注册用户数
     * @apiSuccess {String}  business_channel_stats_total_send_order_num 总派单数
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *             {
     *                  "id": 1,
     *                   "phone": "",
     *                   "name": "121",
     *                   "pay_merchant_id": 1,
     *                   "nickname": "是说",
     *                   "latest_login_time": "",
     *                   "latest_login_ip": "",
     *                   "is_enabled": 1,
     *                   "register_ip": "",
     *                   "remark": "",
     *                   "person_in_charge_name": "波哥帅",
     *                   "create_time": "2017-11-20 18:34:17",
     *                   "distribution_manages_id": 0,
     *                   "distribution_manages_account": "zsy_finance",
     *                   "distribution_manages_number": "",
     *                   "distribution_manages_nickname": "3333",
     *                   "business_channel_stats_today_register_user_num": 4,
     *                   "business_channel_stats_yesterday_register_user_num": 0,
     *                   "business_channel_stats_current_month_register_user_num": 0,
     *                   "business_channel_stats_last_month_register_user_num": 0,
     *                   "business_channel_stats_total_register_user_num": 0,
     *                   "business_channel_stats_total_send_order_num": 0
     *              }
     *          ]
     *     }
     */
    public function getBusinessCompanyList(Request $request)
    {
        $distribution_channel_id = $request->has('company_id') ? $request->input('company_id') : '';
        $distribution_channel_name = $request->has('company_name') ? $request->input('company_name') : '';
        $distribution_manages_name = $request->has('manager_name') ? $request->input('manager_name') : '';

        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $distribution_manages_id = $request->has('distribution_manages_id') ? $request->input('distribution_manages_id') : '';
        $isEnabled = $request->has('is_enabled') ? $request->input('is_enabled') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';

        //商务，只获取到当前商务下的渠道列表
        if ($this->getLoginUserRole() == "business") {
            $distribution_manages_id = $this->getLoginUserId();
        }

        $yesterday_register_user_num_sort = $request->has('yesterday_register_user_num_sort') ? $request->input('yesterday_register_user_num_sort') : '';
        $current_month_register_user_num_sort = $request->has('current_month_register_user_num_sort') ? $request->input('current_month_register_user_num_sort') : '';
        $last_month_register_user_num_sort = $request->has('last_month_register_user_num_sort') ? $request->input('last_month_register_user_num_sort') : '';
        $total_register_user_num_sort = $request->has('total_register_user_num_sort') ? $request->input('total_register_user_num_sort') : '';
        $total_send_order_num_sort = $request->has('total_send_order_num_sort') ? $request->input('total_send_order_num_sort') : '';

        $params = [
            'company_id' => $distribution_channel_id,
            'company_name' => $distribution_channel_name,
            'manager_name' => $distribution_manages_name,
            'start_date' => $start_time,
            'end_date' => $end_time,
            'distribution_manages_id' => $distribution_manages_id,
            'is_enabled' => $isEnabled,
            'search_name' => $search_name,
            'yesterday_register_user_num_sort' => $yesterday_register_user_num_sort,
            'current_month_register_user_num_sort' => $current_month_register_user_num_sort,
            'last_month_register_user_num_sort' => $last_month_register_user_num_sort,
            'total_register_user_num_sort' => $total_register_user_num_sort,
            'total_send_order_num_sort' => $total_send_order_num_sort,
        ];
        //\Log::info('time_check:getListNew:before--:'.time());
        $result = BusinessChannelStatService::getListNew($params);
       // \Log::info('time_check:getListNew:after--:'.time());
        $today = date('Y-m-d');
        //\Log::info('businessChannelStats:' . json_encode($result));
        if ($result) {
            foreach ($result as $item) {
                $company = DB::table('companies')->where('id', $item['company_id'])->first();
                $distribution_channel_ids = [];
                if ($company) {
                    $company_id = $company->id;
                    $company_name = $company->name;
                   // \Log::info('time_check:getCompanyServiceAccounts:before--'.time());
                    $company_service_account = CompanyService::getCompanyServiceAccounts($company_id);
                   // \Log::info('time_check:getCompanyServiceAccounts:after--'.time());
                    foreach ($company_service_account as $company_service_account_item) {
                        $distribution_channel_ids[] = $company_service_account_item->distribution_channel_id;
                    }
                    $yesterdayEnd = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 23:59:59";
                    $recharge_data = OrderStatService::getRechargeAmount($distribution_channel_ids);
                    $item['company_total_charge_amount'] = $recharge_data->total_recharge_amount_sum;// OrderStatService::getRechargeAmount($distribution_channel_ids);

                    $month_begin_time = date('Y-m-01 00:00:00');

                    $item['company_total_charge_amount_current_month'] = $recharge_data->current_month_recharge_amount_sum;//OrderDayStatService::getRechargeByChannels($distribution_channel_ids,$time_range=['begin_time' => $month_begin_time, 'end_time' => $yesterdayEnd]); //OrderService::getAmount($company_order_params_current_month);

                    $item['company_name'] = $company_name;
                    $fans_gender = $company->fans_gender;
                    switch ($fans_gender) {
                        case 1:
                            $item['fans_gender'] = '男粉';
                            break;
                        case 2:
                            $item['fans_gender'] = '女粉';
                            break;
                        case 3:
                            $item['fans_gender'] = '混合粉';
                            break;
                        default:
                            $item['fans_gender'] = '';
                    }
                } else {
                    $item['company_name'] = '';
                    $item['fans_gender'] = '';
                    //\Log::info('company error:' . $item['company_id']);
                }
                //\Log::info('time_check:UserService::getCompanyDayTotal:before:'.time());
                $item['business_channel_stats_today_register_user_num'] = UserService::getCompanyDayTotal($today, $item['company_id']);
                //\Log::info('time_check:UserService::getCompanyTotal:before:'.time());
                $item['business_channel_stats_total_register_user_num'] = UserService::getCompanyTotal($item['company_id']);
                //\Log::info('time_check:UserService::getCompanyTotal:after:'.time());
            }
        }
        return response()->pagination(new BusinessCompanyStatTransformer(), $result);
    }

    public function getCompanyLastWeekPromotion(Request $request)
    {
        $company_id = $request->input('company_id', '');
        if (!$company_id) {
            return response()->error('PARAM_EMPTY');
        }
        $time = $request->input('time', '');
        if (!$time) {
            return response()->error('PARAM_EMPTY');
        }
        if (!in_array($time, ['current', 'last'])) {
            return response()->error('PARAM_ERROR');
        }
        $books = [];
        if ($time == 'current') {
            $current_week_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
            $current_week_end = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y")));
            $books = SendOrderService::getCompanyPromotionBooks($company_id, $current_week_start, $current_week_end);
            //return response()->success($books);
        }
        if ($time == 'last') {
            $beginLastweek = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - 7, date('Y')));
            $endLastweek = date('Y-m-d H:i:s', mktime(23, 59, 59, date('m'), date('d') - date('w') + 7 - 7, date('Y')));
            $books = SendOrderService::getCompanyPromotionBooks($company_id, $beginLastweek, $endLastweek);
        }

        return response()->success($books);
    }

    public function getServiceAccount(Request $request)
    {
        $company_id = $request->input('company_id', '');
        if (empty($company_id)) {
            return response()->error('PARAM_EMPTY');
        }
        $accounts = CompanyService::getCompanyServiceAccounts($company_id);
        return response()->success($accounts);
    }
}
