package com.ruoyi.ems.service.impl;

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

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

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

    @Autowired
    private AreaBuildingMapper areaBuildingMapper;

    @Autowired
    private IObjTagRelService tagRelService;

    /**
     * 查询建筑区域划分
     *
     * @param id 建筑区域划分主键
     * @return 建筑区域划分
     */
    @Override
    public AreaBuildingZoning selectAreaBuildingZoningById(Long id) {
        AreaBuildingZoning zoning = areaBuildingZoningMapper.selectAreaBuildingZoningById(id);
        zoning.setTagCodeList(Arrays.asList(StringUtils.split(zoning.getTagCodes(), ",")));
        return zoning;
    }

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

    @Override
    public List<AreaBuildingZoning> selectByCodes(Set<String> codes) {
        return areaBuildingZoningMapper.selectByCodes(codes);
    }

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

    @Override
    public List<AreaBuildingZoning> selectZoningByBuilding(String buildingCode) {
        AreaBuildingZoning zParam = new AreaBuildingZoning();
        zParam.setBldgCode(buildingCode);
        List<AreaBuildingZoning> list = areaBuildingZoningMapper.selectAreaBuildingZoningList(zParam);
        fillFields(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);
            fillFields(list);

            retList.addAll(list);
        }

        return retList;
    }

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

        if (CollectionUtils.isNotEmpty(areaBuildingZoning.getTagCodeList())) {
            List<String> tagArray = areaBuildingZoning.getTagCodeList();
            List<ObjTagRel> tagRelList = new ArrayList<>();

            for (String tagCode : tagArray) {
                ObjTagRel tagRel = new ObjTagRel(3, tagCode, areaBuildingZoning.getZoningCode());
                tagRelList.add(tagRel);
            }

            tagRelService.insertBatch(tagRelList);
        }

        return count;
    }

    /**
     * 修改建筑区域划分
     *
     * @param areaBuildingZoning 建筑区域划分
     * @return 结果
     */
    @Override
    public int updateAreaBuildingZoning(AreaBuildingZoning areaBuildingZoning) {
        int count = areaBuildingZoningMapper.updateAreaBuildingZoning(areaBuildingZoning);
        if (CollectionUtils.isNotEmpty(areaBuildingZoning.getTagCodeList())) {
            List<String> tagArray = areaBuildingZoning.getTagCodeList();
            List<ObjTagRel> tagRelList = new ArrayList<>();

            for (String tagCode : tagArray) {
                ObjTagRel tagRel = new ObjTagRel(3, tagCode, areaBuildingZoning.getZoningCode());
                tagRelList.add(tagRel);
            }

            tagRelService.deleteByObjCode(3, areaBuildingZoning.getZoningCode());
            tagRelService.insertBatch(tagRelList);
        }
        else {
            tagRelService.deleteByObjCode(3, areaBuildingZoning.getZoningCode());
        }

        return count;
    }

    /**
     * 批量删除建筑区域划分
     *
     * @param ids 需要删除的建筑区域划分主键
     * @return 结果
     */
    @Override
    public int deleteAreaBuildingZoningByIds(Long[] ids) {
        for (Long id : ids) {
            AreaBuildingZoning zoning = areaBuildingZoningMapper.selectAreaBuildingZoningById(id);
            if (null != zoning) {
                tagRelService.deleteByObjCode(3, zoning.getZoningCode());
            }
        }

        return areaBuildingZoningMapper.deleteAreaBuildingZoningByIds(ids);
    }

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

        if (null != zoning) {
            tagRelService.deleteByObjCode(3, zoning.getZoningCode());
        }

        return areaBuildingZoningMapper.deleteAreaBuildingZoningById(id);
    }

    private void fillFields(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);
                zoning.setTagCodeList(Arrays.asList(StringUtils.split(zoning.getTagCodes(), ",")));
            }
        }
    }
}
