package com.liquidnet.client.admin.zhengzai.kylin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.common.core.domain.entity.SysUser;
import com.liquidnet.client.admin.common.utils.ShiroUtils;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.kylin.dao.KylinArtistOperationLogDao;
import com.liquidnet.service.kylin.entity.KylinArtistOperationLog;
import com.liquidnet.service.kylin.mapper.KylinArtistOperationLogMapper;
import com.liquidnet.service.kylin.service.admin.IKylinArtistOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 艺人操作日志 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2026-03-04
 */
@Slf4j
@Service
public class KylinArtistOperationLogServiceImpl extends ServiceImpl<KylinArtistOperationLogMapper, KylinArtistOperationLog>
        implements IKylinArtistOperationLogService {

    @Override
    public void recordLog(String artistId, Integer operationType, String operationContent) {
        // 获取当前用户
        String operatorId = "SYSTEM";
        String operatorName = "系统";
        try {
            SysUser currentUser = ShiroUtils.getSysUser();
            if (currentUser != null) {
                operatorId = currentUser.getUserId().toString();
                operatorName = currentUser.getUserName();
            }
        } catch (Exception e) {
            log.warn("获取当前用户失败，使用系统默认值", e);
        }

        // 调用重载方法
        recordLog(artistId, operationType, operationContent, operatorId, operatorName);
    }

    @Override
    public void recordLog(String artistId, Integer operationType, String operationContent, String operatorId, String operatorName) {
        try {
            // 保存操作日志
            KylinArtistOperationLog operLog = new KylinArtistOperationLog();
            operLog.setLogId(IDGenerator.nextSnowId());
            operLog.setArtistId(artistId);
            operLog.setOperationType(operationType);
            operLog.setOperationContent(operationContent);
            operLog.setOperatorId(operatorId);
            operLog.setOperatorName(operatorName);
            operLog.setCreatedAt(LocalDateTime.now());

            baseMapper.insert(operLog);
            log.info("艺人操作日志记录成功: artistId={}, operationType={}, operator={}", artistId, operationType, operatorName);
        } catch (Exception e) {
            // 日志记录失败不影响主业务
            log.error("保存艺人操作日志失败", e);
        }
    }

    @Override
    public PageInfo<KylinArtistOperationLogDao> getOperationLogs(String artistId, Integer pageNum, Integer pageSize) {
        try {
            PageHelper.startPage(pageNum != null ? pageNum : 1, pageSize != null ? pageSize : 10);
            List<KylinArtistOperationLogDao> list = baseMapper.selectLogsByArtistId(artistId);
            return new PageInfo<>(list);
        } catch (Exception e) {
            log.error("获取艺人操作记录失败, artistId: {}", artistId, e);
            return new PageInfo<>();
        }
    }
}
