package org.eco.als.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.ExtraKGraphTaskBo;
import org.eco.als.domain.bo.ExtraKGraphTaskStatusBo;
import org.eco.als.domain.bo.KGraphTaskBo;
import org.eco.als.domain.vo.KGraphTaskVo;
import org.eco.als.service.IKGraphTaskService;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 知识图谱任务Controller
 *
 * @author wgk
 * @date 2025-01-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/kGraphTask")
public class KGraphTaskController extends BaseController {
    @Resource
    private IKGraphTaskService kGraphTaskService;

    /**
     * 查询知识图谱任务列表
     */
    @GetMapping("/list")
    public CommonResult<PageResult<KGraphTaskVo>> list(KGraphTaskBo kGraphTaskBo) {
        return CommonResult.success(kGraphTaskService.selectPage(kGraphTaskBo));
    }

    /**
     * 获取知识图谱任务详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult<KGraphTaskVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(kGraphTaskService.selectById(id));
    }

    /**
     * 新增知识图谱任务
     */
    @Log(title = "知识图谱任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody KGraphTaskBo kGraphTaskBo) {
        boolean inserted = kGraphTaskService.insert(kGraphTaskBo);
        if (!inserted) {
            return CommonResult.fail("新增知识图谱任务记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 运行知识图谱任务
     */
    @PostMapping("/pro")
    public CommonResult<Void> proTask(@RequestBody KGraphTaskBo kGraphTaskBo) {
        if (StrUtil.isBlank(kGraphTaskBo.getTaskType())) {
            return CommonResult.fail("任务类型不能为空");
        }
        if (ObjectUtil.isEmpty(kGraphTaskBo.getId())) {
            return CommonResult.fail("任务Id不能为空");
        }
        boolean pro = kGraphTaskService.proTask(kGraphTaskBo);
        if (!pro) {
            return CommonResult.fail("执行任务失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改知识图谱任务
     */
    @Log(title = "知识图谱任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody KGraphTaskBo kGraphTaskBo) {
        boolean updated = kGraphTaskService.update(kGraphTaskBo);
        if (!updated) {
            return CommonResult.fail("修改知识图谱任务记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除知识图谱任务
     */
    @Log(title = "知识图谱任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = kGraphTaskService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除知识图谱任务记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 结果回调
     */
    @PostMapping("/result/callback")
    public CommonResult<Void> resultCallback(@Valid @RequestBody ExtraKGraphTaskBo extraKGraphTaskBo) {
        boolean isCallback = kGraphTaskService.resultCallback(extraKGraphTaskBo);
        if (!isCallback) {
            return CommonResult.fail("回调新增结果失败!");
        }
        return CommonResult.success();
    }

    /**
     * 状态回调
     */
    @PostMapping("/status/callback")
    public CommonResult<Void> statusCallback(@Valid @RequestBody ExtraKGraphTaskStatusBo extraKGraphTaskStatusBo) {
        boolean isCallback = kGraphTaskService.statusCallback(extraKGraphTaskStatusBo);
        if (!isCallback) {
            return CommonResult.fail("更新状态失败!");
        }
        return CommonResult.success();
    }
}
