package org.jeecg.modules.ai.service.impl;

import java.io.StringReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jeecg.modules.ai.entity.Room;
import org.jeecg.modules.ai.mapper.RoomMapper;
import org.jeecg.modules.ai.service.IRoomService;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.service.ISysMessageService;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.entity.SysAnnouncementSend;
import org.jeecg.modules.system.service.ISysAnnouncementSendService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.http.webservice.SoapClient;
import cn.hutool.http.webservice.SoapUtil;

/**
 * @Description: 房间表
 * @Author: jeecg-boot
 * @Date:   2023-11-30
 * @Version: V1.0
 */
@Service
@DS("multi-datasource1")
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
	
	@Resource
	private RoomMapper roomMapper;
	
	/**
	 * 创建房间并返回房间地址
	 * @return
	 */
	public String saveRoom(String userId) {
		Room room = new Room();
		room.setName("远程协助"+userId);
		room.setDeleted(0);
		room.setInserted(new Date());
		room.setAllowRecording(1);
		room.setAllowUserQuestions(0);
		room.setAppointment(0);
		room.setAudioOnly(1);
		room.setCapacity(2L);
		room.setChatOpened(0);
		room.setClosed(0);
		room.setDemoRoom(0);
		room.setFilesOpened(0);
		room.setIspublic(1);
		room.setModerated(0);
		room.setSipEnabled(0);
		room.setType("INTERVIEW");
		room.setWaitForRecording(1);
		room.setWaitModerator(0);
		room.setChatModerated(0);
		roomMapper.insert(room);
		//生成房间地址
		//请求地址
        String soapUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
        HashMap<String, Object> map = new HashMap<>();
        map.put("theCityName", "武汉");
        SoapClient soapClient = SoapClient.create(soapUrl)
                //请求方法,命名空间
                .setMethod("web:getWeatherbyCityName", "http://WebXml.com.cn/")
                .setParams(map);
        String sendSoap = soapClient.send(true);
        System.out.println(sendSoap);
        Map<String, String> map1 = new HashMap<String, String>();
        Map<String, String> soapMap = XmlMap(sendSoap, map1);
        System.out.println(JSONObject.toJSONString(soapMap));

		
		String roomUrl = "";
		return roomUrl;
	}
	
	public static Map<String, String> XmlMap(String xml, Map<String, String> map) {
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(new StringReader(xml));
            Element root = doc.getRootElement();
            String path = "";
            if (map.containsKey(root.getName().trim())) {
                path = map.get(root.getName().trim());
                map.remove(root.getName().trim());
            }
            for (Iterator i = root.elementIterator(); i.hasNext(); ) {
                Element element = (Element) i.next();
                if (element.isTextOnly()) {
                    if (path.length() > 0) {
                        map.put(path + element.getName().trim(), element.getTextTrim());
                    } else {
                        map.put(element.getName().trim(), element.getTextTrim());
                    }
                } else {
                    map.put(element.getName().trim(), path + element.getName().trim() + ".");
                    XmlMap(element.asXML(), map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


}
