package com.yida.data.school.index.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.school.EduSchoolMenu;
import com.yida.data.common.core.entity.school.EduYidaAppAccount;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.EduYidaApp;
import com.yida.data.common.service.CommonService;
import com.yida.data.school.index.mapper.EduSchoolMenuMapper;
import com.yida.data.school.index.service.EduSchoolMenuService;
import com.yida.data.school.index.util.AppUtil;
import com.yida.data.school.vo.index.MenuInfoVO;
import com.yida.data.system.feign.RemoteYidaAppAccountService;
import com.yida.data.system.feign.RemoteYidaAppService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service实现
 *
 * @author zhanghaijun
 * @date 2021-07-19 17:21:58
 */
@Service
@Slf4j
@RequiredArgsConstructor
@Transactional
public class EduSchoolMenuServiceImpl extends ServiceImpl
    <EduSchoolMenuMapper, EduSchoolMenu> implements EduSchoolMenuService {

    private final RemoteYidaAppService remoteYidaAppService;
    private final RemoteYidaAppAccountService remoteYidaAppAccountService;
    private final CommonService commonService;

    @Override
    public List<MenuInfoVO> listMenu(Long schoolId, Integer type) {
        List<EduSchoolMenu> list = list(Wrappers.lambdaQuery(new EduSchoolMenu())
            .eq(EduSchoolMenu::getSchoolId, schoolId)
            .eq(EduSchoolMenu::getType, type));
        List<MenuInfoVO> parent = list.stream().filter(x -> x.getParentId() == null).map(x -> {
            MenuInfoVO menuInfo = new MenuInfoVO();
            BeanUtils.copyProperties(x, menuInfo);
            return menuInfo;
        }).collect(Collectors.toList());
        for (MenuInfoVO parentMenu : parent) {
            parentMenu.setChildren(new ArrayList<>());
            for (EduSchoolMenu menu : list) {
                if (ObjectUtil.equal(menu.getParentId(), parentMenu.getId())) {
                    MenuInfoVO child = new MenuInfoVO();
                    BeanUtils.copyProperties(menu, child);
                    parentMenu.getChildren().add(child);
                }
            }
        }
        return parent;
    }

    @Override
    public void saveMenu(EduSchoolMenu menu) {
        if (menu.getContentType() == 2) {
            Dept school = commonService.getDept(menu.getSchoolId());
            menu.setColId(school.getYidaAppColId());
        }
        saveOrUpdate(menu);
        if (menu.getParentId() != null) {
            // 更改父菜单类型
            EduSchoolMenu parent = getById(menu.getParentId());
            if (parent.getContentType() != 3) {
                parent.setContentType(3);
                updateById(parent);
            }
        }
        updateAppMenu(menu.getSchoolId(), menu.getType());
    }

    @Override
    public void delMenuBatch(List<Long> ids) {
        EduSchoolMenu menu = getById(ids.get(0));
        removeByIds(ids);
        updateAppMenu(menu.getSchoolId(), menu.getType());
    }

    /**
     * 更新易达app菜单
     */
    private void updateAppMenu(Long schoolId, Integer type) {
        EduYidaApp yidaApp = commonService.getYidaApp(schoolId);
        EduYidaAppAccount account = remoteYidaAppAccountService.getAccountByType(schoolId, type).getData();
        List<EduSchoolMenu> list = list(Wrappers.lambdaQuery(new EduSchoolMenu())
            .eq(EduSchoolMenu::getSchoolId, schoolId)
            .eq(EduSchoolMenu::getType, type));
        // 找到父菜单
        List<MenuInfoVO> parent = list.stream().filter(x -> x.getParentId() == null).map(x -> {
            MenuInfoVO menuInfo = new MenuInfoVO();
            BeanUtils.copyProperties(x, menuInfo);
            return menuInfo;
        }).collect(Collectors.toList());
        Iterator<MenuInfoVO> iterator = parent.iterator();
        while (iterator.hasNext()) {
            // 遍历填充子菜单
            MenuInfoVO next = iterator.next();
            next.setChildren(new ArrayList<>());
            for (EduSchoolMenu menu : list) {
                if (ObjectUtil.equal(menu.getParentId(), next.getId())) {
                    MenuInfoVO child = new MenuInfoVO();
                    BeanUtils.copyProperties(menu, child);
                    next.getChildren().add(child);
                }
            }
            if (next.getContentType() == 3 && CollUtil.isEmpty(next.getChildren())) {
                // 没有子菜单 且 为空父菜单将被删除
                iterator.remove();
            }
        }
        AppUtil.subscribeSaveMenu(account.getYidaAppAccountId(), yidaApp, parent);
    }
}
