package com.moka.gdtauto.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moka.gdtauto.common.Result;
import com.moka.gdtauto.entity.AdAutoExecuteLog;
import com.moka.gdtauto.service.AdAutoExecuteLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 广告规则执行日志表 前端控制器
 * </p>
 *
 * @author mokamvp
 * @since 2026-03-12
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/execute-log")
public class AdAutoExecuteLogController {

    private final AdAutoExecuteLogService adAutoExecuteLogService;

    /**
     * 分页查询执行日志
     */
    @GetMapping("/list")
    public Result<Page<AdAutoExecuteLog>> list(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = true) Long confId) {
        try {
            LambdaQueryWrapper<AdAutoExecuteLog> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AdAutoExecuteLog::getConfId, confId);
            wrapper.eq(AdAutoExecuteLog::getParentLogId, 0);
            wrapper.orderByDesc(AdAutoExecuteLog::getId);
            Page<AdAutoExecuteLog> result = adAutoExecuteLogService.page(new Page<>(page, size), wrapper);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询执行日志失败", e);
            return Result.fail("查询执行日志失败: " + e.getMessage());
        }
    }

    @GetMapping("/getChildLog")
    public Result<List<AdAutoExecuteLog>> getChildLog(
            @RequestParam(required = true) Long confId,
            @RequestParam(required = true) Long parentId) {
        try {
            LambdaQueryWrapper<AdAutoExecuteLog> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AdAutoExecuteLog::getConfId, confId);
            wrapper.eq(AdAutoExecuteLog::getParentLogId, parentId);
            wrapper.orderByDesc(AdAutoExecuteLog::getId);
            return Result.success(adAutoExecuteLogService.list(wrapper));
        } catch (Exception e) {
            log.error("查询执行日志失败", e);
            return Result.fail("查询执行日志失败: " + e.getMessage());
        }
    }

}
