<?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\BusinessChannelDataTransformer;
use App\Http\Controllers\Manage\Finance\BaseController;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Manage\Services\ManageService;
use App\Modules\OfficialAccount\Services\OfficialAccountService;
use Illuminate\Http\Request;

/**
 * 商务运营-渠道粉丝阈值模块
 * Class BusinessChannelDataController
 * @package App\Http\Controllers\Manage\Channel
 */
class BusinessChannelDataController extends BaseController
{
    /**
     * 获取商务各自的渠道信息
     * @param Request $request
     */
    function getBusinessChannelData(Request $request)
    {
        $result = [];
        $manageId = $request->session()->get('manage_auth');
        $distribution_channel_id = $request->input('distribution_channel_id');
        $distribution_channel_name = $request->input('distribution_channel_name');
        $official_account_name = $request->input('official_account_name');

        if (!empty($manageId)) {
            $manageModel = ManageService::getById($manageId);
            if ($manageModel) {
                $role = $manageModel->role;
                $channels = ($role == 'admin') ? [] : ChannelService::getByDistributionManagesId($manageId);
                if ($distribution_channel_id) {
                    $channels = [$distribution_channel_id];
                }
                \Log::info('getBusinessChannelData_official_account_name:'.$official_account_name.' $distribution_channel_name:'.$distribution_channel_name);
                $result = OfficialAccountService::getBusinessChannelData($channels, $distribution_channel_name, $official_account_name,false);
            }
        }
        return response()->pagination(new BusinessChannelDataTransformer(), $result);
    }

    /**
     * 导出商务各自的渠道信息
     * @param Request $request
     */
    function exportBusinessChannelData(Request $request)
    {
        $result = [];
        $distribution_channel_id = $request->input('distribution_channel_id');
        $distribution_channel_name = $request->input('distribution_channel_name');
        $official_account_name = $request->input('official_account_name');
        
        $manageId = $request->session()->get('manage_auth');
        if (!empty($manageId)) {
            $manageModel = ManageService::getById($manageId);
            if ($manageModel) {
                $role = $manageModel->role;
                $channels = ($role == 'admin') ? [] : ChannelService::getByDistributionManagesId($manageId);
                if ($distribution_channel_id) {
                    $channels = [$distribution_channel_id];
                }
                \Log::info('getBusinessChannelData_official_account_name:'.$official_account_name.' $distribution_channel_name:'.$distribution_channel_name);
                $result = OfficialAccountService::getBusinessChannelData($channels, $distribution_channel_name, $official_account_name,false);
            }

            header("Content-type:application/vnd.ms-excel");
            header("Content-Disposition:attachment;filename=" . "明细" . date("YmdHis") . ".csv");
            echo iconv("UTF-8", "GBK", "\"渠道ID\",\"渠道名称\",\"服务号名称\",\"日关注粉丝阈值\",\"总关注粉丝阈值\",\"当日实时关注粉丝数\",\"当日实时累计总粉丝数\"\r\n");
            foreach ($result as $item) {
                echo("\"" . mb_convert_encoding($item->distribution_channel_id, "GBK", "UTF-8") . "\",");
                echo("\"" . mb_convert_encoding($item->distribution_channel_name, "GBK", "UTF-8") . "\",");
                echo("\"" . mb_convert_encoding($item->service_name, "GBK", "UTF-8") . "\",");
                echo("\"" . mb_convert_encoding($item->subscribe_day_maximum, "GBK", "UTF-8") . "\",");
                echo("\"" . mb_convert_encoding($item->subscribe_top_num, "GBK", "UTF-8") . "\",");
                echo("\"" . mb_convert_encoding($item->day_fans_num, "GBK", "UTF-8") . "\",");
                echo("\"" . $item->day_total_fans_num . "\"\r\n");
            }
            exit();
        }
    }
}