package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ResidentInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.houseInfo.HouseInfo;
import com.ruoyi.system.mapper.HouseInfoMapper;
import com.ruoyi.system.mapper.ResidentInfoMapper;
import com.ruoyi.system.service.IHouseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 房屋信息Service业务层处理
 * 
 * @author boman
 * @date 2025-02-20
 */
@Service
public class HouseInfoServiceImpl implements IHouseInfoService 
{
    @Autowired
    private HouseInfoMapper houseInfoMapper;
    @Autowired
    private ResidentInfoMapper residentInfoMapper;

    /**
     * 查询房屋信息
     * 
     * @param houseId 房屋信息主键
     * @return 房屋信息
     */
    @Override
    public HouseInfo selectHouseInfoByHouseId(Long houseId)
    {
        return houseInfoMapper.selectHouseInfoByHouseId(houseId);
    }

    /**
     * 查询房屋信息列表
     * 
     * @param houseInfo 房屋信息
     * @return 房屋信息
     */
    @Override
    public List<HouseInfo> selectHouseInfoList(HouseInfo houseInfo)
    {
        return houseInfoMapper.selectHouseInfoList(houseInfo);
    }

    /**
     * 查询房屋信息列表带人员信息
     * @param residentInfo
     * @return
     */
    @Override
    public List<ResidentInfo> selectHouseInfoListAndResidentInfo(ResidentInfo residentInfo) {
        return houseInfoMapper.selectHouseInfoListAndResidentInfo(residentInfo);
    }

    /**
     * 新增房屋信息
     * 
     * @param houseInfo 房屋信息
     * @return 结果
     */
    @Override
    public int insertHouseInfo(HouseInfo houseInfo)
    {
        houseInfo.setCreateTime(DateUtils.getNowDate());
        return houseInfoMapper.insertHouseInfo(houseInfo);
    }

    /**
     * 修改房屋信息
     * 
     * @param houseInfo 房屋信息
     * @return 结果
     */
    @Override
    public int updateHouseInfo(HouseInfo houseInfo)
    {
        houseInfo.setUpdateTime(DateUtils.getNowDate());
        return houseInfoMapper.updateHouseInfo(houseInfo);
    }

    /**
     * 批量删除房屋信息
     * 
     * @param houseIds 需要删除的房屋信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseInfoByHouseIds(Long[] houseIds)
    {
        return houseInfoMapper.deleteHouseInfoByHouseIds(houseIds);
    }

    /**
     * 删除房屋信息信息
     * 
     * @param houseId 房屋信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseInfoByHouseId(Long houseId)
    {
        return houseInfoMapper.deleteHouseInfoByHouseId(houseId);
    }

    @Override
    public AjaxResult importHouseInfo(List<HouseInfo> houseInfoList) {
        if (houseInfoList == null || houseInfoList.size() < 1) {
            return AjaxResult.error("当前表格数据为空");
        }
        try{
            for (HouseInfo houseInfo : houseInfoList) {

                houseInfo.setCreateTime(DateUtils.getNowDate());
                houseInfoMapper.insertHouseInfo(houseInfo);
                List<ResidentInfo> residentInfoList = houseInfo.getResidentInfoList();
                for (ResidentInfo residentInfo : residentInfoList) {
                    residentInfo.setHouseId(houseInfo.getHouseId());
                }
                residentInfoMapper.insertResidentInfoList(residentInfoList);

            }
            return AjaxResult.success("导入成功");
        }catch (Exception e){
            return AjaxResult.error("导入失败");
        }
    }
}
