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

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.config.vo.ConfigUnitResourceFieldRespVO;
import com.yhy.module.core.controller.admin.config.vo.ConfigUnitResourceFieldSaveReqVO;
import com.yhy.module.core.service.config.ConfigUnitResourceFieldService;
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;

@Tag(name = "管理后台 - 单位工程界面配置 - 工料机字段")
@RestController
@RequestMapping("/core/config/unit-resource-field")
@Validated
public class ConfigUnitResourceFieldController {

    @Resource
    private ConfigUnitResourceFieldService service;

    @PostMapping("/create")
    @Operation(summary = "创建工料机字段设置")
    @PreAuthorize("@ss.hasPermission('core:config:unit-resource-field:create')")
    public CommonResult<Long> create(@Valid @RequestBody ConfigUnitResourceFieldSaveReqVO reqVO) {
        return success(service.create(reqVO));
    }

    @PostMapping("/batch-create")
    @Operation(summary = "批量创建工料机字段设置")
    @PreAuthorize("@ss.hasPermission('core:config:unit-resource-field:create')")
    public CommonResult<Boolean> batchCreate(@Valid @RequestBody List<ConfigUnitResourceFieldSaveReqVO> createReqVOs) {
        service.batchCreate(createReqVOs);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新工料机字段设置")
    @PreAuthorize("@ss.hasPermission('core:config:unit-resource-field:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody ConfigUnitResourceFieldSaveReqVO reqVO) {
        service.update(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工料机字段设置")
    @Parameter(name = "id", description = "ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:config:unit-resource-field:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        service.delete(id);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取工料机字段设置列表")
    @Parameter(name = "catalogItemId", description = "fields_majors 节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:config:unit-resource-field:query')")
    public CommonResult<List<ConfigUnitResourceFieldRespVO>> getList(@RequestParam("catalogItemId") Long catalogItemId) {
        return success(service.getList(catalogItemId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:config:unit-resource-field:update')")
    public CommonResult<Boolean> swapSort(@RequestParam("nodeId1") Long nodeId1,
                                          @RequestParam("nodeId2") Long nodeId2) {
        service.swapSort(nodeId1, nodeId2);
        return success(true);
    }

    @GetMapping("/hidden-fields")
    @Operation(summary = "获取隐藏字段列表（供工作台调用）")
    @Parameter(name = "catalogItemId", description = "fields_majors 节点ID", required = true)
    public CommonResult<List<String>> getHiddenFields(@RequestParam("catalogItemId") Long catalogItemId) {
        return success(service.getHiddenFields(catalogItemId));
    }
}
