package com.storlead.wecom.service;

import com.alibaba.fastjson.JSON;
import com.storlead.wecom.pojo.vo.CorpWeChatDeptVO;
import com.storlead.wecom.pojo.vo.CorpWeChatUserVO;
import com.storlead.wecom.pojo.vo.WechatToken;
import com.storlead.wecom.properties.CorpWeChatConstants;
import com.storlead.wecom.properties.CorpWeChatProperties;
import com.storlead.wecom.util.CorpWechatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: sp-sales
 * @description:
 * @author: chenkq
 * @create: 2022-07-15 15:29
 */
@Service
public class WxDataSyncService {

    @Autowired
    private WechatOrganizSyncService wechatOrganizSyncService;

    public String syncWxOrganizData(WechatToken t) {
        //GET_DEPT_LIST_URL  GET_DEPARTMENT_SIMPLELIST_URL
        List<CorpWeChatDeptVO> volist = wechatOrganizSyncService.getOrgDeptIdList(CorpWeChatConstants.GET_DEPT_LIST_URL,t.getToken(),null);
        return JSON.toJSONString(volist);
    }

    public List<CorpWeChatUserVO> getWxUserIdList(WechatToken t) {
        List<CorpWeChatUserVO> volist = wechatOrganizSyncService.getUserIdList(CorpWeChatConstants.GET_USER_ID_LISET_URL,t.getToken());
        return volist;
    }

    //
    public CorpWeChatDeptVO getWxDeptDetail(WechatToken t,Long deptId) {
        CorpWeChatDeptVO vo = wechatOrganizSyncService.getDeptDetail(CorpWeChatConstants.GET_DEPARTMENT_GETONE_URL,t.getToken(),deptId);
        return vo;
    }

    public CorpWeChatUserVO getWxUserDetial(WechatToken t,String userid) {
        CorpWeChatUserVO vo = wechatOrganizSyncService.getUserDetail(CorpWeChatConstants.GET_USER_DETAIL_URL,t.getToken(),userid);
        return vo;
    }

    public String syncWxUserData() {
        String corpId = CorpWeChatProperties.getCorpId();
        String corpAddressSecret = CorpWeChatProperties.getCorpAddressSecret();
        WechatToken t = CorpWechatUtil.getAccessToken(CorpWeChatConstants.GET_ACCESS_TOKEN_URL, corpId, corpAddressSecret);
        List<CorpWeChatDeptVO> allVo = wechatOrganizSyncService.getUserListUnderDeptFormTencent(CorpWeChatConstants.GET_USER_LIST_URL,t.getToken(),Long.valueOf("1"));
        return JSON.toJSONString(allVo);
    }

    public CorpWeChatUserVO getWxLoginInfo(WechatToken t,String code) {
        CorpWeChatUserVO vo = wechatOrganizSyncService.getLoginUserInfo(CorpWeChatConstants.GET_USER_LOGIN_URL,t.getToken(),code);
        return vo;
    }
}
