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.QuotaMarketMaterialRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaMarketMaterialSaveReqVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceItemRespVO;
import com.yhy.module.core.service.quota.QuotaMarketMaterialService;
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/market-material")
@Validated
public class QuotaMarketMaterialController {

    @Resource
    private QuotaMarketMaterialService quotaMarketMaterialService;

    @PostMapping("/create")
    @Operation(summary = "添加定额市场主材设备")
    @PreAuthorize("@ss.hasPermission('core:quota:create')")
    public CommonResult<Long> createMarketMaterial(@Valid @RequestBody QuotaMarketMaterialSaveReqVO createReqVO) {
        return success(quotaMarketMaterialService.createMarketMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定额市场主材设备")
    @PreAuthorize("@ss.hasPermission('core:quota:update')")
    public CommonResult<Boolean> updateMarketMaterial(@Valid @RequestBody QuotaMarketMaterialSaveReqVO updateReqVO) {
        quotaMarketMaterialService.updateMarketMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除定额市场主材设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:quota:delete')")
    public CommonResult<Boolean> deleteMarketMaterial(@RequestParam("id") Long id) {
        quotaMarketMaterialService.deleteMarketMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得定额市场主材设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<QuotaMarketMaterialRespVO> getMarketMaterial(@RequestParam("id") Long id) {
        return success(quotaMarketMaterialService.getMarketMaterialList(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<QuotaMarketMaterialRespVO>> getMarketMaterialList(@RequestParam("quotaItemId") Long quotaItemId) {
        return success(quotaMarketMaterialService.getMarketMaterialList(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(quotaMarketMaterialService.getAvailableResourceItems(quotaItemId));
        }
        return success(quotaMarketMaterialService.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(quotaMarketMaterialService.getResourceItemByCode(quotaItemId, code));
    }
}
