package com.jpsoft.shinestar.modules.sys.service.impl;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.sys.dao.UserDAO;
import com.jpsoft.shinestar.modules.sys.entity.User;
import com.jpsoft.shinestar.modules.sys.service.UserService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="userService")
public class UserServiceImpl implements UserService {
	@Resource(name="userDAO")
	private UserDAO userDAO;

	@Override
	public User get(String id) {
		// TODO Auto-generated method stub
		return userDAO.get(id);
	}

	@Override
	public int insert(User model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return userDAO.insert(model);
	}

	@Override
	public int update(User model) {
		// TODO Auto-generated method stub
		return userDAO.update(model);		
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return userDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = userDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<User> list() {
		// TODO Auto-generated method stub
		return userDAO.list();
	}

	@Override
	public List<User> findByCompanyId(String companyId){
		return userDAO.findByCompanyId(companyId);
	}

	@Override
	public List<User> findByCompanyList(Map<String,Object> searchParams,List<Sort> sortList){
		return userDAO.findByCompanyList(searchParams,sortList);
	}
		
	@Override
	public Page<User> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,List<Sort> sortList) {
        Page<User> page = PageHelper.startPage(pageNumber,pageSize).doSelectPage(()->{
            userDAO.search(searchParams,sortList);
        });
        
        return page;
	}

	@Override
	public User findByUserName(String userName) {
		return userDAO.findByUserName(userName);
	}

	@Override
	public boolean hasRole(String userId, String roleName) {
		long count = userDAO.hasRole(userId,roleName);

		return count>0;
	}

	@Override
	public User findByOpenId(String openId) {
		return userDAO.findByOpenId(openId);
	}
}