package com.ruoyi.ems.service.impl;

import com.ruoyi.ems.domain.AreaBuilding;
import com.ruoyi.ems.mapper.AreaBuildingMapper;
import com.ruoyi.ems.mapper.AreaBuildingZoningMapper;
import com.ruoyi.ems.service.IAreaBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 建筑基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-09
 */
@Service
public class AreaBuildingServiceImpl implements IAreaBuildingService {
    @Autowired
    private AreaBuildingMapper areaBuildingMapper;

    @Autowired
    private AreaBuildingZoningMapper areaBuildingZoningMapper;

    /**
     * 查询建筑基本信息
     *
     * @param id 建筑基本信息主键
     * @return 建筑基本信息
     */
    @Override
    public AreaBuilding selectAreaBuildingById(Long id) {
        return areaBuildingMapper.selectAreaBuildingById(id);
    }

    /**
     * 查询建筑基本信息
     *
     * @param code 建筑基本信息code
     * @return 建筑基本信息
     */
    @Override
    public AreaBuilding selectAreaBuildingByCode(String code) {
        return areaBuildingMapper.selectAreaBuildingByCode(code);
    }

    /**
     * 查询建筑基本信息列表
     *
     * @param areaBuilding 建筑基本信息
     * @return 建筑基本信息
     */
    @Override
    public List<AreaBuilding> selectAreaBuildingList(AreaBuilding areaBuilding) {
        return areaBuildingMapper.selectAreaBuildingList(areaBuilding);
    }

    @Override
    public List<AreaBuilding> selectBuildingByAreaCode(String areaCode) {
        AreaBuilding param = new AreaBuilding();
        param.setAreaCode(areaCode);
        return areaBuildingMapper.selectAreaBuildingList(param);
    }

    /**
     * 新增建筑基本信息
     *
     * @param areaBuilding 建筑基本信息
     * @return 结果
     */
    @Override
    public int insertAreaBuilding(AreaBuilding areaBuilding) {
        return areaBuildingMapper.insertAreaBuilding(areaBuilding);
    }

    /**
     * 修改建筑基本信息
     *
     * @param areaBuilding 建筑基本信息
     * @return 结果
     */
    @Override
    public int updateAreaBuilding(AreaBuilding areaBuilding) {
        return areaBuildingMapper.updateAreaBuilding(areaBuilding);
    }

    /**
     * 批量删除建筑基本信息
     *
     * @param ids 需要删除的建筑基本信息主键
     * @return 结果
     */
    @Override
    public int deleteAreaBuildingByIds(Long[] ids) {
        for (Long id : ids) {
            AreaBuilding building = selectAreaBuildingById(id);

            if (null != building) {
                deleteRel(building.getBldgCode());
            }
        }

        return areaBuildingMapper.deleteAreaBuildingByIds(ids);
    }

    /**
     * 删除建筑基本信息信息
     *
     * @param id 建筑基本信息主键
     * @return 结果
     */
    @Override
    public int deleteAreaBuildingById(Long id) {
        AreaBuilding building = selectAreaBuildingById(id);

        if (null != building) {
            deleteRel(building.getBldgCode());
        }

        return areaBuildingMapper.deleteAreaBuildingById(id);
    }

    @Override
    public int deleteAreaBuilding(AreaBuilding building) {
        deleteRel(building.getBldgCode());
        return deleteAreaBuildingById(building.getId());
    }

    public void deleteRel(String bldgCode) {
        areaBuildingZoningMapper.deleteAreaBuildingZoningByBuilding(bldgCode);
    }
}
