package com.hdkj.lt.bf.controller;

import com.hdkj.fhzg.entity.Page;
import com.hdkj.fhzg.request.AutoTaskQueryPageRequest;
import com.hdkj.fhzg.request.AutoTaskQueryRequest;
import com.hdkj.fhzg.response.*;
import com.hdkj.hussar.ApiResponse;
import com.hdkj.lt.bf.service.AutoTaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author rj
 * @date 2026/3/9 17:23
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/autoTask")
@RequiredArgsConstructor
public class AutoTaskController {

    private final AutoTaskService autoTaskService;

    /**
     * 区县线路情况统计
     *
     * @return
     */
    @PostMapping("/getCountryStatistic")
    public ApiResponse<List<AutoTaskFeederResponse>> getCountryStatistic(@RequestBody AutoTaskQueryRequest request) {
        if (StringUtils.isEmpty(request.getCityCode())) {
            return ApiResponse.fail("地市编码不能为空");
        }
        return ApiResponse.success(autoTaskService.getCountryStatistic(request));
    }

    /**
     * 区县线路变更列表
     *
     * @return
     */
    @PostMapping("/getCountryFeederChangeList")
    public ApiResponse<List<AutoTaskFeederChangeResponse>> getCountryFeederChangeList(@RequestBody AutoTaskQueryRequest request) {
        if (StringUtils.isEmpty(request.getCountryCode())) {
            return ApiResponse.fail("区县编码不能为空");
        }
        return ApiResponse.success(autoTaskService.getCountryFeederChangeList(request));
    }


    /**
     * 根据地市获取区县方案成功率列表
     *
     * @return
     */
    @PostMapping("/getCityDetailList")
    public ApiResponse<List<AutoTaskCityDetailResponse>> getCityDetailList(@RequestBody AutoTaskQueryRequest request) {
        if (StringUtils.isEmpty(request.getCityCode())) {
            return ApiResponse.fail("地市编码不能为空");
        }
        return ApiResponse.success(autoTaskService.getCityDetailList(request));
    }

    /**
     * 根据区县获取各线路方案成功率列表
     *
     * @return
     */
    @PostMapping("/getCountryDetailList")
    public ApiResponse<List<AutoTaskCountryDetailResponse>> getCountryDetailList(@RequestBody AutoTaskQueryRequest request) {
        if (StringUtils.isEmpty(request.getCountryCode())) {
            return ApiResponse.fail("区县编码不能为空");
        }
        return ApiResponse.success(autoTaskService.getCountryDetailList(request));
    }

    /**
     * 根据地市获取区县方案成功率折线图数据
     *
     * @return
     */
    @PostMapping("/getCityDetailChart")
    public ApiResponse<List<AutoTaskChartResponse>> getCityDetailChart(@RequestBody AutoTaskQueryRequest request) {
        if (StringUtils.isEmpty(request.getCityCode())) {
            return ApiResponse.fail("地市编码不能为空");
        }
        return ApiResponse.success(autoTaskService.getCityDetailChart(request));
    }

    /**
     * 根据区县获取各线路方案成功率折线图数据
     *
     * @return
     */
    @PostMapping("/getCountryDetailChart")
    public ApiResponse<List<AutoTaskChartResponse>> getCountryDetailChart(@RequestBody AutoTaskQueryRequest request) {
        if (StringUtils.isEmpty(request.getCountryCode())) {
            return ApiResponse.fail("区县编码不能为空");
        }
        return ApiResponse.success(autoTaskService.getCountryDetailChart(request));
    }


    /**
     * 根据地市获取区县方案成功率列表 分页
     *
     * @return
     */
    @PostMapping("/getCityDetailPage")
    public ApiResponse<Page<AutoTaskCityDetailResponse>> getCityDetailPage(@RequestBody AutoTaskQueryPageRequest request) {
        if (StringUtils.isEmpty(request.getCityCode())) {
            return ApiResponse.fail("地市编码不能为空");
        }
        return ApiResponse.success(autoTaskService.getCityDetailPage(request));
    }

    /**
     * 根据区县获取各线路方案成功率列表
     *
     * @return
     */
    @PostMapping("/getCountryDetailPage")
    public ApiResponse<Page<AutoTaskCountryDetailResponse>> getCountryDetailPage(@RequestBody AutoTaskQueryPageRequest request) {
        if (StringUtils.isEmpty(request.getCountryCode())) {
            return ApiResponse.fail("区县编码不能为空");
        }
        return ApiResponse.success(autoTaskService.getCountryDetailPage(request));
    }

    @PostMapping("/export")
    public void export(@RequestBody AutoTaskQueryPageRequest request, HttpServletResponse response) {
        autoTaskService.export(request, response);
    }

    /**
     * 根据地市创建执行计划
     * @param request
     * @return
     */
    @PostMapping("/createTask")
    public ApiResponse<String> createTask(@RequestBody AutoTaskQueryRequest request) {
        if (StringUtils.isEmpty(request.getCityCode())) {
            return ApiResponse.fail("地市编码不能为空");
        }
        return ApiResponse.success(autoTaskService.createTask(request.getCityCode()));
    }


}
