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.ConfigUnitDivisionTemplateRespVO;
import com.yhy.module.core.controller.admin.config.vo.ConfigUnitDivisionTemplateSaveReqVO;
import com.yhy.module.core.service.config.ConfigUnitDivisionTemplateService;
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.PutMapping;
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-division-template")
@Validated
public class ConfigUnitDivisionTemplateController {

    @Resource
    private ConfigUnitDivisionTemplateService service;

    @PostMapping("/create")
    @Operation(summary = "创建分部分项模板节点")
    @PreAuthorize("@ss.hasPermission('core:config:unit-division-template:create')")
    public CommonResult<Long> create(@Valid @RequestBody ConfigUnitDivisionTemplateSaveReqVO reqVO) {
        return success(service.create(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分部分项模板节点")
    @PreAuthorize("@ss.hasPermission('core:config:unit-division-template:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody ConfigUnitDivisionTemplateSaveReqVO reqVO) {
        service.update(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分部分项模板节点（级联删除子节点和引用）")
    @Parameter(name = "id", description = "ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:config:unit-division-template:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        service.delete(id);
        return success(true);
    }

    @GetMapping("/tree")
    @Operation(summary = "获取模板树（按标签页类型）")
    @Parameters({
        @Parameter(name = "catalogItemId", description = "fields_majors 节点ID", required = true),
        @Parameter(name = "tabType", description = "标签页类型：division/measure/other/unit_summary", required = true)
    })
    @PreAuthorize("@ss.hasPermission('core:config:unit-division-template:query')")
    public CommonResult<List<ConfigUnitDivisionTemplateRespVO>> getTree(@RequestParam("catalogItemId") Long catalogItemId,
                                                                        @RequestParam("tabType") String tabType) {
        return success(service.getTree(catalogItemId, tabType));
    }

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