package com.eitc.patient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eitc.patient.domain.CollectCostType;
import com.eitc.patient.mapper.CollectCostTypeMapper;
import com.eitc.patient.service.ICollectCostTypeService;
import com.eitc.patient.tree.CollectCostTypeTree;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * CollectCostTypeServiceImpl
 *
 * @author mtx
 * @date: 2024/05/17 15:39
 */
@Service
public class CollectCostTypeServiceImpl extends ServiceImpl<CollectCostTypeMapper, CollectCostType> implements ICollectCostTypeService {

    /**
     * 查询列表数据
     *
     * @param collectCostType 参数
     * @return 返回结果
     * @author mtx
     * @date: 2024/7/18 19:12
     */
    @Override
    public List<CollectCostType> getList(CollectCostType collectCostType) {
        LambdaQueryWrapper<CollectCostType> eq = Wrappers.<CollectCostType>lambdaQuery()
                .eq(StringUtils.isNotBlank(collectCostType.getCostTypeName()), CollectCostType::getCostTypeName, collectCostType.getCostTypeName())
                .orderByDesc(CollectCostType::getCostTypeName);
        return this.list(eq);
    }

    /**
     * 封装属性结构
     *
     * @param parentId 父级id
     * @param list     数据
     * @return 封装好的数据
     */
    @Override
    public List<CollectCostTypeTree> getChildren(String parentId, List<CollectCostTypeTree> list) {
        return list.stream().filter(t -> Objects.equals(t.getParentId(), parentId)).peek(
                m -> m.setChildrenList(getChildren(m.getId(), list))
        ).collect(Collectors.toList());
    }

    /**
     * 获取面包屑
     *
     * @param parentId id
     * @return 字符串
     * @author mtx
     * @date: 2024/5/31 10:12
     */
    @Override
    public String findCrumb(String parentId) {
        if (!"0".equals(parentId)) {
            // 获取父级
            CollectCostType collectCostType = this.getById(parentId);
            if (Objects.isNull(collectCostType)) {
                return "";
            }
            String costTypeName = collectCostType.getCostTypeName();
            // 循环添加子集面包屑
            String s = findCrumb(collectCostType.getParentId());
            if (StringUtils.isNotBlank(s)) {
                return s + " > " + costTypeName;
            } else {
                return costTypeName;
            }
        }
        return "";
    }

}
