package com.bohuikeji.frame.module.admin.controller;

import com.bohuikeji.frame.module.admin.base.ControllerBase;
import com.bohuikeji.frame.module.admin.component.StaticComponent;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.PageEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysApp;
import com.bohuikeji.frame.module.admin.service.ISysAppService;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.bohuikeji.frame.module.admin.thread.ShutDownThread;
import com.bohuikeji.frame.module.admin.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 系统访问
 * @author 10552
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class FrameSysController extends ControllerBase {
    final ISysConfigService configService;
    final ISysAppService appService;



    /*@RequestMapping("/error")
    public PageEntity error(){
        return PageEntity.success("/error/404");
    }*/

    /**
     * 下载文件
     */
    @RequestMapping( value="/upload/**",method= RequestMethod.GET)
    public void defaultPath(HttpServletRequest request, HttpServletResponse res){
        String path;
        if(request.getRequestURI().contains("tempPath")){
            path=FileUtil.getTempDir();
        }
        else{
            path=FileUtil.getUserDir();
        }
        File file = new File(path +request.getRequestURI().replace(StaticComponent.ctx, ""));
        //res.setContentType(new MimetypesFileTypeMap().getContentType(file));
        //res.setContentType("image/png");
        res.setContentLengthLong(file.length());
        res.setHeader("Accept-Ranges","bytes");
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = res.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(file));
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            log.error("未找到找文件："+e);
            //e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * APP下载
     * @return
     */
    @GetMapping("/app/index")
    public PageEntity appDownload(){
        return PageEntity.page("/app/index").add("appInfo",appService.getById(1));
    }

    /**
     * APP新版下载
     * @param id
     * @return
     */
    @GetMapping("/app/new/download/page/{id}")
    public PageEntity app(@PathVariable Integer id){
        SysApp app=appService.getById(id);
        if(app==null){
            app=appService.getById(1);
        }
        return PageEntity.page("/app/index").add("appInfo",app);
    }

    /**
     * 应用服务优雅关机
     * @param pwd
     * @return
     */
    @GetMapping("/sys/application/shutdown")
    public APIEntity shutDown(String pwd){
        if("tcjsClose".equals(pwd)){
            //通知子线程关机
            new ShutDownThread().start();
            return APIEntity.success("应用程序安全退出......");
        }
        return APIEntity.fail("无权访问！");
    }


}
