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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.quota.vo.*;
import com.yhy.module.core.service.quota.QuotaCatalogItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

/**
 * 定额目录条目 Controller
 * 
 * @author yhy
 */
@Tag(name = "管理后台 - 定额目录条目")
@RestController
@RequestMapping("/core/quota/catalog-item")
@Validated
public class QuotaCatalogItemController {

    @Resource
    private QuotaCatalogItemService quotaCatalogItemService;

    @PostMapping("/create")
    @Operation(summary = "创建定额目录条目")
    @PreAuthorize("@ss.hasPermission('core:quota:create')")
    public CommonResult<Long> createQuotaCatalogItem(@Valid @RequestBody QuotaCatalogItemSaveReqVO createReqVO) {
        return success(quotaCatalogItemService.createQuotaCatalogItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定额目录条目")
    @PreAuthorize("@ss.hasPermission('core:quota:update')")
    public CommonResult<Boolean> updateQuotaCatalogItem(@Valid @RequestBody QuotaCatalogItemSaveReqVO updateReqVO) {
        quotaCatalogItemService.updateQuotaCatalogItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除定额目录条目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:quota:delete')")
    public CommonResult<Boolean> deleteQuotaCatalogItem(@RequestParam("id") Long id) {
        quotaCatalogItemService.deleteQuotaCatalogItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得定额目录条目")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<QuotaCatalogItemRespVO> getQuotaCatalogItem(@RequestParam("id") Long id) {
        return success(quotaCatalogItemService.getQuotaCatalogItemList(id).stream().findFirst().orElse(null));
    }

    @GetMapping("/list")
    @Operation(summary = "获得定额目录条目列表")
    @Parameter(name = "parentId", description = "父节点ID", example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<List<QuotaCatalogItemRespVO>> getQuotaCatalogItemList(@RequestParam(value = "parentId", required = false) Long parentId) {
        return success(quotaCatalogItemService.getQuotaCatalogItemList(parentId));
    }

    @GetMapping("/tree")
    @Operation(summary = "获得定额目录树")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<List<QuotaCatalogItemRespVO>> getQuotaCatalogItemTree() {
        return success(quotaCatalogItemService.getQuotaCatalogItemTree());
    }

    @PostMapping("/bind-specialty")
    @Operation(summary = "绑定工料机专业")
    @PreAuthorize("@ss.hasPermission('core:quota:update')")
    public CommonResult<Boolean> bindResourceSpecialty(@Valid @RequestBody QuotaCatalogItemBindSpecialtyReqVO reqVO) {
        quotaCatalogItemService.bindResourceSpecialty(reqVO.getCatalogItemId(), reqVO.getCategoryTreeId());
        return success(true);
    }

    @PostMapping("/add-directory")
    @Operation(summary = "添加目录节点")
    @PreAuthorize("@ss.hasPermission('core:quota:create')")
    public CommonResult<Long> addDirectory(@Valid @RequestBody QuotaCatalogItemSaveReqVO createReqVO) {
        return success(quotaCatalogItemService.addDirectory(createReqVO));
    }

    @PostMapping("/add-content")
    @Operation(summary = "添加内容节点")
    @PreAuthorize("@ss.hasPermission('core:quota:create')")
    public CommonResult<Long> addContent(@Valid @RequestBody QuotaCatalogItemSaveReqVO createReqVO) {
        return success(quotaCatalogItemService.addContent(createReqVO));
    }
}
