package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.crawler.modules.appium.entity.AppiumDevice;
import cn.com.ctop.crawler.modules.appium.service.IAppiumDeviceService;
import cn.com.ctop.crawler.modules.appium.service.IAppiumJobService;
import cn.com.ctop.crawler.modules.appium.service.IAppiumTaskService;
import org.jeecg.modules.mq.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jeecg-boot
 * 2019年12月5日19:57:58
 */
@RestController
@RequestMapping("mq")
public class TestMqController {
    @Autowired
    private IAppiumDeviceService deviceService;
    @Autowired
    private Sender sender;
    @Autowired
    private IAppiumJobService jobService;
    @Autowired
    private IAppiumTaskService appiumTaskService;

    @GetMapping("hello")
    public Map<String, Object> sayHello() {
        Map<String, Object> result = new HashMap<>();
        sender.send();
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS.getCode());
        return result;
    }

    @GetMapping("task1")
    public Map<String, Object> task1() {
        Map<String, Object> result = new HashMap<>();
        AppiumDevice device = deviceService.getById(1);
        jobService.loginTask(2L, device);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS.getCode());
        return result;
    }

    @GetMapping("task33")
    public void task1(HttpServletResponse response) throws Exception {
        File f = new File("C:\\Users\\宋英豪\\Desktop\\OKR\\2.png");
        if (!f.exists()) {
            return;
        }
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
        byte[] buf = new byte[1024];
        int len = 0;
        response.reset(); // 非常重要
        // 纯下载方式
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
        OutputStream out = response.getOutputStream();
        while ((len = br.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        br.close();
        out.close();
    }
}
