<?php
namespace App\Modules\OfficialAccount\Services;

use App\Modules\Book\Services\BookConfigService;

use App\Modules\OfficialAccount\Models\BatchCustomSendMsgs;


class BatchCustomMsgService
{

    /**
    * 通过id获取关键字
    */
    static function batchCustomMsgById($prams)
    {
        return BatchCustomSendMsgs::batchCustomMsgById($prams['id']);
    }
    
    /**
     * 更新状态
     */
    static function updateBatchCustomSendMsgStatusById($prams)
    {
    	return BatchCustomSendMsgs::updateBatchCustomSendMsgStatusById($prams['id'],$prams['status'],$prams['del_flag']);
    }
    
    /**
     * 获取批次客服消息列表
     */
    static function batchCustomMsgList()
    {
    	return BatchCustomSendMsgs::batchCustomMsgList();
    }
    
    //判断1小时内是否有相同的客服消息插入过
    static function isSendCustomerAtSameTime($customerPrams){
    
    	return BatchCustomSendMsgs::isSendCustomerAtSameTime($customerPrams['distribution_channel_id'],$customerPrams['appid'],$customerPrams['send_time']);
    }
    
    //判断1小时内是否有相同的客服消息插入过
    static function isSendCustomerAtSameTimeSex($customerPrams){
    
    	return BatchCustomSendMsgs::isSendCustomerAtSameTimeSex($customerPrams['send_time'],$customerPrams['sex']);
    }
    
    
    /**
     * 发送客服消息
     */
    static function addBatchCustomSendMsgs($customSendMsgsPrams,&$batch_no)
    {
    	\Log::info('addBatchCustomSendMsgs_start');
    
    	try {
    		//先判断在发送时间段1小时以内，有没有发起过相同的模板消息，如果有发送过，就提示用户已经创建过相同模板消息，不创建新的模板消息
    		$isSendCustomer = BatchCustomSendMsgs::isSendCustomerAtSameTimeSex($customSendMsgsPrams['send_time'],$customSendMsgsPrams['sex']);
    		if (!empty($isSendCustomer)) {
    			\Log::info('addBatchCustomSendMsgs already_send'.json_encode($customSendMsgsPrams));
    			return 4;
    		}
    		
    		$batchCustomSendMsgsPrams = array();
    		$batchCustomSendMsgsPrams['name'] = $customSendMsgsPrams['name'];
    		$batchCustomSendMsgsPrams['send_time'] = $customSendMsgsPrams['send_time'];
    		$batchCustomSendMsgsPrams['content'] = $customSendMsgsPrams['content'];
    		$batchCustomSendMsgsPrams['redirect_url'] = $customSendMsgsPrams['redirect_url'];
    		$batchCustomSendMsgsPrams['sex'] = $customSendMsgsPrams['sex'];
    		$batchCustomSendMsgsPrams['subscribe_time'] = $customSendMsgsPrams['subscribe_time'];
    		$batchCustomSendMsgsPrams['balance'] = $customSendMsgsPrams['balance'];
    		$batchCustomSendMsgsPrams['order_type'] = $customSendMsgsPrams['order_type'];
    		$batchCustomSendMsgsPrams['category_id'] = $customSendMsgsPrams['category_id'];
    		$batchCustomSendMsgsPrams['status'] = $customSendMsgsPrams['status'];
    		$batchCustomSendMsgsPrams['del_flag'] = 0;
    		$batch_create_res = BatchCustomSendMsgs::create($customSendMsgsPrams);
    		$batch_no = isset($batch_create_res->id)?$batch_create_res->id:'';
    		\Log::info('$batch_create_res:'.$batch_no);

    		return 1;
    
    	} catch (\Exception $e) {
    		\Log::info('addBatchCustomSendMsgs_ept:'.$e->getMessage());
    		return 0;
    	}
    
    }
    
    /**
     * 获取批次客服消息列表
     */
    static function autoSendTrusteeShipCustomerMsg($sex)
    {
    	\Log::info('autoSendTrusteeShipCustomerMsg:'.$sex);
    	
    	// 每天晚上19点
    	$customMsgPrams['send_time'] = date('Y-m-d 19:00:00');
    	$customMsgPrams['name'] = '自动发送托管客服消息：'.$customMsgPrams['send_time'];
    	
    	$books = [];
    	// 男生
    	if($sex == 'a'){
    		$books = BookConfigService::getTrusteeShipChannelBook('','男频', 4);
    	}
    	// 女生
    	else{
    		$books = BookConfigService::getTrusteeShipChannelBook('','女频', 4);
    	}
    	$books = CustomMsgService::get_book_full_infos($books,'');
    	
    	// 随机推送内容
    	$customMsgPrams['content'] = json_encode($books);
    	$customMsgPrams['redirect_url'] = '';
    	$customMsgPrams['sex'] = $sex;
    	$customMsgPrams['subscribe_time'] = 'g';// 关注1天前，有过交互的
    	$customMsgPrams['trusteeship'] = '1'; // 托管
    	$customMsgPrams['balance'] = 'z';
    	$customMsgPrams['order_type'] = 'z';
    	$customMsgPrams['category_id'] = 'z';
    	$customMsgPrams['status'] = '1';
    	$customMsgPrams['is_show_list'] = '0';// 分销界面不展示
    	\Log::info($customMsgPrams);
    	$batch_no = '';
    	
    	$batch_res = BatchCustomMsgService::addBatchCustomSendMsgs($customMsgPrams,$batch_no);
    	\Log::info('batch_res:'.$batch_res.' batch_no:'.$batch_no);
    	if($batch_res == 1){
    		$customMsgPrams['batch_no'] = $batch_no;
    		$official_accounts =  OfficialAccountService::getTrusteeshipOfficialAccounts();
    		\Log::info('getTrusteeshipOfficialAccounts');
    		\Log::info($official_accounts);
//     		return true;
    		foreach($official_accounts as $official_account){
    			$customMsgPrams['appid'] = $official_account->appid;
    			$customMsgPrams['distribution_channel_id'] = $official_account->distribution_channel_id;
    			\Log::info('getTrusteeshipOfficialAccounts:appid:'.$official_account->appid.' distribution_channel_id:'.$official_account->distribution_channel_id);
    			 
    			//先判断在发送时间段1小时以内，有没有发起过相同的模板消息，如果有发送过，就提示用户已经创建过相同模板消息，不创建新的模板消息
    			$isSendCustomer = CustomMsgService::isSendCustomerAtSameTimeAndSex($customMsgPrams);
    			if (!empty($isSendCustomer)) {
    				\Log::info('isSendCustomerAtSameTime already_send:appid:'.$customMsgPrams['appid'].' distribution_channel_id:'.$customMsgPrams['distribution_channel_id']);
    				continue;
    			}
    			$customMsgService = CustomMsgService::addCustomSendMsgs($customMsgPrams);
    		}
    	}
    }

}