package com.its.api.its.controller.statistics;

import com.its.api.its.model.dto.statistics.IncidentSectStatDto;
import com.its.api.its.model.dto.statistics.IncidentTypeStatDto;
import com.its.api.its.service.statistics.IncidentStatisticsService;
import com.its.api.utils.ItsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/statistics/incident")
@Api(tags = "03.통계-4.돌발상황통계")
public class IncidentStatisticsController {

    private final IncidentStatisticsService service;

    @ApiOperation(value = "유형별통계-일간통계(TB_INCD_TYPE_DD_STAT)", response = IncidentTypeStatDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/type/dd", produces = {"application/json; charset=utf8"})
    public List<IncidentTypeStatDto> findAllTypeDD(
            @ApiParam(name = "FROM_DT", value = "조회시작일(YYYYMMDD)", example = "20210101", required = true)
            @RequestParam String FROM_DT,
            @ApiParam(name = "TO_DT", value = "조회종료일(YYYYMMDD)", example = "20210102", required = true)
            @RequestParam String TO_DT) {

        String fromDt = FROM_DT + "000000";
        String toDt = TO_DT + "235959";
        return this.service.findAllTypeDD(fromDt, toDt);
    }

    @ApiOperation(value = "유형별통계-월간통계(TB_INCD_TYPE_MN_STAT)", response = IncidentTypeStatDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/type/mn", produces = {"application/json; charset=utf8"})
    public List<IncidentTypeStatDto> findAllTypeMN(
            @ApiParam(name = "FROM_DT", value = "조회시작월(YYYYMM)", example = "20210101", required = true)
            @RequestParam String FROM_DT,
            @ApiParam(name = "TO_DT", value = "조회종료월(YYYYMM)", example = "20210102", required = true)
            @RequestParam String TO_DT) {

        String fromDt = FROM_DT + "01000000";
        String toDt = ItsUtils.getLastDayOfMonth(TO_DT) + "235959";
        return this.service.findAllTypeMN(fromDt, toDt);
    }

    @ApiOperation(value = "유형별통계-년간통계(TB_INCD_TYPE_MN_STAT)", response = IncidentTypeStatDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/type/yy", produces = {"application/json; charset=utf8"})
    public List<IncidentTypeStatDto> findAllTypeYY(
            @ApiParam(name = "FROM_DT", value = "조회시작년도(YYYY)", example = "2022", required = true)
            @RequestParam String FROM_DT,
            @ApiParam(name = "TO_DT", value = "조회종료년도(YYYY)", example = "2022", required = true)
            @RequestParam String TO_DT) {

        String fromDt = FROM_DT + "0101000000";
        String toDt = TO_DT + "1231235959";
        return this.service.findAllTypeYY(fromDt, toDt);
    }

    @ApiOperation(value = "구간별통계-일간통계(TB_INCD_SECT_DD_STAT)", response = IncidentSectStatDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/sect/dd", produces = {"application/json; charset=utf8"})
    public List<IncidentSectStatDto> findAllSectDD(
            @ApiParam(name = "FROM_DT", value = "조회시작일(YYYYMMDD)", example = "20210101", required = true)
            @RequestParam String FROM_DT,
            @ApiParam(name = "TO_DT", value = "조회종료일(YYYYMMDD)", example = "20210102", required = true)
            @RequestParam String TO_DT) {

        String fromDt = FROM_DT + "000000";
        String toDt = TO_DT + "235959";
        return this.service.findAllSectDD(fromDt, toDt);
    }

    @ApiOperation(value = "구간별통계-월간통계(TB_INCD_SECT_MN_STAT)", response = IncidentSectStatDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/sect/mn", produces = {"application/json; charset=utf8"})
    public List<IncidentSectStatDto> findAllSectMN(
            @ApiParam(name = "FROM_DT", value = "조회시작월(YYYYMM)", example = "20210101", required = true)
            @RequestParam String FROM_DT,
            @ApiParam(name = "TO_DT", value = "조회종료월(YYYYMM)", example = "20210102", required = true)
            @RequestParam String TO_DT) {

        String fromDt = FROM_DT + "01000000";
        String toDt = ItsUtils.getLastDayOfMonth(TO_DT) + "235959";
        return this.service.findAllSectMN(fromDt, toDt);
    }

    @ApiOperation(value = "구간별통계-년간통계(TB_INCD_SECT_MN_STAT)", response = IncidentSectStatDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/sect/yy", produces = {"application/json; charset=utf8"})
    public List<IncidentSectStatDto> findAllSectYY(
            @ApiParam(name = "FROM_DT", value = "조회시작년도(YYYY)", example = "2022", required = true)
            @RequestParam String FROM_DT,
            @ApiParam(name = "TO_DT", value = "조회종료년도(YYYY)", example = "2022", required = true)
            @RequestParam String TO_DT) {

        String fromDt = FROM_DT + "0101000000";
        String toDt = TO_DT + "1231235959";
        return this.service.findAllSectYY(fromDt, toDt);
    }

}
