package com.yuchen.saas.park.service.service.property.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.manage.user.api.entity.UserDetail;
import com.yuchen.saas.manage.user.api.feign.IUserClient;
import com.yuchen.saas.park.api.dto.property.PropertyOwnerResidentDto;
import com.yuchen.saas.park.api.dto.property.PropertyUserHouseDto;
import com.yuchen.saas.park.api.dto.property.PropertyUserHouseQueryDto;
import com.yuchen.saas.park.api.dto.property.PropertyVerifyCodeDto;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.property.*;
import com.yuchen.saas.park.api.enums.property.*;
import com.yuchen.saas.park.api.vo.property.PropertyOwnerResidentListVo;
import com.yuchen.saas.park.api.vo.property.PropertyUserHouseBindResVo;
import com.yuchen.saas.park.api.vo.property.PropertyUserHouseListVo;
import com.yuchen.saas.park.service.mapper.property.PropertyUserHouseMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.property.*;
import com.yuchen.saas.parking.api.util.SmsTool;
import org.apache.commons.lang3.StringUtils;
import org.nest.springwrap.core.launch.constant.AppConstant;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.redis.cache.NestRedis;
import org.nest.springwrap.core.secure.ContextUser;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * @Author 蔡文强
 * @Date 2023/11/28 10:56
 * @Description 用户房产表 服务实现类
 */
@Service
public class PropertyUserHouseServiceImpl extends BaseServiceImpl<PropertyUserHouseMapper, PropertyUserHouse> implements IPropertyUserHouseService {

    @Resource
    private PmgBatParkInfoService parkInfoService;
    @Resource
    private IPropertyCustomerArchivesService customerArchivesService;
    @Resource
    private IPropertyHouseOwnerService houseOwnerService;
    @Resource
    private IPropertyHouseResidentService houseResidentService;
    @Resource
    private PropertyHousesService housesService;
    @Resource
    private PropertyBuildingsService buildingsService;
    @Resource
    private NestRedis nestRedis;
    @Resource
    private IUserClient userClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PropertyUserHouseBindResVo bindHouse(PropertyUserHouseDto userHouseDto) {
        PropertyUserHouseBindResVo resVo = new PropertyUserHouseBindResVo();
        ContextUser contextUser = AuthUtil.getUser();
        if (Func.isEmpty(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        Long userDetailId = contextUser.getUserDetailId();

        PropertyUserHouse select = this.lambdaQuery().eq(PropertyUserHouse::getUserDetailId, userDetailId).eq(PropertyUserHouse::getHouseId, userHouseDto.getHouseId())
                .eq(PropertyUserHouse::getIdentity, userHouseDto.getIdentity()).one();
        if (Func.isNotEmpty(select)) {
            throw new ServiceException("已绑定该房产，请勿重复绑定");
        }
        Integer count = this.lambdaQuery().eq(PropertyUserHouse::getUserDetailId, userDetailId).eq(PropertyUserHouse::getHouseId, userHouseDto.getHouseId())
                .ne(PropertyUserHouse::getIdentity, userHouseDto.getIdentity()).count();
        if (count > 0) {
            resVo.setResult(3);
            return resVo;
        }

        PropertyUserHouse userHouse = BeanUtil.copyProperties(userHouseDto, PropertyUserHouse.class);
        userHouse.setUserDetailId(userDetailId);

        PropertyHouses propertyHouses = housesService.getById(userHouseDto.getHouseId());
        if (Func.isEmpty(propertyHouses)) {
            throw new ServiceException("房源不存在");
        }
        userHouse.setParkId(propertyHouses.getParkId());

        //业主
        if (userHouseDto.getIdentity() == 1) {
            LambdaQueryWrapper<PropertyCustomerArchives> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(PropertyCustomerArchives::getCustomerType, CustomerTypeEnum.PERSON.getValue());
            queryWrapper.eq(PropertyCustomerArchives::getCustomerCategory, CustomerCategoryEnum.HOUSE_OWNER.getValue());
            queryWrapper.eq(PropertyCustomerArchives::getCustomerName, userHouseDto.getUserName());
            queryWrapper.and(wrapper -> wrapper
                    .or(item -> item
                            .eq(PropertyCustomerArchives::getLinkPhoneOne, userHouseDto.getUserPhone()))
                    .or(item -> item
                            .eq(PropertyCustomerArchives::getLinkPhoneTwo, userHouseDto.getUserPhone())
                    ));
            PropertyCustomerArchives customerArchives = customerArchivesService.getOne(queryWrapper);
            if (Func.isEmpty(customerArchives)) {
                resVo.setResult(2);
                return resVo;
            }

            PropertyHouseOwner houseOwner = houseOwnerService.lambdaQuery().eq(PropertyHouseOwner::getHouseId, userHouseDto.getHouseId()).eq(PropertyHouseOwner::getCustomerArchivesId, customerArchives.getId()).one();
            if (Func.isEmpty(houseOwner)) {
                resVo.setResult(2);
                return resVo;
            }
            userHouse.setLinkId(houseOwner.getId());
            //更新登记状态
            houseOwner.setRegisterStatus(RegisterStatusEnum.REGISTERED.getValue());
            houseOwnerService.updateById(houseOwner);

        }
        //成员或者租客
        if (userHouseDto.getIdentity() == 2 || userHouseDto.getIdentity() == 3) {
            //验证业主信息
            //客户档案
            LambdaQueryWrapper<PropertyCustomerArchives> ownerQueryWrapper = Wrappers.lambdaQuery();
            ownerQueryWrapper.eq(PropertyCustomerArchives::getCustomerType, CustomerTypeEnum.PERSON.getValue());
            ownerQueryWrapper.eq(PropertyCustomerArchives::getCustomerCategory, CustomerCategoryEnum.HOUSE_OWNER.getValue());
            ownerQueryWrapper.eq(PropertyCustomerArchives::getCustomerName, userHouseDto.getOwnerName());
            ownerQueryWrapper.eq(PropertyCustomerArchives::getLinkPhoneOne, userHouseDto.getOwnerPhone());
            PropertyCustomerArchives ownerCustomerArchives = customerArchivesService.getOne(ownerQueryWrapper);
            if (Func.isEmpty(ownerCustomerArchives)) {
                resVo.setResult(4);
                return resVo;
            }
            //业主表
            PropertyHouseOwner owner = houseOwnerService.lambdaQuery().eq(PropertyHouseOwner::getCustomerArchivesId, ownerCustomerArchives.getId())
                    .eq(PropertyHouseOwner::getHouseId, userHouseDto.getHouseId()).one();
            if (Func.isEmpty(owner)) {
                resVo.setResult(4);
                return resVo;
            }
            PropertyUserHouse ownerUserHouse = this.lambdaQuery().eq(PropertyUserHouse::getIdentity, 1)
                    .eq(PropertyUserHouse::getHouseId, userHouse.getHouseId()).eq(PropertyUserHouse::getUserPhone, userHouseDto.getOwnerPhone()).one();
            if (Func.isEmpty(owner)) {
                resVo.setResult(4);
                return resVo;
            }
            userHouse.setOwnerDetailId(ownerUserHouse.getUserDetailId());

            //查看住户客户档案是否存在
            LambdaQueryWrapper<PropertyCustomerArchives> residentQueryWrapper = Wrappers.lambdaQuery();
            residentQueryWrapper.eq(PropertyCustomerArchives::getCustomerType, CustomerTypeEnum.PERSON.getValue());
            residentQueryWrapper.eq(PropertyCustomerArchives::getCustomerCategory, userHouseDto.getIdentity() == 2 ? CustomerCategoryEnum.OWNER_MEMBER.getValue() : CustomerCategoryEnum.HOUSE_TENANT.getValue());//注意需不需要这个;
            residentQueryWrapper.eq(PropertyCustomerArchives::getLinkPhoneOne, userHouseDto.getUserPhone());
            PropertyCustomerArchives customerArchives = customerArchivesService.getOne(residentQueryWrapper);
            if (Func.isNotEmpty(customerArchives)) {
                //查看住户表
                PropertyHouseResident houseResident = houseResidentService.lambdaQuery().eq(PropertyHouseResident::getCustomerArchivesId, customerArchives.getId()).one();
                if (Func.isNotEmpty(houseResident)) {
                    userHouse.setLinkId(houseResident.getId());
                    //更新登记状态
                    houseResident.setRegisterStatus(RegisterStatusEnum.REGISTERED.getValue());
                    houseResidentService.updateById(houseResident);
                } else {
                    PropertyHouseResident add = new PropertyHouseResident();
                    add.setParkId(customerArchives.getParkId());
                    add.setHouseId(userHouseDto.getHouseId());
                    add.setOwnerId(owner.getId());
                    add.setRelation(userHouse.getRelation());
                    add.setCustomerArchivesId(customerArchives.getId());
                    add.setResidentStatus(ResidentStatusEnum.NORMAL.getValue());
                    add.setCreateType(CreateTypeEnum.AUTO_SYNC.getValue());
                    add.setCreatePeopleId(userDetailId);
                    //登记状态
                    add.setRegisterStatus(RegisterStatusEnum.REGISTERED.getValue());
                    houseResidentService.save(add);
                    userHouse.setLinkId(add.getId());
                }
            } else {//同步客户档案
                PropertyCustomerArchives createCustomerArchives = new PropertyCustomerArchives();
                createCustomerArchives.setParkId(propertyHouses.getParkId());
                createCustomerArchives.setCustomerType(CustomerTypeEnum.PERSON.getValue());
                createCustomerArchives.setCustomerCategory(userHouseDto.getIdentity() == 2 ? CustomerCategoryEnum.OWNER_MEMBER.getValue() : CustomerCategoryEnum.HOUSE_TENANT.getValue());
                createCustomerArchives.setCustomerName(userHouseDto.getUserName());
                createCustomerArchives.setLinkManOne(userHouseDto.getUserName());
                createCustomerArchives.setLinkPhoneOne(userHouseDto.getUserPhone());
                createCustomerArchives.setCreateType(CreateTypeEnum.AUTO_SYNC.getValue());
                createCustomerArchives.setCreatePeopleId(userDetailId);
                createCustomerArchives.setIsLink(YesOrNoEnum.YES.getValue());
                customerArchivesService.save(createCustomerArchives);
                PropertyHouseResident add = new PropertyHouseResident();
                add.setParkId(createCustomerArchives.getParkId());
                add.setHouseId(userHouseDto.getHouseId());
                add.setOwnerId(owner.getId());
                add.setRelation(userHouse.getRelation());
                add.setCustomerArchivesId(createCustomerArchives.getId());
                add.setResidentStatus(ResidentStatusEnum.NORMAL.getValue());
                add.setCreateType(CreateTypeEnum.AUTO_SYNC.getValue());
                add.setCreatePeopleId(userDetailId);
                //登记状态
                add.setRegisterStatus(RegisterStatusEnum.REGISTERED.getValue());
                houseResidentService.save(add);
                userHouse.setLinkId(add.getId());
            }
        }
        save(userHouse);
        resVo.setResult(1);
        return resVo;
    }

    @Override
    public Map<String, String> sendCode(String phone) {
        //验证手机号格式
        if (!Validator.isMobile(phone)) {
            throw new ServiceException("请输入正确的手机号码");
        }
        String code = RandomUtil.randomNumbers(6);
        //code五分钟过期
        nestRedis.setEx(phone, code, 5 * 60L);
        //发送短信
        SmsTool.sendForgotPasswordCodeNotice(phone, code);
        Long expire = nestRedis.ttl(phone);  //查询过期时间
        Map<String, String> result = new HashMap<>();
        result.put("expire", expire + "");
        return result;
    }

    @Override
    public Boolean verifyCode(PropertyVerifyCodeDto verifyCodeDto) {
        String phone = verifyCodeDto.getPhone();
        String code = verifyCodeDto.getCode();
        if (!Validator.isMobile(phone)) {
            throw new ServiceException("请输入正确的手机号码");
        }
        if (StringUtils.isBlank(code)) {
            throw new ServiceException("请输入验证码");
        }
        String saveCode = nestRedis.get(phone); //缓存中的验证码
        Properties props = System.getProperties();
        String profile = props.getProperty("nest.env");
        if (AppConstant.DEV_CODE.equals(profile) || AppConstant.TEST_CODE.equals(profile)) {
            saveCode = "6666";
        }
        if (StringUtils.isBlank(saveCode)) {
            throw new ServiceException("短信验证码过期了");
        }

        if (!saveCode.equals(code)) {
            throw new ServiceException("验证码错误,请重新输入");
        }

        return Boolean.TRUE;
    }

    @Override
    public NestPage<PropertyUserHouseListVo> getUserHouseList(PropertyUserHouseQueryDto queryDto) {
        LambdaQueryWrapper<PropertyUserHouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyUserHouse::getUserDetailId, queryDto.getUserDetailId());
        queryWrapper.eq(PropertyUserHouse::getUserPhone, queryDto.getUserPhone());
        IPage<PropertyUserHouse> page = this.page(Condition.getPage(queryDto), queryWrapper);
        List<PropertyUserHouseListVo> list = BeanUtil.copyProperties(page.getRecords(), PropertyUserHouseListVo.class);
        List<PmgBatParkInfo> parkList = parkInfoService.list();
        Map<Long, PmgBatParkInfo> parkMap = parkList.stream().collect(Collectors.toMap(PmgBatParkInfo::getId, a -> a));
        List<PropertyHouses> houseList = housesService.list();
        Map<Long, PropertyHouses> houseMap = houseList.stream().collect(Collectors.toMap(PropertyHouses::getId, a -> a));
        List<PropertyBuildings> buildingList = buildingsService.list();
        Map<Long, PropertyBuildings> buildingListMap = buildingList.stream().collect(Collectors.toMap(PropertyBuildings::getId, a -> a));
        for (PropertyUserHouseListVo vo : list) {
            vo.setParkName(parkMap.get(vo.getParkId()).getParkName());
            vo.setParkAddr(parkMap.get(vo.getParkId()).getParkGeographicalLocation());
            vo.setHouseName(buildingListMap.get(houseMap.get(vo.getHouseId()).getBuildingId()).getBuildingName() + houseMap.get(vo.getHouseId()).getHouseName());
        }
        IPage<PropertyUserHouseListVo> result = new Page<>(queryDto.getCurrent(), queryDto.getSize(), list.size());
        result.setRecords(list);
        return NestPage.of(result);
    }

    @Override
    public Boolean deleteUserHouse(Long id) {
        PropertyUserHouse userHouse = getById(id);
        userHouse.setIsInvalid(1);
        updateById(userHouse);
        //更新住户表或者业主登记状态
        if (userHouse.getIdentity() == 1) {
            PropertyHouseOwner houseOwner = houseOwnerService.getById(userHouse.getLinkId());
            houseOwner.setRegisterStatus(RegisterStatusEnum.EXPIRED.getValue());
            houseOwnerService.updateById(houseOwner);
        }
        if (userHouse.getIdentity() == 2 || userHouse.getIdentity() == 3) {
            PropertyHouseResident houseResident = houseResidentService.getById(userHouse.getLinkId());
            houseResident.setRegisterStatus(RegisterStatusEnum.EXPIRED.getValue());
            houseResidentService.updateById(houseResident);
        }
        return removeById(id);
    }

    @Override
    public Boolean saveOrUpdateResident(PropertyOwnerResidentDto ownerResidentDto) {
        if (Func.isEmpty(ownerResidentDto.getId())) {
            PropertyUserHouse userHouse = BeanUtil.copyProperties(ownerResidentDto, PropertyUserHouse.class);
            ContextUser contextUser = AuthUtil.getUser();
            if (Func.isEmpty(contextUser)) {
                throw new ServiceException("用户不存在");
            }
            Long userDetailId = contextUser.getUserDetailId();
            //业主用户ID
            userHouse.setOwnerDetailId(userDetailId);
            //住户用户ID
            R<UserDetail> userDetailR = userClient.userDetailByPhone(userHouse.getUserPhone());
            if (Func.isNotEmpty(userDetailR) && Func.isNotEmpty(userDetailR.getData())) {
                userHouse.setUserDetailId(userDetailR.getData().getId());
            }
            //查看住户客户档案是否存在
            LambdaQueryWrapper<PropertyCustomerArchives> residentQueryWrapper = Wrappers.lambdaQuery();
            residentQueryWrapper.eq(PropertyCustomerArchives::getCustomerType, CustomerTypeEnum.PERSON.getValue());
            //todo 这边要判断是租客或者成员
            residentQueryWrapper.eq(PropertyCustomerArchives::getCustomerCategory, ownerResidentDto.getIdentity() == 2 ? CustomerCategoryEnum.OWNER_MEMBER.getValue() : CustomerCategoryEnum.HOUSE_TENANT.getValue());//注意需不需要这个;
            residentQueryWrapper.eq(PropertyCustomerArchives::getLinkPhoneOne, userHouse.getUserPhone());
            PropertyCustomerArchives customerArchives = customerArchivesService.getOne(residentQueryWrapper);
            //业主
            PropertyUserHouse ownerUserHouse = this.lambdaQuery().eq(PropertyUserHouse::getUserDetailId, userDetailId)
                    .eq(PropertyUserHouse::getHouseId, ownerResidentDto.getHouseId())
                    .eq(PropertyUserHouse::getIdentity, 1).one();
            if (Func.isEmpty(ownerUserHouse)) {
                throw new ServiceException("业主不存在");
            }
            LambdaQueryWrapper<PropertyCustomerArchives> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(PropertyCustomerArchives::getCustomerType, CustomerTypeEnum.PERSON.getValue());
            queryWrapper.eq(PropertyCustomerArchives::getCustomerCategory, CustomerCategoryEnum.HOUSE_OWNER.getValue());
            queryWrapper.and(wrapper -> wrapper
                    .or(item -> item
                            .eq(PropertyCustomerArchives::getLinkPhoneOne, ownerUserHouse.getUserPhone()))
                    .or(item -> item
                            .eq(PropertyCustomerArchives::getLinkPhoneTwo, ownerUserHouse.getUserPhone())
                    ));
            PropertyCustomerArchives ownercustomerArchives = customerArchivesService.getOne(queryWrapper);
            if (Func.isEmpty(ownerUserHouse)) {
                throw new ServiceException("业主不存在");
            }

            PropertyHouseOwner owner = houseOwnerService.lambdaQuery().eq(PropertyHouseOwner::getOwnerType, OwnerTypeEnum.PERSON.getValue())
                    .eq(PropertyHouseOwner::getCustomerArchivesId, ownercustomerArchives.getId())
                    .eq(PropertyHouseOwner::getHouseId, userHouse.getHouseId()).one();
            if (Func.isEmpty(owner)) {
                throw new ServiceException("业主不存在");
            }

            if (Func.isNotEmpty(customerArchives)) {
                //查看住户表
                PropertyHouseResident houseResident = houseResidentService.lambdaQuery().eq(PropertyHouseResident::getCustomerArchivesId, customerArchives.getId()).one();
                if (Func.isNotEmpty(houseResident)) {
                    userHouse.setLinkId(houseResident.getId());
                    houseResident.setRegisterStatus(RegisterStatusEnum.REGISTERED.getValue());
                    houseResidentService.updateById(houseResident);
                } else {
                    PropertyHouseResident add = new PropertyHouseResident();
                    add.setParkId(customerArchives.getParkId());
                    add.setHouseId(userHouse.getHouseId());
                    add.setOwnerId(owner.getId());
                    add.setRelation(userHouse.getRelation());
                    add.setCustomerArchivesId(customerArchives.getId());
                    add.setResidentStatus(ResidentStatusEnum.NORMAL.getValue());
                    add.setCreateType(CreateTypeEnum.AUTO_SYNC.getValue());
                    add.setCreatePeopleId(userDetailId);
                    add.setRegisterStatus(RegisterStatusEnum.REGISTERED.getValue());
                    houseResidentService.save(add);
                    userHouse.setLinkId(add.getId());
                }
                customerArchives.setCustomerName(userHouse.getUserName());
                customerArchives.setLinkManOne(userHouse.getUserName());
                customerArchives.setSex(userHouse.getSex());
                customerArchives.setFacePhoto(userHouse.getPhoto());
                customerArchivesService.updateById(customerArchives);
            } else {//同步客户档案
                PropertyCustomerArchives createCustomerArchives = new PropertyCustomerArchives();
                createCustomerArchives.setParkId(userHouse.getParkId());
                createCustomerArchives.setCustomerType(CustomerTypeEnum.PERSON.getValue());
                createCustomerArchives.setCustomerCategory(ownerResidentDto.getIdentity() == 2 ? CustomerCategoryEnum.OWNER_MEMBER.getValue() : CustomerCategoryEnum.HOUSE_TENANT.getValue());
                createCustomerArchives.setCustomerName(userHouse.getUserName());
                createCustomerArchives.setLinkManOne(userHouse.getUserName());
                createCustomerArchives.setLinkPhoneOne(userHouse.getUserPhone());
                createCustomerArchives.setSex(userHouse.getSex());
                createCustomerArchives.setCreateType(CreateTypeEnum.AUTO_SYNC.getValue());
                createCustomerArchives.setCreatePeopleId(userDetailId);
                createCustomerArchives.setIsLink(YesOrNoEnum.YES.getValue());
                customerArchivesService.save(createCustomerArchives);
                PropertyHouseResident add = new PropertyHouseResident();
                add.setParkId(createCustomerArchives.getParkId());
                add.setHouseId(userHouse.getHouseId());
                add.setOwnerId(owner.getId());
                add.setRelation(userHouse.getRelation());
                add.setCustomerArchivesId(createCustomerArchives.getId());
                add.setResidentStatus(ResidentStatusEnum.NORMAL.getValue());
                add.setCreateType(CreateTypeEnum.AUTO_SYNC.getValue());
                add.setCreatePeopleId(userDetailId);
                add.setRegisterStatus(RegisterStatusEnum.REGISTERED.getValue());
                houseResidentService.save(add);
                userHouse.setLinkId(add.getId());
            }
            return save(userHouse);
        } else {
            PropertyUserHouse userHouse = BeanUtil.copyProperties(ownerResidentDto, PropertyUserHouse.class);
            return updateById(userHouse);
        }
    }

    @Override
    public Boolean removeResident(Long id) {
        PropertyUserHouse userHouse = getById(id);
        userHouse.setIsInvalid(1);
        updateById(userHouse);
        //更新住户表或者业主登记状态
        if (userHouse.getIdentity() == 1) {
            PropertyHouseOwner houseOwner = houseOwnerService.getById(userHouse.getLinkId());
            houseOwner.setRegisterStatus(RegisterStatusEnum.EXPIRED.getValue());
            houseOwnerService.updateById(houseOwner);
        }
        if (userHouse.getIdentity() == 2 || userHouse.getIdentity() == 3) {
            PropertyHouseResident houseResident = houseResidentService.getById(userHouse.getLinkId());
            houseResident.setResidentStatus(2);
            houseResident.setRegisterStatus(RegisterStatusEnum.EXPIRED.getValue());
            houseResidentService.updateById(houseResident);
        }
        return removeById(id);
    }

    @Override
    public NestPage<PropertyOwnerResidentListVo> getResidentList(PropertyUserHouseQueryDto queryDto) {
        LambdaQueryWrapper<PropertyUserHouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyUserHouse::getOwnerDetailId, queryDto.getUserDetailId());
        queryWrapper.eq(PropertyUserHouse::getHouseId, queryDto.getHouseId());
        IPage<PropertyUserHouse> page = this.page(Condition.getPage(queryDto), queryWrapper);
        List<PropertyOwnerResidentListVo> list = BeanUtil.copyProperties(page.getRecords(), PropertyOwnerResidentListVo.class);
        IPage<PropertyOwnerResidentListVo> result = new Page<>(queryDto.getCurrent(), queryDto.getSize(), page.getTotal());
        result.setRecords(list);
        return NestPage.of(result);
    }

    @Override
    public List<Long> getUserHouseParkList(Long userDetailId) {
        LambdaQueryWrapper<PropertyUserHouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyUserHouse::getUserDetailId, userDetailId);
        queryWrapper.eq(PropertyUserHouse::getIsInvalid, 0);
        List<PropertyUserHouse> list = this.list(queryWrapper);
        return list.stream().map(PropertyUserHouse::getParkId).distinct().collect(Collectors.toList());
    }

    @Override
    public PropertyUserHouse getByUserDetailIdAndHouseId(Long userDetailId, Long houseId) {
        LambdaQueryWrapper<PropertyUserHouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyUserHouse::getHouseId,houseId)
                .eq(PropertyUserHouse::getUserDetailId,userDetailId)
                .eq(PropertyUserHouse::getIsInvalid,0).last("limit 1");
        return getOne(queryWrapper);
    }

}
