package com.zsc.edu.gateway.modules.system.service.impl;

import com.zsc.edu.gateway.modules.system.entity.Menu;
import com.zsc.edu.gateway.modules.system.entity.Role;
import com.zsc.edu.gateway.modules.system.entity.RoleMenu;
import com.zsc.edu.gateway.modules.system.repo.RoleMenuRepository;
import com.zsc.edu.gateway.modules.system.service.MenuService;
import com.zsc.edu.gateway.modules.system.service.RoleService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class MenuServiceImplTest {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuRepository roleMenuRepository;

    @Test
    public void insertRootMenu() {
        Menu dashboard = new Menu(null, Menu.Type.PAGE, "Dashboard", "/dashboard", "dashboard", "icon-dashboard", true, false, 1, "dashboard", "");
        Menu system = new Menu(null, Menu.Type.PAGE,  "System", "/system", "menu.system", "icon-computer", true, false, 2, "system", "");
        Menu user = new Menu(null, Menu.Type.PAGE,  "User", "/user", "menu.user", "icon-user", true, false, 3, "user", "");
        Menu message = new Menu(null,  Menu.Type.PAGE, "Message", "/message", "消息管理", "icon-message", true, false, 4, "message", "");
        Menu iot = new Menu(null, Menu.Type.PAGE, "Iot", "/iot", "物联网管理", "icon-iot", true, false, 5, "iot", "");
        Menu operationLog = new Menu(null, Menu.Type.PAGE, "OperationLog", "/operationLog", "操作日志", "icon-log", true, false, 6, "operationLog", "");
        menuService.saveBatch(List.of(dashboard, system, user, message, iot, operationLog));
        Menu workplace = new Menu(dashboard.getId(),  Menu.Type.PAGE, "Workplace", "workplace", "工作台", "icon-dashboard", true, false, 1, "dashboard:workplace", "");
        Menu role = new Menu(system.getId(),  Menu.Type.PAGE, "Role", "role", "角色管理", null, true, false, 1, "system:role", "");
        Menu dept = new Menu(system.getId(),  Menu.Type.PAGE, "Dept", "dept", "部门管理", null, true, false, 2, "system:dept", "");
        Menu users = new Menu(system.getId(),  Menu.Type.PAGE, "User", "user", "用户管理", null, true, false, 3, "system:user", "");
        Menu authority = new Menu(system.getId(),  Menu.Type.PAGE,  "Authority", "authority", "权限管理", null, true, false, 4, "system:authority", "");
        Menu menu = new Menu(system.getId(),  Menu.Type.PAGE, "Menu", "menu", "菜单管理", null, true, false, 5, "system:menu", "");
        Menu notice = new Menu(message.getId(), Menu.Type.PAGE, "Notice", "notice", "通知管理", null, true, false, 1, "message:notice", "");
        Menu bulletin = new Menu(message.getId(), Menu.Type.PAGE, "Bulletin", "bulletin", "公告管理", null, true, false, 2, "message:bulletin", "");
        Menu device = new Menu(iot.getId(), Menu.Type.PAGE, "Device", "device", "设备管理", null, true, false, 1, "iot:device", "");
        Menu product = new Menu(iot.getId(), Menu.Type.PAGE, "Product", "product", "产品管理", null, true, false, 2, "iot:product", "");
        Menu tsl = new Menu(product.getId(), Menu.Type.PAGE, "Tsl", "tsl", "物模型管理", null, true, true, 3, "iot:tsl", "");
        Menu log = new Menu(operationLog.getId(), Menu.Type.PAGE, "OperationLog", "operationLog", "操作日志", null, true, false, 1, "operationLog", "");
        menuService.saveBatch(List.of(workplace, dept, users, authority, menu, notice, bulletin, device, product, tsl, role, log));
        Menu roleCreate = new Menu(role.getId(), Menu.Type.OPERATION,  "roleCreate", null, "角色新增", null, true, false, 1, "system:role:create", "");
        Menu roleDelete = new Menu(role.getId(), Menu.Type.OPERATION,   "roleDelete", null, "角色删除", null, true, false, 1, "system:role:delete", "");
        Menu roleUpdate = new Menu(role.getId(),  Menu.Type.OPERATION,  "roleUpdate", null, "角色修改", null, true, false, 1, "system:role:update", "");
        Menu roleQuery = new Menu(role.getId(),  Menu.Type.OPERATION,  "roleQuery", null, "角色查询", null, true, false, 1, "system:role:query", "");
        Menu deptSave = new Menu(dept.getId(),  Menu.Type.OPERATION,  "deptCreate", null, "部门新增", null, true, false, 1, "system:dept:create", "");
        Menu deptUpdate = new Menu(dept.getId(),  Menu.Type.OPERATION,  "deptUpdate", null, "部门修改", null, true, false, 1, "system:dept:update", "");
        Menu deptQuery = new Menu(dept.getId(),  Menu.Type.OPERATION,  "deptQuery", null, "部门查询", null, true, false, 1, "system:dept:query", "");
        Menu deptDelete = new Menu(dept.getId(),  Menu.Type.OPERATION,  "deptDelete", null, "部门删除", null, true, false, 1, "system:dept:delete", "");
        Menu userSave = new Menu(dept.getId(),  Menu.Type.OPERATION,  "userCreate", null, "用户新增", null, true, false, 1, "system:user:create", "");
        Menu userUpdate = new Menu(dept.getId(),  Menu.Type.OPERATION,  "userUpdate", null, "用户修改", null, true, false, 1, "system:user:update", "");
        Menu userQuery = new Menu(dept.getId(),  Menu.Type.OPERATION,  "userQuery", null, "用户查询", null, true, false, 1, "system:user:query", "");
        Menu userDelete = new Menu(dept.getId(),  Menu.Type.OPERATION,  "userDelete", null, "用户删除", null, true, false, 1, "system:user:delete", "");
        Menu menuSave = new Menu(menu.getId(),  Menu.Type.OPERATION,  "menuCreate", null, "菜单新增", null, true, false, 1, "system:menu:create", "");
        Menu menuUpdate = new Menu(menu.getId(),  Menu.Type.OPERATION,  "menuUpdate", null, "菜单修改", null, true, false, 1, "system:menu:update", "");
        Menu menuQuery = new Menu(menu.getId(),  Menu.Type.OPERATION,  "menuQuery", null, "菜单查询", null, true, false, 1, "system:menu:query", "");
        Menu menuDelete = new Menu(menu.getId(),  Menu.Type.OPERATION,  "menuDelete", null, "菜单删除", null, true, false, 1, "system:menu:delete", "");
        Menu noticeCreate = new Menu(notice.getId(), Menu.Type.OPERATION, "noticeCreate", null, "通知新增", null, true, false, 1, "message:notice:create", "");
        Menu noticeUpdate = new Menu(notice.getId(), Menu.Type.OPERATION, "noticeUpdate", null, "通知修改", null, true, false, 1, "message:notice:update", "");
        Menu noticeQuery = new Menu(notice.getId(), Menu.Type.OPERATION, "noticeQuery", null, "通知查询", null, true, false, 1, "message:notice:query", "");
        Menu noticeDelete = new Menu(notice.getId(), Menu.Type.OPERATION, "noticeDelete", null, "通知删除", null, true, false, 1, "message:notice:delete", "");
        Menu bulletinCreate = new Menu(bulletin.getId(), Menu.Type.OPERATION, "bulletinCreate", null, "公告新增", null, true, false, 1, "message:bulletin:create", "");
        Menu bulletinUpdate = new Menu(bulletin.getId(), Menu.Type.OPERATION, "bulletinUpdate", null, "公告修改", null, true, false, 1, "message:bulletin:update", "");
        Menu bulletinQuery = new Menu(bulletin.getId(), Menu.Type.OPERATION, "bulletinQuery", null, "公告查询", null, true, false, 1, "message:bulletin:query", "");
        Menu bulletinDelete = new Menu(bulletin.getId(), Menu.Type.OPERATION, "bulletinDelete", null, "公告删除", null, true, false, 1, "message:bulletin:delete", "");
        Menu deviceQuery = new Menu(device.getId(), Menu.Type.OPERATION, "deviceQuery", null, "设备查询", null, true, false, 1, "iot:device:query", "");
        Menu deviceCreate = new Menu(device.getId(), Menu.Type.OPERATION, "deviceCreate", null, "设备新增", null, true, false, 1, "iot:device:create", "");
        Menu deviceUpdate = new Menu(device.getId(), Menu.Type.OPERATION, "deviceUpdate", null, "设备修改", null, true, false, 1, "iot:device:update", "");
        Menu deviceDelete = new Menu(device.getId(), Menu.Type.OPERATION, "deviceDelete", null, "设备删除", null, true, false, 1, "iot:device:delete", "");
        Menu productQuery = new Menu(product.getId(), Menu.Type.OPERATION, "productQuery", null, "产品查询", null, true, false, 1, "iot:product:query", "");
        Menu productCreate = new Menu(product.getId(), Menu.Type.OPERATION, "productCreate", null, "产品新增", null, true, false, 1, "iot:product:create", "");
        Menu productUpdate = new Menu(product.getId(), Menu.Type.OPERATION, "productUpdate", null, "产品修改", null, true, false, 1, "iot:product:update", "");
        Menu productDelete = new Menu(product.getId(), Menu.Type.OPERATION, "productDelete", null, "产品删除", null, true, false, 1, "iot:product:delete", "");
        Menu eventQuery = new Menu(tsl.getId(), Menu.Type.OPERATION, "eventQuery", null, "事件查询", null, true, false, 1, "iot:event:query", "");
        Menu eventCreate = new Menu(tsl.getId(), Menu.Type.OPERATION, "eventCreate", null, "事件新增", null, true, false, 1, "iot:event:create", "");
        Menu eventUpdate = new Menu(tsl.getId(), Menu.Type.OPERATION, "eventUpdate", null, "事件修改", null, true, false, 1, "iot:event:update", "");
        Menu eventDelete = new Menu(tsl.getId(), Menu.Type.OPERATION, "eventDelete", null, "事件删除", null, true, false, 1, "iot:event:delete", "");
        Menu propertyQuery = new Menu(tsl.getId(), Menu.Type.OPERATION, "propertyQuery", null, "属性查询", null, true, false, 1, "iot:property:query", "");
        Menu propertyCreate = new Menu(tsl.getId(), Menu.Type.OPERATION, "propertyCreate", null, "属性新增", null, true, false, 1, "iot:property:create", "");
        Menu propertyUpdate = new Menu(tsl.getId(), Menu.Type.OPERATION, "propertyUpdate", null, "属性修改", null, true, false, 1, "iot:property:update", "");
        Menu propertyDelete = new Menu(tsl.getId(), Menu.Type.OPERATION, "propertyDelete", null, "属性删除", null, true, false, 1, "iot:property:delete", "");
        Menu serverQuery = new Menu(tsl.getId(), Menu.Type.OPERATION, "serverQuery", null, "服务查询", null, true, false, 1, "iot:server:query", "");
        Menu serverCreate = new Menu(tsl.getId(), Menu.Type.OPERATION, "serverCreate", null, "服务新增", null, true, false, 1, "iot:server:create", "");
        Menu serverUpdate = new Menu(tsl.getId(), Menu.Type.OPERATION, "serverUpdate", null, "服务修改", null, true, false, 1, "iot:server:update", "");
        Menu serverDelete = new Menu(tsl.getId(), Menu.Type.OPERATION, "serverDelete", null, "服务删除", null, true, false, 1, "iot:server:delete", "");
        Menu operationLogQuery = new Menu(operationLog.getId(), Menu.Type.OPERATION, "operationLogQuery", null, "操作日志查询", null, true, false, 1, "operationLog:query", "");
        Menu operationLogDelete = new Menu(operationLog.getId(), Menu.Type.OPERATION, "operationLogDelete", null, "操作日志删除", null, true, false, 1, "operationLog:delete", "");
        menuService.saveBatch(List.of(roleCreate, roleDelete, roleUpdate, roleQuery,
                deptSave, deptUpdate, deptQuery, deptDelete,
                userSave, userUpdate, userQuery, userDelete,
                menuSave, menuUpdate, menuQuery, menuDelete,
                noticeCreate, noticeUpdate, noticeQuery, noticeDelete,
                bulletinCreate, bulletinUpdate, bulletinQuery, bulletinDelete,
                deviceQuery, deviceCreate, deviceUpdate, deviceDelete,
                productQuery, productCreate, productUpdate, productDelete,
                eventQuery, eventCreate, eventUpdate, eventDelete,
                propertyQuery, propertyCreate, propertyUpdate, propertyDelete,
                serverQuery, serverCreate, serverUpdate, serverDelete,
                operationLogQuery, operationLogDelete
        ));
    }

    @Test
    public void test() {
        Role admin = roleService.lambdaQuery().eq(Role::getName, "admin").one();
        menuService.list().forEach(menu -> roleMenuRepository.insert(new RoleMenu(admin.getId(), menu.getId())));
    }

    @Test
    public void testRole() {
        Role admin = roleService.lambdaQuery().eq(Role::getName, "admin").one();
        List<Menu> menus = menuService.selectByRoleId(admin.getId());
        assertEquals(27, menus.size());
    }

    @Test
    public void test2() {
        Menu menu = menuService.getById(7L);
        System.out.println(menu.toString());
    }

}