/*
 * 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.InspectionTarget;
import com.codvision.basic.entity.vo.inspection.InspectionTargetVO;
import com.codvision.basic.service.inspection.InspectionTargetService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author codvision
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/inspectionTarget")
@Api(tags = "检查-对象")
public class InspectionTargetController {

    private final InspectionTargetService service;

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

    @PostMapping("/add")
    @ApiOperation("添加")
    public ResponseEntity<String> add(@Validated @RequestBody InspectionTarget inspectiontarget) {
        inspectiontarget.setDataSource("0");
        service.save(inspectiontarget);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@RequestBody InspectionTarget inspectiontarget) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (inspectiontarget.getId() == null) {
            throw new BusinessException(30002);
        }

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

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

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