package com.mortals.xhx.module.dingding.personal.service.impl;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.module.dingding.AbstractDingTalkService;
import com.mortals.xhx.module.dingding.personal.model.req.workmsg.WorkMsgReq;
import com.mortals.xhx.module.dingding.personal.service.IDingPersonService;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Service("dingPersonService")
@Slf4j
public class DingPersonServiceImpl extends AbstractDingTalkService implements IDingPersonService {


    @Override
    public Rest<String> getPersonByMobile(String mobile) {
        try {
            DingTalkClient client = getDingTalkClient("/topapi/v2/user/getbymobile");
            OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
            req.setMobile(mobile);
            log.info("getPersonByMobile:{}", mobile);
            OapiV2UserGetbymobileResponse rsp = client.execute(req, getToken());
            log.info("OapiV2UserGetbymobileResponse:{}", rsp.getBody());
            if (rsp.getErrcode() == 0) {
                return Rest.ok("成功",rsp.getResult().getUserid());
            } else {
                return Rest.fail(String.format("code:%s,msg:%s", rsp.getErrcode(), rsp.getErrmsg()));
            }
        } catch (ApiException e) {
            log.info("根据手机号码查询人员异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<String> getPersonByCode(String code) {
        try {
            DingTalkClient client = getDingTalkClient("/topapi/v2/user/getuserinfo");
            OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
            req.setCode(code);
            log.info("getPersonByCode:{}", code);
            OapiV2UserGetuserinfoResponse rsp = client.execute(req, getToken());
            log.info("OapiV2UserGetuserinfoResponse:{}", rsp.getBody());
            if (rsp.getErrcode() == 0) {
                return Rest.ok("成功",rsp.getResult().getUserid());
            } else {
                return Rest.fail(String.format("code:%s,msg:%s", rsp.getErrcode(), rsp.getErrmsg()));
            }
        } catch (ApiException e) {
            log.info("根据code查询人员异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public Rest<String> sendWorkMsg(WorkMsgReq workMsgReq) {
        try {
            DingTalkClient client = getDingTalkClient("/topapi/message/corpconversation/asyncsend_v2");

            OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
            req.setAgentId(agentId);
            req.setUseridList(workMsgReq.getUseridList());
            req.setMsg(JSON.toJSONString(workMsgReq.getMsg()));
            log.info("sendWorkMsg:{}", JSON.toJSONString(workMsgReq));
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, getToken());
            log.info("OapiMessageResponse:{}", rsp.getBody());
            if (rsp.getErrcode() == 0) {
                return Rest.ok(rsp.getMsg(), rsp.getRequestId());
            } else {
                return Rest.fail(String.format("code:%s,msg:%s", rsp.getErrcode(), rsp.getErrmsg()));
            }
        } catch (ApiException e) {
            log.info("根据用户id发送工作消息异常", e);
            return Rest.fail(e.getMessage());
        }
    }


}
