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

namespace App\Modules\Trade\Services;
use App\Modules\Trade\Models\OrderStat;
use App\Modules\Trade\Services\OrderDayStatService;
use App\Modules\OfficialAccount\Services\OfficialAccountService;

class OrderStatService
{
    /**
     * 获取单个渠道数据
     * @param  $distribution_channel_id 分销渠道ID
     * @return object          
     */
    static function getChannelStat($distribution_channel_id)
    {
        return OrderStat::getByChannelId($distribution_channel_id);  
    }

    /**
     * 更新渠道统计数据
     * @param  $distribution_channel_id 分销渠道ID
     * @return object          
     */
    static function add($distribution_channel_id)
    {
    	$sum_data = OrderDayStatService::getSumByChannelId($distribution_channel_id);
    	$yesterday_sum_data = OrderDayStatService::getYesterdaySumByChannelId($distribution_channel_id);
    	$current_month_sum_data = OrderDayStatService::getCurrentMonthSumByChannelId($distribution_channel_id);
    	$last_month_sum_data = OrderDayStatService::getLastMonthSumByChannelId($distribution_channel_id);

    	$data = [];
    	$data['pay_success_user_num'] = (float)$sum_data->pay_success_user_num;
    	$data['unpaid_num'] = (float)$sum_data->unpaid_num;
    	$data['paid_num'] = (float)$sum_data->paid_num;
        $data['total_recharge_amount'] = (float)$sum_data->total_recharge_amount;
        $data['send_order_num'] = (float)$sum_data->send_order_num;
        $data['real_send_order_num'] = (float)$sum_data->real_send_order_num;
        $data['register_user_num'] = (float)$sum_data->register_user_num;
        $data['ticket_recharge_pay_user_num'] = (float)$sum_data->ticket_recharge_user_num;
        $data['year_pay_user_num'] = (float)$sum_data->year_recharge_user_num;
        $data['promotion_total_uv'] = (float)$sum_data->promotion_total_uv;
        $data['promotion_total_pv'] = (float)$sum_data->promotion_total_pv;
        $data['force_user_num'] = (float)$sum_data->force_user_num;

        //昨日
        $data['yesterday_paid_num'] = (float)$yesterday_sum_data->paid_num;
        $data['yesterday_unpaid_num'] = (float)$yesterday_sum_data->unpaid_num;
        $data['yesterday_recharge_amount'] = (float)$yesterday_sum_data->total_recharge_amount;
        $data['yesterday_ticket_recharge_paid_num'] = (float)$yesterday_sum_data->ticket_recharge_paid_num;
        $data['yesterday_ticket_recharge_unpaid_num'] = (float)$yesterday_sum_data->ticket_recharge_unpaid_num;
        $data['yesterday_ticket_recharge_amount'] = (float)$yesterday_sum_data->ticket_recharge_amount;
        $data['yesterday_year_recharge_paid_num'] = (float)$yesterday_sum_data->year_recharge_paid_num;
        $data['yesterday_year_recharge_unpaid_num'] = (float)$yesterday_sum_data->year_recharge_unpaid_num;
        $data['yesterday_year_recharge_amount'] = (float)$yesterday_sum_data->year_recharge_amount;

        //当月
    	$data['current_month_paid_num'] = (float)$current_month_sum_data->paid_num;
    	$data['current_month_unpaid_num'] = (float)$current_month_sum_data->unpaid_num;
        $data['current_month_recharge_amount'] = (float)$current_month_sum_data->total_recharge_amount;
        $data['current_month_ticket_recharge_paid_num'] = (float)$current_month_sum_data->ticket_recharge_paid_num;
        $data['current_month_ticket_recharge_unpaid_num'] = (float)$current_month_sum_data->ticket_recharge_unpaid_num;
        $data['current_month_ticket_recharge_amount'] = (float)$current_month_sum_data->ticket_recharge_amount;
        $data['current_month_year_recharge_paid_num'] = (float)$current_month_sum_data->year_recharge_paid_num;
        $data['current_month_year_recharge_unpaid_num'] = (float)$current_month_sum_data->year_recharge_unpaid_num;
        $data['current_month_year_recharge_amount'] = (float)$current_month_sum_data->year_recharge_amount;

        //上月
    	$data['last_month_paid_num'] = (float)$last_month_sum_data->paid_num;
    	$data['last_month_unpaid_num'] = (float)$last_month_sum_data->unpaid_num;
    	$data['last_month_recharge_amount'] = (float)$last_month_sum_data->total_recharge_amount;
        $data['last_month_ticket_recharge_paid_num'] = (float)$last_month_sum_data->ticket_recharge_paid_num;
        $data['last_month_ticket_recharge_unpaid_num'] = (float)$last_month_sum_data->ticket_recharge_unpaid_num;
        $data['last_month_ticket_recharge_amount'] = (float)$last_month_sum_data->ticket_recharge_amount;
        $data['last_month_year_recharge_paid_num'] = (float)$last_month_sum_data->year_recharge_paid_num;
        $data['last_month_year_recharge_unpaid_num'] = (float)$last_month_sum_data->year_recharge_unpaid_num;
        $data['last_month_year_recharge_amount'] = (float)$last_month_sum_data->year_recharge_amount;

        //获取公众号数量
        $official_accounts = OfficialAccountService::officialAuthAccountBydistributionChannelId(compact('distribution_channel_id'));
        $official_account_names = [];
        foreach ($official_accounts as $official_account)
        {
            array_push($official_account_names,$official_account->nickname);
        }
        $data['official_account_num'] = count($official_accounts);
        $data['official_account_names'] = implode(',', $official_account_names);

        return OrderStat::add($distribution_channel_id,$data);  
    }

    static function getList($distribution_channel_id,$phone, $is_all=false)
    {
        return OrderStat::getList($distribution_channel_id,$phone,$is_all);
    }

    static function getChannelTotalStat($distribution_channel_ids)
    {
        return OrderStat::getChannelTotalStat($distribution_channel_ids);
    }

    static function getRechargeAmount($distribution_channel_ids)
    {
        return OrderStat::getRechargeAmount($distribution_channel_ids);
    }

}