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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.config.vo.ConfigProjectTreeRespVO;
import com.yhy.module.core.controller.admin.config.vo.ConfigProjectTreeSaveReqVO;
import com.yhy.module.core.controller.admin.config.vo.ConfigProjectTreeSwapSortReqVO;
import com.yhy.module.core.service.config.ConfigProjectTreeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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;

/**
 * 项目界面配置树 Controller
 *
 * @author yhy
 */
@Tag(name = "管理后台 - 项目界面配置树")
@RestController
@RequestMapping("/core/config/project-tree")
@Validated
public class ConfigProjectTreeController {

    @Resource
    private ConfigProjectTreeService configProjectTreeService;

    @PostMapping("/create")
    @Operation(summary = "创建项目界面配置树节点")
    @PreAuthorize("@ss.hasPermission('core:config-project-tree:create')")
    public CommonResult<Long> createConfigProjectTree(@Valid @RequestBody ConfigProjectTreeSaveReqVO createReqVO) {
        return success(configProjectTreeService.createConfigProjectTree(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目界面配置树节点")
    @PreAuthorize("@ss.hasPermission('core:config-project-tree:update')")
    public CommonResult<Boolean> updateConfigProjectTree(@Valid @RequestBody ConfigProjectTreeSaveReqVO updateReqVO) {
        configProjectTreeService.updateConfigProjectTree(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目界面配置树节点")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:config-project-tree:delete')")
    public CommonResult<Boolean> deleteConfigProjectTree(@RequestParam("id") Long id) {
        configProjectTreeService.deleteConfigProjectTree(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取项目界面配置树节点")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:config-project-tree:query')")
    public CommonResult<ConfigProjectTreeRespVO> getConfigProjectTree(@RequestParam("id") Long id) {
        return success(configProjectTreeService.getConfigProjectTree(id));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取项目界面配置树（树形结构）")
    @PreAuthorize("@ss.hasPermission('core:config-project-tree:query')")
    public CommonResult<List<ConfigProjectTreeRespVO>> getConfigProjectTreeList() {
        return success(configProjectTreeService.getConfigProjectTreeList());
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:config-project-tree:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody ConfigProjectTreeSwapSortReqVO swapReqVO) {
        configProjectTreeService.swapSort(swapReqVO);
        return success(true);
    }

    @GetMapping("/industry-options")
    @Operation(summary = "获取行业下拉选项（省市-行业两级结构）")
    @PreAuthorize("@ss.hasPermission('core:config-project-tree:query')")
    public CommonResult<List<ConfigProjectTreeRespVO>> getIndustryOptions() {
        return success(configProjectTreeService.getIndustryOptions());
    }
}
