package com.huimv.admin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.admin.system.entity.ScreenSysAccountGroup;
import com.huimv.admin.system.entity.ScreenSysGroupMenu;
import com.huimv.admin.system.entity.ScreenSysMenu;
import com.huimv.admin.system.mapper.ScreenAccountGroupMapper;
import com.huimv.admin.system.mapper.ScreenGroupMenuMapper;
import com.huimv.admin.system.mapper.ScreenMenuMapper;
import com.huimv.admin.system.service.IScreenSysMenuService;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2022-03-25
 */
@Service
public class ScreenSysMenuServiceImpl extends ServiceImpl<ScreenMenuMapper, ScreenSysMenu> implements IScreenSysMenuService {
    @Autowired
    private ScreenMenuMapper menuMapper;

    @Autowired
    private ScreenAccountGroupMapper accountGroupMapper;

    @Autowired
    private ScreenGroupMenuMapper groupMenuMapper;

    @Override
    public Result addMenu(ScreenSysMenu menu, Integer userId) {

        try {
            if (menu == null){
                return  new Result(10002,"参数错误",false);
            }

            menuMapper.insert(menu);
            Integer id = menu.getId();
            List<ScreenSysAccountGroup> accounts = accountGroupMapper.selectList(new QueryWrapper<ScreenSysAccountGroup>().eq("account_id", userId));
            for (ScreenSysAccountGroup account : accounts) {
                ScreenSysGroupMenu groupMenu = new ScreenSysGroupMenu();
                groupMenu.setGroupId(account.getGroupId());
                groupMenu.setMenuId(id);
                groupMenuMapper.insert(groupMenu);
            }

            return new Result(10000,"添加成功",true);
        }catch (Exception e){
            return new Result(10001,"添加失败",false);
        }
    }

    @Override
    public Result editMenu(ScreenSysMenu menu) {
        try {
            if (menu.getId() == null){
                return new Result(10002,"请选择要修改的菜单",false);
            }
            menuMapper.updateById(menu);
            return new Result(10000,"修改成功",true);
        }catch (Exception e){
            return new Result(10001,"修改失败",false);
        }

    }

    @Override
    public Result removeMenu(Integer menuId) {
        try {
            QueryWrapper<ScreenSysMenu> menuQueryWrapper = new QueryWrapper<>();
            menuQueryWrapper.eq("parent_id",menuId);
            List<ScreenSysMenu> lists = menuMapper.selectList(menuQueryWrapper);
            if (lists != null && lists.size()>0){
                for (ScreenSysMenu menu : lists) {
                    removeMenu(menu.getId());
                    menuMapper.deleteById(menu);
                }
            }
            menuMapper.deleteById(menuId);
        }catch (Exception e){
            return new Result(10001,"删除失败",false);
        }
        return new Result(10000,"删除成功",true);
    }

    //查询菜单，只查一次数据库
    @Override
    public Result optimizeList() {
        QueryWrapper<ScreenSysMenu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.orderByAsc("sort");

        List<ScreenSysMenu> all = menuMapper.selectList(menuQueryWrapper);
        List list = optimizeListUtil(0,all);
        return new Result(ResultCode.SUCCESS,list);
    }

    @Override
    public Result list1() {
        QueryWrapper<ScreenSysMenu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.orderByAsc("sort");
        menuQueryWrapper.eq("weight",0);
        List<ScreenSysMenu> all = menuMapper.selectList(menuQueryWrapper);
        List list = optimizeListUtil(0,all);
        return new Result(ResultCode.SUCCESS,list);
    }

    @Override
    public List<Map> optimizeListUtil(int parentId, List<ScreenSysMenu> menuEntityList){
        List<Map> list = new ArrayList<>();
        for (ScreenSysMenu menuEntity : menuEntityList) {
            if (menuEntity.getParentId() == parentId){
                Map map = new HashMap();
                map.put("id", menuEntity.getId());
                map.put("title", menuEntity.getTitle());
                map.put("parentId", menuEntity.getParentId());
                map.put("url", menuEntity.getUrl());
                map.put("sort", menuEntity.getSort());
                map.put("value", menuEntity.getId());
                map.put("pid", menuEntity.getParentId());
                map.put("permission", menuEntity.getPermission());
                map.put("weight", menuEntity.getWeight());
                if (menuEntityIsChildren(menuEntity,menuEntityList) ){
                    map.put("children",optimizeListUtil(menuEntity.getId(),menuEntityList));
                }else {
                    map.put("children",new ArrayList<>());
                }
                list.add(map);
            }
        }
        return list;
    }



    //判断是否有children
    private boolean menuEntityIsChildren(ScreenSysMenu menuEntity, List<ScreenSysMenu> menuEntityList) {
        for (ScreenSysMenu Menu : menuEntityList) {
            if (menuEntity.getId().equals(Menu.getParentId()) ){
                return true;
            }
        }
        return false;
    }
}
