package com.yihu.jw.base.service.role;

import com.yihu.jw.base.dao.role.RoleMenuDao;
import com.yihu.jw.entity.base.role.RoleMenuDO;
import com.yihu.jw.restmodel.base.role.MenuVO;
import com.yihu.mysql.query.BaseJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色菜单
 * @author yeshijie on 2018/9/26.
 */
@Service
public class RoleMenuService extends BaseJpaService<RoleMenuDO, RoleMenuDao> {

    @Autowired
    private RoleMenuDao roleMenuDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<RoleMenuDO> findRoleMenu(String roleId){
        return roleMenuDao.findByRoleId(roleId);
    }

    public List<MenuVO> findMenuByRoleId(String roleId){
        String sql ="SELECT " +
                " m.id as \"id\", " +
                " m.parent_id AS \"parentId\", " +
                " m.name as \"name\", " +
                " m.menu_sort AS  \"sort\", " +
                " m.url as \"url\", " +
                " m.status as \"status\"," +
                " m.icon as \"icon\"," +
                " m.is_show AS \"show\" " +
                " FROM " +
                " base_menu m " +
                " JOIN base_role_menu r ON m.id = r.menu_id " +
                " WHERE " +
                " r.role_id = '"+roleId+"' " +
                " AND m.status =1 ";
        List<MenuVO> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(MenuVO.class));
        return list;

    }
}
