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

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

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.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.CodeConvert;
import com.cloudcross.ssp.service.ICodeConvertService;


@Controller
@RequestMapping("/operator/main/code-convert")
public  class CodeConvertController extends SimpleController {
	private static final Logger LOG = Logger.getLogger(CodeConvertController.class);
	
	@Autowired
	protected ICodeConvertService codeConvertService;
	
	/**
	 * @return 返回到广告主管理第一页。
	 */
	@RequestMapping
	public String index() {
		return redirect(page("list"));
	}
	
	@RequestMapping("/list")
	public String list(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue="1") int page) {
System.out.println(paramMap.size()+"yyyy");
		
		// 搜索条件：账户类型、账户及姓名
		// 查询总行数
	int totalRow = codeConvertService.countByParamsOperator(paramMap);
	System.out.println("tttttttt"+totalRow);
	Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		
		List<CodeConvert> codeConvertList = codeConvertService.findByParamsOperator(paramMap, pager);
		
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("codeConvertList", codeConvertList);
		return page("list");
	}
	
	
	
	
	/**
	 * @return 创建广告主账号。
	 */
	@RequestMapping("/create")
	public String create() {
		
		return page("create");
	}
	
//	/**
//	 * @param id
//	 *            广告主ID
//	 * @return 广告主编辑页面。
//	 */
	@RequestMapping("/edit/{id}")
	public String edit(Model model, @PathVariable 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"));
	}
		
}

