package com.bootdo.datas.service;


import com.bootdo.common.exception.BDException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @author zhouhj
 */
@Service
public class MessageHuNanService {
    private static final Logger logger = LoggerFactory.getLogger(MessageHuNanService.class);

    @Value("${hunan_message_gateway.url:#{null}}")
    private String gateWayUrl;

    @Value("${hunan_message_gateway.spcode:#{null}}")
    private String gatewaySpCode;

    @Value("${hunan_message_gateway.loginname:#{null}}")
    private String gatewayLoginName;

    @Value("${hunan_message_gateway.password:#{null}}")
    private String gatewayPassword;

    @Value("${hunan_message_gateway.f:#{null}}")
    private String riskF;


    /**
     * 安全风险评估审核驳回通知
     * @param phoneNumbers 手机字符串，多个使用逗号分隔
     * @param dataName 数据项名称
     */
    public void sendMessage(String phoneNumbers, String dataName) throws BDException {
        String MessageContent = "【湖南数据安全服务平台】现有涉及贵单位的重要数据和核心数据目录备案审核被驳回信息同步给您，" +
                "被驳回数据项名称是："+dataName+"，请尽快登录系统进行处理。";
        String templateId = "your_template_id";
        long currentTimeMillis = System.currentTimeMillis();
        String SerialNumber = String.valueOf(currentTimeMillis);
        try {
            URL obj = new URL(gateWayUrl);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=GB2312");
            con.setDoOutput(true);

            String postParams = "SpCode=" + URLEncoder.encode(gatewaySpCode, "GB2312") +
                    "&LoginName=" + URLEncoder.encode(gatewayLoginName, "GB2312") +
                    "&Password=" + URLEncoder.encode(gatewayPassword, "GB2312") +
                    "&MessageContent=" + URLEncoder.encode(MessageContent, "GB2312") +
                    "&UserNumber=" + URLEncoder.encode(phoneNumbers, "GB2312") +
                    "&templateId=" + URLEncoder.encode(templateId, "GB2312") +
                    "&SerialNumber=" + URLEncoder.encode(SerialNumber, "GB2312") +
                    "&f=" + URLEncoder.encode(riskF, "GB2312");

            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(postParams);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "GB2312"));
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                logger.info("请求成功，响应内容：");
                logger.info(response.toString());
            } else {
                logger.info("请求失败，状态码：" + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
