package cn.com.countrygarden.bi.main.controller.canteen;
import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Result;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;

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

import cn.com.countrygarden.bi.main.model.canteen.CanteenScore;
import cn.com.countrygarden.bi.main.dao.canteen.CanteenScoreMapper;
import cn.com.countrygarden.bi.main.dto.CanteenScorestatisticsDTO;
import cn.com.countrygarden.bi.main.service.canteen.CanteenScoreService;
import cn.com.countrygarden.bi.main.service.dup.impl.ExcelHelper;

/**
 * 饭堂调查得分-控制层实现
 *
 * @author 吕伟峰
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2018-08-13
 */
@RestController("CanteenScoreController")
@RequestMapping("/api/canteenScore")
public class CanteenScoreController extends BaseController{
    @Autowired
    private CanteenScoreService canteenScoreService;

    @Autowired
    private CanteenScoreMapper canteenScoreMapper;

    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    public Result list(CanteenScore canteenScore,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request){
        return  canteenScoreService.queryPageList(canteenScore,page,size);
    }
    
    //调查统计分数1
    @GetMapping(value = "/getcascore", produces = "application/json;charset=utf-8")
    public Result getcascore(CanteenScorestatisticsDTO cantesore,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request){
        return  canteenScoreService.getcascore(cantesore,page,size);
    }
    
    //查询身份证是否答过体
	 	@GetMapping(value = "/bdpsndoccadati", produces = "application/json;charset=utf-8")
	 	public Result bdpsndoccadati(@RequestParam(value="bdid",required=true)String bdid,
	 			@RequestParam int canteeninsdid,
	 			HttpServletRequest request) {
	 			
	 		return  canteenScoreService.bdpsndoccadati(bdid,canteeninsdid);
	 	}
	 	//重复提交，删除历史数据
	 @PostMapping(value="/updatescore")
	 public Result updatescore(@RequestParam(value="bdid",required=true)String bdid,
		 			@RequestParam int canteeninsdid){
	         return  canteenScoreService.updatescore(bdid,canteeninsdid);
	  }
	 
	 //查看详情
	@GetMapping(value = "/viewdetailsv", produces = "application/json;charset=utf-8")
	public Result viewdetailsv(@RequestParam(value="rqid",required=true)Integer rqid,
 			@RequestParam(value="ciid",required=true) Integer ciid) {
		
		 return  canteenScoreService.viewdetailsv(rqid,ciid);
	}

    @GetMapping(value = "/one", produces = "application/json;charset=utf-8")
    public Result queryById(@RequestParam(value = "id", required = true)Integer id,
                            HttpServletRequest request){
        return  canteenScoreService.queryById(id);
    }

    @PostMapping(value = "/add")
    public Result add(String answer,
                      HttpServletRequest request){
        return  canteenScoreService.add(answer,getAccessToken(request));
    }

    @PutMapping(value = "/edit")
    public Result edit(CanteenScore canteenScore, HttpServletRequest request){
        return  canteenScoreService.edit(canteenScore,getAccessToken(request));
    }

    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)Integer id){
        return  canteenScoreService.del(id);
    }
       
  //导出数据
    @RequestMapping(value = "/excantensurvey", produces = "multipart/form-data",  method = RequestMethod.GET)
    public void excantensurvey(CanteenScorestatisticsDTO cantesore,
    		HttpServletRequest request, HttpServletResponse response) throws ParseException {
    	
    	Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        ExcelHelper helper = new ExcelHelper();
        helper.excanteworkbookscore(workbook,sheet);//创建表头
       
        helper.excantesocxlsa(canteenScoreMapper.getcascore(cantesore),workbook,sheet);
        
        response.setContentType("multipart/form-data");
        String fileName = "problemexport.xlsx";
        try {
            fileName = URLEncoder.encode("饭堂满意度调查统计.xlsx","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
        OutputStream outputStream =null;
        try{
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (Exception ex){

        } finally {
            try{
                workbook.close();
            } catch (Exception ex){

            }
        }
    	
    }
    
    //区域统计图
    @GetMapping(value = "/canteenareatu", produces = "application/json;charset=utf-8")
    public Result canteenareatu(@RequestParam(value="areaId",required=false)String areaId,                 
                       HttpServletRequest request){
    	
        return  canteenScoreService.canteenareatu(areaId);
    }
    
    //项目统计图
    @GetMapping(value = "/canteenprojectu", produces = "application/json;charset=utf-8")
    public Result canteenprojectu(@RequestParam(value="areaId",required=false)String areaId,                   
                       HttpServletRequest request){
        return  canteenScoreService.canteenprojectu(areaId);
    }
    
    


}