package cn.cslg.pas.controller;

import cn.cslg.pas.common.JsonPage;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.dto.*;
import cn.cslg.pas.common.model.vo.ApplicationScenarioEventNumberVO;
import cn.cslg.pas.common.model.vo.InvestigationTypeProjectNumberVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.service.IEventService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 事件的Controller层
 *
 * @Author chenyu
 * @Date 2023/4/2
 */
@Tag(name = "事件的管理模块")
@RequiredArgsConstructor
@Slf4j
@RequestMapping(Constants.API_VERSION_V2 + "/event")
@RestController
public class EventController {
    private final IEventService eventService;

    @Operation(summary = "新增专题库时批量新增新事件（无事件id）")
    @PostMapping("/addNew")
    public String addNew(@RequestBody List<EventAddNewDTO> eventAddNewDTOs) {
        log.info("开始处理【新增专题库时批量新增新事件（无事件id）】的请求，请求参数为：{}", eventAddNewDTOs);
        List<Integer> eventIds;
        try {
            eventIds = eventService.addNew(eventAddNewDTOs);
        } catch (XiaoShiException e) {
            return Response.error(e.getMessage());
        }
        return Response.success(eventIds);
    }

    @Operation(summary = "新增事件")
    @PostMapping("/addNewEvent")
    public String addNewEvent(@RequestBody ProjectEventAddNewDTO projectEventAddNewDTO) {
        log.info("开始处理【新增事件】的请求，请求参数为：{}", projectEventAddNewDTO);
        try {
            eventService.addNewEvent(projectEventAddNewDTO);
        } catch (XiaoShiException e) {
            return Response.error(e.getMessage());
        }
        return Response.success("新增单个事件完成");
    }

    @Operation(summary = "查询事件")
    @PostMapping("/query")
    public String query(@RequestBody EventQueryPageDTO eventQueryPageDTO) {
        log.info("开始处理【分页查询事件】的请求，请求参数为：{}", eventQueryPageDTO);
        return Response.success(eventService.query(eventQueryPageDTO));

    }

    @Operation(summary = "查询9大应用场景分别对应的事件数量")
    @GetMapping("/queryApplicationScenarioEventNumber")
    public String queryApplicationScenarioEventNumber() {
        log.info("开始处理【查询9大应用场景分别对应的事件数量】的请求");
        List<ApplicationScenarioEventNumberVO> queryResults = eventService.queryApplicationScenarioEventNumber();
        return Response.success(queryResults);
    }

    @Operation(summary = "根据应用场景value和事件名称分页查询事件清单")
    @PostMapping("/queryEvents")
    public String queryEvents(@RequestBody EventQueryPageDTO eventQueryPageDTO) {
        log.info("开始处理【根据应用场景value和事件名称分页查询事件清单】的请求，请求参数为：{}", eventQueryPageDTO);
        JsonPage jsonPage = eventService.queryEvents(eventQueryPageDTO);
        return Response.success(jsonPage);
    }

    @Operation(summary = "根据应用场景和事件id查询调查类型对应的专题库数量")
    @PostMapping("/queryInvestigationTypeProjectNumber")
    public String queryInvestigationTypeProjectNumber(@RequestBody QueryInvestigationTypeProjectNumberDTO queryInvestigationTypeProjectNumberDTO) {
        List<InvestigationTypeProjectNumberVO> queryResults = eventService.queryInvestigationTypeProjectNumber(queryInvestigationTypeProjectNumberDTO);
        return Response.success(queryResults);
    }


    @Operation(summary = "修改事件")
    @PostMapping("/update")
    public String update(@RequestBody EventUpdateDTO eventUpdateDTO) {
        log.info("开始处理【修改事件】的请求，请求参数为：{}", eventUpdateDTO);
        try {
            eventService.update(eventUpdateDTO);
        } catch (XiaoShiException e) {
            return Response.error(e.getMessage());
        }
        return Response.success("修改完成");

    }

    @Operation(summary = "删除事件")
    @GetMapping("/delete")
    public String delete(Integer eventId, Integer projectId) {
        log.info("开始处理【删除事件】的请求，请求参数为：{}, {}", eventId, projectId);
        try {
            eventService.delete(eventId, projectId);
        } catch (XiaoShiException e) {
            return Response.error(e.getMessage());
        }
        return Response.success("删除完成");
    }

}
