package com.vote.system.service.impl;

import com.vctgo.common.core.domain.R;
import com.vctgo.system.api.RemoteBranchKindergartenService;
import com.vctgo.system.api.domain.dto.SysBranchKindergartenDto;
import com.vote.system.domain.vo.VoteSysBranchKindergartenVo;
import com.vote.system.service.VoteSysBranchKindergartenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author feng
 * @date 2024/8/22
 */
@Service
public class VoteSysBranchKindergartenServiceImpl implements VoteSysBranchKindergartenService {


    @Autowired
    private RemoteBranchKindergartenService remoteBranchKindergartenService;


    /**
     * 查询园区列表
     */
    @Override
    public List<VoteSysBranchKindergartenVo> selectByDistrictId(String districtId) {
        R<List<SysBranchKindergartenDto>> kindergarten = remoteBranchKindergartenService.get(null, null, districtId);
        // 如果数据为空，直接返回空列表
        List<SysBranchKindergartenDto> kindergartenList = kindergarten.getData();
        if (kindergartenList == null || kindergartenList.isEmpty()) {
            return Collections.emptyList();
        }
        return kindergartenList.stream()
                .map(item -> {
                    VoteSysBranchKindergartenVo kindergartenVo = new VoteSysBranchKindergartenVo();
                    kindergartenVo.setId(item.getId());
                    kindergartenVo.setLocationName(item.getKindergartenName());
                    return kindergartenVo;
                })
                .collect(Collectors.toList());
    }


    /**
     * 保存
     *
     * @param sysBranchKindergartenDto
     * @return
     */
    @Override
    @Transactional
    public int save(SysBranchKindergartenDto sysBranchKindergartenDto) {
        R<Integer> add = remoteBranchKindergartenService.add(sysBranchKindergartenDto);
        return add.getData();
    }


}
