/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.base.api.entity.SysMenu;
import com.tofly.base.entity.BaseSys;
import com.tofly.base.entity.BaseSysmenu;
import com.tofly.base.entity.MySysMenu;
import com.tofly.base.service.BaseSysService;
import com.tofly.base.service.BaseSysmenuService;
import com.tofly.common.core.util.JSONUtil;
import com.tofly.common.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.stereotype.Service;
import com.tofly.base.mapper.BaseSysmenuMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author wangjian
 * @date Mon Jul 06 00:00:00 CST 2020
 */
@Service("baseSysmenuService")
public class BaseSysmenuServiceImpl extends ServiceImpl<BaseSysmenuMapper, BaseSysmenu> implements BaseSysmenuService {

    @Autowired
    private BaseSysService baseSysService;

    @Override
    @Transactional
    public boolean updateSysmenu(Map<String,String> map) {
        List<String> keyList = map.keySet().stream().collect(Collectors.toList());
        baseMapper.delete(Wrappers.<BaseSysmenu>lambdaQuery()
                .in(BaseSysmenu::getSysid,keyList));
        baseSysService.remove(Wrappers.<BaseSys>lambdaQuery()
                .in(BaseSys::getSysid,keyList));
        List<BaseSysmenu> valueList=new ArrayList<>();
        map.forEach((k,v)->{
            String menus=(String)v;
            JSONObject o=JSONObject.parseObject(menus);
            String name = o.getString("name");
            String values = o.getString("value");
            String type = o.getString("type");
            String notes = o.getString("notes");
            BaseSys baseSys=new BaseSys();
            baseSys.setSysid(k);
            baseSys.setSysname(name);
            baseSys.setNotes(notes);
            baseSys.setType(type);
            baseSysService.save(baseSys);
            List<MySysMenu> mySysMenus = JSON.parseArray(values, MySysMenu.class);
            mySysMenus.stream().forEach(sysmenu->{
                sysmenu.setSysid(k);
                addChild(valueList,sysmenu);
            });
        });
        valueList.forEach(sysmenu -> baseMapper.insert(sysmenu));
        return true;
    }

    private void  addChild(List<BaseSysmenu> list,MySysMenu mySysMenu){
        list.add(mySysMenu);
        List<MySysMenu> child=mySysMenu.getChildren();
        if(child==null || child.size()==0){
            return;
        }
        String sysid=mySysMenu.getSysid();
        child.forEach(mySysMenu1 -> {
            mySysMenu1.setSysid(sysid);
            mySysMenu1.setParentPathid(mySysMenu.getPathId());
            addChild(list,mySysMenu1);
        });
    }

    @Override
    public List<MySysMenu> getTreeSys(String sysid,String pathIds) {
        List<BaseSysmenu> list = this.list(Wrappers.<BaseSysmenu>lambdaQuery()
        .eq(BaseSysmenu::getSysid,sysid).orderByAsc(BaseSysmenu::getId));
        list.sort(Comparator.comparing(BaseSysmenu::getId));
        List<String> list1 = Arrays.asList(pathIds.split(","));
        Set<String> set=new HashSet<>();
        set.addAll(list1);
        getPrant(set,list);
        List<BaseSysmenu> collect = list.stream().filter(s -> set.contains(s.getPathId())).collect(Collectors.toList());
        return getTreeSys(collect);
    }

    private void getPrant(Set<String> set,List<BaseSysmenu> list){
        List<String> collect = list.stream().filter(sys -> set.contains(sys.getPathId()))
                .map(sys -> sys.getParentPathid())
                .filter(s-> StringUtil.isNotEmpty(s))
                .collect(Collectors.toList());
        if(collect!=null && collect.size()>0){
            set.addAll(collect);
            Set set1=new HashSet();
            set1.addAll(collect);
            getPrant(set1,list);
        }
    }

    @Override
    public List<MySysMenu> getTreeSys(String sysid) {
        List<BaseSysmenu> list = this.list(Wrappers.<BaseSysmenu>lambdaQuery()
                .eq(BaseSysmenu::getSysid,sysid).orderByAsc(BaseSysmenu::getId));
        return getTreeSys(list);
    }

    @Override
    public List<MySysMenu> getTreeSys(List<BaseSysmenu> list) {
        List<MySysMenu> children=
                JSONObject.parseArray(JSONObject.toJSONString(list), MySysMenu.class);
        List<MySysMenu> children1 =
                JSONUtil.parTree(children, MySysMenu::getPathId, MySysMenu::getParentPathid, MySysMenu::getChildren);
        return children1;
    }


}
