package com.yihu.hos.system.service;

import com.yihu.hos.system.dao.RoleDao;
import com.yihu.hos.system.model.SystemRole;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/19.
 */
@Service("roleManager")
public class RoleManager {

    public static final String BEAN_ID = "roleManager";

    @Resource(name = RoleDao.BEAN_ID)
    private RoleDao roleDao;

   
    public Result getRoleList(Map<String, Object> params) throws Exception {

        return roleDao.getRoleList(params);
    }

   
    public SystemRole getRoleById(String roleId) throws Exception {
        return roleDao.getEntity(SystemRole.class, roleId);
    }

    @Transactional
    public Result addRole(SystemRole obj) throws Exception {
        SystemRole roleTemp = roleDao.getRoleByName(obj.getName());
        if (roleTemp != null) {
            return Result.error("角色名称已经存在");
        }
        roleDao.saveEntity(obj);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateRole(SystemRole obj) throws Exception {
        SystemRole roleTemp = roleDao.getRoleByNameWithOutId(obj.getName(), obj.getId());
        if (roleTemp != null) {
            return Result.error("角色名称已经存在");
        }
        SystemRole systemRole = roleDao.getEntity(SystemRole.class, obj.getId());
        systemRole.setName(obj.getName());
        systemRole.setRemark(obj.getRemark());
        return Result.success("更新成功");
    }

    @Transactional
    public Result activityRole(String roleid, String activityFlag) throws Exception {
        try {
            roleDao.activityRole(roleid, activityFlag);
            return new ActionResult(true, "状态修改成功！");
        } catch (Exception ex) {
            return new ActionResult(false, "状态修改失败！");
        }
    }

    @Transactional
    public Result deleteRole(String id) throws Exception {
        SystemRole sr = roleDao.getEntity(SystemRole.class, id);
        roleDao.deleteEntity(sr);
        return Result.success("删除成功");
    }
}
