package com.bs.df.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.common.core.domain.entity.SysUser;
import com.bs.common.utils.SecurityUtils;
import com.bs.df.domain.DfBrokerProduct;
import com.bs.df.mapper.DfBrokerMapper;
import com.bs.df.domain.DfBroker;
import com.bs.df.service.IDfBrokerProductService;
import com.bs.df.service.IDfBrokerService;
import com.bs.system.service.ISysUserService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 代理商经纪Service业务层处理
 *
 * @author bs
 * @date 2024-04-06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DfBrokerServiceImpl  extends MPJBaseServiceImpl<DfBrokerMapper, DfBroker> implements IDfBrokerService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IDfBrokerService dfBrokerService;
    @Autowired
    private IDfBrokerProductService dfBrokerProductService;
    public List<Long> filterByUser() {
        //SecurityUtils.getUserId();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        List<DfBroker> allSubordinates = findAllSubordinates(deptId);
        List<Long> brokerIds = allSubordinates.stream()
                .map(DfBroker::getId)
                .collect(Collectors.toList());
        brokerIds.add(deptId);
        return brokerIds;
    }

    public List<Long> filterByUserId(Long userId) {
        SysUser sysUser = userService.selectUserById(userId);
        Long deptId = sysUser.getDeptId();
        List<DfBroker> allSubordinates = findAllSubordinates(deptId);
        List<Long> brokerIds = allSubordinates.stream()
                .map(DfBroker::getId)
                .collect(Collectors.toList());
        brokerIds.add(deptId);
        return brokerIds;
    }

    public List<DfBroker> findAllSubordinates(Long brokerId) {
        List<DfBroker> result = new ArrayList<>();
        // 查询当前brokerId的所有下级
        List<DfBroker> subordinates = dfBrokerService.list(new LambdaQueryWrapper<DfBroker>().eq(DfBroker::getParentId, brokerId));
        result.addAll(subordinates);
        for (DfBroker subordinate : subordinates) {
            List<DfBroker> subSubordinates = findAllSubordinates(subordinate.getId());
            result.addAll(subSubordinates);
        }
        return result;
    }

    public List<Long> getProductIdsByBrokerId() {
        List<Long> longs = filterByUser();
        List<DfBrokerProduct> list = dfBrokerProductService.list(new LambdaQueryWrapper<DfBrokerProduct>()
                .in(DfBrokerProduct::getBrokerId, longs)
                .eq(DfBrokerProduct::getStaus, "1")
                .or()
                .in(DfBrokerProduct::getUserId, SecurityUtils.getUserId()));
        return list.stream().map(DfBrokerProduct::getProductId).collect(Collectors.toList());
    }

    public List<Long> getProductIdsByBrokerIdByUserId(Long UserId) {
        List<Long> longs = filterByUserId(UserId);
        List<DfBrokerProduct> list = dfBrokerProductService.list(new LambdaQueryWrapper<DfBrokerProduct>()
                .in(DfBrokerProduct::getBrokerId, longs)
                .eq(DfBrokerProduct::getStaus, "1"));
        return list.stream().map(DfBrokerProduct::getProductId).collect(Collectors.toList());
    }



}
