package com.itjob.service;

import java.util.*;

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

/**
 * 开启关闭时间设置的业务逻辑接口实现类.
 */
public class CallTimeLimitManagerImpl implements CallTimeLimitManager {
	protected BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}
	
	/**
	 * 查询开启关闭时间设置
	 * @param userNumber 用户帐号
	 * @param limitType 类型(0:关闭，1:开启，-1：查询开启和关闭时间)
	 * @return 返回当前开启关闭时间列表
	 */
	public List getCallTimeLimitList(String userNumber, int limitType) throws Exception {
		List<Map> returnList = new ArrayList<Map>();
		List<CallTimeLimit> list = null;
		LinkedList<Object> params = new LinkedList<Object>();
		params.add(userNumber);
		if (limitType != -1) {
			params.add(limitType);
			list = dao.hqlQuery("from CallTimeLimit where userNumber=? and limitType=?",
						params.toArray());
		} else {
			list = dao.hqlQuery("from CallTimeLimit where userNumber=?",
						params.toArray());
		}
		
		CallTimeLimit callTimeLimit = null;
		Map<String, String> map = null;
		for (int i = 0; i < list.size(); i++) {
			map = new HashMap<String, String>();
			callTimeLimit = (CallTimeLimit) list.get(i);
			map.put("num", String.valueOf(i + 1));
			map.put("id", String.valueOf(callTimeLimit.getId()));
			map.put("startTime", callTimeLimit.getStartTime());
			map.put("endTime", callTimeLimit.getEndTime());
			if (callTimeLimit.getLimitType() == 1) {
				map.put("limitType", "开启");
			} else {
				map.put("limitType", "关闭");
			}
			returnList.add(map);
		}
		return returnList;
	}
	
	/**
	 * 添加开启关闭时间设置
	 * @param callTimeLimit 开启关闭时间对象
	 */
	public void insertCallTimeLimit(CallTimeLimit callTimeLimit) throws Exception {
		dao.saveObject(callTimeLimit);
	}
	
	/**
	 * 根据id获得开启关闭时间对象
	 * @param id 开启关闭时间对象的主键ID
	 */
	public CallTimeLimit getCallTimeLimit(long id) throws Exception {
		CallTimeLimit callTimeLimit = (CallTimeLimit) dao.getObject(CallTimeLimit.class, id);
		return callTimeLimit;
	}

	/**
	 * 修改开启关闭时间设置
	 * @param callTimeLimit 开启关闭时间对象
	 */
	public void updateCallTimeLimit(CallTimeLimit callTimeLimit) throws Exception {
		CallTimeLimit callTimeLimitPo = (CallTimeLimit) dao.getObject(CallTimeLimit.class,
							callTimeLimit.getId());
		callTimeLimitPo.setLimitType(callTimeLimit.getLimitType());
		callTimeLimitPo.setStartTime(callTimeLimit.getStartTime());
		callTimeLimitPo.setEndTime(callTimeLimit.getEndTime());
		dao.updateObject(callTimeLimitPo);
	}
	
	/**
	 * 删除开启关闭时间设置
	 * @param id 开启关闭时间对象的主键ID
	 */
	public void deleteCallTimeLimit(String[] idArray) throws Exception {
		CallTimeLimit callTimeLimit = null;
		for (int i = 0; i < idArray.length; i++) {
			callTimeLimit = (CallTimeLimit) dao.getObject(CallTimeLimit.class,
								Long.parseLong(idArray[i]));
			dao.deleteObject(callTimeLimit);
		}
	}
}
