package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.Utils.DateUtil;
import cn.hnthyy.thmz.Utils.HttpUtil;
import cn.hnthyy.thmz.Utils.SignUtil;
import cn.hnthyy.thmz.service.thmz.HaiCiAdapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Slf4j
//@Service
public class HaiCiAdapterServiceImpl implements HaiCiAdapterService {
    private static final String th_pwd = "hnthyy2213";

    @Value("${haiciServiceUrl}")
    private String haiciServiceUrl;
    //卡号变更通知开关
    @Value("${sendNoticeToHaiCi}")
    private boolean sendNoticeToHaiCi;

    /**
     * 更新病人卡号 消息通知
     *
     * @param oldCardNo 老卡号
     * @param cardNo    新卡号
     * @param timeStamp 变更时间
     */
    @Override
    public void cardChangeNotice(String oldCardNo, String cardNo, Date timeStamp) {
        if(!sendNoticeToHaiCi){
            return;
        }
        try {
            String timeStampStr= DateUtil.fomart(timeStamp, "yyyy-MM-dd HH:mm:ss");
            Map<String, String> map = getMap(oldCardNo, cardNo, timeStampStr);
            HttpUtil.httpPost(haiciServiceUrl, getXMLString(oldCardNo,cardNo,timeStampStr),"application/xml");
            log.info(map.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void commonMessageNotice(String cardNo, String message, Date timeStamp) {
        try {
            String timeStampStr= DateUtil.fomart(timeStamp, "yyyy-MM-dd HH:mm:ss");
            Map<String, String> map = getCommonMessageMap(cardNo, message, timeStampStr);
            HttpUtil.httpPost(haiciServiceUrl, getCommonMessageXMLString(cardNo,message,timeStampStr),"application/xml");
            log.info(map.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void rejectCallbackScheduleOfMedical(String reqNo) {

    }

    /**
     * 生成map以进行Sign加密  变更卡号参数
     */
    private Map<String, String> getMap(String oldCardNo, String cardNo, String timeStamp){
        Map<String, String> map = new HashMap<>();
        map.put("serviceCode", "CardChangeNotice");
        map.put("partnerId", "hnthyy");
        map.put("timeStamp", timeStamp);
        map.put("actionType", "1");
        map.put("cardType", "1");
        map.put("oldCardNo", oldCardNo);
        map.put("cardNo", cardNo);
        return map;
    }

    /**
     * 生成向海鹚发送请求的参数 变更卡号参数
     * */
    private String getXMLString(String oldCardNo,String cardNo,String timeStamp) throws Exception {
        Map<String, String> map = getMap(oldCardNo,cardNo,timeStamp);
        return "<?xml version='1.0' encoding='UTF-8'?>\n"
                + "<Request>\n"
                + "<serviceCode>CardChangeNotice</serviceCode>\n"
                + "<partnerId>hnthyy</partnerId>\n"
                + "<timeStamp>" + timeStamp + "</timeStamp>\n"
                + "<password>" + SignUtil.getSignValue(map, th_pwd) + "</password>\n"
                + "<actionType>1</actionType>\n"
                + "<cardType>1</cardType>\n"
                + "<cardNo>" + cardNo + "</cardNo>\n"
                + "<oldCardNo>" + oldCardNo + "</oldCardNo>\n"
                + "</Request>";
    }



    /**
     * 生成map以进行Sign加密  发送通用消息参数
     */
    private Map<String, String> getCommonMessageMap(String cardNo, String message, String timeStamp){
        Map<String, String> map = new HashMap<>();
        map.put("serviceCode", "SendMessageNotice");
        map.put("partnerId", "hnthyy");
        map.put("timeStamp", timeStamp);
        map.put("cardNo", cardNo);
        map.put("admissionNum", "");
        map.put("inNo", "");
        map.put("msgType", "0");
        map.put("msgContext", message);
        map.put("channel", "WX");
        return map;
    }

    /**
     * 生成向海鹚发送请求的参数 发送通用消息参数
     * */
    private String getCommonMessageXMLString(String cardNo, String message, String timeStamp) throws Exception {
        Map<String, String> map = getCommonMessageMap(cardNo,message,timeStamp);
        return "<?xml version='1.0' encoding='UTF-8'?>\n"
                + "<Request>\n"
                + "<serviceCode>SendMessageNotice</serviceCode>\n"
                + "<partnerId>hnthyy</partnerId>\n"
                + "<timeStamp>" + timeStamp + "</timeStamp>\n"
                + "<password>" + SignUtil.getSignValue(map, th_pwd) + "</password>\n"
                + "<cardNo>" + cardNo + "</cardNo>\n"
                + "<admissionNum></admissionNum>\n"
                + "<inNo></inNo>\n"
                + "<msgType>0</msgType>\n"
                + "<msgContext>"+message+"</msgContext>\n"
                + "<channel>WX</channel>\n"
                + "</Request>";
    }
}
