package com.njcn.oracle.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.oracle.bo.param.MigrationParam;
import com.njcn.oracle.bo.po.DayPlt;
import com.njcn.oracle.mapper.DayPltMapper;
import com.njcn.oracle.mybatis.service.impl.ReplenishMybatisServiceImpl;
import com.njcn.oracle.service.IDayPltService;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2023-12-28
 */
@Service
public class DayPltServiceImpl extends ReplenishMybatisServiceImpl<DayPltMapper, DayPlt> implements IDayPltService {
    @Override
    public List<DayPlt> queryData(MigrationParam migrationParam) {
        //查询时间范围内的数据
        LambdaQueryWrapper<DayPlt> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(DayPlt::getTimeid, migrationParam.getStartTime(), migrationParam.getEndTime());
        if (CollectionUtil.isNotEmpty(migrationParam.getLineIds())) {
            lambdaQueryWrapper.in(DayPlt::getLineid, migrationParam.getLineIds());
        }
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }


    @Override
    @DS("target")
    public void clearTargetData(MigrationParam migrationParam) {
        LambdaQueryWrapper<DayPlt> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(DayPlt::getTimeid, migrationParam.getStartTime(), migrationParam.getEndTime());
        if (CollectionUtil.isNotEmpty(migrationParam.getLineIds())) {
            lambdaQueryWrapper.in(DayPlt::getLineid, migrationParam.getLineIds());
        }
        this.baseMapper.delete(lambdaQueryWrapper);
    }

    /**
     * 默认500分片，如果字段超过20以上，建议重写方法调整为1000分片，重写时不要忘记@DS注解
     *
     * @param data 数据集合
     */
    @Override
    @DS("target")
    public void insertBatchByDB(List<DayPlt> data) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        this.insertBatchBySlice(data, 100);
        stopWatch.stop();
        System.out.printf("DayPlt总计:%d条，耗时执行时长：%f 秒.%n", data.size(), stopWatch.getTotalTimeSeconds());
    }

}
