package com.sptg.permissions.biz.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sptg.permissions.api.entity.Menu;
import com.sptg.permissions.biz.mapper.MenuMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

/**
 *            
 *                
 * description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MenuServiceTest {

    @Resource
    private MenuMapper menuMapper;

    @Test
    public void updateClientCount(){
        Menu menu = this.menuMapper.selectById(1);
        this.recursion(menu);
    }

    private void recursion(Menu menu){
        if (null == menu){
            return;
        }
        //  获取子菜单列表
        Wrapper<Menu> wrapper = new EntityWrapper<>();
        wrapper.eq("l_pid",menu.getId());
        wrapper.notIn("i_status",-1);
        List<Menu> menuList = this.menuMapper.selectList(wrapper);
        menu.setClientCount(menuList.size());
        this.menuMapper.updateById(menu);
        //  开始递归
        for (int i = 0; i < menuList.size(); i++){
            Menu client = menuList.get(i);
            client.setSort(i);
            this.menuMapper.updateById(client);
            this.recursion(client);
        }
    }

}
