package com.ruoyi.ems.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.ems.domain.AreaBuilding;
import com.ruoyi.ems.mapper.AreaBuildingMapper;
import com.ruoyi.ems.service.IAreaBuildingService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ems.mapper.AreaBuildingZoningMapper;
import com.ruoyi.ems.domain.AreaBuildingZoning;
import com.ruoyi.ems.service.IAreaBuildingZoningService;

/**
 * 建筑区域划分Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-09
 */
@Service
public class AreaBuildingZoningServiceImpl implements IAreaBuildingZoningService {
    @Autowired
    private AreaBuildingZoningMapper areaBuildingZoningMapper;

    @Autowired
    private AreaBuildingMapper areaBuildingMapper;

    /**
     * 查询建筑区域划分
     *
     * @param id 建筑区域划分主键
     * @return 建筑区域划分
     */
    @Override
    public AreaBuildingZoning selectAreaBuildingZoningById(Long id) {
        return areaBuildingZoningMapper.selectAreaBuildingZoningById(id);
    }

    /**
     * 查询建筑区域划分
     *
     * @param code 建筑区域划分主键
     * @return 建筑区域划分
     */
    @Override
    public AreaBuildingZoning selectAreaBuildingZoningByCode(String code) {
        return areaBuildingZoningMapper.selectAreaBuildingZoningByCode(code);
    }

    /**
     * 查询建筑区域划分列表
     *
     * @param areaBuildingZoning 建筑区域划分
     * @return 建筑区域划分
     */
    @Override
    public List<AreaBuildingZoning> selectAreaBuildingZoningList(AreaBuildingZoning areaBuildingZoning) {
        List<AreaBuildingZoning> list = areaBuildingZoningMapper.selectAreaBuildingZoningList(areaBuildingZoning);
        fillAreaPath(list);
        return list;
    }

    @Override
    public List<AreaBuildingZoning> selectZoningByBuilding(String buildingCode) {
        AreaBuildingZoning zParam = new AreaBuildingZoning();
        zParam.setBldgCode(buildingCode);
        List<AreaBuildingZoning> list = areaBuildingZoningMapper.selectAreaBuildingZoningList(zParam);
        fillAreaPath(list);
        return list;
    }

    @Override
    public List<AreaBuildingZoning> selectZoningByBuildings(List<String> buildingCodes) {
        List<AreaBuildingZoning> retList = new ArrayList<>();

        for (String buildCode : buildingCodes) {
            AreaBuildingZoning zParam = new AreaBuildingZoning();
            zParam.setBldgCode(buildCode);
            List<AreaBuildingZoning> list = areaBuildingZoningMapper.selectAreaBuildingZoningList(zParam);
            fillAreaPath(list);
            retList.addAll(list);
        }

        return retList;
    }

    /**
     * 新增建筑区域划分
     *
     * @param areaBuildingZoning 建筑区域划分
     * @return 结果
     */
    @Override
    public int insertAreaBuildingZoning(AreaBuildingZoning areaBuildingZoning) {
        return areaBuildingZoningMapper.insertAreaBuildingZoning(areaBuildingZoning);
    }

    /**
     * 修改建筑区域划分
     *
     * @param areaBuildingZoning 建筑区域划分
     * @return 结果
     */
    @Override
    public int updateAreaBuildingZoning(AreaBuildingZoning areaBuildingZoning) {
        return areaBuildingZoningMapper.updateAreaBuildingZoning(areaBuildingZoning);
    }

    /**
     * 批量删除建筑区域划分
     *
     * @param ids 需要删除的建筑区域划分主键
     * @return 结果
     */
    @Override
    public int deleteAreaBuildingZoningByIds(Long[] ids) {
        return areaBuildingZoningMapper.deleteAreaBuildingZoningByIds(ids);
    }

    /**
     * 删除建筑区域划分信息
     *
     * @param id 建筑区域划分主键
     * @return 结果
     */
    @Override
    public int deleteAreaBuildingZoningById(Long id) {
        return areaBuildingZoningMapper.deleteAreaBuildingZoningById(id);
    }

    private void fillAreaPath(List<AreaBuildingZoning> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            List<AreaBuilding> areaBuildings = areaBuildingMapper.selectAreaBuildingList(new AreaBuilding());
            Map<String, AreaBuilding> areaBuildingsMap = areaBuildings.stream()
                .collect(Collectors.toMap(AreaBuilding::getBldgCode, Function.identity()));

            for (AreaBuildingZoning zoning : list) {
                String areaName = null;
                String buildingName = null;

                AreaBuilding areaBuilding = areaBuildingsMap.get(zoning.getBldgCode());
                areaName = areaBuilding != null ? areaBuilding.getAreaShortName() + "/" : "*/";
                buildingName = zoning.getBldgName() != null ? zoning.getBldgName() : "*";

                zoning.setAreaPath(areaName + buildingName);
            }
        }
    }
}
