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.PermissionDAO;
import com.jpsoft.shinestar.modules.sys.entity.Permission;
import com.jpsoft.shinestar.modules.sys.service.PermissionService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="permissionService")
public class PermissionServiceImpl implements PermissionService {
	@Resource(name="permissionDAO")
	private PermissionDAO permissionDAO;

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

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

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

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

	@Override
	public boolean exist(String path, String method) {
		// TODO Auto-generated method stub
		int count = permissionDAO.exist(path,method);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<Permission> list() {
		// TODO Auto-generated method stub
		return permissionDAO.list();
	}
		
	@Override
	public Page<Permission> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,List<Sort> sortList) {
        Page<Permission> page = PageHelper.startPage(pageNumber,pageSize).doSelectPage(()->{
            permissionDAO.search(searchParams,sortList);
        });
        
        return page;
	}

	@Override
	public boolean hasPermitted(String userId, String path, String method) {
		return permissionDAO.hasPermitted(userId,path,method)>0 ? true : false;
	}
}