package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.KGraphEntityBo;
import org.eco.als.domain.vo.KGraphEntityVo;
import org.eco.als.service.IKGraphEntityService;
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.*;

import java.util.List;

/**
 * 知识图谱实体关系Controller
 *
 * @author wgk
 * @date 2025-01-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/kGraphEntity")
public class KGraphEntityController extends BaseController {
    @Resource
    private IKGraphEntityService kGraphEntityService;

    /**
     * 查询知识图谱实体关系列表
     */
    @SaCheckPermission("als:kGraphEntity:list")
    @GetMapping("/list")
    public CommonResult<PageResult<KGraphEntityVo>> list(KGraphEntityBo kGraphEntityBo) {
        return CommonResult.success(kGraphEntityService.selectPage(kGraphEntityBo));
    }

    /**
     * 获取知识图谱实体关系详细信息
     */
    @SaCheckPermission("als:kGraphEntity:query")
    @GetMapping(value = "/{id}")
    public CommonResult<KGraphEntityVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(kGraphEntityService.selectById(id));
    }
    /**
     * 获取知识图谱实体关系详细信息
     */
    @GetMapping(value = "/selectListByTaskId/{id}")
    public CommonResult<List<KGraphEntityVo>> selectListByTaskId(@PathVariable Long id) {
        return CommonResult.success(kGraphEntityService.selectListByTaskId(id));
    }

    /**
     * 新增知识图谱实体关系
     */
    @SaCheckPermission("als:kGraphEntity:add")
    @Log(title = "知识图谱实体关系", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody KGraphEntityBo kGraphEntityBo) {
        boolean inserted = kGraphEntityService.insert(kGraphEntityBo);
        if (!inserted) {
            return CommonResult.fail("新增知识图谱实体关系记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 新增知识图谱实体关系
     */
    @RepeatSubmit()
    @PostMapping("/saveBatch")
    public CommonResult<Void> saveBatch(@Valid @RequestBody List<KGraphEntityBo> kGraphEntityBoList) {
        boolean inserted = kGraphEntityService.saveBatch(kGraphEntityBoList);
        if (!inserted) {
            return CommonResult.fail("新增知识图谱实体关系记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改知识图谱实体关系
     */
    @SaCheckPermission("als:kGraphEntity:edit")
    @Log(title = "知识图谱实体关系", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody KGraphEntityBo kGraphEntityBo) {
        boolean updated = kGraphEntityService.update(kGraphEntityBo);
        if (!updated) {
            return CommonResult.fail("修改知识图谱实体关系记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除知识图谱实体关系
     */
    @SaCheckPermission("als:kGraphEntity:remove")
    @Log(title = "知识图谱实体关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = kGraphEntityService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除知识图谱实体关系记录失败!");
        }
        return CommonResult.success();
    }
}
