package com.gyee.wisdom.dao.sql.system;

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.system.model.Menu;
import com.gyee.wisdom.dao.sql.system.entity.MenuEntity;
import com.gyee.wisdom.dao.sql.system.entity.RoleEntity;
import com.gyee.wisdom.dao.sql.system.entity.UserEntity;
import com.gyee.wisdom.dao.sql.system.repository.MenuRepository;
import com.gyee.wisdom.dao.sql.system.repository.RoleRepository;
import com.gyee.wisdom.dao.sql.system.repository.UserRepository;
import com.gyee.wisdom.dao.system.IMenuDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class JpaMenuDao implements IMenuDao {

    @Autowired
    private MenuRepository menuRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public void add(Menu menu) {

        MenuEntity entity = new MenuEntity(menu);
        entity.setRoles(new ArrayList<>());
        menuRepository.save(entity);
    }

    @Override
    public void update(Menu menu) {
        if (!StringUtils.isEmpty(menu.getId())) {
            MenuEntity entity = new MenuEntity(menu);
            menuRepository.save(entity);
        }

    }

    @Override
    public Menu get(String id) {
        MenuEntity entity = menuRepository.findById(id).get();
        return entity.toData();
    }

    @Override
    public boolean contains(String id) {
        return menuRepository.existsById(id);

    }

    @Override
    public List<Menu> list() {
        List<MenuEntity> entityList = menuRepository.findAll();
        List<Menu> menuList = new ArrayList<Menu>();

        for (MenuEntity data : entityList) {
            menuList.add(data.toData());
        }
        return menuList;
    }

    @Override
    public void remove(String id) {
        menuRepository.deleteById(id);
    }

    @Override
    public void switchStatus(String id, boolean disabled) {
        MenuEntity entity = menuRepository.findById(id).get();
        entity.setDisabled(disabled);
        menuRepository.save(entity);
    }

    @Override
    public List<Menu> roleMenus(String roleId) {
       RoleEntity roleEntity=roleRepository.findById(roleId).get();
       return DataUtil.convertDataList(roleEntity.getMenuEntityList());
    }

    @Override
    public List<Menu> getNavMenus(String userId) {

        UserEntity userEntity=userRepository.findById(userId).get();
        List<RoleEntity> roleEntityList=userEntity.getRoles();

        List<MenuEntity> menuEntityList=roleEntityList.get(0).getMenuEntityList();
        return DataUtil.convertDataList(menuEntityList);
    }

    @Override
    public void UpdateMenuAndRole(String menuId, List<String> rids) {

        MenuEntity menuEntity= menuRepository.findById(menuId).get();
        List<RoleEntity> roleEntityList=roleRepository.findAllById(rids);

        menuEntity.setRoles(roleEntityList);

        menuRepository.save(menuEntity);
    }




}
