/*
 * 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.dto.InspectionTaskDTO;
import com.codvision.basic.entity.inspection.InspectionTask;
import com.codvision.basic.entity.vo.inspection.InspectionTargetVO;
import com.codvision.basic.entity.vo.inspection.InspectionTaskVO;
import com.codvision.basic.service.inspection.InspectionTaskService;
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("/InspectionTask")
@Api(tags = "检查-任务")
@RequiredArgsConstructor
public class InspectionTaskController {

    private final InspectionTaskService service;

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

    @PostMapping("/getTargetCount")
    @ApiOperation(value = "检查对象统计")
    public ResponseEntity<Map<String, Integer>> getTargetCount(@ApiParam(value = "任务编号") @RequestParam(value = "taskId") Integer taskId,
                                                               @RequestParam(required = false) String checkTag,
                                                               @ApiParam(value = "权限oid") @RequestParam(value = "oid", required = false) String oid) {
        return new ResponseEntity<Map<String, Integer>>().data(service.getTargetCount(taskId, checkTag, oid));
    }

    @PostMapping("/getTaskCount")
    @ApiOperation(value = "获取检查任务统计")
    public ResponseEntity<Map<String, Integer>> getTaskCount() {
        return new ResponseEntity<Map<String, Integer>>().data(service.getTaskCount());
    }

    @PostMapping("/getTargets")
    @ApiOperation(value = "获取检查对象")
    public ResponseEntity<IPage<InspectionTargetVO>> getTargets(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<InspectionTargetVO>>().data(service.getTargets(queryPage));
    }

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

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

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

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