package com.jwipc.sys.logic.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.jwipc.exception.DaoException;
import com.jwipc.sys.constant.MenuLanguageType;
import com.jwipc.sys.constant.MenuType;
import com.jwipc.sys.convert.ResourceConvertConvert;
import com.jwipc.sys.dto.MenuTreeDTO;
import com.jwipc.sys.dto.TreeDateDTO;
import com.jwipc.sys.entity.Resources;
import com.jwipc.sys.logic.MenuLogic;
import com.jwipc.sys.model.po.MenuTreePO;
import com.jwipc.sys.model.po.TreeDatePO;
import com.jwipc.sys.model.query.ResourcesListQuery;
import com.jwipc.sys.service.IResourcesService;
import com.jwipc.sys.util.TreeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class MenuLogicImpl implements MenuLogic {
    private final IResourcesService resourcesService;
    private final ResourceConvertConvert resourceConvertConvert;

    /**
     * 菜单列表
     * @param query 查询条件
     * @return 信息
     */
    @Override
    public List<MenuTreeDTO> listAll(ResourcesListQuery query) {
        List<MenuTreePO> menuTreePOList = resourcesService.listAll(query);
        List<MenuTreeDTO> menuTreeDTOList = resourceConvertConvert.treePO2TreeDTO(menuTreePOList);
        List<MenuTreeDTO> menuTree= new TreeUtil().getChildTreeObjects(menuTreeDTOList, 0, "  ");
        return CollUtil.isEmpty(menuTree)?menuTreeDTOList:menuTree;
    }

    /**
     * 菜单树列表
     *
     * @param query 查询条件
     * @return 信息
     */
    @Override
    public List<TreeDateDTO> treeList(ResourcesListQuery query) {
        List<TreeDatePO> treeList = resourcesService.treeList(query);
        this.handleTreeData(treeList);
        return resourceConvertConvert.treePO2TreeDTO1(treeList);
    }


    private void handleTreeData(List<TreeDatePO> treeDateVos) {
        for (TreeDatePO treeDto : treeDateVos) {
            List<TreeDatePO> children = treeDto.getChildren();
            if (null == children) {
                children = Lists.newArrayList();
            }
            for (TreeDatePO urd : treeDateVos) {
                if (urd.getParentId().equals(treeDto.getId())) {
                    children.add(urd);
                    // 判断该父级下的子级是否还有未选中的，如果有该父级节点也改为未选中
                    if (!urd.isChecked()) {
                        treeDto.setChecked(false);
                    }
                }
            }
            treeDto.setChildren(children);
        }
        Iterator<TreeDatePO> it = treeDateVos.iterator();
        while (it.hasNext()) {
            TreeDatePO urd = it.next();
            // 指定需要的根节点
            if (urd.getParentId() != 0) {
                it.remove();
            }
        }
    }

    @Override
    public List<MenuTreeDTO> getSubMenu(Long parentId, List<Long> roleIds) {
        // 用户菜单信息
        List<MenuTreeDTO> list = Lists.newArrayList();
        if (CollUtil.isNotEmpty(roleIds)) {
            List<MenuTreePO> mps = resourcesService.findMenuTreeByIds(roleIds, MenuType.MENU_PC.getType(),
                    MenuLanguageType.CHINESE.getType());
            list.addAll(resourceConvertConvert.treePO2TreeDTO(mps));
        }
        TreeUtil treeUtil = new TreeUtil();
        return treeUtil.getChildMenuTreeDTOs(list, parentId);
    }

    @Override
    public MenuTreeDTO getById(Long id) {
        return resourceConvertConvert.entity2DTO(resourcesService.getById(id));
    }

    @Override
    public void saveOrUpdate(MenuTreeDTO param) {
        param.setType("1");
//        param.setIcon("fa-list");
        param.setLevel(param.getParentId()==0L?1:2);
        //保存
        if (param.getId()==null){
            final LambdaQueryWrapper<Resources> resourcesLambdaQueryWrapper = new LambdaQueryWrapper<Resources>().eq(
                    Resources::getResKey, param.getResKey()).last(" limit 1");
            if (ObjectUtil.isNotNull(resourcesService.getOne(resourcesLambdaQueryWrapper))){
                throw new DaoException("唯一编码重复");
            }
        }else {
            final LambdaQueryWrapper<Resources> resourcesLambdaQueryWrapper = new LambdaQueryWrapper<Resources>().eq(
                    Resources::getResKey, param.getResKey()).ne(Resources::getId,param.getId()).last(" limit 1");
            if (ObjectUtil.isNotNull(resourcesService.getOne(resourcesLambdaQueryWrapper))){
                throw new DaoException("唯一编码重复");
            }
        }
        resourcesService.saveOrUpdate(resourceConvertConvert.dto2entity(param));
    }

    @Override
    public void updateHideFlag(Long id) {
        Resources resources = new Resources();
        resources.setId(id);
        resources.setHideFlag(1);
        resourcesService.updateById(resources);
    }
}