<?php
class emailAction extends baseAction
{
	/**
	  * @method 发送邮件
	  * @url    email/send?token=xxx
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  ema_type           enum   [必填] 发送速度：'普通','紧急','延时'
	  * @param  ema_from           enum   [必填] 来源：'B2C','主站','客户网','CRM'
	  * @param  ema_from_email     string [可选] 发送人邮箱
	  * @param  ema_from_name      string [可选] 发送人
	  * @param  ema_to_email       string [必填] 接收人手机号码，多个逗号分隔
	  * @param  ema_title          string [必填] 标题
	  * @param  ema_text           string [必填] 内容
	  * @param  ema_style          string [可选] 信息类型 例如：邮箱验证，取回密码....
	  * @param  ema_expire_time    int    [可选] 过期时间戳，超过此时间即使失败也不再发送
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":false,"data":'失败原因',"code":0}
	  */
	function send()
	{
		$DataArr = [];
		foreach($_POST as $k=>$v)
		{
			if(stripos($k, 'ema_') === 0)
				$DataArr[$k] = $v;
		}
		$SendEmail = new SendEmail();
		$Res = $SendEmail->send($DataArr);
		echo json_encode($Res);
	}
	
	/**
	  * @method 获取对应ID的信息
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  ema_ids            string   [必填] 邮件ID,使用逗号（,）分隔
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":false,"data":'失败原因',"code":0}
	  */
	function get()
	{
		$Params = mvc::$URL_PARAMS;
		if(empty($Params['ema_ids']))
		{
			LibFc::ReturnJson(false, '邮件ID必填');
			exit;
		}
		$SmsIdArr = [];
		foreach(explode(',', $Params['ema_ids']) as $V)
		{
			$SmsIdArr[] = (int) $V;
		}
		$SendBase = new SendBase();
		$Res = $SendBase->Get('sc_email', ['*'], sprintf(" AND ema_id IN (%s)", implode(',', $SmsIdArr)));
		$Arr = [];
		foreach($Res['data'] as $V)
		{
			$Arr[$V['ema_id']] = $V;
		}
		LibFc::ReturnJson(true, $Arr);
	}


	/**
	  * @method 发送指定ID的邮件
	  * @http  get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  ema_ids            array   [必填] 邮件ID
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":false,"data":'失败原因',"code":0}
	  */
	function send_by_ids()
	{
		$Params = mvc::$URL_PARAMS;
		if(empty($Params['ema_ids']))
		{
			LibFc::ReturnJson(false, '邮件ID必填');
			exit;
		}
		$SendEmail = new SendEmail();
		$Res = $SendEmail->SendByIds(explode(',', $Params['ema_ids']));
		echo json_encode($Res);
	}


	/**
	  * @method 删除对应ID的邮件记录
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  ema_ids            string   [必填] 邮件ID,使用逗号（,）分隔
	  * @author soul
	  * @copyright 2017/4/13
	  * @return {"status":true|false,"data":'',"code":0}
	  */
	function del()
	{
		$Params = mvc::$URL_PARAMS;
		if(empty($Params['ema_ids']))
		{
			LibFc::ReturnJson(false, '邮件ID必填');
			exit;
		}
		$SmsIdArr = [];
		foreach(explode(',', $Params['ema_ids']) as $V)
		{
			$SmsIdArr[] = (int) $V;
		}
		
		$SendBase = new SendBase();
		$SendBase->del('sc_email', sprintf(" AND ema_id IN (%s)", implode(',', $SmsIdArr)));
		LibFc::ReturnJson(true);
	}
}