package com.cloudcross.ssp.web.operator.main;


import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
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.model.Account;
import com.cloudcross.ssp.model.AuditBanner;
import com.cloudcross.ssp.base.utils.freemarker.FreemarkerTemplateProcessor;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.Banner;
import com.cloudcross.ssp.service.IBannerService;;

/**
 * 素材审核功能相关页面。
 * 
 * @author Wanfu
 */
@Controller("operator.bannerAuditController")
@RequestMapping("/operator/main/banner-audit")
public class BannerAuditController extends SimpleController {
	private static final Logger LOG = Logger
			.getLogger(BannerAuditController.class);

	@Autowired
	private IBannerService bannerService;
	
	/*for export excel*/
	@Autowired
	private FreemarkerTemplateProcessor templateProcessor;

	/**
	 * /main/banner-audit地址默认方法
	 * 传入运营商id
	 * @return 成功则返回index页面
	 */
	@RequestMapping
	public String index(Model model,
			@RequestParam Map<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		
		Account account = getLoginUser();
		Long operatorId = getLoginUser().getOperatorId();
		paramMap.put("operatorId", operatorId);
		//查询符合条件的素材个数
		int totalRow = bannerService.countByOperatorIdandParams(operatorId,paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		//获取创意的列表
		List<AuditBanner> bannerList = bannerService.findByOperatorIdandParams(operatorId,paramMap,pager);
		model.addAttribute("bannerList", bannerList);
		model.addAttribute("operatorId", operatorId);
		model.addAttribute("pager", pager);
		return page("index");
	}
	
	/**
	 * 显示list方法。
	 * 根据运营商id，搜索条件和页码显示相应的素材。
	 * @return 成功则返回list页面
	 */
	@RequestMapping("/list")
	public String list(Model model, 
			@RequestParam Map<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		// 查询符合条件的素材个数
		/**
		 * 如果云联审核不通过，则媒体不需要审核，就是不通过
		 * 如果是未审核状态，媒体端是看不到信息的
		 */
		//Integer operatorChecked = Integer.parseInt((String)paramMap.get("operatorChecked"));
		if((String)paramMap.get("operatorChecked") == null||(String)paramMap.get("operatorChecked")==""){
			paramMap.put("operatorChecked", null);
			
		}
		
		Long operatorId = getLoginUser().getOperatorId();
		paramMap.put("operatorId", operatorId);
		model.addAttribute("operatorId", operatorId);
		int totalRow = bannerService.countByOperatorIdandParams(operatorId,paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		//获取创意的列表
		List<AuditBanner> bannerList = bannerService.findByOperatorIdandParams(
				operatorId,paramMap,pager);
		model.addAttribute("pager", pager);
		model.addAttribute("bannerList", bannerList);
		return page("list");
	}

	/**
	 * 单个或者批量审核通过功能。
	 * 通过将表t_banner 字段checked设为3
	 * @return 成功则返回"ok"
	 */
	@RequestMapping("/pass")
	public @ResponseBody String pass(Long[] id) {
		//输出log信息，控制台打印出id的值
		LOG.debug("Args's value: id=" + Arrays.toString(id));
		Long operatorId = getLoginUser().getOperatorId();
		//将id转为List<Long>类型传入updateStatus函数
		if (ArrayUtils.isNotEmpty(id)) {
			for(long i:id){
			AuditBanner auditBanner = bannerService.findByIdOperator(i);
				if(auditBanner.getOperatorChecked()==-1){
					bannerService.addOperatorBanner(i,1,operatorId);
				}else{
					bannerService.updataOperatorBanner(i,1,operatorId);
				}
			}
		}
		return OK;
	}

	/**
	 * 单个或者批量审核通过功能。
	 * 通过将表t_banner 字段checked设为3
	 * @return 成功则返回"ok"
	 */
	@RequestMapping("/reject")
	public @ResponseBody String reject(Long[] id) {
		//输出log信息，控制台打印出id的值
		LOG.debug("Args's value: id=" + Arrays.toString(id));
		Long operatorId = getLoginUser().getOperatorId();
		//将id转为List<Long>类型传入updateStatus函数
		if (ArrayUtils.isNotEmpty(id)) {
			for(long i:id){
				AuditBanner auditBanner = bannerService.findByIdOperator(i);
				if(auditBanner.getOperatorChecked()==-1){
					bannerService.addOperatorBanner(i,2,operatorId);
				}else{
					bannerService.updataOperatorBanner(i,2,operatorId);
				}
				}
		}
		return OK;
	}
	
	/**
	 * 导出excel
	 * 
	 * @param request
	 * @param response
	 * @param paramMap
	 *           
	 */
	@RequestMapping("/exportExcel")
	public void exportOtherReportExcel(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam Map<String, Object> paramMap) {
		
		//获取创意的列表
		Long operatorId = getLoginUser().getOperatorId();
		paramMap.put("operatorId", operatorId);
		List<AuditBanner> bannerList = bannerService.findByOperatorIdandParams(
				operatorId, paramMap, null);
		paramMap.put("bannerList", bannerList);
		for(AuditBanner br: bannerList) {
			System.out.println("luo:" + br);
		}
		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 = 10;
		paramMap.put("column", column);	
		System.out.println("aaaa: " + paramMap);
		String excelTemplate = templateProcessor.processTemplate(
				"excel/banner-audit.ftl", paramMap);
		try {
			PrintWriter out = response.getWriter();
			out.write(excelTemplate);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
