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.yuchen.saas.park.api.entity.property.SaasParkPropertyBuildingMeter;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyPublicMeter;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyPublicMeter;
import com.yuchen.saas.park.api.request.SaasParkPropertyBuildingMeterChangeRequest;
import com.yuchen.saas.park.api.request.SaasParkPropertyPublicMeterChangeRequest;
import com.yuchen.saas.park.api.request.SaasParkPropertyPublicMeterRequest;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyPublicMeterMapper;
import com.yuchen.saas.park.service.service.property.SaasParkPropertyPublicMeterService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 仪表管理-公摊仪表 服务实现类
 * </p>
 *
 * @author miaoqs
 * @since 2024-09-30
 */
@Service
public class SaasParkPropertyPublicMeterServiceImpl extends BaseServiceImpl<SaasParkPropertyPublicMeterMapper, SaasParkPropertyPublicMeter> implements SaasParkPropertyPublicMeterService {

    /**
     * 公摊仪表 分页查询
     * @param req
     * @return
     */
    @Override
    public IPage<SaasParkPropertyPublicMeter> pageByparam(SaasParkPropertyPublicMeterRequest req) {
        // 分页查询
        Query query = new Query();
        query.setCurrent(req.getCurrent());
        query.setSize(req.getSize());
        IPage<SaasParkPropertyPublicMeter> pages = page(Condition.getPage(query),
                new LambdaQueryWrapper<SaasParkPropertyPublicMeter>()
                        .eq(Func.isNotEmpty(req.getParkId()), SaasParkPropertyPublicMeter::getParkId,
                                req.getParkId())
                        .eq(Func.isNotEmpty(req.getMeterTypeId()), SaasParkPropertyPublicMeter::getMeterTypeId,
                                req.getMeterTypeId())
                        .eq(Func.isNotEmpty(req.getApportionType()), SaasParkPropertyPublicMeter::getApportionType,
                                req.getApportionType())
                        .eq(Func.isNotEmpty(req.getState()), SaasParkPropertyPublicMeter::getState,
                                req.getState())
                        .isNull(Func.isNotEmpty(req.getBindingRatesId())&& req.getBindingRatesId().equals("0"), SaasParkPropertyPublicMeter::getRatesId)
                        .isNotNull(Func.isNotEmpty(req.getBindingRatesId())&& req.getBindingRatesId().equals("1"), SaasParkPropertyPublicMeter::getRatesId)
                        .orderByDesc(SaasParkPropertyPublicMeter::getCreateTime));
        return pages;

    }

    /**
     * 公摊仪表 更换仪表
     * @param changeMeter
     */
    @Override
    public void changeMeter(SaasParkPropertyPublicMeterChangeRequest changeMeter) {

        SaasParkPropertyPublicMeter oldMeter = changeMeter.getOldMeter();
        SaasParkPropertyPublicMeter newMeter = changeMeter.getNewMeter();

        //校验旧仪表更换时读数是否大于上次读数
        BigDecimal changeReading = oldMeter.getChangeReading();
        BigDecimal lastReading = oldMeter.getLastReading();
        if(lastReading.compareTo(changeReading)<0){
            throw new RuntimeException("原仪表更换时读数不能小于上次读数");
        }
        // 校验旧仪表更换日期是否大于上次抄表日期
        Date lastReadingDate = oldMeter.getLastReadingDate();
        Date changeDate = oldMeter.getChangeDate();
        if(changeDate.compareTo(lastReadingDate)<=0){
            throw new RuntimeException("原仪表更换时读数时间不能小于等于上次抄表日期");
        }

        // TODO 校验旧仪表被更换时读数是否和最后一期抄表读数一致，不一致则新生成一条抄表数据

        oldMeter.setState("2");
        oldMeter.setIzChange("1");
        this.updateById(oldMeter);

        newMeter.setParkId(oldMeter.getParkId());
        newMeter.setRoomId(oldMeter.getRoomId());
        newMeter.setState("1");
        oldMeter.setIzChange("0");
        this.updateById(newMeter);

    }
}
