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.Read;
import com.cloudcross.ssp.common.utils.LangUtil;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.Message;
import com.cloudcross.ssp.service.IMessageService;

@Controller("operator.messageController")
@RequestMapping("/operator/main/ad/message")
public class MessageController extends SimpleController{
	
	@Autowired
	private IMessageService messageService;
	
	/**
	 * @return 返回到广告主资质审核第一页。
	 */
	@RequestMapping
	public String index() {	
		return redirect(page("list"));
	}
	
	@RequestMapping("/list")
	public String list(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue="4") int type,
			@RequestParam(defaultValue="1") int page) {
		//按信息类型搜索用
				if (type!= 4){
						paramMap.put("type", new Integer(type));
				}else {
					paramMap.put("type", null);
				}
		int totalRow = messageService.countByParams(paramMap);
		
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<Message> messageList = messageService.findByParams(paramMap, pager);
		
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("messageList", messageList);
		return page("list");
	}
	
	
	
	@RequestMapping("enable")
	public @ResponseBody String enable(Long[] id) {
		if (ArrayUtils.isNotEmpty(id)) {
			messageService.updateRead(LangUtil.array2List(id),Read.enable.value);
		}
		return OK;
	}
	
	@RequestMapping("disable")
	public @ResponseBody String disable(Long[] id) {
		if (ArrayUtils.isNotEmpty(id)) {
			messageService.updateRead(LangUtil.array2List(id),Read.disable.value); 
		}
		return OK;
	}
}