package cn.timer.api.utils.aliyun;

import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import cn.timer.api.bean.qyzx.QyzxEmpLogin;
import cn.timer.api.bean.qyzx.businessService.QyzxRemainingQuantity;
import cn.timer.api.config.enums.PinType;
import cn.timer.api.dto.qyzx.EntRegisterDto;

/**
 * aliyun阿里云短信服务
 * 
 * @author dsc
 *
 */
public class AliyunSMS {

	final static String CODE_NAME = "TemplateCode";// 短信模板代码参数名
	final static String REGION_ID = "cn-shenzhen";// 地区参数
	final static String PROJECT_NAME = "8小时人事管家";// 项目名
	final static String ACCESSKEY_ID = "LTAI4FuaShJWQ1dggsFWG5CC";
	final static String SECRET = "EJ6qToT4T4u0B5Rb6qrta9WkyGHvGR";
	final static String TEMPLATE_PARAM = "TemplateParam";// 自定义参数
	final static String DOMAIN = "dysmsapi.aliyuncs.com";
	final static String VERSION = "2017-05-25";
	final static String ACTION_SEND = "SendSms"; // 发短信
	final static String ACTION_QUERY = "QuerySendDetails"; // 短信查询
	final static String PAGE_SIZE = "1";
	final static String CURRENT_PAGE = "1";

	/**
	 * 
	 * @param phone
	 * @param templateCode 1."身份验证验证码" 2."登录确认验证码" 3."登录异常验证码" 4."用户注册验证码"
	 *                     5."修改密码验证码" 6."信息变更验证码"
	 */
	@SuppressWarnings("deprecation")
	public JSONObject authCode(EntRegisterDto entRegisterDto,
			@RequestParam(required = false, defaultValue = "1") Integer templateCode, Integer code) {
		
		String phone = entRegisterDto.getPhone();

		DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESSKEY_ID, SECRET);
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain(DOMAIN);
		request.setVersion(VERSION);
		request.setAction(ACTION_SEND);
		request.putQueryParameter("RegionId", REGION_ID);
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", PROJECT_NAME);

		switch (templateCode) {
		case 1:
			request.putQueryParameter(CODE_NAME, PinType.AUTHENTICATION.getCode());
			break;
		case 2:
			request.putQueryParameter(CODE_NAME, PinType.LOGIN_CONFIRMATION.getCode());
			break;
		case 3:
			request.putQueryParameter(CODE_NAME, PinType.LOGIN_ABNORMAL.getCode());
			break;
		case 4:
			request.putQueryParameter(CODE_NAME, PinType.REGISTER.getCode());
			break;
		case 5:
			request.putQueryParameter(CODE_NAME, PinType.CHANGE_PASSWORD.getCode());
			break;
		case 6:
			request.putQueryParameter(CODE_NAME, PinType.CHANGE_INFORMATION.getCode());
			break;
		default:
			request.putQueryParameter(CODE_NAME, PinType.AUTHENTICATION.getCode());
		}
		// 【8小时人事管家】验证码934169,您正在尝试修改登录密码,请妥善保管账户信息。
		request.putQueryParameter(TEMPLATE_PARAM, "{\"code\":\"" + code + "\"}");
		CommonResponse response = null;
		try {
			response = client.getCommonResponse(request);
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}

		return JSONObject.parseObject(response.getData());
	}

	/**
	 * 短信内容查询(短信发送日期,支持查询最近30天的记录)
	 */
	@SuppressWarnings("deprecation")
	public String QuerySendDetails(String phone, String date, String bizId) {
		DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESSKEY_ID, SECRET);
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain(DOMAIN);
		request.setVersion(VERSION);
		request.setAction(ACTION_QUERY);
		request.putQueryParameter("RegionId", REGION_ID);
		request.putQueryParameter("PhoneNumber", phone);
		request.putQueryParameter("SendDate", date);
		request.putQueryParameter("PageSize", PAGE_SIZE);
		request.putQueryParameter("CurrentPage", CURRENT_PAGE);
		request.putQueryParameter("BizId", bizId);
		String a = null;
		try {
			CommonResponse response = client.getCommonResponse(request);

			a = response.getData();

		} catch (Exception e) {
			e.printStackTrace();
		}
		System.err.println(a);
		if (a != null && a.contains("OK")) {
			return a;// 代表请求成功
		}
		return "获取失败";
	}

	/**
	 * 提醒功能
	 * 
	 * @param name   用户名字
	 * @param htname 要提醒的东西的名字
	 * @param time   提醒的时间
	 * @param phone
	 * @return 
	 */
	@SuppressWarnings("deprecation")
	public static String remind(String name, String htname, String time, String phone) {
		
		Integer orgCode = QyzxEmpLogin.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxEmpLogin>().eq(QyzxEmpLogin::getPhone, phone)).getOrgId();
		System.err.println("orgCode"+orgCode);
		QyzxRemainingQuantity quantity = QyzxRemainingQuantity.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxRemainingQuantity>()
						.eq(QyzxRemainingQuantity::getOrgCode, orgCode)
						.orderByAsc(QyzxRemainingQuantity::getExpireDate));
		if (quantity == null || quantity.getRemainder() == null || quantity.getRemainder() <= 0) {
			System.err.println("请充钱");
			return"请先充值购买短信套餐!";
		}
		
		DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESSKEY_ID, SECRET);
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain(DOMAIN);
		request.setVersion(VERSION);
		request.setAction(ACTION_SEND);
		request.putQueryParameter("RegionId", REGION_ID);
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", PROJECT_NAME);
		request.putQueryParameter(CODE_NAME, PinType.REMIND.getCode());
		request.putQueryParameter(TEMPLATE_PARAM,
				"{\"name\":\"" + name + "\", \"htname\":\"" + htname + "\",\"time\":\"" + time + "\"}");

		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return "发送成功";
	}

}