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

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
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.web.SimpleController;
import com.cloudcross.ssp.common.consts.CheckStatus;
import com.cloudcross.ssp.common.utils.LangUtil;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.Audit;
import com.cloudcross.ssp.service.IAuditService;

@Controller("operator.auditController")
@RequestMapping("/operator/main/ad/audit")
public class AuditController extends SimpleController{
	
	@Autowired
	private IAuditService auditService;
	
	/**
	 * @return 返回到广告主资质审核第一页。
	 */
	@RequestMapping
	public String index() {	
		return redirect(page("list"));
	}
	
	@RequestMapping("/list")
	public String list(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue="2") int checkStatus,
			@RequestParam(defaultValue="1") int page) {
		//按状态搜索用
				if (checkStatus != 2){
						paramMap.put("checkStatus", new Integer(checkStatus));
				}else {
					paramMap.put("checkStatus", null);
				}
		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);
		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);
		model.addAllAttributes(paramMap);
		model.addAttribute("audit", audit);
		model.addAttribute("id", auditId);
		return page("details");
	}
	
	
	@RequestMapping("enable")
	public @ResponseBody String enable(Long[] id) {
		if (ArrayUtils.isNotEmpty(id)) {
			auditService.updateStatus(LangUtil.array2List(id),CheckStatus.enable.value);
		}
		return OK;
	}
	
	@RequestMapping("disable")
	public @ResponseBody String disable(Long[] id) {
		if (ArrayUtils.isNotEmpty(id)) {
			auditService.updateStatus(LangUtil.array2List(id),CheckStatus.disable.value); 
		}
		return OK;
	}
}