package com.tarena.oss.userservice.service;

import java.util.Collection;

import com.tarena.oss.userservice.dao.UserServiceMgmtDAO;
import com.tarena.oss.userservice.pojo.UserService;
import com.tarena.oss.userservice.pojo.UserServiceCondDTO;

public class UserServiceMgmtServiceImpl implements UserServiceMgmtService {
	private UserServiceMgmtDAO dao;
	
	public void setDao(UserServiceMgmtDAO dao) {
		this.dao = dao;
	}
	
	/**
	 * 增加一条业务帐号
	 */
	public void addUserService(UserService userservice) {
		dao.insertUserService(userservice);
	}
	
	/**
	 * 修改一条业务帐号
	 */
	public void modifyUserService(UserService userservice) {
		dao.updateUserService(userservice);
	}

	/**
	 * 删除一条业务帐号
	 */
	public void removeUserService(Integer id) {
		UserService us=dao.queryUserService(id);
		dao.deleteUserService(us);
	}
	
	/**
	 * 根据id查找一条业务帐号
	 */
	public UserService findUserService(Integer id) {
		return dao.queryUserService(id);
	}

	/**
	 * 查找所有的业务帐号
	 */
	public Collection<UserService> findUserServices() {
		return dao.queryUserServices();
	}

	/**
	 * 根据查询条件查找一条或者多条业务帐号
	 */
	public Collection<UserService> findUserServices(UserServiceCondDTO dto) {
		String str =this.makeQryString(dto);
		System.out.println("sql :"+str);
		return dao.queryUserServices(str);
	}

	/**
	 * 根据当前页数查找一条或者多条业务帐号
	 */
	public Collection<UserService> findUserServices(int currentPage) {
		return dao.queryUserServices(currentPage);
	}

	/**
	 * 根据当前页数和查询条件查找一条或者多条业务帐号
	 */
	public Collection<UserService> findUserServices(UserServiceCondDTO dto,int currentPage) {
		String str =makeQryString(dto);
		System.out.println("sql: "+str);
		return dao.queryUserServices(str, currentPage);
	}

	/**
	 * 查询所有业务帐号的总数
	 */
	public int findUserServiceCnts() {
		return dao.queryUserServiceCnt();
	}
	
	/**
	 * 根据查询条件查找符合条件的业务帐号的总数
	 */
	public int findUserServicesByCond(UserServiceCondDTO dto) {
		String str = makeQryString(dto);
		return dao.queryUserServiceCnt(str);
	}
	
	/**
	 * 拼接查询条件
	 */
	public String makeQryString(UserServiceCondDTO dto){
		StringBuffer sb = new StringBuffer();
		String loginname=dto.getLoginname();
		String labip =dto.getLabip();
		String status =dto.getStatus()+"";
		String username=dto.getUsername();
		
		if(username!=null && !username.trim().equals("")){
			sb.append(" and us.user.name like '%").append(username).append("%'");
		}
		
		if(loginname !=null && !loginname.trim().equals("")){
			sb.append(" and us.loginname like '%").append(loginname).append("%'");
		}
		
		if(labip !=null && !labip.trim().equals("")){
			sb.append(" and us.labip like '%").append(labip).append("%'");
		}
		
		if(status!=null && !status.trim().equals("")){
			if(status.equals("-1")){
				sb.append(" and us.status in(0 , 1)");
			}else{
				sb.append(" and us.status = ").append(status);
			}
			
		}
		
		System.out.println("makeQryString :"+sb.toString());
		
		return sb.toString();
	}
	

}
