package com.cloudcross.ssp.web.back.main.audit;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.model.Audit;
import com.cloudcross.ssp.service.IAuditService;
import com.cloudcross.ssp.base.utils.freemarker.FreemarkerTemplateProcessor;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;

@Controller("back.auditController")
@RequestMapping("/back/main/audit/audit")
public class AuditController extends SimpleController{
	
	@Autowired
	private IAuditService auditService;
	@Autowired
	private FreemarkerTemplateProcessor templateProcessor;
	/**
	 * @return 返回到广告主资质审核第一页。
	 */
	@RequestMapping
	public String index(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue="1") int page) {	
		int totalRow = auditService.countByParams(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		System.out.println("luo0:" + paramMap);
		pager.setTotalRow(totalRow);
		List<Audit> auditList = auditService.findByParams(paramMap, pager);
		
		System.out.println(auditList.toString());
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("auditList", auditList);
		return page("index");
	}
	
	@RequestMapping("/list")
	public String list(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue="1") int page) {
		//按状态搜索用
		if(paramMap.get("checked").equals("")) { 
			paramMap.put("checkStatus", null);
		}
		if(paramMap.get("checked").equals("1")) { 
			paramMap.put("checkStatus", 1);
		}
		if(paramMap.get("checked").equals("2")) { 
			paramMap.put("checkStatus", -1);
		}
		if(paramMap.get("checked").equals("0")) { 
			paramMap.put("checkStatus", 0);
		}
		System.out.println("luo1:" + paramMap);
		System.out.println("liu"+paramMap.toString());
		int totalRow = auditService.countByParams(paramMap);
		
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<Audit> auditList = auditService.findByParams(paramMap, pager);
		
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("auditList", auditList);
		for(Audit audit:auditList){
			System.out.println(audit);
		}
		return page("list");
	}
	/**
	 * 用来查看资质审核
	 * @param model
	 * @param paramMap
	 * @param auditId
	 * @return
	 */
	@RequestMapping("/details")
	public String update(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam Long auditId) {
		Audit audit = auditService.findById(auditId);
		System.out.println("luo2:" + paramMap);
		model.addAllAttributes(paramMap);
		model.addAttribute("audit", audit);
		model.addAttribute("auditId", auditId);
		return page("details");
	}
	
	
	@RequestMapping("check")
	public @ResponseBody String enable(@RequestParam Map<String, Object> paramMap) {
		if(paramMap.get("checkMemo")==null || paramMap.get("checkMemo")==""){
			paramMap.put("checkMemo", "审核通过");
		}
		System.out.println(paramMap.toString());
			auditService.updateCheckStatus(paramMap);
		return OK;
	}
	//导出audit列表页Excel用
	
	@RequestMapping("/exportDataReportExcel")
	public void exportDataReportExcel(Model model,HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam Map<String, Object> paramMap) {
			//model.addAttribute("advertiserId", advertiserId);
		//按状态搜索用
		System.out.println("luo3:" + paramMap);
		if(paramMap.get("checked").equals("")) { 
			paramMap.put("checkStatus", null);
		}
		if(paramMap.get("checked").equals("1")) { 
			paramMap.put("checkStatus", 1);
		}
		if(paramMap.get("checked").equals("2")) { 
			paramMap.put("checkStatus", -1);
		}
		if(paramMap.get("checked").equals("0")) { 
			paramMap.put("checkStatus", 0);
		}
		/**
		 * 参数和list里要一样
		 */
		List<Audit> reportList = auditService.findDeliverEffectAnalysis(paramMap);
		System.out.println("aaa"+reportList.toString());
		paramMap.put("dataList", reportList);
		response.reset();
		// Content-Type:application/vnd.ms-excel;charset=utf8或者text/xml;charset=utf8
		response.setContentType("application/vnd.ms-excel;charset=utf8");
		// 设置excel文件名称
		SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "Report" + df2.format(new Date());
		response.setHeader("Content-Disposition", "attachment;filename="
				+ fileName + ".xls");
		// 需要对excel的列和行的总数进行指定
		int column = 9;
		paramMap.put("dataSize", reportList.size());
		paramMap.put("column", column);
		paramMap.put("advertiserName", "广告主列表");
		paramMap.put("startDate", "2015-04-01");
		paramMap.put("endDate", "2015-06-01");
		String excelOrder = templateProcessor.processTemplate("excel/auditReport.ftl", paramMap);
		try {
			PrintWriter out = response.getWriter();
			out.write(excelOrder);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
			
//	@RequestMapping("disable")
//	public @ResponseBody String disable(Long[] id) {
//		if (ArrayUtils.isNotEmpty(id)) {
//			auditService.updateStatus(LangUtil.array2List(id),CheckStatus.fail.value); 
//		}
//		
//		
//		return OK;
//	}
}