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.AccountGroup;
import com.huimv.admin.system.entity.GroupMenu;
import com.huimv.admin.system.entity.Menu;
import com.huimv.admin.system.mapper.AccountGroupMapper;
import com.huimv.admin.system.mapper.GroupMenuMapper;
import com.huimv.admin.system.mapper.MenuMapper;
import com.huimv.admin.system.service.IMenuService;
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 2021-09-09
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private AccountGroupMapper accountGroupMapper;

    @Autowired
    private GroupMenuMapper groupMenuMapper;

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

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

             menuMapper.insert(menu);
            Integer id = menu.getId();
            List<AccountGroup> accounts = accountGroupMapper.selectList(new QueryWrapper<AccountGroup>().eq("account_id", userId));
            for (AccountGroup account : accounts) {
                GroupMenu groupMenu = new GroupMenu();
                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(Menu 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<Menu> menuQueryWrapper = new QueryWrapper<>();
            menuQueryWrapper.eq("parent_id",menuId);
            List<Menu> lists = menuMapper.selectList(menuQueryWrapper);
            if (lists != null && lists.size()>0){
                for (Menu 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<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.orderByAsc("sort");

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

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

    @Override
    public List<Map> optimizeListUtil(int parentId, List<Menu> menuEntityList){
        List<Map> list = new ArrayList<>();
        for (Menu 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());
                map.put("ifShow", menuEntity.getIfShow());
                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(Menu menuEntity, List<Menu> menuEntityList) {
        for (Menu Menu : menuEntityList) {
            if (menuEntity.getId().equals(Menu.getParentId()) ){
                return true;
            }
        }
        return false;
    }
}
