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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.yhy.module.core.controller.admin.resource.vo.ResourceItemQueryReqVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceItemRespVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceItemSaveReqVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourcePriceSaveReqVO;
import com.yhy.module.core.dal.dataobject.resource.ResourcePriceDO;
import com.yhy.module.core.service.resource.ResourceItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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;

@Tag(name = "管理后台 - 工料机项与价格")
@RestController
@RequestMapping("/core/resource/items")
@Validated
public class ResourceItemController {

    @Resource
    private ResourceItemService resourceItemService;

    @GetMapping
    @Operation(summary = "分页查询工料机项")
    public CommonResult<PageResult<ResourceItemRespVO>> page(@Valid ResourceItemQueryReqVO reqVO) {
        return success(resourceItemService.pageItems(reqVO));
    }

    @PostMapping
    @Operation(summary = "创建工料机项")
    public CommonResult<Long> create(@Valid @RequestBody ResourceItemSaveReqVO reqVO) {
        return success(resourceItemService.createItem(reqVO));
    }

    @PutMapping
    @Operation(summary = "更新工料机项")
    public CommonResult<Boolean> update(@Valid @RequestBody ResourceItemSaveReqVO reqVO) {
        resourceItemService.updateItem(reqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除工料机项")
    public CommonResult<Boolean> delete(@PathVariable("id") Long id) {
        resourceItemService.deleteItem(id);
        return success(true);
    }

    // 价格相关
    @GetMapping("/{id}/prices")
    @Operation(summary = "查询工料机价格列表")
    public CommonResult<List<ResourcePriceDO>> listPrices(@PathVariable("id") Long id) {
        return success(resourceItemService.listPrices(id));
    }

    @PostMapping("/{id}/prices")
    @Operation(summary = "新增工料机价格")
    public CommonResult<Long> createPrice(@PathVariable("id") Long id, @Valid @RequestBody ResourcePriceSaveReqVO reqVO) {
        reqVO.setResourceId(id);
        return success(resourceItemService.createPrice(reqVO));
    }

    @PutMapping("/prices")
    @Operation(summary = "更新工料机价格")
    public CommonResult<Boolean> updatePrice(@Valid @RequestBody ResourcePriceSaveReqVO reqVO) {
        resourceItemService.updatePrice(reqVO);
        return success(true);
    }

    @DeleteMapping("/prices/{priceId}")
    @Operation(summary = "删除工料机价格")
    public CommonResult<Boolean> deletePrice(@PathVariable("priceId") Long priceId) {
        resourceItemService.deletePrice(priceId);
        return success(true);
    }
}
