package com.hdkj.lt.si.controller.algorithm;

import com.hdkj.hussar.ApiResponse;
import com.hdkj.lt.core.mvc.BaseController;
import com.hdkj.lt.si.modle.algorithm.IndicatorStatusResponse;
import com.hdkj.lt.si.service.algorithm.AlgorithmService;
import lombok.AllArgsConstructor;
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 java.util.Map;

/**
 * 算法服务接口
 *
 * @Description 对外暴露指标计算接口，前端每15分钟调用一次
 */
@RestController
@AllArgsConstructor
@RequestMapping("/algorithm")
public class AlgorithmController extends BaseController {

    private final AlgorithmService algorithmService;

    /**
     * 查询指标计算结果
     *
     * @return 指标计算结果
     */
    @PostMapping(value = "/indicators")
    public ApiResponse<IndicatorStatusResponse> queryIndicators() {
        return data(algorithmService.queryIndicators());
    }

    /**
     * 查询静态网架评估结果并入库
     * <p>
     * 入参：batchId（批次ID）、countyId（区县ID，即运维单位ID maintOrgId）
     *
     * @return 主表ID
     */
    @PostMapping(value = "/static-grid")
    public ApiResponse<Long> queryStaticGridIndicators(@RequestBody Map<String, Object> params) {
        String batchId = params.get("batchId") != null ? String.valueOf(params.get("batchId")) : null;
        String countyId = params.get("countyId") != null ? String.valueOf(params.get("countyId")) : null;
        String countySid = params.get("countySid") != null ? String.valueOf(params.get("countySid")) : null;
        String countyName = params.get("countyName") != null ? String.valueOf(params.get("countyName")) : null;
        String topologyVersion = params.get("topologyVersion") != null ? String.valueOf(params.get("topologyVersion")) : null;
        return data(algorithmService.queryStaticGridIndicators(batchId, countyId, countySid, countyName, topologyVersion));
    }

    /**
     * 手动触发静态网架评估（覆盖更新）
     * <p>
     * 先删除该区县已有数据，再调算法重新计算并入库。
     * 当定时任务失败时可通过此端点手动重跑。
     * <p>
     * 入参：countyId、countySid、countyName、topologyVersion（可选）
     *
     * @return 主表ID
     */
    @PostMapping(value = "/static-grid/manual-trigger")
    public ApiResponse<Long> triggerManualStaticGrid(@RequestBody Map<String, Object> params) {
        String countyId = params.get("countyId") != null ? String.valueOf(params.get("countyId")) : null;
        String countySid = params.get("countySid") != null ? String.valueOf(params.get("countySid")) : null;
        String countyName = params.get("countyName") != null ? String.valueOf(params.get("countyName")) : null;
        String topologyVersion = params.get("topologyVersion") != null ? String.valueOf(params.get("topologyVersion")) : null;
        return data(algorithmService.triggerManualStaticGrid(countyId, countySid, countyName, topologyVersion));
    }
}
