BaseController.java 6.67 KB
package com.bsth.controller;

import com.bsth.common.ResponseCode;
import com.bsth.service.BaseService;
import com.bsth.service.schedule.utils.DataImportExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @ClassName: BaseController 
 * @Description: TODO(基础的Controller实现) 
 * @author PanZhao 
 * @date 2016年3月17日 下午12:44:06 
 * 
 * @param <T>
 * @param <ID> 主键类型
 */
public class BaseController<T, ID extends Serializable> {
	
	@Autowired
	protected BaseService<T, ID> baseService;
    @Autowired
    DataImportExportService dataImportExportService;
	
	/**
	 * 
	 * @Title: list 
	 * @Description: TODO(多条件分页查询) 
	 * @param @param map 查询条件
	 * @param @param page 页码
	 * @param @param size 每页显示数量
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Page<T> list(@RequestParam Map<String, Object> map,
			@RequestParam(defaultValue = "0") int page,
			@RequestParam(defaultValue = "10") int size,
			@RequestParam(defaultValue = "id") String order,
			@RequestParam(defaultValue = "DESC") String direction){

		Direction d;
		
		if(null != direction && direction.equals("ASC"))
			d = Direction.ASC;
		else
			d = Direction.DESC;
		
		return baseService.list(map, new PageRequest(page, size, new Sort(d, order)));
	}
	
	/**
	 * 
	 * @Title: list 
	 * @Description: TODO(多条件查询) 
	 * @param @param map
	 * @throws
	 */
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public Iterable<T> list(@RequestParam Map<String, Object> map){
		return baseService.list(map);
	}
	
	/**
	 * 
	 * @Title: save 
	 * @Description: TODO(持久化对象) 
	 * @param @param t
	 * @param @return    设定文件 
	 * @return Map<String,Object>  {status: 1(成功),-1(失败)}
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Map<String, Object> save(T t){
		return baseService.save(t);
	}
	
	/**
	 * 
	 * @Title: findById 
	 * @Description: TODO(根据主键获取单个对象) 
	 * @param @param id
	 * @throws
	 */
	@RequestMapping(value="/{id}",method = RequestMethod.GET)
	public T findById(@PathVariable("id") ID id){
		return baseService.findById(id);
	}
	
	/**
	 * 
	 * @Title: delete 
	 * @Description: TODO(根据主键删除对象) 
	 * @param @param id
	 * @throws
	 */
	@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
	public Map<String, Object> delete(@PathVariable("id") ID id){
		return baseService.delete(id);
	}

    /**
     * 上传数据文件,并使用ktr转换文件导入数据。
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/dataImport", method = RequestMethod.POST)
    public Map<String, Object> uploadDataAndImport(MultipartFile file) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

        try {
            // 获取ktr转换文件绝对路径
            File ktrfile = new File(this.getClass().getResource(getDataImportKtrClasspath()).toURI());
            System.out.println(ktrfile.getAbsolutePath());
            // 导入数据
            dataImportExportService.fileDataImport(file, ktrfile);

            resultMap.put("status", ResponseCode.SUCCESS);
            resultMap.put("msg", "导入成功");
        } catch (Exception exp) {
            exp.printStackTrace();
            resultMap.put("status", ResponseCode.ERROR);
            resultMap.put("msg", exp.getLocalizedMessage());
        }

        return resultMap;
    }

    /**
     * 使用ktr导出数据。
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/dataExport", method = RequestMethod.GET)
    public void dataExport(HttpServletResponse response) throws Exception {
        // 1、使用ktr转换获取输出文件
        File ktrfile = new File(this.getClass().getResource(getDataExportKtrClasspath()).toURI());
        File outputfile = dataImportExportService.fileDataOutput(
                getDataExportFilename(),
                ktrfile);

        System.out.println(outputfile.getName());
        String filePath = outputfile.getAbsolutePath();
        String fp[] = filePath.split(File.separator);
        String fileName = fp[fp.length - 1];

        // TODO:使用ktr获取导出数据

        response.setHeader("conent-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + "东东");

        OutputStream os = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(os);

        InputStream is = null;

        is = new FileInputStream(filePath);
        BufferedInputStream bis = new BufferedInputStream(is);

        int length = 0;
        byte[] temp = new byte[1 * 1024 * 10];

        while ((length = bis.read(temp)) != -1) {
            bos.write(temp, 0, length);
        }
        bos.flush();
        bis.close();
        bos.close();
        is.close();
    }

    /**
     * @return 数据导出的ktr转换文件类路径。
     */
    protected String getDataExportKtrClasspath() {
        // 默认返回异常,子类如果要使用导出功能,必须覆写此方法,指定ktr文件类路径
        throw new RuntimeException("必须override,并指定ktr classpath");
    }

    /**
     * @return 导出文件名。
     */
    protected String getDataExportFilename() {
        // 默认返回异常,子类如果要使用导出功能,必须覆写此方法,指定导出的文件路径名
        throw new RuntimeException("必须override,并指定导出文件名");
    }

    /**
     * @return 数据导入的ktr转换文件类路径。
     */
    protected String getDataImportKtrClasspath() {
        // 默认返回异常,子类如果要使用导入功能,必须覆写此方法,指定ktr文件类路径
        throw new RuntimeException("必须override,并指定ktr classpath");
    }
	
}