package cn.wizzer.app.web.modules.controllers.open;

import cn.wizzer.app.sys.modules.models.Sys_app_conf;
import cn.wizzer.app.sys.modules.models.Sys_app_list;
import cn.wizzer.app.sys.modules.models.Sys_app_task;
import cn.wizzer.app.sys.modules.services.SysAppConfService;
import cn.wizzer.app.sys.modules.services.SysAppListService;
import cn.wizzer.app.sys.modules.services.SysAppTaskService;
import cn.wizzer.app.web.commons.filter.ApiDeploySignFilter;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.boot.starter.ftp.FtpService;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Streams;
import org.nutz.lang.Times;
import org.nutz.lang.stream.StringInputStream;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 应用管理服务端接口
 * Created by wizzer on 2019/3/8.
 */
@IocBean
@At("/open/api/deploy")
@Filters({@By(type = ApiDeploySignFilter.class)})
public class ApiDeployController {
    private final static Log log = Logs.get();
    @Inject
    @Reference
    private SysAppTaskService sysAppTaskService;
    @Inject
    @Reference
    private SysAppListService sysAppListService;
    @Inject
    @Reference
    private SysAppConfService sysAppConfService;
    @Inject
    private FtpService ftpService;

    @At("/task")
    @Ok("json")
    @POST
    public Object task(@Param("hosts") String[] hosts, @Param("hostname") String hostname) {
        try {
            List<Sys_app_task> list = sysAppTaskService.query(Cnd.where("name", "in", hosts).and("hostName", "=", hostname).and("status", "=", 0));
            List<String> ids = new ArrayList<>();
            for (Sys_app_task task : list) {
                ids.add(task.getId());
            }
            sysAppTaskService.update(Chain.make("status", 1), Cnd.where("id", "in", ids));
            return Result.success("获取成功").addData(list);
        } catch (Exception e) {
            return Result.error("获取失败");
        }
    }

    @At("/report")
    @Ok("json")
    @POST
    public Object report(@Param("hosts") String[] hosts, @Param("hostname") String hostname, @Param("taskid") String taskid, @Param("status") int status, @Param("msg") String msg) {
        try {
            sysAppTaskService.update(Chain.make("status", status).add("pushResult", msg).add("pushAt", Times.getTS()), Cnd.where("id", "=", taskid));
            return Result.success("执行成功");
        } catch (Exception e) {
            return Result.error("执行失败");
        }
    }

    @At("/jar/download")
    @Ok("void")
    public void jarDownload(@Param("name") String name, @Param("version") String version, HttpServletResponse response) {
        try {
            Sys_app_list sysAppList = sysAppListService.fetch(Cnd.where("appName", "=", name).and("appVersion", "=", version).and("disabled", "=", false));
            if (sysAppList != null) {
                String fileName = sysAppList.getAppName() + ".jar";
                response.setHeader("Content-Type", "application/java-archive");
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
                response.setContentLengthLong(sysAppList.getFileSize());
                ftpService.download(sysAppList.getFilePath(), response.getOutputStream());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @At("/conf/download")
    @Ok("void")
    public void confDownload(@Param("name") String name, @Param("version") String version, HttpServletResponse response) {
        try {
            Sys_app_conf conf = sysAppConfService.fetch(Cnd.where("confName", "=", name).and("confVersion", "=", version).and("disabled", "=", false));
            if (conf != null) {
                String fileName = conf.getConfName() + "-" + conf.getConfVersion() + ".properties";
                response.setHeader("Content-Type", "text/plain");
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
                try (InputStream in = new StringInputStream(conf.getConfData())) {
                    Streams.writeAndClose(response.getOutputStream(), in);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

@@ -1616,36 +1616,35 @@
 ct task(@Param(%22
-host
+app
 s%22) String%5B%5D hos
@@ -1632,36 +1632,35 @@
 apps%22) String%5B%5D 
-host
+app
 s, @Param(%22hostn
@@ -1784,20 +1784,19 @@
 , %22in%22, 
-host
+app
 s).and(%22
@@ -2080,24 +2080,345 @@
 in%22, ids));%0A
+            //%E5%A4%A7%E4%BA%8E3%E5%88%86%E9%92%9F%E5%B0%86%E4%BB%BB%E5%8A%A1%E8%AE%BE%E7%BD%AE%E4%B8%BA%E8%B6%85%E6%97%B6%0A            long now3 = Times.getTS() - 3 * 60;%0A            sysAppTaskService.update(Chain.make(%22status%22, 3).add(%22pushAt%22, Times.getTS()).add(%22pushResult%22, %22%E4%BB%BB%E5%8A%A1%E8%B6%85%E6%97%B6%22),%0A                    Cnd.where(%22name%22, %22in%22, apps).and(%22hostName%22, %22=%22, hostname).and(%22status%22, %22=%22, 1).and(%22opAt%22, %22%3C%22, now3));%0A
             

应用管理-接口-大于3分钟将任务设置为超时