package com.xdap.self_development.controller;


import com.definesys.mpaas.common.http.Response;
import com.xdap.self_development.controller.request.AnalysisRequest;
import com.xdap.self_development.controller.request.AnalysisVerRequest;
import com.xdap.self_development.controller.request.ModelComparisonExportRequest;
import com.xdap.self_development.domain.pojo.EngineParamDetailPojo;
import com.xdap.self_development.exception.CommonException;
import com.xdap.self_development.service.TcDataQueryService;
import org.springframework.validation.BindingResult;
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.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * TC数据查询控制器
 * 提供TC数据查询相关的功能，包括根据版本、子系统-零部件查询参数数据，以及数据导出功能
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/tcDataQuery")
public class TcDataQueryController {

    @Resource
    private TcDataQueryService tcDataQueryService;

    /**
     * 根据机型ID、版本和子系统-零部件查询参数数据
     *
     * @param analysisRequest 分析版本请求
     * @return 参数数据映射
     */
    @PostMapping("/tcDataSelectByVersion")
    public Response tcDataSelectByVersion(
            @Valid @RequestBody AnalysisVerRequest analysisRequest,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        Map<String, List<EngineParamDetailPojo>> stringListHashMap = tcDataQueryService
                .tcDataSelectByVersion(analysisRequest);
        return Response.ok().data(stringListHashMap);
    }

    /**
     * 根据机型ID和子系统-零部件查询参数数据
     *
     * @param analysisRequest 分析请求
     * @return 参数数据映射
     */
    @PostMapping("/tcDataSelectByPartsNames")
    public Response tcDataSelectByPartsNames(@RequestBody AnalysisRequest analysisRequest) {
        Map<String, List<EngineParamDetailPojo>> stringListHashMap = tcDataQueryService.tcDataSelectAllByPartsName(analysisRequest);
        return Response.ok().data(stringListHashMap);
    }
    /**
     * 根据机型ID和子系统-零部件查询参数数据（备份--未使用）
     *
     * @param analysisRequest 分析请求
     * @return 参数数据映射
     */
    @PostMapping("/tcDataSelectAllByPartsNameBack")
    public Response tcDataSelectAllByPartsNameBack(@RequestBody AnalysisRequest analysisRequest) {
        Map<String, List<EngineParamDetailPojo>> stringListHashMap = tcDataQueryService.tcDataSelectAllByPartsNameBack(analysisRequest);
        return Response.ok().data(stringListHashMap);
    }
    /**
     * 根据机型ID和子系统-零部件+userID下载为excel
     *
     * @param request  模型比较导出请求
     * @param response HTTP响应
     * @return 操作结果响应
     */
    @PostMapping("/modelComparisonExport")
    public Response modelComparisonExport(@RequestBody ModelComparisonExportRequest request,HttpServletResponse response) {
        tcDataQueryService.modelComparisonExport(request,response);
        return Response.ok().data("导出成功");
    }
    /**
     * 下载TC数据ads_prd_f_yfsjglpt_fdjlbjcjb
     *
     * @param response HTTP响应
     * @return 操作结果响应
     */
    @PostMapping("/syncA")
    public Response excelExportA(HttpServletResponse response) {
        try {
            tcDataQueryService.excelExportA(response);
        } catch (Exception e) {
            return Response.error("导出失败" + e.getMessage());
        }
        return Response.ok().data("导出成功");
    }
    /**
     * 下载TC数据ads_prd_f_tc_flsxb
     *
     * @param response HTTP响应
     * @return 操作结果响应
     */
    @PostMapping("/syncB")
    public Response excelExportB(HttpServletResponse response) {
        try {
            tcDataQueryService.excelExportB(response);
        } catch (Exception e) {
            return Response.error("导出失败" + e.getMessage());
        }
        return Response.ok().data("导出成功");
    }

}
