package com.gyee.frame.controller.health;


import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.common.feign.RemoteServiceBuilder;
import com.gyee.frame.mapper.auto.WindturbineMapper;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.export.TsPointData;
import com.gyee.frame.service.Alertrule2ervice;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
import com.gyee.frame.service.WindTurbineTestingPointDiService;
import com.gyee.frame.service.health.MetricsService;
import com.gyee.frame.service.health.MetricsUniformCodeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/analysis")
@Api(value = "可靠性分析" ,tags =  "可靠性分析")
public class ReliabilityAnalysisController {

    @Resource
    private Alertrule2ervice alertrule2ervice;
    @Resource
    private RemoteServiceBuilder remoteService;
    @Resource
    private MetricsService metricsService;
    @Resource
    private MetricsUniformCodeService uniformCodeService;
    @Resource
    private WindTurbineTestingPointAiService windAIService;
    @Resource
    private WindTurbineTestingPointDiService windDIService;
    @Autowired
    private WindturbineMapper windturbineMapper;


    /**
     * 查询报警规则中点的数据
     * @param station
     * @param wtId
     * @param name
     * @param startTs
     * @param endTs
     * @return
     */
    @GetMapping("/detail")
    public AjaxResult analysisDetails(
            @RequestParam(name = "station") String station,
            @RequestParam(name = "wtId") String wtId,
            @RequestParam(name = "name") String name,
            @RequestParam(name = "startTs") long startTs,
            @RequestParam(name = "endTs") long endTs,
            @RequestParam(name = "interval", required = false) Optional<Integer> interval){

        List<Object> list = new ArrayList<>();
        Windturbine wtmid = windturbineMapper.selectByPrimaryKey(wtId);

        List<String> codes = alertrule2ervice.getUniformCodeByNameAndStation(name, station, wtmid.getModelid());
        if (codes == null || codes.size() == 0)
            return AjaxResult.successData(AjaxStatus.success.code, list);

        for (String code : codes){
            Map<String, Object> map = new HashMap<>();
            if (code.startsWith("AI")){
                WindTurbineTestingPointAi windAI = windAIService.getWindTurbineTestingPointAi2(wtId, code);
                List<TsPointData> data = null;
                if (interval.isPresent() && interval.get() > 0)
                    data = remoteService.ShardingService().getHistorySnap(windAI.getId(), startTs, endTs, interval.get());
                else
                    data = remoteService.ShardingService().getHistoryRaw(windAI.getId(), startTs, endTs);

                // 查询点的单位和描述属性
                String unit = "--";
                String desc = "----";
                /*List<MetricsUniformCode> metricsList = uniformCodeService.getListByStationAndUniforCode(station, windAI.getUniformcode());
                if (metricsList != null && metricsList.size() > 0) {
                    MetricsUniformCode metricsUniformCode = metricsList.get(0);
                    List<Metrics> metrics = metricsService.getMetricsByMetricsId(metricsUniformCode.getMetricid());
                    if (metrics != null && metrics.size() > 0){
                        unit = metrics.get(0).getUnitname();
                        desc = metrics.get(1).getMetricname();
                    }
                }*/
                MetricsCode metrics=uniformCodeService.getListName(wtId,windAI.getUniformcode());
                String nm = metrics==null?"":metrics.getName();
                map.put("unit", unit);
                map.put("name", nm);
                map.put("data", data);

                list.add(map);
            }
        }

        return AjaxResult.successData(AjaxStatus.success.code, list);
    }


    @Resource
    WindTurbineTestingPointAiService windTurbineTestingPointAiService;

    @GetMapping("/test")
    public AjaxResult get(){
        List<String> list = alertrule2ervice.getUniformCodeByNameAndStations();

        List<String> modelID = alertrule2ervice.getModelID();


//        for (String model : modelID)
//            System.out.println(model);

        List<String> winds = windTurbineTestingPointAiService.findPointByUniformcode(list, modelID);

        return AjaxResult.successData(AjaxStatus.success.code, winds);
    }

}
