package com.ruoyi.web.controller.jarvis;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jarvis.domain.BatchPublishItem;
import com.ruoyi.jarvis.domain.BatchPublishTask;
import com.ruoyi.jarvis.domain.request.BatchPublishRequest;
import com.ruoyi.jarvis.domain.request.ParseLineReportRequest;
import com.ruoyi.jarvis.service.IBatchPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 批量发品Controller
 * 
 * @author ruoyi
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/jarvis/batchPublish")
public class BatchPublishController extends BaseController
{
    @Autowired
    private IBatchPublishService batchPublishService;

    /**
     * 解析线报消息
     */
    @PostMapping("/parse")
    public AjaxResult parseLineReport(@RequestBody @Validated ParseLineReportRequest request)
    {
        try {
            List<Map<String, Object>> products = batchPublishService.parseLineReport(request);
            return AjaxResult.success(products);
        } catch (Exception e) {
            logger.error("解析线报消息失败", e);
            return AjaxResult.error("解析失败: " + e.getMessage());
        }
    }

    /**
     * 批量发品
     */
    @Log(title = "批量发品", businessType = BusinessType.INSERT)
    @PostMapping("/publish")
    public AjaxResult batchPublish(@RequestBody @Validated BatchPublishRequest request)
    {
        try {
            Long taskId = batchPublishService.batchPublish(request);
            return AjaxResult.success("任务已创建", taskId);
        } catch (Exception e) {
            logger.error("批量发品失败", e);
            return AjaxResult.error("批量发品失败: " + e.getMessage());
        }
    }

    /**
     * 查询批量发品任务列表
     */
    @GetMapping("/task/list")
    public TableDataInfo listTasks(BatchPublishTask task)
    {
        startPage();
        List<BatchPublishTask> list = batchPublishService.selectTaskList(task);
        return getDataTable(list);
    }

    /**
     * 查询批量发品任务详情
     */
    @GetMapping("/task/{taskId}")
    public AjaxResult getTask(@PathVariable("taskId") Long taskId)
    {
        BatchPublishTask task = batchPublishService.getTaskById(taskId);
        return AjaxResult.success(task);
    }

    /**
     * 查询批量发品明细列表
     */
    @GetMapping("/item/list/{taskId}")
    public AjaxResult listItems(@PathVariable("taskId") Long taskId)
    {
        List<BatchPublishItem> items = batchPublishService.getItemsByTaskId(taskId);
        return AjaxResult.success(items);
    }

    /**
     * 手动重试任务（重新调度待发布/发布失败的明细）
     */
    @PostMapping("/task/retry/{taskId}")
    public AjaxResult retryTask(@PathVariable("taskId") Long taskId)
    {
        batchPublishService.retryTask(taskId);
        return AjaxResult.success("已触发重试");
    }
}

