package com.kcim.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.dao.mapper.RoleMenuMapper;
import com.kcim.dao.model.RoleMenu;
import com.kcim.dao.model.dto.RoleMenuDTO;
import com.kcim.vo.CommonSelectVO;
import com.kcim.vo.CommonVO;
import com.kcim.service.RoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("roleMenuService")
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {


    /**
     * 通过roleId 获取对应的菜单列表
     *
     * @param roleId
     * @param hospId
     * @return
     */
    @Override
    public List<CommonVO> getMenuByRoleId(Long roleId, Long hospId) {
        return baseMapper.getMenuByRoleId(roleId, hospId);
    }


    /**
     * 绑定角色和菜单关系时选择列表可回显
     *
     * @param roleId 角色id
     * @param hospId
     * @return CommonSelectVO
     */
    @Override
    public List<CommonVO> roleMenuList(Long roleId, Long hospId) {
        return baseMapper.getMenuByRoleId(roleId, hospId);
//        List<Menu> list = menuService.list(
//                new LambdaQueryWrapper<Menu>().eq(Menu::getHospId, hospId)
//        );
//        if (CollUtil.isEmpty(list)) {
//            return Collections.emptyList();
//        }
//        List<CommonSelectVO> all = list.stream().map(i -> {
//            CommonSelectVO commonSelectVO = new CommonSelectVO();
//            commonSelectVO.setIsSelect(false);
//            commonSelectVO.setParentId(i.getParentId());
//            commonSelectVO.setId(i.getMenuId());
//            commonSelectVO.setName(i.getName());
//            return commonSelectVO;
//        }).collect(Collectors.toList());
//
//        List<RoleMenu> roleMenus = this.list(
//                new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId)
//        );
//        for (CommonSelectVO commonSelectVO : all) {
//            for (RoleMenu roleMenu : roleMenus) {
//                if (roleMenu.getMenuId().equals(commonSelectVO.getId())) {
//                    commonSelectVO.setIsSelect(true);
//                }
//            }
//        }
//        List<CommonSelectVO> roots = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
//        List<CommonSelectVO> vos = new ArrayList<>();
//        for (CommonSelectVO root : roots) {
//            vos.addAll(this.getMenuVO(root, all));
//        }
//        return vos;
    }

    private List<CommonSelectVO> getMenuVO(CommonSelectVO root, List<CommonSelectVO> all) {
        List<CommonSelectVO> list = new ArrayList<>();
        list.add(root);
        for (CommonSelectVO j : all) {
            if (j.getParentId().equals(root.getId())) {
                List<CommonSelectVO> children = root.getChildren();
                if (CollUtil.isEmpty(children)) {
                    children = new ArrayList<>();
                }
                children.add(j);
                root.setChildren(children);
                this.getMenuVO(j, all);
            }
        }

        return list;
    }

    /**
     * 编辑角色和菜单关系
     *
     * @param roleMenuDTO {@link RoleMenuDTO}
     * @param hospId      医院id
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void saveRoleMenu(RoleMenuDTO roleMenuDTO, Long hospId) {
        Long roleId = roleMenuDTO.getRoleId();
        this.remove(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId).eq(RoleMenu::getHospId, hospId));
        List<Long> menuIds = roleMenuDTO.getMenuIds();
        if (CollUtil.isNotEmpty(menuIds)) {
            List<RoleMenu> roleMenus = menuIds.stream().map(i -> {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId).setMenuId(i).setHospId(hospId);
                return roleMenu;
            }).collect(Collectors.toList());
            this.saveBatch(roleMenus);
        }
    }
}