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


import org.apache.commons.lang.StringUtils;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.ContractStatistical;
import cn.com.countrygarden.bi.main.model.review.ReviewEvaluate;
import cn.com.countrygarden.bi.main.model.review.ReviewScore;
import cn.com.countrygarden.bi.main.dao.review.ReviewScoreMapper;
import cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO;
import cn.com.countrygarden.bi.main.service.dup.impl.ExcelHelper;
import cn.com.countrygarden.bi.main.service.review.ReviewQuestionnaireProblemService;
import cn.com.countrygarden.bi.main.service.review.ReviewScoreService;
import org.apache.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import freemarker.core.ReturnInstruction.Return;

/**
 * 评选结果-业务逻辑层实现
 *
 * @author 汤婷
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2018-06-25
 */
@RestController("ReviewScoreController")
@RequestMapping("/api/reviewScore")
public class ReviewScoreController extends BaseController{
    @Autowired
    private ReviewScoreService reviewScoreService;

    @Autowired
    private ReviewQuestionnaireProblemService reviewquestionnaireProblemService;
    
    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    public Result list(ReviewScore reviewScore,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request){
        return  reviewScoreService.queryPageList(reviewScore,page,size);
    }

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

    @PutMapping(value = "/edit")
    public Result edit(ReviewScore reviewScore, HttpServletRequest request){
    	    	
        return  reviewScoreService.edit(reviewScore,getAccessToken(request));
    }
    
    @PostMapping(value="/delsoce")
    public Result delsoce(Integer id,Integer questionnaireId,Integer judgeId,HttpServletRequest request){
        return  reviewScoreService.delsoce(judgeId);
    }
    
    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)Integer id){
        return  reviewScoreService.del(id);
    }
     //调查详细
    @GetMapping(value = "/scoreproblem", produces = "application/json;charset=utf-8")
    public Result scoreproblem(ReviewScoreProblemDTO scoreproblem,@RequestParam(value = "size", required = false, defaultValue = "3") int size,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
            HttpServletRequest request) {
    	
              //   System.out.println(reviewScoreService.scoreproblem(scoreproblem, page, size));
    	return reviewScoreService.scoreproblem(scoreproblem, page, size);
    }
    
    @GetMapping(value = "/scoreraid", produces = "application/json;charset=utf-8")
    public Result scoreraid(ReviewScoreProblemDTO scoreproblem,
			       HttpServletRequest request) {
    	

    	return reviewScoreService.scoreraid(scoreproblem);
    }
    
    //打印界面数据
    @GetMapping(value = "/scoredtos", produces = "application/json;charset=utf-8")
    public Result scoredtos(@RequestParam(value="investigatedid",required=true)Integer investigatedid,
    		@RequestParam(value="gradeid",required=true)Integer gradeid,
            HttpServletRequest request) {
    	//hasRole(roleCode)
    	return reviewScoreService.scoredtos(investigatedid,gradeid);
    }
    
    @GetMapping(value = "/scoredtost", produces = "application/json;charset=utf-8")
    public Result scoredtost(@RequestParam(value="investigatedid",required=true)Integer investigatedid,
    		@RequestParam(value="gradeid",required=true)Integer gradeid,
            HttpServletRequest request) {
    	//hasRole(roleCode)
    	return reviewScoreService.scoredtost(investigatedid,gradeid);
    }
    
    @GetMapping(value = "/scoredtosx", produces = "application/json;charset=utf-8")
    public Result scoredtostx(@RequestParam(value="investigatedid",required=true)Integer investigatedid,
    		@RequestParam(value="gradeid",required=true)Integer gradeid,
            HttpServletRequest request) {
    	//hasRole(roleCode)
    	return reviewScoreService.scoredtosx(investigatedid,gradeid);
    }
    
     //综合评价
    @GetMapping(value = "/evaluationproblem", produces = "application/json;charset=utf-8")
    public Result evaluationproblem(@RequestParam(value="investigatedid",required=true)Integer investigatedid,
            HttpServletRequest request) {   	    	
    	
    	return reviewScoreService.evaluationproblem(investigatedid);
    }
    
    @GetMapping(value = "/reviewevaluate", produces = "application/json;charset=utf-8")
    public Result reviewevaluate(@RequestParam(value="investigatedid",required=true)Integer investigatedid,
    		@RequestParam(value="questionnaireid",required=true)Integer questionnaireid,
            HttpServletRequest request) {   	    	
    	
    	return reviewScoreService.reviewevaluate(investigatedid,questionnaireid);
    }
    
    @PutMapping(value = "/updateedit")
    public Result updateedit(ReviewEvaluate rewet,HttpServletRequest request){
    	 return  reviewScoreService.updateedit(rewet,getAccessToken(request));
    	
       
    }
    
    @PostMapping(value = "/updataflag")
    public Result updataflag(@RequestParam(value="id",required=true)Integer id,@RequestParam(value="questionnaireId",required=true)Integer questionnaireId,
    		@RequestParam(value="judgeId",required=true)Integer judgeId
    		,HttpServletRequest request) {
    	
    	//return reviewScoreService.updataflag(id, judgeId, questionnaireId);
    	 return  reviewScoreService.delsoce(judgeId);
    }
    
    //导出数据
    @RequestMapping(value = "/evaluationproblemexport", produces = "multipart/form-data",  method = RequestMethod.GET)
    public void evaluationproblemexport(@RequestParam(value="investigatedid",required=true)Integer investigatedid, 
    		@RequestParam(value="questionnaireId",required=true)Integer questionnaireId,
    		HttpServletRequest request, HttpServletResponse response) throws ParseException {
    	
    	Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        ExcelHelper helper = new ExcelHelper();
        //helper.createreviewscore(workbook,sheet);//创建表头
        //helper.createproblemexport(reviewScoreService.evaluationproblemexport(investigatedid),workbook,sheet);
        helper.createproblemexport(reviewScoreService.reviewjudgegrade(investigatedid,questionnaireId),
        		reviewScoreService.reviewrinqu(investigatedid),reviewquestionnaireProblemService.questionoblemsex(questionnaireId,1),
        		reviewScoreService.scoreproblemexcl(investigatedid,questionnaireId),
        				reviewquestionnaireProblemService.questionoblemswenex(questionnaireId,1),reviewScoreService.scoreproblemwenexcl(investigatedid,questionnaireId)
        		,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){

            }
        }
    	
    }
    




}