package com.sckw.manage.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.manage.api.RemoteManageService;
import com.sckw.manage.api.model.dto.res.EntAddressResDto;
import com.sckw.manage.api.model.dto.res.FindEntCooperateResVo;
import com.sckw.manage.dao.KwmAddressMapper;
import com.sckw.manage.model.entity.KwmAddress;
import com.sckw.manage.model.entity.KwmCooperate;
import com.sckw.manage.model.vo.req.FindCooperateByEntReqVo;
import com.sckw.manage.model.vo.req.FindEntCooperateReqVo;
import com.sckw.manage.model.vo.res.FindCooperateByEntResVo;
import com.sckw.manage.service.KwmCooperateManageService;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.EntCacheResDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author czh
 * @desc 基础信息+合作管理远程接口实现
 * @date 2023/7/31
 */
@DubboService(group = "design", version = "1.0.0")
public class RemoteManageServiceImpl implements RemoteManageService {

    @Autowired
    private KwmAddressMapper kwmAddressMapper;

    @Autowired
    private KwmCooperateManageService kwmCooperateManageService;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    /**
     * @param idList 企业id集合
     * @return Map<Long, List<EntAddressResDto>>
     * @desc: 根据企业id查下面的地址
     * @author: czh
     * @date: 2023/7/31
     */
    @Override
    public Map<Long, List<EntAddressResDto>> queryEntAddressByEntIdList(List<Long> idList) {
        Map<Long, List<EntAddressResDto>> map = new HashMap<>(4);
        LambdaQueryWrapper<KwmAddress> kwmAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        kwmAddressLambdaQueryWrapper.in(KwmAddress::getEntId, idList).
                                     eq(KwmAddress::getDelFlag, Global.NO);
        List<KwmAddress> kwmAddressList = kwmAddressMapper.selectList(kwmAddressLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(kwmAddressList)) {
            return map;
        }

        List<EntAddressResDto> entAddressResDtoList = BeanUtils.copyToList(kwmAddressList, EntAddressResDto.class);
        return entAddressResDtoList.stream().collect(Collectors.groupingBy(EntAddressResDto::getEntId));
    }

    @Override
    public List<FindEntCooperateResVo> findEntCooperate(Long entId, Long targetEntId, Integer cooperateType) {
        FindEntCooperateReqVo findEntCooperateReqVo = new FindEntCooperateReqVo();
        findEntCooperateReqVo.setId(entId);
        findEntCooperateReqVo.setCooperateType(cooperateType);
        findEntCooperateReqVo.setTargetEntId(targetEntId);
        List<com.sckw.manage.model.vo.res.FindEntCooperateResVo> entCooperate = kwmCooperateManageService.findEntCooperate(findEntCooperateReqVo);
        if (CollectionUtils.isEmpty(entCooperate)) {
            return Collections.emptyList();
        }
        return BeanUtils.copyToList(entCooperate, FindEntCooperateResVo.class);
    }


    /**
     * @param entId 集团企业id
     * @return FindEntCooperateResVo
     * @desc: 查询集团企业包括其主体单位下的合作单位所属的集团企业
     * @author: czh
     * @date: 2023/9/1
     */
    @Override
    public List<FindEntCooperateResVo> findAllCooperateEnt(Long entId) {
        if (Objects.isNull(entId)) {
            return Collections.emptyList();
        }
        List<Long> ourEntIds = new ArrayList<>();
        ourEntIds.add(entId);
        EntCacheResDto entCacheResDto = remoteSystemService.queryEntTreeById(entId);
        if (Objects.nonNull(entCacheResDto)) {
            List<EntCacheResDto> child = entCacheResDto.getChild();
            if (CollectionUtils.isNotEmpty(child)) {
                ourEntIds.addAll(child.stream().map(EntCacheResDto::getId).toList());
            }
        }

        //获取当前集团下所有的企业id，遍历，找到合作单位
        List<Long> list = new ArrayList<>();
        List<FindEntCooperateResVo> findEntCooperateResVoList = new ArrayList<>();
        for (Long ourEntId : ourEntIds) {
            FindCooperateByEntReqVo findCooperateByEntReqVo = new FindCooperateByEntReqVo();
            findCooperateByEntReqVo.setEntId(ourEntId);
            List<FindCooperateByEntResVo> cooperateByEnt = kwmCooperateManageService.findCooperateByEnt(findCooperateByEntReqVo);
            if (CollectionUtils.isNotEmpty(cooperateByEnt)) {
                List<Long> targetEntIds = cooperateByEnt.stream().map(FindCooperateByEntResVo::getTargetEntId).toList();
                Map<Long, EntCacheResDto> longEntCacheResDtoMap = remoteSystemService.queryEntTreeByIds(targetEntIds);
                for (Long targetEntId : targetEntIds) {
                    EntCacheResDto entCacheResDto1 = longEntCacheResDtoMap.get(targetEntId);
                    if (Objects.nonNull(entCacheResDto1)) {
                        Long id = entCacheResDto1.getId();
                        if (list.contains(id)) {
                            continue;
                        }
                        list.add(id);
                        FindEntCooperateResVo findEntCooperateResVo = new FindEntCooperateResVo();
                        findEntCooperateResVo.setEntId(id);
                        findEntCooperateResVo.setEntName(entCacheResDto1.getFirmName());
                        findEntCooperateResVoList.add(findEntCooperateResVo);
                    }
                }
            }
        }

        return findEntCooperateResVoList;
    }
}
