package com.goomood.phm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.goomood.common.annotation.Log;
import com.goomood.common.annotation.RepeatSubmit;
import com.goomood.common.core.controller.BaseController;
import com.goomood.common.core.domain.AjaxResult;
import com.goomood.common.core.page.TableDataInfo;
import com.goomood.common.enums.BusinessType;
import com.goomood.common.utils.StringUtils;
import com.goomood.phm.domain.KGraphTask;
import com.goomood.phm.domain.dto.CataiDTO;
import com.goomood.phm.domain.dto.ExtraKGraphTaskDTO;
import com.goomood.phm.domain.dto.ExtraKGraphTaskStatusDTO;
import com.goomood.phm.service.IKGraphTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/kGraphTask")
public class KGraphTaskController extends BaseController {
    @Resource
    private IKGraphTaskService kGraphTaskService;


    /**
     * 查询知识图谱任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(KGraphTask po) {
        startPage();
        List<KGraphTask> kGraphTasks = kGraphTaskService.selectPage(po);
        return getDataTable(kGraphTasks);
    }
    /**
     * 获取知识图谱任务详细信息
     */
    @GetMapping(value = "/selectOne/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        KGraphTask one = kGraphTaskService.selectById(id);
        return success(one);
    }
    /**
     * 新增知识图谱任务
     */
    @Log(title = "知识图谱任务", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody KGraphTask po) {
        boolean inserted = kGraphTaskService.insert(po);
        if (!inserted) {
            return AjaxResult.error("新增知识图谱任务记录失败");
        }
        return AjaxResult.success("新增知识图谱任务记录成功");
    }
    /**
     * 运行知识图谱任务
     */
    @PostMapping("/pro")
    public AjaxResult proTask(@RequestBody KGraphTask po) {
        if (StringUtils.isBlank(po.getTaskType())) {
            return AjaxResult.error("任务类型不能为空");
        }
        if (ObjectUtils.isEmpty(po.getId())) {
            return AjaxResult.error("任务Id不能为空");
        }
        boolean pro = kGraphTaskService.proTask(po);
        if (!pro) {
            return AjaxResult.error("执行任务失败！");
        }
        return success();
    }

    /**
     * 修改知识图谱任务
     */
    @Log(title = "知识图谱任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody KGraphTask po) {
        boolean updated = kGraphTaskService.update(po);
        if (!updated) {
            return AjaxResult.error("修改知识图谱任务记录失败!");
        }
        return success();
    }

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

    /**
     * 结果回调
     */
    @PostMapping("/result/callback")
    public AjaxResult resultCallback(@Valid @RequestBody ExtraKGraphTaskDTO dto) {
        boolean isCallback = kGraphTaskService.resultCallback(dto);
        if (!isCallback) {
            return AjaxResult.error("回调新增结果失败!");
        }
        return success();
    }
    /**
     * 状态回调
     */
    @PostMapping("/status/callback")
    public AjaxResult statusCallback(@Valid @RequestBody ExtraKGraphTaskStatusDTO extraKGraphTaskStatusdto) {
        boolean isCallback = kGraphTaskService.statusCallback(extraKGraphTaskStatusdto);
        if (!isCallback) {
            return AjaxResult.error("更新状态失败!");
        }
        return success();
    }


    @PostMapping("/catai")
    public AjaxResult catai(@RequestBody CataiDTO dto){
        return AjaxResult.success(kGraphTaskService.catai(dto));
    }

}
