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.QuotaResourceRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaResourceSaveReqVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceItemRespVO;
import com.yhy.module.core.service.quota.QuotaResourceService;
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/resource")
@Validated
public class QuotaResourceController {

    @Resource
    private QuotaResourceService quotaResourceService;

    @PostMapping("/create")
    @Operation(summary = "添加定额工料机组成")
    @PreAuthorize("@ss.hasPermission('core:quota:create')")
    public CommonResult<Long> createQuotaResource(@Valid @RequestBody QuotaResourceSaveReqVO createReqVO) {
        return success(quotaResourceService.createQuotaResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定额工料机组成")
    @PreAuthorize("@ss.hasPermission('core:quota:update')")
    public CommonResult<Boolean> updateQuotaResource(@Valid @RequestBody QuotaResourceSaveReqVO updateReqVO) {
        quotaResourceService.updateQuotaResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除定额工料机组成")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:quota:delete')")
    public CommonResult<Boolean> deleteQuotaResource(@RequestParam("id") Long id) {
        quotaResourceService.deleteQuotaResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得定额工料机组成")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<QuotaResourceRespVO> getQuotaResource(@RequestParam("id") Long id) {
        return success(quotaResourceService.getQuotaResourceList(id).stream().findFirst().orElse(null));
    }

    @GetMapping("/list")
    @Operation(summary = "获得定额工料机组成列表")
    @Parameter(name = "quotaItemId", description = "定额子目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<List<QuotaResourceRespVO>> getQuotaResourceList(@RequestParam("quotaItemId") Long quotaItemId) {
        return success(quotaResourceService.getQuotaResourceList(quotaItemId));
    }

    @GetMapping("/available-list")
    @Operation(summary = "获取可选的工料机列表（已过滤范围）")
    @Parameter(name = "quotaItemId", description = "定额子目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<List<ResourceItemRespVO>> getAvailableResourceItems(@RequestParam("quotaItemId") Long quotaItemId) {
        return success(quotaResourceService.getAvailableResourceItems(quotaItemId));
    }
}
