package com.zcloud.service.sms.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.sms.SMSLogMapper;
import com.zcloud.service.sms.SMSLogService;
import com.zcloud.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 说明：短信记录
 * 作者：luoxiaobao
 * 时间：2021-02-26
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class SMSLogServiceImpl implements SMSLogService {

	@Autowired
	private SMSLogMapper msmloginMapper;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		msmloginMapper.save(pd);
	}

	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		msmloginMapper.delete(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		msmloginMapper.edit(pd);
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return msmloginMapper.datalistPage(page);
	}

	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAll(PageData pd)throws Exception{
		return msmloginMapper.listAll(pd);
	}

	/**本公司已发送短信数
	 * @param pd
	 * @throws Exception
	 */
	public int msUsedCount(PageData pd)throws Exception{
		return msmloginMapper.msUsedCount(pd);
	}

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return msmloginMapper.findById(pd);
	}

	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		msmloginMapper.deleteAll(ArrayDATA_IDS);
	}
	/**
	 * 发生短信的日志
	 */
	public void addForAliSmsUtil(Map<String ,Object> sendmap , String corpInfoId , SendSmsResponse ssms ) throws Exception{
		StringBuffer retMess = new StringBuffer();
		if(sendmap.get("type").equals("1")) {
			 retMess.append("time:"+sendmap.get("time").toString()+";");
	    }
		if(sendmap.get("type").equals("2")) {
        	//request.setTemplateParam("{\"time\":\""+sendmap.get("time").toString()+"\"}");
        }
	    if(sendmap.get("type").equals("3")) {
	    	retMess.append("corpname:"+sendmap.get("corpname").toString()+";");
	    	retMess.append("qname:"+sendmap.get("qname").toString()+";");
	    	retMess.append("month:"+sendmap.get("month").toString()+";");
	    	retMess.append("time:"+sendmap.get("time").toString()+";");
	    }
	    if(sendmap.get("type").equals("4")) {

	    	retMess.append("pname:"+sendmap.get("pname").toString()+";");
	    	retMess.append("qname:"+sendmap.get("qname").toString()+";");
	    	retMess.append("month:"+sendmap.get("month").toString()+";");
	    	retMess.append("time:"+sendmap.get("time").toString()+";");
	    }
	    if(sendmap.get("type").equals("5")) {
	    	retMess.append("qname:"+sendmap.get("qname").toString()+";");
	    }
		retMess.append("requestId:"+ssms.getRequestId()+";");
		retMess.append("bizId:"+ssms.getBizId()+";");
		retMess.append("code:"+ssms.getCode()+";");
		retMess.append("message:"+ssms.getMessage()+";");

		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

		PageData pd = new PageData();
		pd.put("MSMLOGIN_ID", uuid);	//主键
		pd.put("PHONE", sendmap.get("phone").toString());
		pd.put("ISDELETE", "0");
		pd.put("CONTENT", sendmap.get("templateCode").toString());
		pd.put("STATE", ssms.getCode());
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));
		pd.put("CORPINFO_ID", corpInfoId);
		pd.put("RET_MESSAGE", retMess.toString());
		this.save(pd);
	}

	/**
	 * 发生有限空间审核表短信的日志
	 */
	public void addForAliSmsLimitUtil(Map<String ,Object> sendmap , String corpInfoId , SendSmsResponse ssms ) throws Exception{
		StringBuffer retMess = new StringBuffer();
		if(sendmap.get("type").equals("1")) {
			 retMess.append("name:"+sendmap.get("name").toString()+";");
	    }
		if(sendmap.get("type").equals("2")) {
			retMess.append("tname:"+sendmap.get("tname").toString()+";");
			retMess.append("name:"+sendmap.get("name").toString()+";");
        }
	    if(sendmap.get("type").equals("3")) {
	    	retMess.append("tname:"+sendmap.get("tname").toString()+";");
	    	retMess.append("sname:"+sendmap.get("sname").toString()+";");
	    	retMess.append("name:"+sendmap.get("name").toString()+";");
	    }
		if(sendmap.get("type").equals("4")) {
			retMess.append("tsname:"+sendmap.get("sname").toString()+";");
			retMess.append("name:"+sendmap.get("name").toString()+";");
        }
		retMess.append("requestId:"+ssms.getRequestId()+";");
		retMess.append("bizId:"+ssms.getBizId()+";");
		retMess.append("code:"+ssms.getCode()+";");
		retMess.append("message:"+ssms.getMessage()+";");

		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

		PageData pd = new PageData();
		pd.put("MSMLOGIN_ID", uuid);	//主键
		pd.put("PHONE", sendmap.get("phone").toString());
		pd.put("ISDELETE", "0");
		pd.put("CONTENT", sendmap.get("templateCode").toString());
		pd.put("STATE", ssms.getCode());
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));
		pd.put("CORPINFO_ID", corpInfoId);
		pd.put("RET_MESSAGE", retMess.toString());
		this.save(pd);
	}

	/**
	 * 通过短信工具类发送的短信日志
	 */
	public void addForAliSmsToolUtil(Map<String ,Object> sendmap , String corpInfoId , SendSmsResponse ssms, List<PageData> paramsList) throws Exception{
		StringBuffer retMess = new StringBuffer();
		// 遍历参数
		if(paramsList != null && paramsList.size() > 0) {
        	for(PageData par: paramsList) {
        		retMess.append(par.get("name").toString()+":"+par.get("value").toString()+";");
        	}
        }
		retMess.append("requestId:"+ssms.getRequestId()+";");
		retMess.append("bizId:"+ssms.getBizId()+";");
		retMess.append("code:"+ssms.getCode()+";");
		retMess.append("message:"+ssms.getMessage()+";");

		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

		PageData pd = new PageData();
		pd.put("MSMLOGIN_ID", uuid);	//主键
		pd.put("PHONE", sendmap.get("phone").toString());
		pd.put("ISDELETE", "0");
		pd.put("CONTENT", sendmap.get("templateCode").toString());
		pd.put("STATE", ssms.getCode());
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));
		pd.put("CORPINFO_ID", corpInfoId);
		pd.put("RET_MESSAGE", retMess.toString());
		this.save(pd);
	}
}

