package org.xyzh.system.service.impl;

import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xyzh.api.system.dto.TbSysLogDTO;
import org.xyzh.api.system.service.LogService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.utils.StringUtils;
import org.xyzh.common.utils.id.IdUtil;
import org.xyzh.system.mapper.log.TbSysLogMapper;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @description    系统日志服务实现类
 * @filename    LogServiceImpl.java
 * @author    yslg
 * @copyright yslg
 * @since 2026-01-01
 */
@DubboService(
    version = "1.0.0",
    group = "system",
    timeout = 3000,
    retries = 0
)
public class LogServiceImpl implements LogService {

    private static final Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);

    private static final String MSG_LOG_PARAM_REQUIRED = "日志参数不能为空";
    private static final String MSG_FILTER_PARAM_REQUIRED = "查询条件不能为空";
    private static final String MSG_PAGE_PARAM_REQUIRED = "分页参数不能为空";

    @Resource
    private TbSysLogMapper sysLogMapper;

    @Override
    public ResultDomain<TbSysLogDTO> addSysLog(TbSysLogDTO sysLog) {
        if (sysLog == null) {
            return ResultDomain.failure(MSG_LOG_PARAM_REQUIRED);
        }

        try {
            // 设置默认值
            if (StringUtils.isBlank(sysLog.getLogId())) {
                sysLog.setLogId(IdUtil.generateID());
            }
            if (StringUtils.isBlank(sysLog.getOptsn())) {
                sysLog.setOptsn(IdUtil.generateID());
            }
            if (sysLog.getCreateTime() == null) {
                sysLog.setCreateTime(new Date());
            }
            if (sysLog.getDeleted() == null) {
                sysLog.setDeleted(false);
            }

            int rows = sysLogMapper.insertLog(sysLog);
            if (rows > 0) {
                logger.info("添加系统日志成功, logId={}", sysLog.getLogId());
                return ResultDomain.success("添加系统日志成功", sysLog);
            }
            logger.warn("添加系统日志失败, logId={}", sysLog.getLogId());
            return ResultDomain.failure("添加系统日志失败");
        } catch (Exception e) {
            logger.error("添加系统日志异常", e);
            return ResultDomain.failure("添加系统日志异常: " + e.getMessage());
        }
    }

    @Override
    public ResultDomain<Integer> countSysLog(TbSysLogDTO filter) {
        if (filter == null) {
            return ResultDomain.failure(MSG_FILTER_PARAM_REQUIRED);
        }

        try {
            int count = sysLogMapper.getLogCount(filter);
            return ResultDomain.success("统计日志数量成功", count);
        } catch (Exception e) {
            logger.error("统计日志数量异常", e);
            return ResultDomain.failure("统计日志数量异常: " + e.getMessage());
        }
    }

    @Override
    public ResultDomain<TbSysLogDTO> getSysLogList(TbSysLogDTO filter) {
        try {
            List<TbSysLogDTO> list = sysLogMapper.getLogByFilter(filter);
            if (list == null) {
                list = Collections.emptyList();
            }
            return ResultDomain.success("获取日志列表成功", list);
        } catch (Exception e) {
            logger.error("获取日志列表异常", e);
            return ResultDomain.failure("获取日志列表异常: " + e.getMessage());
        }
    }

    @Override
    public ResultDomain<TbSysLogDTO> getSysLogPage(PageRequest<TbSysLogDTO> pageRequest) {
        if (pageRequest == null) {
            return ResultDomain.failure(MSG_PAGE_PARAM_REQUIRED);
        }

        try {
            PageParam pageParam = pageRequest.getPageParam();
            TbSysLogDTO filter = pageRequest.getFilter();

            // 查询总数
            int total = sysLogMapper.getLogCount(filter);
            pageParam.setTotal(total);
            pageParam.setTotalPages(pageParam.getPageSize() == 0 ? 0 :
                (int) Math.ceil((double) total / pageParam.getPageSize()));

            // 查询分页数据
            List<TbSysLogDTO> data = sysLogMapper.getLogPageByFilter(filter, pageParam);
            if (data == null) {
                data = Collections.emptyList();
            }

            PageDomain<TbSysLogDTO> pageDomain = new PageDomain<>(pageParam, data);
            return ResultDomain.success("分页查询日志成功", pageDomain);
        } catch (Exception e) {
            logger.error("分页查询日志异常", e);
            return ResultDomain.failure("分页查询日志异常: " + e.getMessage());
        }
    }
}
