package com.fs.wx.kf.service.impl;

import cn.hutool.core.io.resource.InputStreamResource;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fs.wx.kf.dto.*;
import com.fs.wx.kf.service.IWeixinKfService;
import com.fs.wx.kf.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class WeixinKfServiceImpl implements IWeixinKfService {
//    String corpid="ww44239b22628b206c";
//    String corpsecret="hGwu42FBESJGzemWP2aKFpi3nbCRtNskl_WC9y-6stM";
//    String token="JyRqdfGpuc3Wr43A29vAUI3";
//    String encodingAESKey="7pxHnmyMplwTkoSmR5V00tbsShVoFs22WaTfoItocPR";

    @Override
    public WeixinKfTokenVO getToken(String corpid,String corpsecret) {
        String url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret;

        String result =HttpUtil.get(url);
        WeixinKfTokenVO vo=JSONUtil.toBean(result, WeixinKfTokenVO.class);
        return vo;

    }

    @Override
    public WeixinKfMsgVO getMsg(String corpid,String corpsecret,WeixinKfMsgDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/kf/sync_msg?access_token="+tokenVO.getAccess_token();
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(dto));

        WeixinKfMsgVO vo=JSONUtil.toBean(result, WeixinKfMsgVO.class);
        return vo;
    }

    @Override
    public WeixinKfMsgSendVO sendMsg(String corpid,String corpsecret,WeixinKfMsgSendDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_token="+tokenVO.getAccess_token();
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(dto));

        WeixinKfMsgSendVO vo=JSONUtil.toBean(result, WeixinKfMsgSendVO.class);
        return vo;
    }

    @Override
    public WeixinKfMsgSendVO sendEventMsg(String corpid,String corpsecret,WeixinKfEventMsgSendDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token="+tokenVO.getAccess_token();
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(dto));
        WeixinKfMsgSendVO vo=JSONUtil.toBean(result, WeixinKfMsgSendVO.class);
        return vo;
    }

    @Override
    public WeixinKfAddEditAccountVO addAccount(String corpid,String corpsecret,WeixinKfAddEditAccountDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/kf/account/add?access_token="+tokenVO.getAccess_token();
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(dto));
        WeixinKfAddEditAccountVO vo=JSONUtil.toBean(result, WeixinKfAddEditAccountVO.class);
        return vo;
    }

    @Override
    public WeixinKfAddEditAccountVO editAccount(String corpid,String corpsecret,WeixinKfAddEditAccountDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/kf/account/update?access_token="+tokenVO.getAccess_token();
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(dto));
        WeixinKfAddEditAccountVO vo=JSONUtil.toBean(result, WeixinKfAddEditAccountVO.class);
        return vo;
    }

    @Override
    public WeixinKfDelAccountVO delAccount(String corpid,String corpsecret,WeixinKfDelAccountDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/kf/account/del?access_token="+tokenVO.getAccess_token();
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(dto));
        WeixinKfDelAccountVO vo=JSONUtil.toBean(result, WeixinKfDelAccountVO.class);
        return vo;
    }

    @Override
    public WeixinKfUploadVO uploadFile(String corpid,String corpsecret,WeixinKfUploadDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token="+tokenVO.getAccess_token()+"&type="+dto.getType();
        Map<String, Object> params = new HashMap<>();
        params.put("files", createIs(dto.getFile()));
        String result = HttpRequest.post(url).header(Header.CONTENT_TYPE.getValue(), ContentType.MULTIPART.getValue()).form(params).execute().body();
        WeixinKfUploadVO vo=JSONUtil.toBean(result, WeixinKfUploadVO.class);
        return vo;


    }
    @Override
    public String  getFile(String corpid,String corpsecret,WeixinKfGetFileDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token="+tokenVO.getAccess_token()+"&media_id="+dto.getMedia_id();
        return url;
    }

    @Override
    public WeixinKfGetAccountUrlVO getAccountUrl(String corpid,String corpsecret,WeixinKuGetAccountUrlDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/kf/add_contact_way?access_token="+tokenVO.getAccess_token();
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(dto));
        WeixinKfGetAccountUrlVO vo=JSONUtil.toBean(result, WeixinKfGetAccountUrlVO.class);
        return vo;
    }

    @Override
    public WeixinKfUserVO getUserInfo(String corpid, String corpsecret, WeixinKfUserDTO dto) {
        WeixinKfTokenVO tokenVO=getToken(corpid,corpsecret);
        String url="https://qyapi.weixin.qq.com/cgi-bin/kf/customer/batchget?access_token="+tokenVO.getAccess_token();
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(dto));
        WeixinKfUserVO vo=JSONUtil.toBean(result, WeixinKfUserVO.class);
        return vo;
    }

    /**
     * 文件流转换
     *
     * @param file
     * @return
     */
    public InputStreamResource createIs(MultipartFile file) {
        InputStreamResource isr = null;
        try {
            isr = new InputStreamResource(file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
        }
        return isr;
    }


}
