package org.fouram.service;

import java.util.List;

import org.fouram.core.base.model.Page;
import org.fouram.core.base.model.PageData;
import org.fouram.core.base.service.BaseService;
import org.fouram.core.util.UUIDUtil;
import org.fouram.entity.SysRole;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysRoleService extends BaseService {

	/**
	 * 新增
	 */
	public void save(PageData pd) throws Exception {
		save("SysRoleMapper.save", pd);
	}

	/*
	 * 删除
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void delete(PageData pd) throws Exception {
		delete("SysRoleMapper.delete", pd);
		delete("SysRoleMapper.deleteRoleMenu", pd);
	}

	/**
	 * 修改
	 */
	public void edit(PageData pd) throws Exception {
		update("SysRoleMapper.edit", pd);
	}

	/**
	 * 列表
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> list(Page page) throws Exception {
		return (List<PageData>) findList("SysRoleMapper.listPage", page);
	}

	/**
	 * 列表(全部)
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listAll(PageData pd) throws Exception {
		return (List<PageData>) findList("SysRoleMapper.listAll", pd);
	}

	/**
	 * 通过id获取数据
	 */
	public PageData findById(PageData pd) throws Exception {
		return (PageData) findObject("SysRoleMapper.findById", pd);
	}

	/**
	 * 查询列表总数
	 */
	public Long findCount(PageData pd) throws Exception {
		return (Long) findObject("SysRoleMapper.findCount", pd);
	}

	/**
	 * 批量删除
	 */
	public void deleteAll(String[] ArrayDATA_IDS) throws Exception {
		delete("SysRoleMapper.deleteAll", ArrayDATA_IDS);
	}
	
	public SysRole getRoleById(String roleId) throws Exception {
		return (SysRole) findObject("SysRoleMapper.getRoleById", roleId);
	}

	/**
	 * 更新角色增删改查权限
	 * @param msg
	 * @param pd
	 * @throws Exception
	 */
	public void updateRoleQx(String msg, PageData pd) throws Exception {
		update("SysRoleMapper." + msg, pd);
	}
	
	/**
	 * 更新角色菜单
	 * @param pd
	 * @throws Exception
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateRoleMenu(String id,String menuIds) throws Exception {
		delete("SysRoleMapper.deleteRoleMenuById",id);
		String [] ids = menuIds.split("\\,");
		if(ids.length > 0){
			for(int i = 0;i < ids.length;i++){
				PageData pd = new PageData();
				pd.put("id", UUIDUtil.get32UUID());
				pd.put("role_id", id);
				pd.put("menu_id", ids[i]);
				save("SysRoleMapper.saveRoleMenu", pd);
			}
		}
	}
}
