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

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

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.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.common.utils.SqlHelper;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.Wifi;
import com.cloudcross.ssp.model.WifiGroup;
import com.cloudcross.ssp.model.WifiGroupInfo;
import com.cloudcross.ssp.model.pojo.WifiPojo;
import com.cloudcross.ssp.service.IWifiGroupInfoService;
import com.cloudcross.ssp.service.IWifiGroupService;
import com.cloudcross.ssp.service.IWifiService;
/**
 * 
 *
 * 类名称：WifiGroupController
 * 类描述：热点分组
 * 创建人：zhoujun
 * 修改人：zhoujun
 * 修改时间：2015年11月19日 下午12:26:50
 * 修改备注：
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("/operator/main/ap/apgroup")
public class ApgroupController extends SimpleController{
	private static final Logger LOG = Logger.getLogger(ApgroupController.class);
	
	@Autowired
	private IWifiGroupInfoService wifiGroupInfoService;
	@Autowired
	private IWifiGroupService wifiGroupService;
	@Autowired
	private IWifiService wifiService;
	
	/**
	 * 媒体自用分组list页数据加载
	 * list：(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * 创建人：zhoujun
	 * 修改人：zhoujun
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 *String
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/list")
	public String list(Model model,@RequestParam Map<String,Object>paramMap,@RequestParam(defaultValue="1")int page){
		//通过登录账号得到当前登录的运营商id
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		
		Pager pager = new Pager();
		//查询符合条件的总的条数
		int totalRow = wifiService.countWifiGroupByCondition(paramMap);
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<WifiPojo> wifiList = wifiService.queryWifiGroupByCondition(paramMap, pager);
		model.addAttribute("pager",pager);
		model.addAttribute("wifiList", wifiList);
		return page("list");
	}
	
	/**
	 * 到创建分组页
	 * create：(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * 创建人：zhoujun
	 * 修改人：zhoujun
	 * @param model
	 * @return
	 *String
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/create")
	public String create(Model model){
		model.addAttribute("operatorId", getLoginUser().getOperatorId());
		return page("create");
	}
	
	/**
	 * 点击分组，看到分组详情
	 * queryWifiByCondition：(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * 创建人：zhoujun
	 * 修改人：zhoujun
	 * @param page
	 * @param apgroupId
	 * @param apmac
	 * @return
	 *List<WifiPojo>
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/wifidetail")
	public List<WifiPojo> queryWifiByCondition(@RequestParam(defaultValue="1")int page,String apgroupId,String apmac){
		List<WifiPojo> wifiList = null;
		Map<String,Object>condition = new HashMap<String,Object>();
		if(null != apgroupId && !"".equals(apgroupId) ){
			condition.put("apgroupId", apgroupId);
		}
		if(null != apmac && !"".equals(apmac)){
			condition.put("apmac", apmac);
		}
		Pager pager = new Pager();
		int totalRow = wifiService.queryWifiCountByCondition(condition);
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		wifiList = wifiService.queryWifiByCondition(condition,pager);
		return wifiList;
	}
	
	/**
	 *创建分组页添加分组
	 * addGroupName：(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * 创建人：zhoujun
	 * 修改人：zhoujun
	 * @param name
	 * @return
	 *String
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/addGroupName")
	@ResponseBody
	public String  addGroupName(String name){
		String result = "";
		Boolean bool = isGroupNameExist(name);
		if(bool){//已存在
			result = "组名已存在";
		}else{
			WifiGroupInfo wifiGroupInfo = new WifiGroupInfo();
			wifiGroupInfo.setName(name);
			wifiGroupInfo.setStatus(0);//开启
			wifiGroupInfo.setUpdated(new Date());
			this.wifiGroupInfoService.add(wifiGroupInfo);
			result = "添加成功";
		}
		return result;
	}
	
	/**
	 * 判断分组名是否存在，存在返回true,不存在返回false
	 * @param name
	 * @return
	 */
	@RequestMapping("/isExist")
	public Boolean isGroupNameExist(String name){
		Boolean flag = false;
		Map<String,Object>condition = new HashMap<String,Object>();
		condition.put("name",name);
		condition.put("operatorId", getLoginUser().getOperatorId());
		List<WifiGroupInfo> list = this.wifiGroupInfoService.queryWifiGroupInfoByCondition(condition);
		if(list != null && list.size() > 0){
			flag = true;//名称已存在
		}
		return flag;
	}
	/**
	 * 分组页查询所有分组名
	 * queryWifiGroupInfoNames：(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * 创建人：zhoujun
	 * 修改人：zhoujun
	 * @param model
	 *void
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/queryGroupName")
	@ResponseBody
	public void queryWifiGroupInfoNames(Model model){
		Map<String,Object> condition = new HashMap<String,Object>();
		condition.put("operatorId", getLoginUser().getOperatorId());
		List<WifiGroupInfo> listGroupName = this.wifiGroupInfoService.findByParams(condition, null);
		model.addAttribute("listGroupName", listGroupName);
	}
	
	@RequestMapping("/update")
	public String list(Model model, @RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		
		//通过登录账号得到当前登录的运营商id
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		
		String cn = null;
		String cnCity = null;
		String searchValue = null;
		/**
		 * 下面对传递过来的几个参数进行处理
		 */
		if(null != paramMap.get("cn") && !"".equals(paramMap.get("cn"))) {
			cn = String.valueOf(paramMap.get("cn"));
		} 
		
		if(null != paramMap.get("cnCity") && !"".equals(paramMap.get("cnCity"))) {
			cnCity = String.valueOf(paramMap.get("cnCity"));
		}
		paramMap.put("cn", cn);
		paramMap.put("cnCity", cnCity);
		
		//按热点名称和详细地址搜索
		if(null != paramMap.get("searchValue") && !"".equals(paramMap.get("searchValue"))) {
				String name = (String) paramMap.get("searchValue");
				searchValue = (String) paramMap.get("searchValue");
				paramMap.put("searchValue", SqlHelper.doLike(name.trim()));
		} else {
			paramMap.put("searchValue", null);
		}
		
		int totalRow = 0;
		Pager pager = null;
		pager = new Pager();
		//查询符合条件的总的条数
		totalRow = wifiService.findWifiCountByCondition(paramMap);
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		
		List<Wifi> wifiList = wifiService.findWifiByCondition(paramMap, pager);
		List<WifiGroupInfo> listGroupName = this.wifiGroupInfoService.findByParams(null, null);
		//如果下拉框传的是默认值，则回传一个对应的值给前端
		if(null == cn) {
			cn = "省";
		}
		
		if(null == cnCity) {
			cnCity = "市";
		}
		paramMap.put("cn", cn);
		paramMap.put("cnCity", cnCity);
		paramMap.put("searchValue", searchValue);
		model.addAttribute("pager",pager);
		model.addAllAttributes(paramMap);
		model.addAttribute("wifiList", wifiList);
		model.addAttribute("listGroupName", listGroupName);
		return page("edit");
	}
	
	/**
	 * 想分组里添加热点
	 * addWifiGroup：(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * 创建人：zhoujun
	 * 修改人：zhoujun
	 * @param model
	 * @param wifiId
	 * @param groupId
	 * @return
	 *String
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping("/addWifiGroup")
	@ResponseBody
	public String addWifiGroup(Model model,long wifiId,long groupId){
		Boolean flag = false;
		String result = "";
		WifiGroup wifiGroup = new WifiGroup();
		wifiGroup.setGroupId(groupId);
		wifiGroup.setWifiId(wifiId);
		wifiGroup.setStatus(0);
		wifiGroup.setSupdated(new Date());
		flag = this.wifiGroupService.add(wifiGroup);
		if(flag){
			result = "成功";
		}else{
			result = "失败";
		}
		return result;	
	}
	
}
