package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.domain.houseInfo.HouseInfo;
import org.dromara.domain.houseInfo.bo.HouseInfoBo;
import org.dromara.domain.houseInfo.vo.HouseInfoVo;
import org.dromara.mapper.HouseInfoMapper;
import org.dromara.service.IHouseInfoService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 房屋信息Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class HouseInfoServiceImpl implements IHouseInfoService {

    private final HouseInfoMapper baseMapper;

    /**
     * 查询房屋信息
     *
     * @param houseId 主键
     * @return 房屋信息
     */
    @Override
    public HouseInfoVo queryById(Long houseId){
        return baseMapper.selectVoById(houseId);
    }

    /**
     * 分页查询房屋信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 房屋信息分页列表
     */
    @Override
    public TableDataInfo<HouseInfoVo> queryPageList(HouseInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HouseInfo> lqw = buildQueryWrapper(bo);
        Page<HouseInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的房屋信息列表
     *
     * @param bo 查询条件
     * @return 房屋信息列表
     */
    @Override
    public List<HouseInfoVo> queryList(HouseInfoBo bo) {
        LambdaQueryWrapper<HouseInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HouseInfo> buildQueryWrapper(HouseInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HouseInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HouseInfo::getHouseId);
        lqw.like(StringUtils.isNotBlank(bo.getOwnerName()), HouseInfo::getOwnerName, bo.getOwnerName());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), HouseInfo::getLocation, bo.getLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), HouseInfo::getTenantId, bo.getTenantId());
        lqw.eq(StringUtils.isNotBlank(bo.getRightType()), HouseInfo::getRightType, bo.getRightType());
        lqw.eq(bo.getArea() != null, HouseInfo::getArea, bo.getArea());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentNumber()), HouseInfo::getDocumentNumber, bo.getDocumentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getPropertyUnitNumber()), HouseInfo::getPropertyUnitNumber, bo.getPropertyUnitNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getCoOwnership()), HouseInfo::getCoOwnership, bo.getCoOwnership());
        lqw.eq(StringUtils.isNotBlank(bo.getUsageType()), HouseInfo::getUsageType, bo.getUsageType());
        lqw.eq(StringUtils.isNotBlank(bo.getUsagePeriod()), HouseInfo::getUsagePeriod, bo.getUsagePeriod());
        lqw.eq(bo.getRegistrationDate() != null, HouseInfo::getRegistrationDate, bo.getRegistrationDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCoOwner()), HouseInfo::getCoOwner, bo.getCoOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailedAddress()), HouseInfo::getDetailedAddress, bo.getDetailedAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getHasParkingSpace()), HouseInfo::getHasParkingSpace, bo.getHasParkingSpace());
        lqw.eq(StringUtils.isNotBlank(bo.getParkingNumber()), HouseInfo::getParkingNumber, bo.getParkingNumber());
        lqw.like(StringUtils.isNotBlank(bo.getCommunityName()), HouseInfo::getCommunityName, bo.getCommunityName());
        lqw.eq(bo.getHouseStatus() != null, HouseInfo::getHouseStatus, bo.getHouseStatus());
        return lqw;
    }

    /**
     * 新增房屋信息
     *
     * @param bo 房屋信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HouseInfoBo bo) {
        HouseInfo add = MapstructUtils.convert(bo, HouseInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setHouseId(add.getHouseId());
        }
        return flag;
    }

    /**
     * 修改房屋信息
     *
     * @param bo 房屋信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HouseInfoBo bo) {
        HouseInfo update = MapstructUtils.convert(bo, HouseInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HouseInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除房屋信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
