package cn.iocoder.yudao.module.inspection.service.impl;

import cn.iocoder.yudao.module.inspection.entity.InspectionBusinessChannel;
import cn.iocoder.yudao.module.inspection.mapper.InspectionBusinessChannelMapper;
import cn.iocoder.yudao.module.inspection.service.InspectionBusinessChannelService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class InspectionBusinessChannelServiceImpl extends ServiceImpl<InspectionBusinessChannelMapper, InspectionBusinessChannel> implements InspectionBusinessChannelService {
    @Override
    public List<InspectionBusinessChannel> getChannelTree() {
        // 查询所有业务渠道（父节点）
        return this.list();
    }

    /**
     * 获取业务渠道和客户来源
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Map<String, Object> getBusinessChannelByUserId(Long userId) {
        Map<String, Object> map = new HashMap<>();
        // 客户来源
        InspectionBusinessChannel channel = baseMapper.getBusinessChannelByUserId(userId);
        if (channel == null) {
            return null;
        }
        map.put("channel", channel);
        // 根据客户来源的父id查询业务渠道
        InspectionBusinessChannel business = getOne(Wrappers.<InspectionBusinessChannel>lambdaQuery()
                .eq(InspectionBusinessChannel::getId, channel.getPid()));
        if (business != null) {
            map.put("business", business);
        }
        return map;
    }

}
