package com.unis.base.server.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.util.ExcelUtils;
import com.unis.base.server.entity.TyChubeilyynbao;
import com.unis.base.server.entity.TyHlyDr;
import com.unis.base.server.service.CblyBbService;
import com.unis.common.utils.DateUtil;
import com.unis.common.utils.ResponseWrapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 储备粮油报表
 * @author My Sunshine
 *
 */
@RestController
@RequestMapping(value="/chubeilyBb")
@Api(tags = "储备粮油月/年报表")
public class CblyBbController {

	
	@Autowired
	private CblyBbService cblyBbService;
	
	
	/**
     * 列表
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectPageByEntity",produces = MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.POST)
    @ApiOperation(value="储备粮油月/年报表列表", notes = "查询数据列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "区分地方1 市级2  区级3", paramType = "query"),
            @ApiImplicitParam(name = "baobiaoshijian", value = "报表时间", paramType = "query"),
            @ApiImplicitParam(name = "yuebaoornianbao", value = "区分月报/年报  1月报/2年报", paramType = "query")
    })
    public List<TyChubeilyynbao> selectPageByEntity(HttpServletRequest request, @RequestBody TyChubeilyynbao chubeilyynbao) throws Exception {
    		
        return cblyBbService.queryList(chubeilyynbao);
    }
	
	/**
	 * 导入
	 */
	@ApiOperation(value = "储备粮油月/年报导入", notes = "储备粮油月/年报导入数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file", value = "文件对象集合")
			,@ApiImplicitParam(name = "cjdw", value = "组织编码")
			,@ApiImplicitParam(name = "type", value = "区分地方1 市级2  区级3")
			,@ApiImplicitParam(name = "baobiaoshijian", value = "报表时间 例：月报：2023年10月 年报：2023年")
			,@ApiImplicitParam(name = "yuebaoornianbao", value = "区分月报/年报  1月报/2年报")
	})
	@PostMapping(value= "/importByEntity", headers= "content-type= multipart/form-data", consumes= "multipart/*")
	public ResponseWrapper importByEntity(HttpServletRequest request, HttpServletResponse response
			,@RequestParam(value= "file", required = true) MultipartFile file
			,@RequestPart(value = "cjdw", required = true) String cjdw
			,@RequestParam(value = "type", required = false) Integer type
			,@RequestParam(value = "baobiaoshijian", required = false) String baobiaoshijian
			,@RequestParam(value = "yuebaoornianbao", required = true) Integer yuebaoornianbao) throws IOException {
			
		if(file== null){
			return ResponseWrapper.fail("无文件数据");
		}
		//获得文件名
        String fileName = file.getOriginalFilename();
        //判断文件是否是excel文件
        if (!fileName.endsWith("xls") && !fileName.endsWith("xlsx")) {
        	return ResponseWrapper.invalid();
        }
		
	    return cblyBbService.importByEntityResult(file,cjdw,type,baobiaoshijian,yuebaoornianbao);
		
	}
	
	
	@ApiOperation(value = "储备粮油月/年报---根据条件导出数据", notes = "储备粮油月/年报报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "区分地方1 市级2  区级3", paramType = "query"),
            @ApiImplicitParam(name = "baobiaoshijian", value = "报表时间", paramType = "query"),
            @ApiImplicitParam(name = "yuebaoornianbao", value = "区分月报/年报  1月报/2年报", paramType = "query")
    })
    @RequestMapping(value="/exportByEntity",produces = MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.POST)
    public void exportByEntity(HttpServletRequest request, HttpServletResponse response
            ,Integer type,String baobiaoshijian,Integer yuebaoornianbao ) throws Exception {
            
		
		HSSFWorkbook wb = cblyBbService.exportByEntity(type,baobiaoshijian,yuebaoornianbao);
		
		
		 //导出
        String fileName = wb.getSheetAt(0).getSheetName();

        try {
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            //response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
            response.setHeader("content-disposition", "attachment;filename="
       				+ new String(DateUtil.getDate()) + ".xls");
            response.setContentType("application/vnd.ms-excel;charset=gb2312");
            OutputStream stream = response.getOutputStream();
            if (null != wb && null != stream) {
                wb.write(stream);// 将数据写出去  
                wb.close();
                stream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
	
}
