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.*;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.property.*;
import com.yuchen.saas.park.api.vo.property.PropertyHouseMeterAppPageVo;
import com.yuchen.saas.park.api.vo.property.PropertyHouseMeterDetailVo;
import com.yuchen.saas.park.api.vo.property.PropertyHouseMeterPageVo;
import com.yuchen.saas.park.api.vo.property.PropertyHouseMeterReadingByAppVo;
import com.yuchen.saas.park.service.mapper.property.PropertyHouseMeterMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.property.*;
import com.yuchen.saas.park.service.util.BigdecimalUtil;
import com.yuchen.saas.parking.api.util.DateUtils;
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.tool.utils.Func;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 房间仪表 服务实现类
 * </p>
 *
 * @author cxj
 * @since 2023-11-21
 */
@Service
public class PropertyHouseMeterServiceImpl extends BaseServiceImpl<PropertyHouseMeterMapper, PropertyHouseMeter> implements IPropertyHouseMeterService {
    private final static BigDecimal MAX_NUM = BigdecimalUtil.stringConvertBigDecimal("9999.99");
    private final static BigDecimal RATE = BigdecimalUtil.ONE;
    private final static BigDecimal ATTRITION_RATE = BigdecimalUtil.ZERO;

    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Resource
    private IPropertyMeterService iPropertyMeterService;

    @Resource
    private PropertyBuildingsService propertyBuildingsService;

    @Resource
    private PropertyHousesService propertyHousesService;

    @Resource
    private IPropertyHouseMeterReadingService iPropertyHouseMeterReadingService;

    @Resource
    private PropertyFeeStandardService propertyFeeStandardService;

    @Lazy
    @Resource
    private IPropertyPublicMeterService iPropertyPublicMeterService;

    @Resource
    private IPropertyHouseBindFeeService iPropertyHouseBindFeeService;

    @Override
    public NestPage<PropertyHouseMeterPageVo> getPage(PropertyHouseMeterQueryDto dto) {
        LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
        if (Func.isNotEmpty(dto.getIsBinding())) {
            if(dto.getIsBinding()==1){
                queryWrapper.isNotNull(PropertyHouseMeter::getChargingStandardId);
                queryWrapper.gt(PropertyHouseMeter::getChargingStandardId,0);
            }
            if(dto.getIsBinding()==0){
                queryWrapper.isNull(PropertyHouseMeter::getChargingStandardId);
            }

        }
        queryWrapper.eq(Func.isNotEmpty(dto.getParkId()), PropertyHouseMeter::getParkId, dto.getParkId())
                .eq(Func.isNotEmpty(dto.getResourceCode()), PropertyHouseMeter::getResourceCode, dto.getResourceCode())
                .eq(Func.isNotEmpty(dto.getMeterId()), PropertyHouseMeter::getMeterId, dto.getMeterId())
                .eq(Func.isNotEmpty(dto.getChargingStandardId()), PropertyHouseMeter::getChargingStandardId, dto.getChargingStandardId())
                .eq(Func.isNotEmpty(dto.getIsSealed()), PropertyHouseMeter::getIsSealed, dto.getIsSealed())
                .orderByDesc(PropertyHouseMeter::getCreateTime);
        IPage<PropertyHouseMeter> page = this.page(Condition.getPage(dto), queryWrapper);
        IPage<PropertyHouseMeterPageVo> result = new Page<>();
        Func.copy(page, result);
        List<PropertyHouseMeterPageVo> list = page.getRecords().stream().map(a -> {
            PropertyHouseMeterPageVo vo = new PropertyHouseMeterPageVo();
            Func.copy(a, vo);
            PmgBatParkInfo park = pmgBatParkInfoService.getById(a.getParkId());
            if (Func.isNotEmpty(park)) {
                vo.setParkName(park.getParkName());
            }
            PropertyMeter meter = iPropertyMeterService.getById(a.getMeterId());
            if (Func.isNotEmpty(meter)) {
                vo.setMeterName(meter.getMeterName());
            }
            PropertyBuildings buildings = propertyBuildingsService.getById(a.getBuildingId());
            if (Func.isNotEmpty(buildings)) {
                vo.setBuildingName(buildings.getBuildingName());
            }
            PropertyHouses houses = propertyHousesService.getById(a.getHouseId());
            if (Func.isNotEmpty(houses)) {
                vo.setHouseName(houses.getHouseName());
            }
            if(Func.isNotEmpty(vo.getChargingStandardId())){
                PropertyFeeStandard feeStandard = propertyFeeStandardService.getById(vo.getChargingStandardId());
                if(Func.isNotEmpty(feeStandard)){
                    vo.setChargingStandardName(feeStandard.getFeeName());
                }
            }
            if(Func.isNotEmpty(vo.getParentId())&&vo.getParentId()>0){
                PropertyPublicMeter publicMeter = iPropertyPublicMeterService.getById(vo.getParentId());
                if(Func.isNotEmpty(publicMeter)){
                    vo.setParentName(publicMeter.getMeterFullName());
                }
            }

            return vo;
        }).collect(Collectors.toList());
        result.setRecords(list);
        return NestPage.of(result);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrUpdate(PropertyHouseMeterAddDto dto) {
        PropertyMeter meter = iPropertyMeterService.getById(dto.getMeterId());
        if (Func.isEmpty(meter)) {
            throw new ServiceException("所选仪表类型不存在,请重新刷新!");
        }
        PropertyHouseMeter houseMeter;
        Boolean flag=false;
        if (Func.isNotEmpty(dto.getId())) {
            flag=true;
            PropertyHouseMeter oldHouseMeter = getById(dto.getId());

            if (Func.isEmpty(oldHouseMeter)) {
                throw new ServiceException("所选数据不存在,请重新刷新!");
            }
            houseMeter = Func.copy(dto, PropertyHouseMeter.class);
            houseMeter.setId(oldHouseMeter.getId());
            if(iPropertyHouseMeterReadingService.countByIsCharging(oldHouseMeter.getId())>0){
                throw new ServiceException("仪表已产生账单已计费,不能进行编辑!");
            }
            //获取最新一条抄表记录
            PropertyHouseMeterReading houseMeterReading = iPropertyHouseMeterReadingService.getByHouseMeterIdAndQuery(houseMeter.getId(), null, null, null, null, 1);
            //是不是更改仪表数据
            if(Func.isNotEmpty(houseMeterReading)){
                if (Func.isNotEmpty(houseMeterReading.getIsInit()) && houseMeterReading.getIsInit() == 1) {
                }else{
                    if(Func.equalsSafe(dto.getInitNum(),oldHouseMeter.getInitNum())){
                        throw new ServiceException("仪表已有抄表数据,初始读数不能修改!");
                    }
                    if(Func.equalsSafe(dto.getInitTime(),oldHouseMeter.getInitTime())){
                        throw new ServiceException("仪表已有抄表数据,初始日期不能修改!");
                    }
                    if(Func.equalsSafe(dto.getParkId(),oldHouseMeter.getParkId())){
                        throw new ServiceException("仪表已有抄表数据,园区不能修改!");
                    }
                    if(Func.equalsSafe(dto.getBuildingId(),oldHouseMeter.getBuildingId())){
                        throw new ServiceException("仪表已有抄表数据,楼栋不能修改!");
                    }
                    if(Func.equalsSafe(dto.getHouseId(),oldHouseMeter.getHouseId())){
                        throw new ServiceException("仪表已有抄表数据,房间不能修改!");
                    }if(Func.equalsSafe(dto.getHouseId(),oldHouseMeter.getHouseId())){
                        throw new ServiceException("仪表已有抄表数据,房间不能修改!");
                    }
                }
            }



        } else {
            houseMeter = Func.copy(dto, PropertyHouseMeter.class);
        }

        PmgBatParkInfo park = pmgBatParkInfoService.getById(dto.getParkId());
        if (Func.isEmpty(park)) {
            throw new ServiceException("所选园区不存在,请重新刷新!");
        }
        houseMeter.setParkId(dto.getParkId());
        houseMeter.setResourceType(1);
        houseMeter.setResourceId(dto.getParkId());
        //todo 设置园区的资源代码,目前园区还没有资源代码
        houseMeter.setResourceCode(null);
        if (Func.isNotEmpty(dto.getBuildingId())) {
            PropertyBuildings buildings = propertyBuildingsService.getById(dto.getBuildingId());
            if (Func.isEmpty(buildings)) {
                throw new ServiceException("所选楼栋不存在,请重新刷新!");
            }
            houseMeter.setParkId(buildings.getParkId());
            houseMeter.setBuildingId(dto.getBuildingId());
            houseMeter.setResourceCode(buildings.getResourceCode());
            houseMeter.setResourceId(dto.getBuildingId());
            houseMeter.setResourceType(2);
        } else {
            houseMeter.setBuildingId(null);
            houseMeter.setFloorNum(null);
            houseMeter.setUnitNum(null);
            houseMeter.setHouseId(null);
        }

        if (Func.isNotEmpty(dto.getHouseId())) {
            PropertyHouses houses = propertyHousesService.getById(dto.getHouseId());
            if (Func.isEmpty(houses)) {
                throw new ServiceException("所选房间已不存在!");
            }
            houseMeter.setParkId(houses.getParkId());
            houseMeter.setBuildingId(houses.getBuildingId());
            houseMeter.setFloorNum(houses.getFloorNum());
            houseMeter.setUnitNum(houses.getUnitNum());
            houseMeter.setHouseId(houses.getId());
            houseMeter.setResourceCode(houses.getResourceCode());
            houseMeter.setResourceId(houses.getId());
            houseMeter.setResourceType(3);
        } else {
            houseMeter.setFloorNum(null);
            houseMeter.setUnitNum(null);
            houseMeter.setHouseId(null);
        }

        LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyHouseMeter::getParkId, dto.getParkId())
                .eq(Func.isNotEmpty(dto.getBuildingId()), PropertyHouseMeter::getBuildingId, dto.getBuildingId())
                .eq(Func.isNotEmpty(dto.getHouseId()), PropertyHouseMeter::getHouseId, dto.getHouseId())
                .eq(PropertyHouseMeter::getResourceType, houseMeter.getResourceType())
                .eq(PropertyHouseMeter::getMeterId, dto.getMeterId())
                .eq(PropertyHouseMeter::getMeterFullName, dto.getMeterFullName())
                .ne(Func.isNotEmpty(dto.getId()), PropertyHouseMeter::getId, dto.getId());
        if (count(queryWrapper) > 0) {
            throw new ServiceException("同个仪表类型，在同个资源里不能重名!");
        }

        saveOrUpdate(houseMeter);
        if(Func.isNotEmpty(houseMeter.getIsMeterReading())&&houseMeter.getIsMeterReading()==1){
            return iPropertyHouseMeterReadingService.add(houseMeter.getId(),dto.getInitNum(),dto.getInitTime(),flag);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addList(PropertyHouseMeterAddListDto dto) {

        PropertyBuildings buildings = null;
        if (Func.isNotEmpty(dto.getBuildingId())) {
            buildings = propertyBuildingsService.getById(dto.getBuildingId());
            if (Func.isEmpty(buildings)) {
                throw new ServiceException("所选楼栋不存在,请重新刷新!");
            }
        }
        PropertyHouses houses = null;
        if (Func.isNotEmpty(dto.getHouseId())) {
            houses = propertyHousesService.getById(dto.getHouseId());
            if (Func.isEmpty(houses)) {
                throw new ServiceException("所选房间已不存在!");
            }
        }
        Date now = new Date();

        for (PropertyHouseMeterAddListDto.HouseMeter item : dto.getHouseMeterList()) {
            PropertyHouseMeter baseDto = new PropertyHouseMeter();
            PropertyMeter meter = iPropertyMeterService.getById(item.getMeterId());
            if (Func.isEmpty(meter)) {
                throw new ServiceException("所选仪表类型不存在,请重新刷新!");
            }
            baseDto.setParkId(dto.getParkId());
            baseDto.setResourceType(1);
            if (Func.isNotEmpty(buildings)) {
                buildings = propertyBuildingsService.getById(dto.getBuildingId());
                baseDto.setParkId(buildings.getParkId());
                baseDto.setBuildingId(dto.getBuildingId());
                baseDto.setResourceType(2);
            }
            if (Func.isNotEmpty(houses)) {
                baseDto.setParkId(houses.getParkId());
                baseDto.setBuildingId(houses.getBuildingId());
                baseDto.setFloorNum(houses.getFloorNum());
                baseDto.setUnitNum(houses.getUnitNum());
                baseDto.setResourceCode(houses.getResourceCode());
                baseDto.setResourceType(3);
            }
            LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(PropertyHouseMeter::getParkId, dto.getParkId())
                    .eq(Func.isNotEmpty(dto.getBuildingId()), PropertyHouseMeter::getBuildingId, dto.getBuildingId())
                    .eq(Func.isNotEmpty(dto.getHouseId()), PropertyHouseMeter::getHouseId, dto.getHouseId())
                    .eq(PropertyHouseMeter::getResourceType, baseDto.getResourceType())
                    .eq(PropertyHouseMeter::getMeterId, item.getMeterId())
                    .eq(PropertyHouseMeter::getMeterFullName, item.getMeterFullName());
            if (count(queryWrapper) > 0) {
                throw new ServiceException("同个仪表类型，在同个资源里不能重名!");
            }

            baseDto.setMeterId(item.getMeterId());
            baseDto.setMeterFullName(item.getMeterFullName());
            baseDto.setMaxNum(MAX_NUM);
            baseDto.setInitNum(item.getInitNum());
            baseDto.setInitTime(now);
            baseDto.setRate(RATE);
            baseDto.setAttritionRate(ATTRITION_RATE);
            baseDto.setIsMeterReading(1);
            baseDto.setRemark(item.getRemark());
            save(baseDto);
            if(Func.isNotEmpty(baseDto.getIsMeterReading())&&baseDto.getIsMeterReading()==1){
                 iPropertyHouseMeterReadingService.add(baseDto.getId(),baseDto.getInitNum(),baseDto.getInitTime(),false);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id) {
        PropertyHouseMeter houseMeter = getById(id);
        if (Func.isEmpty(houseMeter)) {
            throw new ServiceException("删除对象不存在,请重新刷新!");
        }
        if (Func.isNotEmpty(houseMeter.getIsUse()) && houseMeter.getIsUse() == 1) {
            throw new ServiceException("当前仪表用量已计费，无法删除!");
        }

        boolean flag = deleteLogic(Arrays.asList(id));
        if (flag) {
            //todo 删除抄表数据
        }

        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateMeter(PropertyHouseMeterUpdateDto dto) {
        PropertyHouseMeter houseMeter = getById(dto.getId());
        if (Func.isEmpty(houseMeter)) {
            throw new ServiceException("所选数据不存在,请重新刷新!");
        }
        if(Func.isEmpty(houseMeter.getPrimevalId())){
            houseMeter.setPrimevalId(houseMeter.getId());
        }
        PropertyHouseMeterReading houseMeterReading = iPropertyHouseMeterReadingService.getByHouseMeterIdAndQuery(houseMeter.getId(), null, null, null, null, 1);
        if(Func.isNotEmpty(dto.getUpdateMeterNum())){
            if(Func.isNotEmpty(houseMeterReading)){
                if(Func.isNotEmpty(houseMeterReading.getIsInit())&&houseMeterReading.getIsInit()==1){
                    if(BigdecimalUtil.lt(dto.getUpdateMeterNum(),houseMeterReading.getMeterReadingNum())){
                        throw new ServiceException("原仪表更换时读数不能小于原仪表已抄表的读数!");
                    }
                    if(dto.getUpdateMeterTime().getTime()<houseMeterReading.getMeterReadingTime().getTime()){
                        throw new ServiceException("原仪表更换日期不能小于原仪表已抄表的日期!");
                    }
                }else{
                    if(Func.isNotEmpty(houseMeterReading.getIsCharging())&&houseMeterReading.getIsCharging()==1){
                        if(BigdecimalUtil.lt(dto.getUpdateMeterNum(),houseMeterReading.getMeterReadingNum())){
                            throw new ServiceException("原仪表更换时读数不能小于原仪表已抄表的读数!");
                        }
                        if(dto.getUpdateMeterTime().getTime()<houseMeterReading.getMeterReadingTime().getTime()){
                            throw new ServiceException("原仪表更换日期不能小于原仪表已抄表的日期!");
                        }
                    }else{
                        if(BigdecimalUtil.lt(dto.getUpdateMeterNum(),houseMeterReading.getLastMeterReadingNum())){
                            throw new ServiceException("原仪表更换时读数不能小于原仪表已抄表的读数!");
                        }
                        if(dto.getUpdateMeterTime().getTime()<houseMeterReading.getLastMeterReadingTime().getTime()){
                            throw new ServiceException("原仪表更换日期不能小于原仪表已抄表的日期!");
                        }
                    }
                }
            }
        }


        PropertyHouseMeter newHouseMeter = Func.copy(houseMeter, PropertyHouseMeter.class);
        newHouseMeter.setId(null);
        newHouseMeter.setLastId(houseMeter.getId());
        newHouseMeter.setUpdateMeterNum(dto.getUpdateMeterNum());
        newHouseMeter.setUpdateMeterTime(dto.getUpdateMeterTime());
        newHouseMeter.setMeterFullName(dto.getMeterFullName());
        newHouseMeter.setMaxNum(dto.getMaxNum());
        newHouseMeter.setInitNum(dto.getInitNum());
        newHouseMeter.setInitTime(dto.getInitTime());
        newHouseMeter.setRate(dto.getRate());
        newHouseMeter.setAttritionRate(dto.getAttritionRate());
        newHouseMeter.setConcentratorCode(dto.getConcentratorCode());
        //需求要求默认可抄表
        newHouseMeter.setIsMeterReading(1);
        newHouseMeter.setPrimevalId(houseMeter.getPrimevalId());

        LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyHouseMeter::getParkId, newHouseMeter.getParkId())
                .eq(Func.isNotEmpty(newHouseMeter.getBuildingId()), PropertyHouseMeter::getBuildingId, newHouseMeter.getBuildingId())
                .eq(Func.isNotEmpty(newHouseMeter.getHouseId()), PropertyHouseMeter::getHouseId, newHouseMeter.getHouseId())
                .eq(PropertyHouseMeter::getResourceType, newHouseMeter.getResourceType())
                .eq(PropertyHouseMeter::getMeterId, newHouseMeter.getMeterId())
                .eq(PropertyHouseMeter::getMeterFullName, newHouseMeter.getMeterFullName());
        if (count(queryWrapper) > 0) {
            throw new ServiceException("同个仪表类型，在同个资源里不能重名!");
        }
        //添加换表时的原表抄表记录
        if(Func.isNotEmpty(houseMeter.getIsMeterReading())&&houseMeter.getIsMeterReading()==1){
            iPropertyHouseMeterReadingService.add(houseMeter.getId(),dto.getUpdateMeterNum(),dto.getUpdateMeterTime(),false);
        }

        //旧表设置为封存不可用状态
        houseMeter.setIsSealed(1);
        houseMeter.setIsMeterReading(0);
        updateById(houseMeter);
        save(newHouseMeter);
        // 添加初始抄表记录
        if(Func.isNotEmpty(newHouseMeter.getIsMeterReading())&&newHouseMeter.getIsMeterReading()==1){
            iPropertyHouseMeterReadingService.add(newHouseMeter.getId(),dto.getInitNum(),dto.getUpdateMeterTime(),false);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean binding(List<PropertyMeterBindingDto> list) {
        StringBuilder str = new StringBuilder();
        List<PropertyHouseMeter> dtoList = new ArrayList<>(list.size());
        for (PropertyMeterBindingDto item : list) {
            PropertyHouseMeter houseMeter = getById(item.getId());
            if (Func.isEmpty(houseMeter)) {
                str.append("所选数据已不存在,请刷新数据;");
                continue;
            } else {
                if (Func.isNotEmpty(houseMeter.getChargingStandardId())) {
                    str.append("仪表{" + houseMeter.getMeterFullName() + "}已经被绑定收费;");
                    continue;
                }
            }
            PropertyHouseBindFee byHouseIdAndFeeStandardId = iPropertyHouseBindFeeService.getByHouseIdAndFeeStandardId(houseMeter.getHouseId(), item.getChargingStandardId());
            if(Func.isEmpty(byHouseIdAndFeeStandardId)){
                str.append("仪表{" + houseMeter.getMeterFullName() + "}所选收费标准未绑定该房源;");
                continue;
            }
            PropertyFeeStandard feeStandard = propertyFeeStandardService.getById(byHouseIdAndFeeStandardId.getFeeStandardId());
            if(Func.isEmpty(feeStandard)){
                str.append("仪表{" + houseMeter.getMeterFullName() + "}所选收费标准已不存在;");
                continue;
            }
            if(feeStandard.getFeeMark()!=2){
                str.append("仪表{" + houseMeter.getMeterFullName() + "}所选收费标准不是按用量收费;");
                continue;
            }

            houseMeter.setChargingStandardId(item.getChargingStandardId());
            dtoList.add(houseMeter);
        }
        if (str.length() > 0) {
            throw new ServiceException(str.toString());
        }
        if (Func.isNotEmpty(dtoList)) {
            updateBatchById(dtoList);
        }

        return true;
    }

    @Override
    public PropertyHouseMeterDetailVo getHouseMeterById(Long id) {
        PropertyHouseMeter houseMeter = getById(id);
        if (Func.isEmpty(houseMeter)) {
            throw new ServiceException("所选数据已不存在,请刷新数据;");
        }
        PropertyHouseMeterDetailVo vo = Func.copy(houseMeter, PropertyHouseMeterDetailVo.class);
        PropertyMeter meter = iPropertyMeterService.getById(vo.getMeterId());
        if (Func.isNotEmpty(meter)) {
            vo.setMeterName(meter.getMeterName());
        }
        PmgBatParkInfo parkInfo = pmgBatParkInfoService.getById(vo.getParkId());
        if(Func.isNotEmpty(parkInfo)){
            vo.setParkName(parkInfo.getParkName());
        }
        PropertyHouses houses = propertyHousesService.getById(vo.getBuildingId());
        if(Func.isNotEmpty(houses)){
            vo.setHouseName(houses.getHouseName());
        }
        PropertyBuildings buildings = propertyBuildingsService.getById(vo.getBuildingId());
        if(Func.isNotEmpty(buildings)){
            vo.setBuildingName(buildings.getBuildingName());
        }
        if(iPropertyHouseMeterReadingService.countByIsCharging(houseMeter.getId())>0){
            vo.setIsCharging(1);
        }else{
            vo.setIsCharging(0);
        }

        PropertyHouseMeterReading houseMeterReading = iPropertyHouseMeterReadingService.getByHouseMeterIdAndQuery(houseMeter.getId(), null, null, null, null, 1);
        if(Func.isNotEmpty(houseMeterReading)){
            if(Func.isNotEmpty(houseMeterReading.getIsInit())&&houseMeterReading.getIsInit()==1){
                vo.setLastReadingNum(houseMeterReading.getMeterReadingNum());
                vo.setLastReadingTime(houseMeterReading.getMeterReadingTime());
            }else{
                if(Func.isNotEmpty(houseMeterReading.getIsCharging())&&houseMeterReading.getIsCharging()==1){
                    vo.setLastReadingNum(houseMeterReading.getMeterReadingNum());
                    vo.setLastReadingTime(houseMeterReading.getMeterReadingTime());
                }else{
                    vo.setLastReadingNum(houseMeterReading.getLastMeterReadingNum());
                    vo.setLastReadingTime(houseMeterReading.getLastMeterReadingTime());
                }
            }
            if (Func.isNotEmpty(houseMeterReading.getIsInit()) && houseMeterReading.getIsInit() == 1) {
                vo.setIsReading(0);
            }else{
                vo.setIsReading(1);
            }
        }
        return vo;
    }

    @Override
    public IPage<PropertyHouseMeter> getPageByHouse(PropertyHouseMeterReadingQueryDto dto) {

        LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
        if(Func.isNotEmpty(dto.getMeterReadingTime())){
            Date monthFirstDayDate = DateUtils.getMonthFirstDayDate(dto.getMeterReadingTime());
            queryWrapper.ge(PropertyHouseMeter::getInitTime,monthFirstDayDate);
        }
        queryWrapper.eq(Func.isNotEmpty(dto.getParkId()), PropertyHouseMeter::getParkId, dto.getParkId())
                .eq(Func.isNotEmpty(dto.getMeterId()), PropertyHouseMeter::getMeterId, dto.getMeterId())
                .eq(Func.isNotEmpty(dto.getBuildingId()), PropertyHouseMeter::getBuildingId, dto.getBuildingId())
                .eq(PropertyHouseMeter::getIsMeterReading,1)
                .orderByDesc(PropertyHouseMeter::getCreateTime);
        IPage<PropertyHouseMeter> page = this.page(Condition.getPage(dto), queryWrapper);
        return page;
    }

    @Override
    public List<PropertyHouseMeter> listByParkIdAndBuildingIdAndMeterId(Long parkId, Long buildingId, Long meterId) {
        LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Func.isNotEmpty(parkId), PropertyHouseMeter::getParkId, parkId)
                .eq(Func.isNotEmpty(meterId), PropertyHouseMeter::getMeterId, meterId)
                .eq(Func.isNotEmpty(buildingId), PropertyHouseMeter::getBuildingId, buildingId)
                .isNotNull(PropertyHouseMeter::getBuildingId)
                .eq(PropertyHouseMeter::getIsSealed,0)
                .orderByDesc(PropertyHouseMeter::getBuildingId);
        return list(queryWrapper);
    }

    @Override
    public List<PropertyHouseMeter> listByParentId(Long parentId) {
        LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyHouseMeter::getParentId,parentId)
                .eq(PropertyHouseMeter::getIsSealed,0);
        return list(queryWrapper);
    }

    @Override
    public List<PropertyHouseMeter> listByParentIdAndHouseId(Long parentId, Long houseId) {
        LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyHouseMeter::getParentId,parentId)
                .eq(PropertyHouseMeter::getHouseId,houseId)
                .eq(PropertyHouseMeter::getIsSealed,0);
        return list(queryWrapper);
    }

    @Override
    public Boolean updateIsSealed(PropertyMeterUpdateIsSealedDto dto) {
        PropertyHouseMeter houseMeter = getById(dto.getId());
        if (Func.isEmpty(houseMeter)) {
            throw new ServiceException("所选数据已不存在,请刷新数据;");
        }
        if(Func.equalsSafe(dto.getIsSealed(),houseMeter.getIsSealed())){
            return false;
        }
        houseMeter.setIsSealed(dto.getIsSealed());
        return updateById(houseMeter);
    }

    @Override
    public List<PropertyHouseMeterAppPageVo> getListByApp(PropertyHouseMeterAppQueryDto dto) {
        LambdaQueryWrapper<PropertyHouseMeter> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyHouseMeter::getParkId,dto.getParkId())
                .eq(PropertyHouseMeter::getMeterId,dto.getMeterId())
                .eq(Func.isNotEmpty(dto.getBuildingId()),PropertyHouseMeter::getBuildingId,dto.getBuildingId())
                .like(Func.isNotEmpty(dto.getMeterFullName()),PropertyHouseMeter::getMeterFullName,dto.getMeterFullName())
                .eq(PropertyHouseMeter::getIsMeterReading,1)
                .eq(PropertyHouseMeter::getIsSealed,0)
                .like(Func.isNotEmpty(dto.getResourceCode()),PropertyHouseMeter::getResourceCode,dto.getResourceCode());
        Date monthFirstDayDate = DateUtils.getMonthFirstDayDate(new Date());
        if(dto.getIsReading()==0){
            queryWrapper.and(wrapper -> wrapper
                    .or(item -> item.isNull(PropertyHouseMeter::getFinallyMeterReadingTime))
                    .or(item -> item.lt(PropertyHouseMeter::getFinallyMeterReadingTime,monthFirstDayDate)));
        }
        //最后的抄表时间,大于本月的开始时间就认为已抄表
        if(dto.getIsReading()==1){
            queryWrapper.ge(PropertyHouseMeter::getFinallyMeterReadingTime,monthFirstDayDate);
        }
        queryWrapper.orderByDesc(PropertyHouseMeter::getBuildingId,PropertyHouseMeter::getFloorNum,PropertyHouseMeter::getResourceCode);
        List<PropertyHouseMeter> list =list(queryWrapper);

        List<PropertyHouseMeterAppPageVo> result = list.stream().map(a -> {
            PropertyHouseMeterAppPageVo vo = new PropertyHouseMeterAppPageVo();
            Func.copy(a, vo);
            PropertyMeter meter = iPropertyMeterService.getById(a.getMeterId());
            if (Func.isNotEmpty(meter)) {
                vo.setMeterName(meter.getMeterName());
            }
            PropertyHouses houses = propertyHousesService.getById(a.getHouseId());
            if (Func.isNotEmpty(houses)) {
                vo.setHouseName(houses.getHouseName());
            }
            return vo;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public PropertyHouseMeterReadingByAppVo getDetailByApp(Long id) {
        return iPropertyHouseMeterReadingService.getDetailByApp(id);
    }
}
