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

import com.huimv.admin.domain.primary.SysMenuEntity;
import com.huimv.admin.repo.primary.SysMenuEntityRepository;
import com.huimv.admin.service.primary.IMenuService;
import com.huimv.admin.utils.Result;
import com.huimv.admin.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

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

@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private SysMenuEntityRepository sysMenuEntityRepository;

    @Override
    public Result addMenu(SysMenuEntity menu) {
        System.out.println(menu);
        try {
            if (menu == null){
                return  new Result(10002,"参数错误",false);
            }
            sysMenuEntityRepository.save(menu);
            return new Result(10000,"添加成功",true);
        }catch (Exception e){
            return new Result(10001,"添加失败",false);
        }
    }

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

    }

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

   //查询菜单，只查一次数据库
    @Override
    public Result optimizeList() {
        Sort sort = Sort.by(Sort.Direction.ASC, "sort");
        List<SysMenuEntity> all = sysMenuEntityRepository.findAll(sort);
        List list = optimizeListUtil(0,all);
        return new Result(ResultCode.SUCCESS,list);
    }

    @Override
    public List<Map> optimizeListUtil(int parentId, List<SysMenuEntity> menuEntityList){
        List<Map> list = new ArrayList<>();
        for (SysMenuEntity menuEntity : menuEntityList) {
            if (menuEntity.getParentId() == parentId){
                Map map = new HashMap();
                map.put("id", menuEntity.getId());
                map.put("menuName", menuEntity.getMenuName());
                map.put("parentId", menuEntity.getParentId());
                map.put("url", menuEntity.getUrl());
                map.put("sort", menuEntity.getSort());
                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(SysMenuEntity menuEntity, List<SysMenuEntity> menuEntityList) {
        for (SysMenuEntity sysMenuEntity : menuEntityList) {
            if (menuEntity.getId().equals(sysMenuEntity.getParentId()) ){
                return true;
            }
        }
        return false;
    }




}
