package com.yutou.nas.Controllers;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.nas.interfaces.DownloadInterface;
import com.yutou.nas.utils.QQBotManager;
import com.yutou.nas.utils.Tools;
import org.springframework.stereotype.Controller;
import com.yutou.nas.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Controller
public class QQBot {
    @RequestMapping("/qq/bot/test.do")
    @ResponseBody
    public String test(){
        return "123";
    }

    @RequestMapping("/qq/bot/send.do")
    @ResponseBody
    public String send(String msg,String imgUrl){
        JSONObject json=new JSONObject();
        try {
            String ret;
            if(StringUtils.isEmpty(imgUrl)){
                ret= QQBotManager.getInstance().sendMessage(msg);
            }else{
                ret=QQBotManager.getInstance().sendMessage(imgUrl,msg);
            }
            json.put("code",0);
            json.put("msg",ret);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
       return json.toJSONString();
    }
    @RequestMapping("/qq/bot/sendAndImg.do")
    @ResponseBody
    public String botMsg(String msg,@RequestParam("file") MultipartFile file){
        JSONObject json=new JSONObject();
        if(file==null){
            json.put("code",-1);
            json.put("msg","图片为空");
            return json.toJSONString();
        }
        String ret;
        try {
            String path = Tools.createFile("qq_image",file, file.getName());
            ret=QQBotManager.getInstance().sendMessage(path,msg);
        } catch (Exception e) {
            e.printStackTrace();
            ret=e.getLocalizedMessage();
        }
        json.put("code",0);
        json.put("msg",ret);
        return json.toJSONString();
    }
}
