package thyyxxk.webserver.service.wxapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.utils.SnowFlakeId;
import thyyxxk.webserver.utils.TokenUtil;

@Service
@Slf4j
public class SendWxInfoService {

    private static final String SEND_MSG_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";
    private static final String CREATE_GROUP_URL = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token=ACCESS_TOKEN";
    private static final String SEND_GROUP_MSG = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=ACCESS_TOKEN";
    private static final int AGENT_ID = 1000041;
    private static final int SAFE_MODE = 0;

    public JSONObject sendCorpWxMsg(String touser, String content) {
        String requestUrl= SEND_MSG_URL.replace("ACCESS_TOKEN", TokenUtil.getWeComSendMsgToken());
        JSONObject msgObj = new JSONObject();
        msgObj.put("content", content);
        JSONObject param = new JSONObject();
        param.put("agentid", AGENT_ID);
        param.put("touser", touser.replaceAll(",", "|"));
        param.put("toparty", "");
        param.put("totag", "");
        param.put("msgtype", "text");
        param.put("text", msgObj);
        param.put("safe", SAFE_MODE);
        log.info("推送企业信息消息 >>> {}", param);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(requestUrl, param, String.class);
        JSONObject result = JSONObject.parseObject(response);
        log.info("推送企业信息消息结果 >>> {}", result);
        return result;
    }

    public JSONObject createGroupChat(String name) {
        String url = CREATE_GROUP_URL.replace("ACCESS_TOKEN", TokenUtil.getWeComSendMsgToken());
        JSONObject msgobj = new JSONObject();
        msgobj.put("name", name);
        msgobj.put("owner", "1038");
        JSONArray userlist = new JSONArray();
        userlist.add("1038");
        userlist.add("2801");
        msgobj.put("userlist", userlist);
        msgobj.put("chatid", SnowFlakeId.instance().nextId());
        log.info("创建企业微信群聊 >>> {}", msgobj);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(url, msgobj, String.class);
        JSONObject result = JSONObject.parseObject(response);
        log.info("创建企业微信群聊结果 >>> {}", result);
        return result;
    }

    public JSONObject sendGroupMsg(String chatid, String content) {
        String url = SEND_GROUP_MSG.replace("ACCESS_TOKEN", TokenUtil.getWeComSendMsgToken());
        JSONObject msgobj = new JSONObject();
        msgobj.put("chatid", chatid);
        msgobj.put("msgtype", "text");
        JSONObject text = new JSONObject();
        text.put("content", content);
        msgobj.put("text", text);
        msgobj.put("safe", 0);
        log.info("发送企业微信群聊消息 >>> {}", msgobj);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(url, msgobj, String.class);
        JSONObject result = JSONObject.parseObject(response);
        log.info("发送企业微信群聊消息结果 >>> {}", result);
        return result;
    }
}
