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

import java.util.List;

import market.Com.vo.ResultModel;

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.PrivilegeMapper;
import com.hnshituo.core.auth.service.PrivilegeService;
import com.hnshituo.core.auth.vo.Function;
import com.hnshituo.core.auth.vo.Privilege;

@RemoteService(path="core/auth/privilege")
public class PrivilegeServiceImpl extends BaseServiceImpl<Privilege, Integer> implements PrivilegeService {

	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel save(int roleId, List<Function> funcs) {
		ResultModel rm=new ResultModel();
		deleteByRoleId(roleId);
		if(funcs!=null&&funcs.size()>0){
			for(Function functions:funcs){
				Privilege privilege=new Privilege();
				privilege.setRoleId(roleId);
				privilege.setFuncId(functions.getId());
				privilege.setType(0);
				this.getMapper(PrivilegeMapper.class).doAdd(privilege);
			}
		}
//			this.getMapper(PrivilegeMapper.class).insertWithFuncs(roleId, funcs);
		rm.setState("200");
		return rm;
	}
	
	/*@RemoteService(path="save2")
	public void saveWithFuncIds(int roleId, String[] funcIds) {
		
	}*/
	
	public Integer[] getByRoleId(int roleId) {
		return this.getMapper(PrivilegeMapper.class).getPrivilegeByRoleId(roleId);
	}
	
	public void deleteByRoleId(int roleId) {
		this.getMapper(PrivilegeMapper.class).deleteByRoleId(roleId);
	}
}
