package com.centreproject.system.service.sync.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.centreproject.common.enums.SyncStatus;
import com.centreproject.system.domain.sync.kpi.DataSyncLog;
import com.centreproject.system.mapper.sync.kpi.DataSyncLogMapper;
import com.centreproject.system.service.sync.IDataSyncLogService;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * 同步日志Service业务层处理
 *
 * @author zhonglei.xu
 */
@Service
public class DataSyncLogServiceImpl extends ServiceImpl<DataSyncLogMapper, DataSyncLog>
        implements IDataSyncLogService {

    /**
     * @param dataSyncLog
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<DataSyncLog> getDataSyncLogPage(DataSyncLog dataSyncLog, int pageNum, int pageSize) {
        return this.baseMapper.selectPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<DataSyncLog>()
                        .like(dataSyncLog.getTraceId() != null, DataSyncLog::getTraceId, dataSyncLog.getTraceId())
                        .like(dataSyncLog.getSourceSystem() != null, DataSyncLog::getSourceSystem, dataSyncLog.getSourceSystem())
                        .eq(dataSyncLog.getStatus() != null, DataSyncLog::getStatus, dataSyncLog.getStatus())
                        .orderByDesc(DataSyncLog::getCreateTime));
    }

    /**
     * 同步任务开始
     *
     * @param jobId        任务ID
     * @param traceId      追踪ID
     * @param sourceSystem 源系统
     * @param sourceTable  源表
     * @param targetTable  目标表
     */
    @Override
    public void startSync(Long jobId, String traceId, String sourceSystem,
                          String sourceTable, String targetTable) {
        DataSyncLog dataSyncLog = new DataSyncLog().setJobId(jobId)
                .setTraceId(traceId)
                .setSourceSystem(sourceSystem)
                .setSourceTable(sourceTable)
                .setTargetTable(targetTable)
                .setDataCount(0)
                .setStatus(SyncStatus.RUNNING.getCode())
                .setStartTime(Date.from(ZonedDateTime.now(ZoneId.of("Asia/Riyadh")).toInstant()))
                .setCreateTime(Date.from(ZonedDateTime.now(ZoneId.of("Asia/Riyadh")).toInstant()))
                .setCreateBy("SYNC-USER");
        this.baseMapper.insert(dataSyncLog);
    }

    /**
     * 同步任务开始
     *
     * @param jobId        任务ID
     * @param traceId      追踪ID
     * @param sourceSystem 源系统
     * @param sourceTable  源表
     * @param targetTable  目标表
     */
    @Override
    public void ruleSync(Long jobId, String traceId, String sourceSystem,
                          String sourceTable, String targetTable,String errorMsg) {
        DataSyncLog dataSyncLog = new DataSyncLog()
                .setErrorMsg(errorMsg)
                .setJobId(jobId)
                .setTraceId(traceId)
                .setSourceSystem(sourceSystem)
                .setSourceTable(sourceTable)
                .setTargetTable(targetTable)
                .setDataCount(0)
                .setStatus(SyncStatus.RUNNING.getCode())
                .setStartTime(Date.from(ZonedDateTime.now(ZoneId.of("Asia/Riyadh")).toInstant()))
                .setCreateTime(Date.from(ZonedDateTime.now(ZoneId.of("Asia/Riyadh")).toInstant()))
                .setCreateBy("SYNC-USER");
        this.baseMapper.insert(dataSyncLog);
    }

    /**
     * 记录同步完成日志
     *
     * @param traceId   关联的追踪ID
     * @param dataCount 同步数据量
     * @param status    是否成功
     * @param errorMsg  错误信息(可选)
     */
    @Override
    public void endSync(String traceId, int dataCount,
                        SyncStatus status, String errorMsg) {
        DataSyncLog log = this.baseMapper.selectOne(
                new LambdaQueryWrapper<DataSyncLog>()
                        .eq(DataSyncLog::getTraceId, traceId));
        log = null == log ? new DataSyncLog() : log;
        log.setDataCount(dataCount)
                .setLogId(null)
                .setStatus(status.getCode())
                .setErrorMsg(errorMsg)
                .setEndTime(Date.from(ZonedDateTime.now(ZoneId.of("Asia/Riyadh")).toInstant()))
                .setDuration(System.currentTimeMillis() - log.getStartTime().getTime());

        this.baseMapper.insert(log);

    }

}
