package org.jeecg.modules.ZyNrgcc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.ZyNrgcc.entity.BuZyNrgccOnline;
import org.jeecg.modules.ZyNrgcc.mapper.BuZyNrgccOnlineMapper;
import org.jeecg.modules.ZyNrgcc.service.IBuZyNrgccOnlineService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: bu_zy_nrgcc_online
 * @Author: jeecg-boot
 * @Date:   2025-12-05
 * @Version: V1.0
 */
@Slf4j
@Service
public class BuZyNrgccOnlineServiceImpl extends ServiceImpl<BuZyNrgccOnlineMapper, BuZyNrgccOnline> implements IBuZyNrgccOnlineService {

    @Override
    public IPage<BuZyNrgccOnline> queryHistoryDataByMonth(Page<BuZyNrgccOnline> page, String month) {
        // 构造历史表名
        String tableName = "bu_zy_nrgcc_history_" + month;
        // 查询历史数据
        return baseMapper.queryHistoryDataByMonth(page, tableName);
    }
    
    @Override
    public IPage<BuZyNrgccOnline> queryHistoryDataByTimeRange(Page<BuZyNrgccOnline> page, String startTime, String endTime) {
        // 根据开始时间确定表名（这里假设按月分表）
        String month = startTime.substring(0, 7).replace("-", "");
        String tableName = "bu_zy_nrgcc_history_" + month;
        // 查询历史数据
        return baseMapper.queryHistoryDataByTimeRange(page, tableName, startTime, endTime);
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public boolean updateById(BuZyNrgccOnline entity) {
//        log.info("准备更新实体: {}", entity);
//        // Use the mapper directly to ensure the update happens
//        int result = baseMapper.updateById(entity);
//        boolean updated = result > 0;
//        log.info("更新结果: {}", updated);
//        if (updated) {
//            BuZyNrgccOnline updatedEntity = this.getById(entity.getId());
//            log.info("更新后数据库中的数据: {}", updatedEntity);
//        }
//        return updated;
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateHistoricalData(BuZyNrgccOnline entity, String month) {
        log.info("准备更新历史数据实体: {}, 月份: {}", entity, month);
        // 构造历史表名
        String tableName = "bu_zy_nrgcc_history_" + month; // 修正表名
        // 使用自定义SQL更新历史数据
        boolean result = baseMapper.updateHistoricalData(entity, tableName);
        log.info("历史数据更新结果: {}", result);
        return result;
    }
}