package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.ImNetworkHereWhiteDao;
import com.yonge.cooleshow.biz.dal.entity.ImNetworkHereWhite;
import com.yonge.cooleshow.biz.dal.service.ImNetworkHereWhiteService;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.utils.http.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * (RongyunHereWhite)表服务实现类
 *
 * @author zx
 * @since 2022-06-13 18:16:24
 */
@Service("rongyunHereWhiteService")
public class ImNetworkHereWhiteServiceImpl extends ServiceImpl<ImNetworkHereWhiteDao, ImNetworkHereWhite> implements ImNetworkHereWhiteService {

    private final static Logger log = LoggerFactory.getLogger(ImNetworkHereWhiteServiceImpl.class);

    @Value("${cn.rongcloud.hereWhite.url}")
    private String hereWhiteUrl;
    @Value("${cn.rongcloud.hereWhite.token}")
    private String hereWhiteToken;

    @Override
    public ImNetworkHereWhiteDao getDao() {
        return this.baseMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ImNetworkHereWhite create(String name, Integer userNum, Long courseScheduleId) throws Exception {
        log.error("create HereWhite name:{},userNum:{},courseScheduleId:{}",name,userNum,courseScheduleId);
        JSONObject json = new JSONObject();
        json.put("name",name);
        json.put("limit",userNum);
        json.put("mode","persistent");
        String url = "/room?token=" + hereWhiteToken;
        JSONObject jsonObject = JSONObject.parseObject(requestParam(json, url));
        if(jsonObject.getString("code").equals("200")){
            ImNetworkHereWhite imNetworkHereWhite = this.getDao().findByCourseScheduleIdLock(courseScheduleId);
            if(imNetworkHereWhite == null){
                JSONObject room = jsonObject.getJSONObject("msg").getJSONObject("room");
                imNetworkHereWhite = new ImNetworkHereWhite();
                Date date = new Date();
                imNetworkHereWhite.setCourseScheduleId(courseScheduleId);
                imNetworkHereWhite.setName(room.getString("name"));
                imNetworkHereWhite.setLimit(room.getInteger("limit"));
                imNetworkHereWhite.setTeamId(room.getInteger("teamId"));
                imNetworkHereWhite.setAdminId(room.getInteger("adminId"));
                imNetworkHereWhite.setMode(room.getString("mode"));
                imNetworkHereWhite.setTemplate(room.getString("template"));
                imNetworkHereWhite.setRegion(room.getString("region"));
                imNetworkHereWhite.setUuid(room.getString("uuid"));
                imNetworkHereWhite.setRoomToken(jsonObject.getJSONObject("msg").getString("roomToken"));
                imNetworkHereWhite.setUpdatedAt(date);
                imNetworkHereWhite.setCreatedAt(date);
                baseMapper.insert(imNetworkHereWhite);
            }
            return imNetworkHereWhite;
        }else {
            throw new BizException(jsonObject.getString("msg"));
        }
    }

    private String requestParam(JSONObject json,String url) throws Exception {
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Content-Type","application/json");
        return HttpUtil.postForHttps(hereWhiteUrl + url, json.toJSONString(), headers);
    }

}

