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.model.Account;
import com.cloudcross.ssp.model.CodeConvert;
import com.cloudcross.ssp.service.ICodeConvertService;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;


@Controller("advertiser.codeConvertController")
@RequestMapping("/advertiser/main/code-convert")
public  class CodeConvertController extends SimpleController {
	@Autowired
	protected ICodeConvertService codeConvertService;
	
	/**
	 * @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 = codeConvertService.countByParams(paramMap);
		System.out.println("tttttttt"+totalRow);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		
		List<CodeConvert> codeConvertList = codeConvertService.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("codeConvertList", codeConvertList);
		return page("list");
	}
	
	
	
	
	/**
	 * @return 创建广告主账号。
	 */
	@RequestMapping("/create")
	public String create() {
		
		return page("create");
	}
	

	@RequestMapping("/edit")
	public String edit(Model model, @RequestParam long id) {
		// 根据id从数据库中查询转化代码对象。
		CodeConvert codeConvert = codeConvertService.findById(id);
		System.out.println("aaa:" + codeConvert);
		model.addAttribute("codeConvert", codeConvert);
		return page("edit");
	}

@RequestMapping("/save")
public String save(
			@ModelAttribute("form") CodeConvert codeConvert) {
		
   	if (codeConvert.getId() == null){
   		codeConvertService.add(codeConvert);
   	}else{
   		codeConvertService.edit(codeConvert);
   	}
		return redirect(page("list"));
	}

	@RequestMapping("/getcode")
	public @ResponseBody String getCode(
				@RequestParam long id,
				@RequestParam int type
				) {
	   	return codeConvertService.getCode(id,type);
	}
}

