package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ModelFieldPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ModelFieldRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ModelFieldSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ModelFieldDO;
import cn.iocoder.yudao.module.system.service.biz.ModelFieldService;
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 static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author: jinyd
 * @CreateTime: 2024/10/17  15:04
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 设备模型字段映射")
@RestController
@RequestMapping("/device/model-field")
@Validated
public class ModelFieldController {

    @Resource
    private ModelFieldService modelFieldService;

    @PostMapping("/create")
    @Operation(summary = "创建设备模型字段映射")
    @PreAuthorize("@ss.hasPermission('device:model-field:create')")
    public CommonResult<Long> createModelField(@Valid @RequestBody ModelFieldSaveReqVO createReqVO) {
        return success(modelFieldService.createModelField(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备模型字段映射")
    @PreAuthorize("@ss.hasPermission('device:model-field:update')")
    public CommonResult<Boolean> updateModelField(@Valid @RequestBody ModelFieldSaveReqVO updateReqVO) {
        modelFieldService.updateModelField(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备模型字段映射")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:model-field:delete')")
    public CommonResult<Boolean> deleteModelField(@RequestParam("id") Long id) {
        modelFieldService.deleteModelField(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备模型字段映射")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:model-field:query')")
    public CommonResult<ModelFieldRespVO> getModelField(@RequestParam("id") Long id) {
        ModelFieldDO modelField = modelFieldService.getModelField(id);
        return success(BeanUtils.toBean(modelField, ModelFieldRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备模型字段映射分页")
    @PreAuthorize("@ss.hasPermission('device:model-field:query')")
    public CommonResult<PageResult<ModelFieldRespVO>> getModelFieldPage(@Valid ModelFieldPageReqVO pageReqVO) {
        PageResult<ModelFieldDO> pageResult = modelFieldService.getModelFieldPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ModelFieldRespVO.class));
    }

}
