package cn.com.ctop.oa.modules.service.impl;

import cn.com.ctop.common.module.entity.CtopCorpWexinUser;
import cn.com.ctop.common.module.service.ICtopCorpWexinUserService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.oa.modules.constant.QYWxConstant;
import cn.com.ctop.oa.modules.service.IAppchatCreateService;
import cn.com.ctop.oa.modules.service.IWechatTokenService;
import cn.com.ctop.oa.modules.service.IWechatUserListService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.TreeMap;

import static org.jeecg.common.constant.GeneralConstant.ACCESS_TOKEN;
import static org.jeecg.common.constant.GeneralConstant.CODE;

@Service
@Slf4j
public class AppchatCreateServiceImpl implements IAppchatCreateService {
    @Autowired
    private IWechatTokenService tokenService;
    @Autowired
    private IWechatUserListService userListService;
    @Autowired
    private ICtopCorpWexinUserService ctopCorpWexinUserService;

    @Override
    public void createAppChat(String name, String owner, JSONArray userlist) {
        try {
            Map<String, Object> qywxAccessToken = tokenService.getQYWXAccessToken();
            if ((Integer) qywxAccessToken.get(CODE) != 0) {
                throw new Exception("获取token失败");
            }
            System.err.println(qywxAccessToken);
            JSONObject params = new JSONObject();
            params.put("name", "易车时报发送");
            params.put("owner", "qy01338f036d3ce902287758b529");
            JSONArray userList = new JSONArray();
            userList.add("qy01338f036d3ce902287758b529");
            userList.add("qy01ae8f626d56e90128175730d2");
            userList.add("YuMeng");
            params.put("userlist", userList);
            String resultStr = HttpUtils.httpPostRequestTest(QYWxConstant.APPCHAT_CREATE + "?access_token=" + qywxAccessToken.get(ACCESS_TOKEN), params.toJSONString(), null);
            System.err.println(resultStr);
        } catch (Exception e) {
            log.error("员工考勤入库异常，", e);
        }
    }

    @Override
    public void getChatInfo(String chatid) {
        Map<String, Object> qywxAccessToken = tokenService.getQYWXAccessToken();
        TreeMap params = new TreeMap();
        params.put("chatid", chatid);
        params.put("access_token", qywxAccessToken.get(ACCESS_TOKEN));
        String resultStr = HttpUtils.httpGetRequest(QYWxConstant.APPCHAT_GET, null, params);
        JSONObject jsonObject = JSONObject.parseObject(resultStr);
        if (Check.isNull(jsonObject)) {
            return;
        }
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode != 0) {
            log.error("获取群聊信息返回code失败");
            return;
        }
        JSONObject chat_info = jsonObject.getJSONObject("chat_info");
        if (Check.isNull(chat_info)) {
            return;
        }
        JSONArray userlist = chat_info.getJSONArray("userlist");
        if (Check.isNull(userlist)) {
            return;
        }
        StringBuilder text = new StringBuilder();
        text.append("请及时发送该时段易车时报！");
        text.append("\n");
        text.append("16:30和21:30必须提前10分钟准时发出!");
        text.append("\n");
        text.append("\n");
        for (int i = 0; i < userlist.size(); i++) {
            String userId = userlist.getString(i);
            CtopCorpWexinUser userInfo = ctopCorpWexinUserService.getInfoByUserId(userId);
            if (Check.isNull(userInfo)) {
                continue;
            }
            if (userInfo.getWexinName().equals("于蒙蒙") || userInfo.getWexinName().equals("吴永前")) {
                continue;
            }
            text.append("@" + userInfo.getWexinName() + "");
        }
        JSONObject postJson = new JSONObject();
        postJson.put("chatid", chatid);
        postJson.put("msgtype", "text");
        JSONObject content = new JSONObject();
        content.put("content", text);
        postJson.put("text", content);
        postJson.put("safe", 0);
        String postStr = HttpUtils.httpPostRequestTest(QYWxConstant.APPCHAT_SEND + "?access_token=" + qywxAccessToken.get(ACCESS_TOKEN), postJson.toJSONString(), null);
        log.info("定时发送消息返回结果：{}", postStr);

    }
}
