package com.hnshituo.core.auth.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.mapper.RoleMapper;
import com.hnshituo.core.auth.service.HomeService;
import com.hnshituo.core.auth.service.PrivilegeService;
import com.hnshituo.core.auth.service.RoleService;
import com.hnshituo.core.auth.service.UserRoleService;
import com.hnshituo.core.auth.vo.Role;

@RemoteService(path="core/auth/role")
public class RoleServiceImpl extends BaseServiceImpl<Role, Integer> implements RoleService {

	public List<Role> query(Role t) {
		t = (t==null) ? new Role() : t;
		StringBuilder condition = new StringBuilder();
		if (StringUtils.isNotEmpty(t.getName())) {
			condition.append(" NAME LIKE '%" + t.getName() + "%'");
		}
		/*String[] dataPrgs = (String[]) this.getRequest().getAttribute(HomeService.REQUEST_DATA_PRG);
		if (dataPrgs != null) {
			String deptIdCondition = super.getDataPrgCondition("CREATE_DEPT_ID", dataPrgs);
			if (deptIdCondition.length() > 0) {
				condition.append(" AND ").append(deptIdCondition);
			} else {
				return null;
			}
		}*/
		return this.getMapper(RoleMapper.class).findWithCondition(null, condition.toString(), "name" , 0, 0);
	}
	
	public List<Role> findByName(String name) {
		List<Role> list = this.getMapper(RoleMapper.class).findByName(name);
		return list;
	}
	
	public Role add(Role t) throws Exception {
		if (t== null) return null;
		int count=this.getMapper(RoleMapper.class).findMaxId();
		t.setId(count+1);
		t.setCreateTime(new Date());
		t.setCreateMan((String)getSession().getAttribute(HomeService.SESSION_USER_NAME));
		super.insert(t);
		return t;
	}
	
	public Role modify(Role t) throws Exception {
		if (t== null) return null;
		t.setUpdateTime(new Date());
		t.setUpdateMan((String)getSession().getAttribute(HomeService.SESSION_USER_NAME));
		super.update(t);
		return t;
	}

	
	@Transactional(value="tm", rollbackFor=Exception.class)
	public void doDelete(Integer id) throws Exception {
		this.getBean(PrivilegeService.class).deleteByRoleId(id);
		this.getBean(UserRoleService.class).deleteByRoleId(id);
		super.delete(id);
	}
	
	
	@Transactional(value="tm", rollbackFor=Exception.class)
	@RemoteService(path="save")
	public void saveDataPrg(int roleId, String url, String[] departmentIds) throws Exception {
		this.deleteByRoleIdAndFuncId(roleId, url);
		if (departmentIds != null && departmentIds.length > 0) {
			this.getMapper(RoleMapper.class).insertDataPrg(roleId, url, departmentIds);
		}
	}
	
	public void deleteByRoleIdAndFuncId(int roleId, String url) throws Exception {
		this.getMapper(RoleMapper.class).deleteDataPrgByRoleIdAndFuncId(roleId, url);
	}
	
	public boolean hasWholeDataPrg(String userId) throws Exception {
		return this.getMapper(RoleMapper.class).hasWholeDataPrg(userId);
	}
	
	public String[] getDataPrg(String userId, String url) throws Exception {
		//如果拥有全局数据权限，则查url为空的数据权限
		url = this.hasWholeDataPrg(userId) ? "" : url;
		String[] dataPrgs = this.getMapper(RoleMapper.class).getDataPrg(userId, url);
		return dataPrgs;
	}

	public Role doQueryById(Integer roleId) {
		Role role=this.getMapper(RoleMapper.class).findById(roleId);
		return role;
	}
	
}
