<?php

namespace App\Http\Controllers\Channel\OfficialAccount;

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

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

/**
 * 智能推送
 * @author zhoulingjie
 *
 */
class SmartPushsController extends ChannelBaseController
{

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

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/smartPushMsgById 通过id获取自定义智能推荐内容
     * @apiGroup OfficialAccount
     * @apiName smartPushMsgById
     * @apiParam   {String}  id 关键字信息id.
     */

    function smartPushMsgById(Request $request)
    {

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

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

        $smartPushMsg['id'] = $id;

        $smartPushMsgService = SmartPushMsgService::smartPushMsgById($smartPushMsg);

        return response()->item(new SmartPushMsgTransformer(), $smartPushMsgService);
    }

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/smartPushByDistributionChannelIdAndCategorySex 获取自定义智能推送内容
     * @apiGroup OfficialAccount
     * @apiName smartPushByDistributionChannelIdAndCategorySex
     * @apiParam   {String}  category_type 类别.
     * @apiParam   {String}  sex 性别.
     */

    function smartPushByDistributionChannelIdAndCategorySex(Request $request)
    {

        $distribution_channel_id = $this->getChannelId();
        $category_type = $request->has('category_type') ? $request->input('category_type') : '';
        if(empty($category_type)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $sex = $request->has('sex') ? $request->input('sex') : '';
        if(empty($sex)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $smartPushsMsg = array();
        $smartPushsMsg['distribution_channel_id'] = $distribution_channel_id;
        $smartPushsMsg['category_type'] = $category_type;
        $smartPushsMsg['sex'] = $sex;

        $smartPushsMsgService = SmartPushMsgService::smartPushByDistributionChannelIdAndCategorySex($smartPushsMsg);
        if($smartPushsMsgService->description){
        	$smartPushsMsgService->description = json_decode($smartPushsMsgService->description);
        }
        return response()->item(new SmartPushMsgTransformer(), $smartPushsMsgService);
    }
    
    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/checkSexAllEdit 检测是否男女都编辑过了
     * @apiGroup OfficialAccount
     * @apiName checkSexAllEdit
     * @apiParam   {String}  category_type 类别.
     */
    
    function checkSexAllEdit(Request $request)
    {
    
    	$distribution_channel_id = $this->getChannelId();
    	$category_type = $request->has('category_type') ? $request->input('category_type') : '';
    	if(empty($category_type)) {
    		return response()->error("PARAM_EMPTY");
    	}
    
    	$smartPushsMsg = array();
    	$smartPushsMsg['distribution_channel_id'] = $distribution_channel_id;
    	$smartPushsMsg['category_type'] = $category_type;
    
    	$resultStatus = SmartPushMsgService::checkSexAllEdit($smartPushsMsg);
    	if ($resultStatus == 1) {
    		return response()->success();
    	}else {
    		return response()->error('SMART_PUSHS_NOT_ALL_EDIT');
    	}
    }



    /**
     * @apiVersion 1.0.0
     * @api {POST} OfficialAccount/addSmartPushMsg 添加自定义智能推送
     * @apiGroup OfficialAccount
     * @apiName addSmartPushMsg
     * @apiParam   {String}  name 标题.
     * @apiParam   {String}  category_type 分类.
     * @apiParam   {String}  content 内容.
     * @apiParam   {String}  sex 性别 男:a  女:b.
     */

    function addSmartPushMsg(Request $request)
    {
    	\Log::info('addSmartPushMsg_start');
    	\Log::info($request->all());

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

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

        $content = $request->has('content') ? $request->input('content') : '';
        if(empty($content)) {
        	return response()->error("PARAM_EMPTY");
        }
        
        $sex = $request->has('sex') ? $request->input('sex') : '';
        if(empty($sex)) {
        	return response()->error("PARAM_EMPTY");
        }
        $description = $request->has('description') ? $request->input('description') : '';
        $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");
        }
        
        $distribution_channel_id = $this->getChannelId();

        $smartPushMsgs['name'] = $name;
        $smartPushMsgs['category_type'] = $category_type;
        $smartPushMsgs['content'] = json_encode($content);
        $smartPushMsgs['description'] = json_encode($description);
        $smartPushMsgs['book_name'] = $book_name;
        $smartPushMsgs['chapter_name'] = $chapter_name;
        $smartPushMsgs['sex'] = $sex;
        $smartPushMsgs['distribution_channel_id'] = $distribution_channel_id;
        $smartPushMsgs['status'] = 1;
        
        $resultStatus = SmartPushMsgService::addSmartPushMsg($smartPushMsgs);

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

    }
    
    /**
     * @apiVersion 1.0.0
     * @api {POST} OfficialAccount/updateSmartPushMsg 更新自定义智能推送
     * @apiGroup OfficialAccount
     * @apiName updateSmartPushMsg
     * @apiParam   {String}  id 主键.
     * @apiParam   {String}  name 标题.
     * @apiParam   {String}  category_type 分类.
     * @apiParam   {String}  content 内容.
     * @apiParam   {String}  sex 性别 男:a  女:b.
     */
    
    function updateSmartPushMsg(Request $request)
    {
    	\Log::info('updateSmartPushMsg_start');
    	\Log::info($request->all());
    
    	$id = $request->has('id') ? $request->input('id') : '';
    	if(empty($id)) {
    		return response()->error("PARAM_EMPTY");
    	}
    	
    	$name = $request->has('name') ? $request->input('name') : '';
    	if(empty($name)) {
    		return response()->error("PARAM_EMPTY");
    	}
    
    	$category_type = $request->has('category_type') ? $request->input('category_type') : '';
    	if(empty($category_type)) {
    		return response()->error("PARAM_EMPTY");
    	}
    
    	$content = $request->has('content') ? $request->input('content') : '';
    	if(empty($content)) {
    		return response()->error("PARAM_EMPTY");
    	}
    
    	$sex = $request->has('sex') ? $request->input('sex') : '';
    	if(empty($sex)) {
    		return response()->error("PARAM_EMPTY");
    	}
    	$description = $request->has('description') ? $request->input('description') : '';
    	$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");
    	}
    	
    	$distribution_channel_id = $this->getChannelId();
    
    	$smartPushMsgs['id'] = $id;
    	$smartPushMsgs['name'] = $name;
    	$smartPushMsgs['category_type'] = $category_type;
    	$smartPushMsgs['content'] = json_encode($content);
    	$smartPushMsgs['description'] = json_encode($description);
    	$smartPushMsgs['book_name'] = $book_name;
    	$smartPushMsgs['chapter_name'] = $chapter_name;
    	$smartPushMsgs['sex'] = $sex;
    	$smartPushMsgs['distribution_channel_id'] = $distribution_channel_id;
    	$smartPushMsgs['status'] = 1;
    
    	$resultStatus = SmartPushMsgService::updateSmartPushMsg($smartPushMsgs);
    
    	if ($resultStatus == 1) {
    		return response()->success();
    	}elseif ($resultStatus == 2) {
    		return response()->error('SMART_PUSHS_UNSUCCESSFUL');
    	}elseif ($resultStatus == 3) {
    		return response()->error('SMART_PUSHS_UNSUCCESSFUL');
    	}
    	elseif ($resultStatus == 0) {
    		return response()->error('SMART_PUSHS_UNSUCCESSFUL');
    	}else{
    		return response()->error('SMART_PUSHS_UNSUCCESSFUL');
    	}
    
    }

}