package cn.iocoder.yudao.module.smartfactory.controller.admin.size;

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.smartfactory.controller.admin.size.vo.SizePageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.size.vo.SizeRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.size.vo.SizeSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.size.SizeDO;
import cn.iocoder.yudao.module.smartfactory.service.size.SizeService;
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;

@Tag(name = "管理后台 - 规格")
@RestController
@RequestMapping("/smartfactory/size")
@Validated
public class SizeController {

    @Resource
    private SizeService sizeService;

    @PostMapping("/create")
    @Operation(summary = "创建规格")
    @PreAuthorize("@ss.hasPermission('smartfactory:size:create')")
    public CommonResult<Long> createSize(@Valid @RequestBody SizeSaveReqVO createReqVO) {
        return success(sizeService.createSize(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新规格")
    @PreAuthorize("@ss.hasPermission('smartfactory:size:update')")
    public CommonResult<Boolean> updateSize(@Valid @RequestBody SizeSaveReqVO updateReqVO) {
        sizeService.updateSize(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除规格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:size:delete')")
    public CommonResult<Boolean> deleteSize(@RequestParam("id") Long id) {
        sizeService.deleteSize(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得规格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:size:query')")
    public CommonResult<SizeRespVO> getSize(@RequestParam("id") Long id) {
        SizeDO size = sizeService.getSize(id);
        return success(BeanUtils.toBean(size, SizeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得规格分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:size:query')")
    public CommonResult<PageResult<SizeRespVO>> getSizePage(@Valid SizePageReqVO pageReqVO) {
        PageResult<SizeDO> pageResult = sizeService.getSizePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SizeRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得规格列表 | 用于前端下拉框")
    @PreAuthorize("@ss.hasPermission('smartfactory:size:query')")
    public CommonResult<List<SizeRespVO>> getSize() {

        List<SizeDO> sizes = sizeService.getListSize();
        return success(BeanUtils.toBean(sizes, SizeRespVO.class));
    }
}