package com.ningdatech.pmapi.sys.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.pmapi.AppTests;
import com.ningdatech.pmapi.sys.model.entity.Menu;
import com.ningdatech.pmapi.sys.model.entity.Role;
import com.ningdatech.pmapi.sys.model.entity.RoleMenu;
import com.ningdatech.pmapi.user.entity.enumeration.RoleEnum;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * <p>
 * IMenuServiceTest
 * </p>
 *
 * @author WendyYang
 * @since 11:18 2023/1/29
 */
class IMenuServiceTest extends AppTests {

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IRoleService roleService;

    /**
     * 初始化固定角色
     */
    @Test
    public void roleService() {
        roleService.remove(null);
        AtomicLong integer = new AtomicLong(0);
        Arrays.stream(RoleEnum.values()).map(w -> {
            Role role = new Role();
            role.setId(integer.incrementAndGet());
            role.setCode(w.name());
            role.setName(w.getDesc());
            role.setDescribe(w.getDesc());
            role.setFixed(true);
            role.setCreateOn(LocalDateTime.now());
            return role;
        }).forEach(roleService::save);
    }

    @Test
    public void test() throws SQLException {
        menuService.remove(null);
        List<Entity> menu = Db.use().findAll("nd_menu");
        menu.forEach(System.out::println);
        menu.forEach(w -> {
            Menu bean = w.toBean(Menu.class);
            menuService.save(bean);
        });
    }


    @Autowired
    private IRoleMenuService roleMenuService;

    @Test
    public void initMenu() {
        menuService.remove(null);
        roleMenuService.remove(Wrappers.lambdaQuery(RoleMenu.class)
                .eq(RoleMenu::getRoleId, 1));
        String str = FileUtil.readString("/Users/wendy/Desktop/long_text_2023-02-13-15-28-42.txt", "UTF-8");
        List<JSONObject> obj = JSONUtil.toList(str, JSONObject.class);
        save(obj, 0);
        menuService.list().forEach(w -> {
            roleMenuService.save(new RoleMenu() {{
                setRoleId(1L);
                setMenuId(w.getId());
            }});
        });
    }

    public void save(List<JSONObject> objs, long parentId) {
        objs.forEach(w -> {
            Menu menu = new Menu();
            menu.setActiveMenu(w.getStr("activeMenu"));
            menu.setComponent(w.getStr("component"));
            menu.setIcon(w.getStr("icon"));
            menu.setPath(w.getStr("path"));
            menu.setName(w.getStr("path").replace("/", ""));
            menu.setTopMenu(w.getStr("topMenu"));
            menu.setTitle(w.getStr("title"));
            menu.setHidden(w.getInt("hidden"));
            menu.setPid(parentId);
            menu.setRedirect(w.getStr("redirect"));
            menu.setSort(w.getInt("sort"));
            menu.setDataScopeOption("");
            menu.setHasDataScope(false);
            menuService.save(menu);
            List<JSONObject> children = w.getBeanList("children", JSONObject.class);
            if (CollUtil.isNotEmpty(children)) {
                save(children, menu.getId());
            }
        });
    }

}