package com.cool.store.service.impl;

import com.cool.store.dao.SysMenuDAO;
import com.cool.store.dao.SysRoleDAO;
import com.cool.store.dao.SysRoleMenuDAO;
import com.cool.store.dto.role.RoleUpdateDTO;
import com.cool.store.entity.SysRoleDO;
import com.cool.store.entity.SysRoleMenuDO;
import com.cool.store.enums.PlatFormTypeEnum;
import com.cool.store.service.RoleService;
import com.cool.store.vo.role.RoleAuthInfoVO;
import com.cool.store.vo.role.RolePageVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchenbiao
 * @FileName: RoleServiceImpl
 * @Description:
 * @date 2023-07-25 15:06
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private SysRoleDAO sysRoleDAO;
    @Resource
    private SysMenuDAO sysMenuDAO;
    @Resource
    private SysRoleMenuDAO sysRoleMenuDAO;

    @Override
    public PageInfo<RolePageVO> getRolePage(Integer pageNum, Integer pageSize) {
        Page<SysRoleDO> page = sysRoleDAO.getRolePage(pageNum, pageSize);
        PageInfo resultPage = new PageInfo(page);
        List<RolePageVO> resultList = RolePageVO.convertList(page);
        resultPage.setList(resultList);
        return resultPage;
    }

    @Override
    public RoleAuthInfoVO getRoleAuthDetail(String roleId) {
        SysRoleDO roleDetail = sysRoleDAO.getRoleDetail(roleId);
        if(Objects.isNull(roleDetail)){
            return null;
        }
        List<SysRoleMenuDO> roleMenuAuths = sysRoleMenuDAO.getRoleMenuByRoleId(roleId);
        return RoleAuthInfoVO.convertVO(roleDetail, roleMenuAuths);
    }

    @Override
    public Integer updateRoleAuth(String userId, RoleUpdateDTO param) {
        return sysRoleMenuDAO.updateRoleAuth(param.getRoleId(), param.getMenuIds());
    }
}
