package com.aegis.emergency.alert.service.impl;

import java.util.Date;
import java.util.List;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aegis.common.core.domain.model.LoginUser;
import com.aegis.common.exception.ServiceException;
import com.aegis.common.utils.SecurityUtils;
import com.aegis.common.utils.StringUtils;
import com.aegis.emergency.alert.constant.AlertLogAction;
import com.aegis.emergency.alert.domain.DutyAlertLog;
import com.aegis.emergency.alert.mapper.DutyAlertLogMapper;
import com.aegis.emergency.alert.service.DutyAlertLogService;
import org.springframework.stereotype.Service;

/**
 * 警情操作日志 Service 实现
 *
 * 统一记录与查询日志
 *
 * @author aegis
 */
@Service
public class DutyAlertLogServiceImpl extends ServiceImpl<DutyAlertLogMapper, DutyAlertLog>
        implements DutyAlertLogService {

    @Override
    public List<DutyAlertLog> listByCondition(DutyAlertLog condition) {
        LambdaQueryWrapper<DutyAlertLog> wrapper = new LambdaQueryWrapper<>();
        if (condition != null) {
            if (condition.getAlertId() != null) {
                wrapper.eq(DutyAlertLog::getAlertId, condition.getAlertId());
            }
            if (StringUtils.isNotEmpty(condition.getAction())) {
                wrapper.eq(DutyAlertLog::getAction, condition.getAction());
            }
            if (condition.getCreatedAt() != null) {
                wrapper.ge(DutyAlertLog::getCreatedAt, condition.getCreatedAt());
            }
        }
        wrapper.orderByDesc(DutyAlertLog::getCreatedAt);
        return list(wrapper);
    }

    @Override
    public List<DutyAlertLog> listByAlertId(Long alertId) {
        if (alertId == null) {
            throw new ServiceException("警情ID不能为空");
        }
        LambdaQueryWrapper<DutyAlertLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DutyAlertLog::getAlertId, alertId)
                .orderByDesc(DutyAlertLog::getCreatedAt);
        return list(wrapper);
    }

    @Override
    public void recordLog(Long alertId, AlertLogAction action, Object payload) {
        if (alertId == null) {
            throw new ServiceException("警情ID不能为空");
        }
        if (action == null) {
            throw new ServiceException("操作类型不能为空");
        }

        DutyAlertLog log = new DutyAlertLog();
        log.setAlertId(alertId);
        log.setAction(action.getCode());
        log.setOperatorId(SecurityUtils.getUserId());
        
        // 优先使用昵称，如果昵称为空则使用用户名
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String operatorName = StringUtils.isNotEmpty(loginUser.getUser().getNickName()) 
            ? loginUser.getUser().getNickName() 
            : loginUser.getUser().getUserName();
        log.setOperatorName(operatorName);
        
        log.setCreatedAt(new Date());

        if (payload != null) {
            log.setPayload(JSON.toJSONString(payload));
        }

        save(log);
    }
}