package com.yhy.module.core.controller.admin.config;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.config.vo.ConfigUnitTabRefRespVO;
import com.yhy.module.core.controller.admin.config.vo.ConfigUnitTabRefSaveReqVO;
import com.yhy.module.core.service.config.ConfigUnitTabRefService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 单位工程界面配置 - 标签页引用")
@RestController
@RequestMapping("/core/config/unit-tab-ref")
@Validated
public class ConfigUnitTabRefController {

    @Resource
    private ConfigUnitTabRefService service;

    @PostMapping("/save-refs")
    @Operation(summary = "批量保存引用（覆盖式）")
    @PreAuthorize("@ss.hasPermission('core:config:unit-tab-ref:create')")
    public CommonResult<Boolean> saveRefs(@Valid @RequestBody ConfigUnitTabRefSaveReqVO reqVO) {
        service.saveRefs(reqVO);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取引用列表（含模板节点详情）")
    @Parameters({
        @Parameter(name = "catalogItemId", description = "fields_majors 节点ID", required = true),
        @Parameter(name = "tabType", description = "标签页类型：measure/other/unit_summary", required = true)
    })
    @PreAuthorize("@ss.hasPermission('core:config:unit-tab-ref:query')")
    public CommonResult<List<ConfigUnitTabRefRespVO>> getList(@RequestParam("catalogItemId") Long catalogItemId,
                                                              @RequestParam("tabType") String tabType) {
        return success(service.getList(catalogItemId, tabType));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "取消单个引用")
    @Parameter(name = "id", description = "引用ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:config:unit-tab-ref:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        service.delete(id);
        return success(true);
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:config:unit-tab-ref:update')")
    public CommonResult<Boolean> swapSort(@RequestParam("nodeId1") Long nodeId1,
                                          @RequestParam("nodeId2") Long nodeId2) {
        service.swapSort(nodeId1, nodeId2);
        return success(true);
    }
}
