package com.ruoyi.bst.area.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.area.service.AreaAssembler;
import com.ruoyi.bst.areaSub.domain.AreaSubQuery;
import com.ruoyi.bst.areaSub.domain.enums.AreaSubType;
import com.ruoyi.bst.areaSub.domain.vo.AreaSubCountTypeByAreaVO;
import com.ruoyi.bst.areaSub.service.AreaSubDashboard;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class AreaAssemblerImpl implements AreaAssembler {

    @Autowired
    private AreaSubDashboard areaSubDashboard;

    @Override
    public void assembleSubAreaCount(List<AreaVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }

        AreaSubQuery query = new AreaSubQuery();
        query.setAreaIds(CollectionUtils.map(list, AreaVO::getId));
        List<AreaSubCountTypeByAreaVO> countList = areaSubDashboard.selectTypeCountGroupByAreaId(query);

        for (AreaVO area : list) {

            // 停车区
            AreaSubCountTypeByAreaVO parkingAreaCount = countList.stream()
                .filter(item -> Objects.equals(item.getAreaId(), area.getId()) && AreaSubType.PARKING.getCode().equals(item.getType()))
                .findFirst().orElse(null);
            if (parkingAreaCount != null) {
                area.setParkingAreaCount(parkingAreaCount.getCount());
            } else {
                area.setParkingAreaCount(0);
            }

            // 禁停区
            AreaSubCountTypeByAreaVO noParkingAreaCount = countList.stream()
                .filter(item -> Objects.equals(item.getAreaId(), area.getId()) && AreaSubType.NO_PARKING.getCode().equals(item.getType()))
                .findFirst().orElse(null);
            if (noParkingAreaCount != null) {
                area.setNoParkingAreaCount(noParkingAreaCount.getCount());
            } else {
                area.setNoParkingAreaCount(0);
            }

            // 禁行区
            AreaSubCountTypeByAreaVO noRideAreaCount = countList.stream()
                .filter(item -> Objects.equals(item.getAreaId(), area.getId()) && AreaSubType.NO_RIDE.getCode().equals(item.getType()))
                .findFirst().orElse(null);
            if (noRideAreaCount != null) {
                area.setNoRideAreaCount(noRideAreaCount.getCount());
            } else {
                area.setNoRideAreaCount(0);
            }
        }
    }

    
}