package com.huiju.estateagents.cleancode;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.entity.*;
import com.huiju.estateagents.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api")
public class CleanCode {

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ITaRoleService taRoleService;

    @Autowired
    private ITaRoleMenuService taRoleMenuService;

    @Autowired
    private ITaRoleButtonService taRoleButtonService;

    @Autowired
    private ISysButtonInMenuService sysButtonInMenuService;

    @RequestMapping(value = "/admin/clean/menurole", method = RequestMethod.GET)
    public void cleanMenu() {
        List<SysMenu> sysMenuList = sysMenuService.list();

        QueryWrapper<TaRole> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("is_admin",1);
        List<TaRole> roleList = taRoleService.list(roleQueryWrapper);

        roleList.forEach(e -> {
            sysMenuList.forEach(s -> {
                TaRoleMenu taRoleMenu = new TaRoleMenu();
                taRoleMenu.setRoleId(e.getRoleId());
                taRoleMenu.setMenuId(s.getMenuId());
                taRoleMenuService.save(taRoleMenu);
            });
        });
    }

    @RequestMapping(value = "/admin/clean/buttonrole", method = RequestMethod.GET)
    public void cleanButton() {
        List<SysButtonInMenu> buttlist = sysButtonInMenuService.list();

        QueryWrapper<TaRole> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("is_admin",1);
        List<TaRole> roleList = taRoleService.list(roleQueryWrapper);

        roleList.forEach(e -> {
            buttlist.forEach(s -> {
                TaRoleButton taRoleButton = new TaRoleButton();
                taRoleButton.setBtnId(s.getBtnId());
                taRoleButton.setRoleId(e.getRoleId());
                taRoleButtonService.save(taRoleButton);
            });
        });
    }
}
