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

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.park.api.dto.property.PropertyEnterpriseAddressListQueryDto;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.property.PropertyEnterpriseAddress;
import com.yuchen.saas.park.api.vo.property.PropertyEnterpriseAddressListVo;
import com.yuchen.saas.park.api.vo.property.PropertyHousesVo;
import com.yuchen.saas.park.service.mapper.property.PropertyEnterpriseAddressMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.property.IPropertyEnterpriseAddressService;
import com.yuchen.saas.park.service.service.property.PropertyHousesService;
import lombok.extern.slf4j.Slf4j;
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.tool.utils.BeanUtil;
import org.springframework.stereotype.Service;

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

/**
 * @Author 蔡文强
 * @Date 2024/1/2 10:56
 * @Description 物业企业入驻地址表 服务实现类
 */
@Slf4j
@Service
public class PropertyEnterpriseAddressServiceImpl extends BaseServiceImpl<PropertyEnterpriseAddressMapper, PropertyEnterpriseAddress> implements IPropertyEnterpriseAddressService {

    @Resource
    private PmgBatParkInfoService parkInfoService;
    @Resource
    private PropertyHousesService housesService;

    @Override
    public NestPage<PropertyEnterpriseAddressListVo> getEnterpriseAddressList(PropertyEnterpriseAddressListQueryDto queryDto) {
        LambdaQueryWrapper<PropertyEnterpriseAddress> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyEnterpriseAddress::getDeptId, queryDto.getEnterpriseId());
        IPage<PropertyEnterpriseAddress> page = this.page(Condition.getPage(queryDto), queryWrapper);
        List<PropertyEnterpriseAddressListVo> list = BeanUtil.copyProperties(page.getRecords(), PropertyEnterpriseAddressListVo.class);
        Map<Long, PmgBatParkInfo> parkInfoMap = getParkInfoMap();
        for (PropertyEnterpriseAddressListVo vo : list) {
            vo.setParkName(parkInfoMap.get(vo.getParkId()).getParkName());
            vo.setParkAddr(parkInfoMap.get(vo.getParkId()).getParkGeographicalLocation());
            PropertyHousesVo propertyHousesVo = housesService.getPropertyHousesDetail(vo.getHouseId());
            //拼接显示楼栋+单元+楼层+房间名称。
            vo.setAddress(propertyHousesVo.getBuildingName() + propertyHousesVo.getHouseName());
        }
        IPage<PropertyEnterpriseAddressListVo> result = new Page<>(queryDto.getCurrent(), queryDto.getSize(), page.getTotal());
        result.setRecords(list);
        return NestPage.of(result);
    }

    @Override
    public PropertyEnterpriseAddress getByDeptAndHouseId(Long deptId,Long houseId) {
        LambdaQueryWrapper<PropertyEnterpriseAddress> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyEnterpriseAddress::getDeptId,deptId)
                .eq(PropertyEnterpriseAddress::getHouseId,houseId)
                .eq(PropertyEnterpriseAddress::getIsValid,0).last("limit 1");
        return getOne(queryWrapper);
    }

    /**
     * @description 获取园区信息列表Map
     */
    private Map<Long, PmgBatParkInfo> getParkInfoMap() {
        List<PmgBatParkInfo> parkList = parkInfoService.getAllPark();
        return parkList.stream().collect(Collectors.toMap(PmgBatParkInfo::getId, a -> a));
    }

}
