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

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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.utils.Pager;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.CodeClass;
import com.cloudcross.ssp.service.ICodeClassService;


@Controller("advertiser.codeClassController")
@RequestMapping("/advertiser/main/code-class")
public  class CodeClassController extends SimpleController {
	@Autowired
	protected ICodeClassService codeClassService;
	
	/**
	 * @return 返回到广告主管理第一页。
	 */
	@RequestMapping
	public String index() {
		return redirect(page("list"));
	}
	
	@RequestMapping("/list")
	public String list(Model model,HttpServletRequest request,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue="1") int page) {
		
		Object role = request.getSession().getAttribute("role");
		Object advertiserName = request.getSession().getAttribute("advertiserName");
		Account users = (Account) request.getSession().getAttribute("userSession");
		if(role.equals("advertiser")){
			paramMap.put("advertiserId", users.getAdvertiserId());
			paramMap.put("selectedAdvertiserName", advertiserName);
		}	
			// 查询总行数
		int totalRow = codeClassService.countByParams(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
	
		List<CodeClass> codeClassList = codeClassService.findByParams(paramMap, pager);
		if(!paramMap.containsKey("advertiserId") && role.equals("agent")){
			model.addAttribute("advertiserId", -1);
			model.addAttribute("selectedAdvertiserName", "请选择广告主");
		}
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("codeClassList", codeClassList);
		return page("list");
	}
	
	
	
	
	/**
	 * @return 创建广告主账号。
	 */
	@RequestMapping("/create")
	public String create() {
		return page("create");
	}
	
	/**
	 * @param id
	 *            广告主ID
	 * @return 广告主编辑页面。
	 */
	@RequestMapping("/edit")
	public String edit(Model model, @RequestParam long id) {
		// 根据id从数据库中查询代码分类对象。
		CodeClass codeClass = codeClassService.findById(id);
		System.out.println("aaa:" + codeClass);
		model.addAttribute("codeClass", codeClass);
		return page("edit");
	}


	/**
	 * 在保存对象前要验证数据是否合法。
	 * 
	 * @return 保存成功返回到广告主管理页面。
	 */
@RequestMapping("/save")
public String save(
			@ModelAttribute("form") CodeClass codeClass) {
		// 校验参数

   	
   	if (codeClass.getId() == null){
   		codeClassService.add(codeClass);
   	}else{
   		codeClassService.edit(codeClass);
   	}
		return redirect(page("list"));
	}
		
	@RequestMapping("/getcode")
	public @ResponseBody String getCode(
				@RequestParam long id,
				@RequestParam int type
				) {
	   	return codeClassService.getCode(id,type);
	}
 
}

