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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.boq.vo.BoqSubItemRespVO;
import com.yhy.module.core.controller.admin.boq.vo.BoqSubItemSaveReqVO;
import com.yhy.module.core.controller.admin.boq.vo.BoqSubItemSwapSortReqVO;
import com.yhy.module.core.service.boq.BoqSubItemService;
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/boq/sub-item")
@Validated
public class BoqSubItemController {

    @Resource
    private BoqSubItemService boqSubItemService;

    @PostMapping("/create")
    @Operation(summary = "创建清单子项")
    @PreAuthorize("@ss.hasPermission('core:boq:sub-item:create')")
    public CommonResult<Long> createBoqSubItem(@Valid @RequestBody BoqSubItemSaveReqVO createReqVO) {
        return success(boqSubItemService.createBoqSubItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新清单子项")
    @PreAuthorize("@ss.hasPermission('core:boq:sub-item:update')")
    public CommonResult<Boolean> updateBoqSubItem(@Valid @RequestBody BoqSubItemSaveReqVO updateReqVO) {
        boqSubItemService.updateBoqSubItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除清单子项")
    @Parameter(name = "id", description = "子项ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:boq:sub-item:delete')")
    public CommonResult<Boolean> deleteBoqSubItem(@RequestParam("id") Long id) {
        boqSubItemService.deleteBoqSubItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取清单子项")
    @Parameter(name = "id", description = "子项ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:boq:sub-item:query')")
    public CommonResult<BoqSubItemRespVO> getBoqSubItem(@RequestParam("id") Long id) {
        return success(boqSubItemService.getBoqSubItem(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获取清单子项列表")
    @Parameter(name = "boqItemTreeId", description = "清单项树ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:boq:sub-item:query')")
    public CommonResult<List<BoqSubItemRespVO>> getBoqSubItemList(@RequestParam("boqItemTreeId") Long boqItemTreeId) {
        return success(boqSubItemService.getBoqSubItemList(boqItemTreeId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:boq:sub-item:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody BoqSubItemSwapSortReqVO swapReqVO) {
        boqSubItemService.swapSort(swapReqVO);
        return success(true);
    }
}
