/*
 * 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.InspectionChecklist;
import com.codvision.basic.service.inspection.InspectionChecklistService;
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("/InspectionChecklist")
@Api(tags = "检查-条目")
@RequiredArgsConstructor
public class InspectionChecklistController {

    private final InspectionChecklistService service;

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

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

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

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