/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/28
 */
package org.springblade.modules.business.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointRelEntity;
import org.springblade.modules.business.monitor.dto.WaterFlowRateHistoryInfoDTO;
import org.springblade.modules.business.monitor.entity.WaterFlowRateHistoryInfoEntity;
import org.springblade.modules.business.monitor.mapper.WaterFlowRateHistoryMapper;
import org.springblade.modules.business.monitor.service.IWaterFlowRateHistoryService;
import org.springblade.modules.business.monitor.vo.ConsumerWaterFlowCountInfoVO;
import org.springblade.modules.business.monitor.vo.OrgWaterFlowCountInfoVO;
import org.springblade.modules.business.monitor.vo.WaterFlowRateHistoryInfoVO;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


/***
 * Date:2022/8/28
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Service
@Slf4j
public class WaterFlowRateHistoryServiceImpl extends BaseServiceImpl<WaterFlowRateHistoryMapper, WaterFlowRateHistoryInfoEntity> implements IWaterFlowRateHistoryService {

    @Override
    public IPage<WaterFlowRateHistoryInfoVO> selectPage(IPage<WaterFlowRateHistoryInfoVO> page, WaterFlowRateHistoryInfoDTO dto) {
        return page.setRecords(baseMapper.selectPage(page, dto));
    }

    @Override
    public Long rtuUpDataCount(WaterFlowRateHistoryInfoDTO dto) {
        return baseMapper.rtuUpDataCount(dto);
    }

    @Override
    public List<OrgWaterFlowCountInfoVO> orgWaterUseCount(WaterFlowRateHistoryInfoDTO dto) {
        return baseMapper.orgWaterUseCount(dto);
    }

    @Override
    public List<ConsumerWaterFlowCountInfoVO> consumerWaterUseCount(WaterFlowRateHistoryInfoDTO dto) {
        return baseMapper.consumerWaterUseCount(dto);
    }

    @Override
    public double getDayAccw(String mpcd) {
        Date tm = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tm);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        Date et =  calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        Date st = calendar.getTime();
        LambdaQueryWrapper<WaterFlowRateHistoryInfoEntity> wrapper = Wrappers.<WaterFlowRateHistoryInfoEntity>query().lambda();
        wrapper.eq(WaterFlowRateHistoryInfoEntity::getMpcd,mpcd);
        wrapper.ge(WaterFlowRateHistoryInfoEntity::getTm,st);
        wrapper.orderByAsc(WaterFlowRateHistoryInfoEntity::getTm);
        wrapper.last("limit 1");
        WaterFlowRateHistoryInfoEntity first = this.getOne(wrapper);
        if (Func.notNull(first)){
            wrapper = Wrappers.<WaterFlowRateHistoryInfoEntity>query().lambda();
            wrapper.eq(WaterFlowRateHistoryInfoEntity::getMpcd,mpcd);
            wrapper.ge(WaterFlowRateHistoryInfoEntity::getTm,et);
            wrapper.orderByAsc(WaterFlowRateHistoryInfoEntity::getTm);
            wrapper.last("limit 1");
            WaterFlowRateHistoryInfoEntity last = this.getOne(wrapper);
            if (Func.notNull(last)){
                if (Func.notNull(first.getAccw()) && Func.notNull(last.getAccw())){
                    if (last.getAccw()>first.getAccw()){
                        // log.info("last accw1 {} {} first accw2 {} {}",last.getAccw(),last.getAccw().doubleValue(),first.getAccw(),first.getAccw().doubleValue());
                        return last.getAccw().doubleValue()-first.getAccw().doubleValue();
                    }
                }
            }
        }
        return 0;
    }

}
