package com.fizz.business.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fizz.business.domain.LogData;
import com.fizz.business.domain.RollHistory;
import com.fizz.business.form.LogDataForm;
import com.fizz.business.mapper.LogDataMapper;
import com.fizz.business.service.LogDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class LogDataServiceImpl extends ServiceImpl<LogDataMapper, LogData> implements LogDataService {

    @Override
    public PageInfo<LogData> pageList(LogDataForm logDataForm) {

        // 调用分页查询方法，获取EventRecords的分页结果
        PageHelper.startPage(logDataForm.getPageNum(), logDataForm.getPageSize());

        LambdaQueryChainWrapper<LogData> lambdaQuery = lambdaQuery();


        if(ObjectUtil.isNotEmpty(logDataForm.getStartTime())){
            LocalDateTime start = LocalDateTime.of(logDataForm.getStartTime().toLocalDate(), LocalTime.MIN);
            lambdaQuery.ge(LogData::getTimestamp, start);
        }
        if(ObjectUtil.isNotEmpty(logDataForm.getEndTime())){
            LocalDateTime end = LocalDateTime.of(logDataForm.getEndTime().toLocalDate(), LocalTime.MAX);
            lambdaQuery.le(LogData::getTimestamp, end);
        }

        if (StringUtils.isNotBlank(logDataForm.getLogtype())) {
            lambdaQuery.eq(LogData::getLogtype, logDataForm.getLogtype());
        }
        if (StringUtils.isNotBlank(logDataForm.getModule())) {
            lambdaQuery.eq(LogData::getModule, logDataForm.getModule());
        }
        if (StringUtils.isNotBlank(logDataForm.getLogtext())) {
            lambdaQuery.like(LogData::getLogtext, logDataForm.getLogtext());
        }
        if (ObjectUtil.isNotNull(logDataForm.getStatus())) {
            lambdaQuery.eq(LogData::getStatus, logDataForm.getStatus());
        }
        lambdaQuery.orderByDesc(LogData::getTimestamp);


        List<LogData> list = lambdaQuery.list();
        PageInfo<LogData> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public void alarmAck(Integer seqid) {
        lambdaUpdate()
                .set(LogData::getConfirmTime, new Date())
                .eq(LogData::getSeqid, seqid)
                .update();
    }

    @Override
    public void logInfo(String module, String content, Object... args) {
        if (args.length > 0) {
            content = formatMessage(content, args);
        }
        saveLogData("INFORMATION",module, content);
    }

    @Override
    public void logWarn(String module, String content, Object... args) {
        if (args.length > 0) {
            content = formatMessage(content, args);
        }
        saveLogData("WARN",module, content);
    }
    private void saveLogData(String logType,String module, String content){
        LogData logData = new LogData();
        logData.setModule(module);//设置模块名称
        logData.setLogtext(content);//设置日志内容
        logData.setTimestamp(LocalDateTime.now());
        logData.setLogtype(logType);
        logData.setStatus(0);
        logData.setConfirmTime(LocalDateTime.now());
        save(logData);
    }

    private String formatMessage(String pattern, Object[] arguments) {
        if (pattern == null) {
            return null;
        }
        if (arguments == null || arguments.length == 0) {
            return pattern;
        }

        StringBuilder sb = new StringBuilder();
        int argIndex = 0;
        int i = 0;

        while (i < pattern.length()) {
            char c = pattern.charAt(i);
            if (c == '{' && i + 1 < pattern.length() && pattern.charAt(i + 1) == '}') {
                if (argIndex < arguments.length) {
                    sb.append(String.valueOf(arguments[argIndex++]));
                } else {
                    sb.append("{}");
                }
                i += 2;
            } else {
                sb.append(c);
                i++;
            }
        }

        return sb.toString();
    }

}