package com.itjob.service;

import java.util.*;

import org.apache.commons.beanutils.BeanUtils;

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

/**
 * 绑定电话设置的业务逻辑实现类.
 */
public class BindPhoneManagerImpl implements BindPhoneManager {
	protected BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}
	
	/**
	 * 获得某个一号通用户的绑定号码列表
	 * @param userNumber 用户帐号
	 * @return 返回绑定号码列表信息
	 */
	public List getBindPhoneList(String userNumber) throws Exception {
		List<Map> returnList = new ArrayList<Map>();
		LinkedList<Object> params = new LinkedList<Object>();
		params.add(userNumber);
		List<BindPhone> list = dao.hqlQuery("from BindPhone where userNumber=? order by priOrder asc",
				params.toArray());
		
		BindPhone bindPhone = null;
		Map<String, String> map = null;
		for (int i = 0; i < list.size(); i++) {
			map = new HashMap<String, String>();
			bindPhone = (BindPhone) list.get(i);
            map.put("num", String.valueOf(i + 1));
            map.put("id", String.valueOf(bindPhone.getId()));
            map.put("bindPhone", bindPhone.getBindPhone());
            map.put("priOrder", String.valueOf(bindPhone.getPriOrder()));
            if (bindPhone.getPhoneState() == 1) {
            	map.put("phoneState", "激活");
            } else {
            	map.put("phoneState", "未激活");
            }
            
            if (bindPhone.getCalloutFlag() == 1) {
            	map.put("calloutFlag", "允许");
            } else {
            	map.put("calloutFlag", "不允许");
            }
            
            if (bindPhone.getAnswerFlag() == 1) {
            	map.put("answerFlag", "允许");
            } else {
            	map.put("answerFlag", "不允许");
            }
            
            returnList.add(map);
		}
		return returnList;
	}
	
	/**
	 * 保存绑定电话信息
	 * @param bindPhone 绑定电话对象
	 */
	public void insertBindPhone(BindPhone bindPhone) throws Exception {
		bindPhone.setPhoneState(0);
		bindPhone.setAnswerFlag(0);
		bindPhone.setCalloutFlag(0);
		bindPhone.setPriOrder(0);
		dao.saveObject(bindPhone);
	}
	
	/**
	 * 删除绑定电话信息
	 * @param id 绑定电话对象id
	 */
	public void deleteBindPhone(String[] idArray) throws Exception {
		BindPhone bindPhone = null;
		for (int i = 0; i < idArray.length; i++) {
			bindPhone = (BindPhone) dao.getObject(BindPhone.class,
					Long.parseLong(idArray[i]));
			dao.deleteObject(bindPhone);
		}
	}

	/**
	 * 根据id获得绑定电话信息
	 * @param id 绑定电话对象id
	 */
	public BindPhone getBindPhone(long id) throws Exception {
		BindPhone bindPhone = (BindPhone) dao.getObject(BindPhone.class, id);
		return bindPhone;
	}

	/**
	 * 修改某个绑定电话的信息
	 */
	public void updateBindPhone(BindPhone bindPhone) throws Exception {
		BindPhone bindPhonePo = (BindPhone) dao.getObject(BindPhone.class, bindPhone.getId());
		BeanUtils.copyProperties(bindPhonePo, bindPhone);
		dao.updateObject(bindPhonePo);
	}
	
	/**
	 * 切换绑定电话对象是否允许接听标志
	 * @param id 绑定电话对象id
	 */
	public void setAnswerFlag(long id) throws Exception {
		BindPhone bindPhone = (BindPhone) dao.getObject(BindPhone.class, id);
		int answerFlag = bindPhone.getAnswerFlag();
		if (answerFlag == 0) {
			bindPhone.setAnswerFlag(1);
		} else {
			bindPhone.setAnswerFlag(0);
		}
		dao.updateObject(bindPhone);
	}

	/**
	 * 切换绑定电话对象是否允许外呼标志
	 * @param id 绑定电话对象id
	 */
	public void setCalloutFlag(long id) throws Exception {
		BindPhone bindPhone = (BindPhone) dao.getObject(BindPhone.class, id);
		int calloutFlag = bindPhone.getCalloutFlag();
		if (calloutFlag == 0) {
			bindPhone.setCalloutFlag(1);
		} else {
			bindPhone.setCalloutFlag(0);
		}
		dao.updateObject(bindPhone);
	}
	
	/**
	 * 设置多个绑定电话的接听顺序
	 * @param idArray 某个一号通用户的所有绑定号码的id数组
	 * @param priOrderArray 排好的接听顺序数组
	 */
	public void setPriOrder(String[] idArray, String[] priOrderArray) throws Exception {
		BindPhone bindPhone = null;
		for (int i = 0; i < idArray.length; i++) {
			bindPhone = (BindPhone) dao.getObject(BindPhone.class, Integer.parseInt(idArray[i]));
			bindPhone.setPriOrder(Integer.parseInt(priOrderArray[i]));
			dao.updateObject(bindPhone);
		}
	}
	
	/**
	 * 判断用户的某个绑定号码是否已被设置过
	 * @param userNumber 用户帐号
	 * @param bindPhone 绑定号码
	 * @return 已存在时返回1，否则返回0
	 */
	public int validBindPhone(String userNumber, String bindPhone) throws Exception {
		LinkedList<Object> params = new LinkedList<Object>();
		params.add(userNumber);
		params.add(bindPhone);
		List<BindPhone> list = dao.hqlQuery("from BindPhone where userNumber=? and bindPhone=?",
				params.toArray());
		if (list != null && list.size() > 0) {
			return 1;
		} else {
			return 0;
		}
	}
}
