/*
 * 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.InspectionInventory;
import com.codvision.basic.entity.vo.inspection.InspectionInventoryVO;
import com.codvision.basic.service.inspection.InspectionInventoryService;
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.List;
import java.util.Map;

/**
 * @author codvision
 */
@RestController
@RequestMapping("/InspectionInventory")
@Api(tags = "检查清单")
@RequiredArgsConstructor
public class InspectionInventoryController {

    private final InspectionInventoryService service;

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

    @PostMapping("/getInspectionClassByTaskId")
    @ApiOperation(value = "获取任务对应检查项检查")
    public ResponseEntity<List<InspectionInventoryVO>> getInspectionClassByTaskId(@ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                                                                                  @ApiParam(value = "检查任务id") @RequestParam(required = false) Integer taskId) {
        return new ResponseEntity<List<InspectionInventoryVO>>().data(service.getInspectionClassByTaskId(keyword, taskId));
    }

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

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

        service.updateById(inspectionInventory);
        return responseEntity.data(inspectionInventory);
    }

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

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