<?php

namespace App\Http\Controllers\Channel\OfficialAccount;

use App\Http\Controllers\Channel\BaseController as ChannelBaseController;

use App\Http\Controllers\Channel\OfficialAccount\Transformers\WechatKeyWordsMsgTransformer;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use App\Modules\OfficialAccount\Services\WechatKeywordMsgService;
use App\Modules\Statistic\Services\SendStatistics;

class WechatKeywordsController extends ChannelBaseController
{



    /**
     * @apiDefine OfficialAccount 公众号
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/wechatKeywordMsgById 通过id获取关键字信息
     * @apiGroup OfficialAccount
     * @apiName wechatKeywordMsgById
     * @apiParam   {String}  id 关键字信息id.
     */

    function wechatKeywordMsgById(Request $request)
    {

        $id = $request->has('id') ? $request->input('id') : '';

        if(empty($id)) {
            return response()->error("PARAM_EMPTY");
        }

        $wechatKeywordMsg['id'] = $id;

        $wechatKeywordMsgService = WechatKeywordMsgService::wechatKeywordMsgById($wechatKeywordMsg);
        $wechatKeywordMsgService = self::get_keyword_statistics($wechatKeywordMsgService);

        return response()->item(new WechatKeyWordsMsgTransformer(), $wechatKeywordMsgService);
    }

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/wechatKeywordMsgBydistributionChannelId 获取推送关键字信息列表
     * @apiGroup OfficialAccount
     * @apiName wechatKeywordMsgBydistributionChannelId
     * @apiParam   {String}  distribution_channel_id 渠道id.
     */

    function wechatKeywordMsgBydistributionChannelId(Request $request)
    {

        $distribution_channel_id = $this->getChannelId();

        $wechatKeywordsMsg['distribution_channel_id'] = $distribution_channel_id;

        $wechatKeywordsMsgService = WechatKeywordMsgService::wechatKeywordMsgBydistributionChannelId($wechatKeywordsMsg);

        foreach($wechatKeywordsMsgService as $key=>$wechatKeywordsMsg){
        	$wechatKeywordsMsgService[$key] = self::get_keyword_statistics($wechatKeywordsMsgService[$key]);
        }
        return response()->pagination(new WechatKeyWordsMsgTransformer(), $wechatKeywordsMsgService);
    }



    /**
     * @apiVersion 1.0.0
     * @api {POST} OfficialAccount/addWechatKeywordMsg 保存关键字信息
     * @apiGroup OfficialAccount
     * @apiName addWechatKeywordMsg
     * @apiParam   {String}  appids 公众号列表，英文逗号隔开.
     * @apiParam   {String}  keywords 关键字列表，英文逗号隔开.
     * @apiParam   {String}  bid 书籍id.
     * @apiParam   {String}  cid 章节id.
     */

    function addWechatKeywordMsg(Request $request)
    {
    	\Log::info('addWechatKeywordMsg_start');
    	\Log::info($request->all());
        
        $appids = $request->has('appids') ? $request->input('appids') : '';
        if(empty($appids)) {
            return response()->error("PARAM_EMPTY");
        }

        $keywords = $request->has('keywords') ? $request->input('keywords') : '';
        if(empty($keywords)) {
            return response()->error("PARAM_EMPTY");
        }


        $bid = $request->has('bid') ? $request->input('bid') : '';
        if(empty($bid)) {
            return response()->error("PARAM_EMPTY");
        }

        $cid = $request->has('cid') ? $request->input('cid') : '';
        if(empty($cid)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $book_name = $request->has('book_name') ? $request->input('book_name') : '';
        if(empty($book_name)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $chapter_name = $request->has('chapter_name') ? $request->input('chapter_name') : '';
        if(empty($chapter_name)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $send_title = $request->has('send_title') ? $request->input('send_title') : '';
        if(empty($send_title)) {
        	return response()->error("PARAM_EMPTY");
        }
        $send_cover = $request->has('send_cover') ? $request->input('send_cover') : '';
        if(empty($send_cover)) {
        	return response()->error("PARAM_EMPTY");
        }
        $send_order_id = $request->has('send_order_id') ? $request->input('send_order_id') : '';
        
        $distribution_channel_id = $this->getChannelId();

        $wechatKeywordMsgs['appids'] = $appids;
        $wechatKeywordMsgs['bid'] = $bid;
        $wechatKeywordMsgs['cid'] = $cid;
        $wechatKeywordMsgs['book_name'] = $book_name;
        $wechatKeywordMsgs['chapter_name'] = $chapter_name;
        $wechatKeywordMsgs['send_title'] = $send_title;
        $wechatKeywordMsgs['send_cover'] = $send_cover;
        $wechatKeywordMsgs['distribution_channel_id'] = $distribution_channel_id;
        $wechatKeywordMsgs['send_order_id'] = $send_order_id;
        $wechatKeywordMsgs['status'] = 1;
        
        $keywords = explode(',',$keywords);
        \Log::info($wechatKeywordMsgs);
        \Log::info($keywords);
        foreach($keywords as $keyword){
        	$wechatKeywordMsgs['keyword'] = $keyword;
        	$resultStatus = WechatKeywordMsgService::addWechatKeywordMsg($wechatKeywordMsgs);
        }

        if ($resultStatus == 1) {
            return response()->success();
        }elseif ($resultStatus == 2) {
            return response()->error('KEYWORDS_UNSUCCESSFUL');
        }elseif ($resultStatus == 3) {
            return response()->error('KEYWORDS_UNSUCCESSFUL');
        }
        elseif ($resultStatus == 0) {
            return response()->error('KEYWORDS_UNSUCCESSFUL');
        }else{
            return response()->error('KEYWORDS_UNSUCCESSFUL');
        }

    }

    /**
     * @apiVersion 1.0.0
     * @api {POST} OfficialAccount/updateWechatKeywordMsg 更新关键字信息
     * @apiGroup OfficialAccount
     * @apiName updateWechatKeywordMsg
     * @apiParam   {String}  id 关键字id.
     * @apiParam   {String}  appids 公众号列表，英文逗号隔开.
     * @apiParam   {String}  keywords 关键字列表，英文逗号隔开.
     * @apiParam   {String}  bid 书籍id. 
     * @apiParam   {String}  cid 章节id.
     */
    
    function updateWechatKeywordMsg(Request $request)
    {
    	\Log::info('updateWechatKeywordMsg_start');
    	$id = $request->has('id') ? $request->input('id') : '';
    	if(empty($id)) {
    		return response()->error("PARAM_EMPTY");
    	}
    	
    	$appids = $request->has('appids') ? $request->input('appids') : '';
        if(empty($appids)) {
            return response()->error("PARAM_EMPTY");
        }

        $keywords = $request->has('keywords') ? $request->input('keywords') : '';
        if(empty($keywords)) {
            return response()->error("PARAM_EMPTY");
        }

        $bid = $request->has('bid') ? $request->input('bid') : '';
        if(empty($bid)) {
            return response()->error("PARAM_EMPTY");
        }

        $cid = $request->has('cid') ? $request->input('cid') : '';
        if(empty($cid)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $book_name = $request->has('book_name') ? $request->input('book_name') : '';
        if(empty($book_name)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $chapter_name = $request->has('chapter_name') ? $request->input('chapter_name') : '';
        if(empty($chapter_name)) {
        	return response()->error("PARAM_EMPTY");
        }
        $send_title = $request->has('send_title') ? $request->input('send_title') : '';
        if(empty($send_title)) {
        	return response()->error("PARAM_EMPTY");
        }
        $send_cover = $request->has('send_cover') ? $request->input('send_cover') : '';
        if(empty($send_cover)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $distribution_channel_id = $this->getChannelId();
        $wechatKeywordMsgs['id'] = $id;
        $wechatKeywordMsgs['appids'] = $appids;
        $wechatKeywordMsgs['bid'] = $bid;
        $wechatKeywordMsgs['cid'] = $cid;
        $wechatKeywordMsgs['send_title'] = $send_title;
        $wechatKeywordMsgs['send_cover'] = $send_cover;
        $wechatKeywordMsgs['book_name'] = $book_name;
        $wechatKeywordMsgs['chapter_name'] = $chapter_name;
        $wechatKeywordMsgs['distribution_channel_id'] = $distribution_channel_id;
        $wechatKeywordMsgs['status'] = 1;
        
        $keywords = explode(',',$keywords);
        \Log::info($wechatKeywordMsgs);
        \Log::info($keywords);
        foreach($keywords as $keyword){
        	$wechatKeywordMsgs['keyword'] = $keyword;
        	$resultStatus = WechatKeywordMsgService::updateWechatKeywordMsg($wechatKeywordMsgs);
        }
    
    	if ($resultStatus == 1) {
    		return response()->success();
    	}elseif ($resultStatus == 2) {
    		return response()->error('KEYWORDS_UNSUCCESSFUL');
    	}elseif ($resultStatus == 3) {
    		return response()->error('KEYWORDS_UNSUCCESSFUL');
    	}
    	elseif ($resultStatus == 0) {
    		return response()->error('KEYWORDS_UNSUCCESSFUL');
    	}else{
    		return response()->error('KEYWORDS_UNSUCCESSFUL');
    	}
    	
    	\Log::info('updateWechatKeywordMsg_end');
    
    }
    

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/updateWechatKeywordMsgStatus 更新关键字信息状态
     * @apiGroup OfficialAccount
     * @apiName updateWechatKeywordMsgStatus
     * @apiParam   {String}  id 关键字信息id.
     * @apiParam   {String}  status 关键字信息状态 0关闭  1开启.
     * @apiSuccessExample {json} Success-Response:
     *
     *       {
     *           "code": 0,
     *           "msg": "",
     *           "data": {
     *           }
     *       }
     */

    function updateWechatKeywordMsgStatus(Request $request)
    {

        $id = $request->has('id') ? $request->input('id') : '';
        if(empty($id)) {
            return response()->error("PARAM_EMPTY");
        }
        
        $status = $request->has('status') ? $request->input('status') : '0';
        $distribution_channel_id = $this->getChannelId();
        
        $wechatKeywordMsg['id'] = $id;
        $wechatKeywordMsg['status'] = $status;
        $wechatKeywordMsg['distribution_channel_id'] = $distribution_channel_id;
        $resultStatus = WechatKeywordMsgService::updateWechatKeywordMsgStatus($wechatKeywordMsg);

        if ($resultStatus == 1) {
            return response()->success();
        }elseif ($resultStatus == 2) { 
            return response()->error('KEYWORDS_UNSUCCESSFUL');
        }elseif ($resultStatus == 4) {
            return response()->error('KEYWORDS_UNSUCCESSFUL');
        }elseif ($resultStatus == 0) {
            return response()->error('KEYWORDS_UNSUCCESSFUL');
        }else{
            return response()->error('KEYWORDS_UNSUCCESSFUL');
        }
        
    }
    
    public static function get_keyword_statistics($wechatKeywordMsgService){
    	$start_date = date('Y-m-d',strtotime('-7 day'));
    	$end_date = date('Y-m-d',strtotime('+1 day'));
    	$stat_type = 'keyword_'.$wechatKeywordMsgService->id;
    	\Log::info('get_keyword_statistics:'.$wechatKeywordMsgService->distribution_channel_id.' start_date:'.$start_date.' end_date:'.$end_date.' stat_type:'.$stat_type);
    	$user_pay_stat = SendStatistics::getWechatMsgUseRecord($wechatKeywordMsgService->distribution_channel_id,$stat_type,$start_date,$end_date,true);
    	\Log::info('get_keyword_statistics_pay_stat');\Log::info($user_pay_stat);
    	$wechatKeywordMsgService->uv = $user_pay_stat['uv'];
    	$wechatKeywordMsgService->click_num = $user_pay_stat['pv'];
    	$wechatKeywordMsgService->new_subscribe_num = '0';
    	$wechatKeywordMsgService->charge_amount = $user_pay_stat['success_order_sum'];
    	return $wechatKeywordMsgService;
    }


}