/*
 * Copyright (c) 2024 codvision.com All Rights Reserved.
 */

package com.codvision.basic.controller.inspection;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.basic.entity.inspection.InspectionRecordItem;
import com.codvision.basic.service.inspection.InspectionRecordItemService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author codvision
 */
@RestController
@RequestMapping("/InspectionRecordItem")
@Api(tags = "检查-记录条目关联")
@RequiredArgsConstructor
public class InspectionRecordItemController {

    private final InspectionRecordItemService service;

    @PostMapping("/getPageResult")
    @ApiOperation("分页列表")
    public ResponseEntity<IPage<InspectionRecordItem>> getPageResult(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<InspectionRecordItem>>().data(service.getPageResult(queryPage));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加(点击 Model 可以查看具体详情)")
    public ResponseEntity<InspectionRecordItem> add(@ApiParam(value = "对象") @RequestBody InspectionRecordItem inspectionRecordItem) {
        service.save(inspectionRecordItem);
        return new ResponseEntity<InspectionRecordItem>().data(inspectionRecordItem);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新: 注意此更新只会更新不会NUll的数据, 若要不展示可置为空字符")
    public ResponseEntity<InspectionRecordItem> update(@ApiParam(value = "对象") @RequestBody InspectionRecordItem inspectionRecordItem) {
        ResponseEntity<InspectionRecordItem> responseEntity = new ResponseEntity<>();
        if (inspectionRecordItem.getId() == null) {
            throw new BusinessException(30002);
        }

        service.updateById(inspectionRecordItem);
        return responseEntity.ok();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public ResponseEntity<InspectionRecordItem> delete(@ApiParam(value = "主键") @RequestParam(value = "key") Integer key) {
        ResponseEntity<InspectionRecordItem> responseEntity = new ResponseEntity<>();
        if (key == null) {
            throw new BusinessException(30002);
        }

        service.removeById(key);
        return responseEntity.ok();
    }
}
