package com.itjob.service;

import java.util.*;

import com.itjob.dao.BaseDao;
import com.itjob.dao.model.BindPhone;
import com.itjob.dao.model.CallBarring;

/**
 * 黑/白名单设置的业务逻辑实现类.
 */
public class CallBarringManagerImpl implements CallBarringManager {
	protected BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}
	
	/**
	 * 查询黑白名单列表
	 * @param userNumber 用户帐号
	 * @param limitType 类型(0:黑名单，1:白名单，-1：黑名名单)
	 * @return 返回某个一号通用户的黑/白名单列表
	 */
	public List getCallBarringList(String userNumber, int limitType) throws Exception {
		List<Map> returnList = new ArrayList<Map>();
		List<CallBarring> list = null;
		LinkedList<Object> params = new LinkedList<Object>();
		params.add(userNumber);
		if (limitType != -1) {
			params.add(limitType);
			list = dao.hqlQuery("from CallBarring where userNumber=? and limitType=?",
						params.toArray());
		} else {
			list = dao.hqlQuery("from CallBarring where userNumber=?",
						params.toArray());
		}
		
		CallBarring callBarring = null;
		Map<String, String> map = null;
		for (int i = 0; i < list.size(); i++) {
			map = new HashMap<String, String>();
			callBarring = (CallBarring) list.get(i);
			map.put("num", String.valueOf(i + 1));
			map.put("id", String.valueOf(callBarring.getId()));
			map.put("limitPhone", callBarring.getLimitPhone());
			map.put("description", callBarring.getDescription());
			if (callBarring.getLimitType() == 0) {
				map.put("limitType", "黑名单");
			} else {
				map.put("limitType", "白名单");
			}
			returnList.add(map);
		}
		return returnList;
	}
	
	/**
	 * 添加黑白名单
	 * @param callBarring 黑白名单对象
	 */
	public void insertCallBarring(CallBarring callBarring) throws Exception {
		dao.saveObject(callBarring);
	}
	
	/**
	 * 根据id获得黑白名单对象
	 * @param id 黑白名单对象的主键ID
	 */
	public CallBarring getCallBarring(long id) throws Exception {
		CallBarring callBarring = (CallBarring) dao.getObject(CallBarring.class, id);
		return callBarring;
	}
	
	/**
	 * 修改黑白名单对象
	 * @param callBarring 黑白名单对象
	 */
	public void updateCallBarring(CallBarring callBarring) throws Exception {
	    CallBarring callBarringPo = (CallBarring) dao.getObject(CallBarring.class, callBarring.getId());
	    callBarringPo.setLimitPhone(callBarring.getLimitPhone());
	    callBarringPo.setLimitType(callBarring.getLimitType());
	    callBarringPo.setDescription(callBarring.getDescription());
		dao.updateObject(callBarringPo);
	}
	
	/**
	 * 删除黑白名单对象
	 * @param id 黑白名单对象的主键ID
	 */
	public void deleteCallBarring(String[] idArray) throws Exception {
		CallBarring callBarring = null;
		for (int i = 0; i < idArray.length; i++) {
			callBarring = (CallBarring) dao.getObject(CallBarring.class,
						Long.parseLong(idArray[i]));
			dao.deleteObject(callBarring);
		}
	}
	
	/**
	 * 判断用户的某个黑白名单号码是否已被设置过
	 * @param userNumber 用户帐号
	 * @param limitPhone 黑白名单号码
	 * @return 已存在时返回1，否则返回0
	 */
	public int validLimitPhone(String userNumber, String limitPhone) throws Exception {
		LinkedList<Object> params = new LinkedList<Object>();
		params.add(userNumber);
		params.add(limitPhone);
		List<BindPhone> list = dao.hqlQuery("from CallBarring where userNumber=? and limitPhone=?",
				params.toArray());
		if (list != null && list.size() > 0) {
			return 1;
		} else {
			return 0;
		}
	}
}
