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

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.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 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/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")
    @Parameter(name = "code", description = "编码（模糊查询）", required = false, example = "C001")
    @Parameter(name = "name", description = "名称（模糊查询）", required = false, example = "水泥")
    @Parameter(name = "spec", description = "型号规格（模糊查询）", required = false, example = "P.O 42.5")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<List<ResourceItemRespVO>> getAvailableResourceItems(
            @RequestParam("quotaItemId") Long quotaItemId,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "spec", required = false) String spec) {
        // 如果没有任何查询条件，使用原方法（性能更好）
        if (code == null && name == null && spec == null) {
            return success(quotaResourceService.getAvailableResourceItems(quotaItemId));
        }
        // 有查询条件时使用带过滤的方法
        return success(quotaResourceService.getAvailableResourceItemsWithFilter(quotaItemId, code, name, spec));
    }

    @GetMapping("/get-by-code")
    @Operation(summary = "根据编码查询可用工料机（精确匹配）")
    @Parameter(name = "quotaItemId", description = "定额基价ID", required = true, example = "1")
    @Parameter(name = "code", description = "工料机编码", required = true, example = "C001")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<ResourceItemRespVO> getResourceItemByCode(
            @RequestParam("quotaItemId") Long quotaItemId,
            @RequestParam("code") String code) {
        return success(quotaResourceService.getResourceItemByCode(quotaItemId, code));
    }

    // 【已删除】后台定额调整功能改为纯展示效果，以下接口已删除：
    // - /apply-adjustment
    // - /apply-dynamic-adjustment
    // - /apply-dynamic-merge
}
