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

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
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.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
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.back.model.AuditBanner;
import com.cloudcross.ssp.back.service.IAuditBannerService;
import com.cloudcross.ssp.base.utils.freemarker.FreemarkerTemplateProcessor;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.consts.Status;
import com.cloudcross.ssp.common.utils.LangUtil;
import com.cloudcross.ssp.common.utils.Pager;

/**
 * 
 * @author jiapeng
 *
 */
@Controller
@RequestMapping("/back/main/audit/audit-banner")
public class AuditBannerController extends SimpleController{
	
	private static final Logger LOG = Logger
			.getLogger(AuditBannerController.class);
	
@Autowired
private IAuditBannerService iabs;
@Autowired
private FreemarkerTemplateProcessor templateProcessor;
	
@RequestMapping
public String index(Model model,
		@RequestParam HashMap<String, Object> paramMap,
		@RequestParam(defaultValue="1") int page) {	
	
	//统计所有审核素材
	int totalRow = iabs.countByParams(paramMap);
	
	Pager pager = new Pager();
	pager.setPage(page);
	pager.setTotalRow(totalRow);
	
	List<AuditBanner> auditBannerList = iabs.findByParams(paramMap, pager);
	model.addAllAttributes(paramMap);
	model.addAttribute("pager", pager);
	model.addAttribute("auditBannerList", auditBannerList);
	return page("index");
}

@RequestMapping("/list")
public String list(Model model,
		@RequestParam HashMap<String, Object> paramMap,
		@RequestParam(defaultValue="1") int page) {
	//统计所有审核素材
	int totalRow = iabs.countByParams(paramMap);
	
	Pager pager = new Pager();
	pager.setPage(page);
	pager.setTotalRow(totalRow);
	
	List<AuditBanner> auditBannerList = iabs.findByParams(paramMap, pager);
	model.addAllAttributes(paramMap);
	model.addAttribute("pager", pager);
	model.addAttribute("auditBannerList", auditBannerList);
	return page("banner-audit");
}

@RequestMapping("enable")
public @ResponseBody String enable(Long[] id) {
	LOG.debug("arg's value="+Arrays.toString(id));
	if (ArrayUtils.isNotEmpty(id)) {
		iabs.updateStatus(LangUtil.array2List(id),Status.backpass.value);
	}
	return OK;
}

@RequestMapping("disable")
public @ResponseBody String disable(@RequestParam Long[] id,String memo) {
	
	if (ArrayUtils.isNotEmpty(id)) {
		iabs.UpcheckStatus(LangUtil.array2List(id),Status.backnopass.value,memo); 
	}
	return OK ;
}

@RequestMapping("/auditBannerExcel")
public void auditBannerExcel
(HttpServletRequest request,
		HttpServletResponse response,
		@RequestParam Map<String, Object> paramMap) {
	
	List<AuditBanner> auditBannerList = iabs.findAudit(paramMap);
	paramMap.put("auditBannerList", auditBannerList);
	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("yyyy-MM-dd-HH:mm:ss");
	String fileName = "素材审核" + df2.format(new Date());
	response.setHeader("Content-Disposition", "attachment;filename="
			+ fileName + ".xls");
	// 需要对excel的列的总数进行指定
	int column = 9;
	paramMap.put("downTime", df2.format(new Date()));
	paramMap.put("column", column);
	String excelTemplate = templateProcessor.processTemplate(
			"excel/auditBanner.ftl", paramMap);
	try {
		PrintWriter out = response.getWriter();
		out.write(excelTemplate);
		out.flush();
		out.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

}
