package cn.iocoder.yudao.module.product.controller.admin.inspectionitem;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.inspectionitem.vo.InspectionItemPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.inspectionitem.vo.InspectionItemRespVO;
import cn.iocoder.yudao.module.product.controller.admin.inspectionitem.vo.InspectionItemSaveReqVO;
import cn.iocoder.yudao.module.product.convert.inspectionitem.InspectionItemConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.inspectionitem.InspectionItemDO;
import cn.iocoder.yudao.module.product.service.inspectionitem.InspectionItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
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;

@Api(tags = "管理后台 - 检验-检测项目")
@RestController
@RequestMapping("/product/inspection-item")
@Validated
public class InspectionItemController {

    @Resource
    private InspectionItemService inspectionItemService;

    @PutMapping("/update")
    @ApiOperation("更新检验-检测项目")
    @PreAuthorize("@ss.hasPermission('product:inspection-item:update')")
    public CommonResult<Boolean> updateInspectionItem(@Valid @RequestBody InspectionItemSaveReqVO updateReqVO) {
        inspectionItemService.updateInspectionItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除检验-检测项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:inspection-item:delete')")
    public CommonResult<Boolean> deleteInspectionItem(@RequestParam("id") Long id) {
        inspectionItemService.deleteInspectionItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得检验-检测项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:inspection-item:query')")
    public CommonResult<InspectionItemRespVO> getInspectionItem(@RequestParam("id") Long id) {
        InspectionItemDO inspectionItem = inspectionItemService.getInspectionItem(id);
        return success(InspectionItemConvert.INSTANCE.convert(inspectionItem));
    }

    @GetMapping("/page")
    @ApiOperation("获得检验-检测项目分页")
    @PreAuthorize("@ss.hasPermission('product:inspection-item:query')")
    public CommonResult<PageResult<InspectionItemRespVO>> getInspectionItemPage(@Valid InspectionItemPageReqVO pageVO) {
        PageResult<InspectionItemDO> pageResult = inspectionItemService.getInspectionItemPage(pageVO);
        return success(InspectionItemConvert.INSTANCE.convertPage(pageResult));
    }

}
