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.DataFluc;
import com.njcn.oracle.mapper.DataFlucMapper;
import com.njcn.oracle.mybatis.service.impl.ReplenishMybatisServiceImpl;
import com.njcn.oracle.service.IDataFlucService;
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 DataFlucServiceImpl extends ReplenishMybatisServiceImpl<DataFlucMapper, DataFluc> implements IDataFlucService {

    @Override
    public List<DataFluc> queryData(MigrationParam migrationParam) {
        LambdaQueryWrapper<DataFluc> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(DataFluc::getTimeid, migrationParam.getStartTime(), migrationParam.getEndTime());
        if (CollectionUtil.isNotEmpty(migrationParam.getLineIds())) {
            lambdaQueryWrapper.in(DataFluc::getLineid, migrationParam.getLineIds());
        }
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }


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

    @Override
    @DS("target")
    public void insertBatchByDB(List<DataFluc> data) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        this.insertBatchBySlice(data, 100);
        stopWatch.stop();
        System.out.printf("DataFluc总计:%d条，耗时执行时长：%f 秒.%n", data.size(), stopWatch.getTotalTimeSeconds());

    }





}
