package com.ydd.oms.controller;

import com.ydd.framework.core.controller.BaseController;
import com.ydd.oms.config.shiro.ShiroAdmin;
import com.ydd.oms.service.sys.CommonService;
import org.apache.shiro.SecurityUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

/**
 * Controller - oms基类
 *
 * @author xingkong1221
 * @since 2017-09-27
 */
public class OmsController extends BaseController {

    @Resource
    private CommonService commonService;



    /**
     * 获取登录管理员
     *
     * @return 管理员
     */
    public ShiroAdmin getLoginAdmin() {
        return (ShiroAdmin) SecurityUtils.getSubject().getPrincipal();
    }

    public void writeExcel(File excel, String name, HttpServletResponse response){
        response.setContentLength((int) excel.length());
        response.setHeader("Content-Disposition", "attachment;filename="
                + name+".xlsx");
        response.setContentType("application/octet-stream");
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(excel);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(
                    fileInputStream);
            byte[] b = new byte[bufferedInputStream.available()];
            bufferedInputStream.read(b);
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(b);
            bufferedInputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            excel.delete();
        }
    }

}
