package com.cloudcross.ssp.web.back.main.dsp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.internet.HeaderTokenizer.Token;
import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
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.Common;
import com.cloudcross.ssp.common.utils.TokenUtil;
import com.cloudcross.ssp.model.DspAgent;
import com.cloudcross.ssp.model.Log;
import com.cloudcross.ssp.service.IDspAgentService;
import com.cloudcross.ssp.service.ILogService;

/**
 * 
 * 类名称：DspAccountController
 * 类描述：dsp账户
 * 创建人：rongying
 * 创建时间：2015.12.16
 *
 */

@Controller("back.dspAgentController")
@RequestMapping("/back/main/dsp/dsp-agent")
public class DspAgentController extends SimpleController{
	
	@SuppressWarnings("unused")
	private static final Logger LOG = Logger.getLogger(DspAgentController.class);
	
	@Autowired
	private IDspAgentService dspAgentService;
	
	@Autowired
	private ILogService logService;
	
	@RequestMapping("/list")
	public String list(Model model,@RequestParam Map<String,Object> paramMap) {
		model.addAllAttributes(paramMap);
		List<DspAgent> dspAgentList = new ArrayList<DspAgent>();
		dspAgentList = dspAgentService.getAllDspName(paramMap);
		model.addAttribute("dspAgentList", dspAgentList);
		return page("list");
	}
	
	/**
	 * 根据id查找dsp账户对象
	 * 
	 * @param dspId
	 * 			dsp账户id
	 * @return
	 */
	@RequestMapping("/getDspAgentById")
	public @ResponseBody DspAgent getDspAgentById(Model model,Long dspId){
		DspAgent dspAgent = new DspAgent();
		dspAgent = dspAgentService.getDspAgentById(dspId);
		return dspAgent;
	}
	
	/**
	 * 添加dsp账户
	 * @param paramMap 参数，json数组
	 * @return
	 */
	@RequestMapping("/save")
	public String save(Model model, HttpServletRequest request, @ModelAttribute("form") DspAgent dspAgent){
		if ((dspAgent.getId()) == 0) {
			dspAgent.setToken(TokenUtil.getToken());//获取token令牌
			dspAgentService.addDspAgent(dspAgent);
		} else {
			dspAgentService.editDspAgent(dspAgent);
		}
		Log log = new Log();
		//对应模块
		log.setModule("DspAgent");
		//对应表名
		log.setTableName("t_dsp_agent");
		if ((dspAgent.getId()) == 0) {
			Long id = this.getDataMaxId(dspAgent.getName(), null);
        	//增加对应的id
			log.setRecordId(id);
			//对应操作类型
			log.setActionType("新增");
			//对应的操作
			log.setAction("新增了一个dsp账户：" + dspAgent.getName());
		}else{
			//增加对应的id
			log.setRecordId(dspAgent.getId());
			//对应操作类型
			log.setActionType("编辑");
			//对应的操作
			log.setAction("编辑原来的dsp账户为：" + dspAgent.getName());
		}
		//下面这几个字段目前后端数据没有得到。所以仅作测试用！
		log.setActionTime("100");
		log.setAccountId(new Long(getLoginUser().getId()));
		log.setUserIp(Common.toIpAddr(request));
		log.setAccountName(getLoginUser().getAccountName());
		logService.add(log);
		// 跳转到管理页面。
		return redirect(page("list"));
	}
	
	/**
	 * 根据id查找dsp账户对象
	 * 
	 * @param dspId
	 * 			dsp账户id
	 * @return
	 */
	@RequestMapping("/isRepeat")
	public @ResponseBody boolean isRepeat(String name, Long dspId){
		boolean isRepeat = false;
		Long id = this.getDataMaxId(name, dspId);
		if(id != null){
			isRepeat = true;
		}
		return isRepeat;
	}
	
	/**
	 * 获取最大的id，根据条件查询
	 * @param name
	 * @param dspId
	 * @return 返回查询的值
	 */
	private Long getDataMaxId(String name, Long dspId){
		Map<String,Object> repeatmap = new HashMap<String,Object>();
		repeatmap.put("name", name);
		repeatmap.put("dspId", dspId);
    	Long id = dspAgentService.selectMaxId(repeatmap);
		return id;
	}
}
