package com.gxwebsoft.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.app.mapper.AppEventMapper;
import com.gxwebsoft.app.service.AppEventService;
import com.gxwebsoft.app.entity.AppEvent;
import com.gxwebsoft.app.param.AppEventParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 应用操作动态Service实现
 *
 * @author 科技小王子
 * @since 2026-03-28 21:29:44
 */
@Slf4j
@Service
public class AppEventServiceImpl extends ServiceImpl<AppEventMapper, AppEvent> implements AppEventService {

    @Override
    public PageResult<AppEvent> pageRel(AppEventParam param) {
        PageParam<AppEvent, AppEventParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<AppEvent> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<AppEvent> listRel(AppEventParam param) {
        List<AppEvent> list = baseMapper.selectListRel(param);
        PageParam<AppEvent, AppEventParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public AppEvent getByIdRel(Integer id) {
        AppEventParam param = new AppEventParam();
        param.setId(id.longValue());
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    @Async
    public void logEvent(Long websiteId, String eventType, String title, String content,
                         Long operatorId, String operatorName, Long refId, String refType) {
        try {
            AppEvent event = new AppEvent();
            event.setWebsiteId(websiteId);
            event.setEventType(eventType);
            event.setTitle(title);
            event.setContent(content);
            event.setOperatorId(operatorId);
            event.setOperator(operatorName);
            event.setRefId(refId);
            event.setRefType(refType);
            event.setStatus(0);
            save(event);
            log.debug("记录事件成功，websiteId={}, eventType={}, title={}", websiteId, eventType, title);
        } catch (Exception e) {
            log.warn("记录事件失败，websiteId={}, eventType={}: {}", websiteId, eventType, e.getMessage());
        }
    }

    @Override
    @Async
    public void logEvent(Long websiteId, String eventType, String title, Long operatorId, String operatorName) {
        logEvent(websiteId, eventType, title, null, operatorId, operatorName, null, null);
    }

    @Override
    public AppEvent getLatestEvent(Long websiteId) {
        return getOne(new LambdaQueryWrapper<AppEvent>()
                .eq(AppEvent::getWebsiteId, websiteId)
                .orderByDesc(AppEvent::getCreateTime)
                .last("LIMIT 1"));
    }

}
