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

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.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;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.DspAgent;
import com.cloudcross.ssp.service.IDspAgentService;
import com.cloudcross.ssp.service.IDspBannerService;
import com.cloudcross.ssp.web.back.main.audit.AuditBannerController;


/**
 * 运营商素材审核功能
 * 
 * */

@Controller
@RequestMapping("/operator/main/dsp/audit-dsp")
public class AuditDspController extends SimpleController{
	@Autowired
	IDspBannerService dspBannerService;
	@Autowired
	IDspAgentService dspAgentService;
	@Autowired
	private FreemarkerTemplateProcessor templateProcessor;
	
	private static final Logger LOG = Logger
			.getLogger(AuditBannerController.class);

	@RequestMapping("/list")
	public String list(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue="1") int page) {
		Map<String,Object> showMap = null;
		String path;
		String bannerName;
		
		Account account = getLoginUser();
		Long operatorId = getLoginUser().getOperatorId();
		paramMap.put("operatorId", operatorId);
		
		//统计所有审核素材
		int totalRow = dspBannerService.countByParamsOperator(operatorId,paramMap);
		List<DspAgent> dspAgentList = dspAgentService.findAllAgent();
		
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<Map<String, Object>> auditList = dspBannerService.findByParamsOpeator(operatorId,paramMap,pager);
		for(int i=0;i<auditList.size();i++){
			showMap = auditList.get(i);
			path = (String)showMap.get("path");
			bannerName = path.substring(path.lastIndexOf("\\")+1);
			showMap.put("bannerName", bannerName);
			showMap.put("name",showMap.get("name"));
			auditList.set(i, showMap);
		}
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("auditList", auditList);
		model.addAttribute("dspAgentList",dspAgentList);
		return page("list");
	}
	
	
	@RequestMapping("/enable")
	public @ResponseBody String enable(Long[] id) {
		LOG.debug("arg's value="+Arrays.toString(id));
		Long operatorId = getLoginUser().getOperatorId();
		if (ArrayUtils.isNotEmpty(id)) {
			for(long i:id){
				int checked = dspBannerService.getState(operatorId,i);
				if(checked ==-1){
					dspBannerService.addBannerOperator(operatorId,i);
				}else{
					dspBannerService.updateOperatorPass(i,Status.backpass.value);
				}
			}
			
		}
		return OK;
	}

	@RequestMapping("/disable")
	public @ResponseBody String disable(Long[] id,String memo) {
		Long operatorId = getLoginUser().getOperatorId();
		if (ArrayUtils.isNotEmpty(id)) {
			for(long i:id){
				int checked = dspBannerService.getState(operatorId,i);	
				if(checked !=-1){
					dspBannerService.updateOperatorNoPass(i,Status.backnopass.value,memo);
				}
			}
			
		}
		return OK ;
	}
	
	@RequestMapping("/auditDspExcel")
	public void auditBannerExcel
	(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam Map<String, Object> paramMap) {
		Map<String,Object> showMap = null;
		String path;
		String type;
		String bannerName;
		List<Map<String, Object>> auditList = dspBannerService.findByParamMap(paramMap);
		for(int i=0;i<auditList.size();i++){
			showMap = auditList.get(i);
			path = (String)showMap.get("path");
			bannerName = path.substring(path.lastIndexOf("\\")+1);
			showMap.put("bannerName", bannerName);
			showMap.put("name",showMap.get("name"));
			auditList.set(i, showMap);
		}
		paramMap.put("auditList", auditList);
		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 = "DSP素材审核" + df2.format(new Date());
		response.setHeader("Content-Disposition", "attachment;filename="
				+ fileName + ".xls");
		// 需要对excel的列的总数进行指定
		int column = 10;
		paramMap.put("downTime", df2.format(new Date()));
		paramMap.put("column", column);
		paramMap.put("dataSize", (auditList.size() + 100));
		String excelTemplate = templateProcessor.processTemplate(
				"excel/auditOperatorDsp.ftl", paramMap);
		try {
			PrintWriter out = response.getWriter();
			out.write(excelTemplate);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
