package com.lightinit.hsdataplatformresdir.controller.admin;

import com.lightinit.hsdataplatformresdir.common.ConfigUtils;
import com.lightinit.hsdataplatformresdir.common.SysContants;
import com.lightinit.hsdataplatformresdir.entity.BaseExample;

import com.lightinit.hsdataplatformresdir.entity.ResourceDataWithBLOBs;
import com.lightinit.hsdataplatformresdir.model.admin.APIOpenViewModel;
import com.lightinit.hsdataplatformresdir.model.admin.DataCatelogResourceModel;
import com.lightinit.hsdataplatformresdir.model.admin.LoginUser;

import com.lightinit.hsdataplatformresdir.service.admin.IDataCatelogResourceService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;

@Controller
@RequestMapping("/admin/dataCatelogResource")
public class DataCatelogResourceController {
    @Autowired
    private IDataCatelogResourceService iDataCatelogResourceService;

    /**
     * 跳转页面
     * @return
     */
    @RequestMapping("index.htm")
    public ModelAndView index () {
        ModelAndView modelAndView = new ModelAndView("admin/dataCatelogResource/index");
        return modelAndView;
    }

    /**
     * 查询列表
     * @param
     * @param pageNo
     * @return
     */
    @RequestMapping("list.action")
    public ModelAndView list(HttpSession session,@RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/dataCatelogResource/list");
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<DataCatelogResourceModel> outputModel = iDataCatelogResourceService.queryList(loginUser.getId(),pager);
        pager.setTotal(iDataCatelogResourceService.queryCount(loginUser.getId()));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }
    @RequestMapping("toDetail.htm/{id}")
    public ModelAndView toDetail(@PathVariable Long id) {
        APIOpenViewModel model = iDataCatelogResourceService.toDetail(id);

        ModelAndView modelAndView = new ModelAndView("admin/dataCatelogResource/view");
        modelAndView.addObject("busiModel",model);
        return modelAndView;
    }

    @RequiresAuthentication
    @RequestMapping("download.action/{id}")
    public void download(@PathVariable Long id, HttpSession session, HttpServletResponse resp) throws Exception {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ResourceDataWithBLOBs resourceData=iDataCatelogResourceService.downloadById(id,loginUser);//简单实现，service没有验证登录用户
        String dataFile = resourceData.getDataFile();
        String filename = StringUtils.substringAfterLast(dataFile, File.separator);
        filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
        String urlPath= ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.UPLOAD_FILE_ROOT_DIR);
        File file =new File(urlPath+dataFile);
        resp.reset();
        resp.setContentType("application/octet-stream");
        resp.setCharacterEncoding("utf-8");
        resp.setContentLength((int) file.length());
        resp.setHeader("Content-Disposition", "attachment;filename=" + filename);
        byte[] buff = new byte[1024];
        OutputStream os = null;
        os = resp.getOutputStream();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        int i = 0;
        while ((i = bis.read(buff)) != -1) {
            os.write(buff, 0, i);
            os.flush();
        }
        if(bis!=null){
            bis.close();
        }
    }
}
