package com.gyee.wisdom.alarm.sharding.controller;

import com.gyee.wisdom.alarm.sharding.config.AjaxResult;
import com.gyee.wisdom.alarm.sharding.cache.TestingPointCache;
import com.gyee.wisdom.alarm.sharding.entity.*;
import com.gyee.wisdom.alarm.sharding.mapper.WindturbineMapper;
import com.gyee.wisdom.alarm.sharding.service.Alertrule2Service;
import com.gyee.wisdom.alarm.sharding.service.WindTurbineTestingPointAiService;
import com.gyee.wisdom.alarm.sharding.service.WindTurbineTestingPointDiService;
import com.gyee.wisdom.alarm.sharding.taos.ReadValue;
import com.gyee.wisdom.alarm.sharding.taos.TsData;
import com.gyee.wisdom.alarm.sharding.transport.restful.RestfulClient;
import com.gyee.wisdom.alarm.sharding.util.AjaxStatus;
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 Alertrule2Service alertrule2ervice;
    @Resource
    private ReadValue readValue;
    @Resource
    private WindTurbineTestingPointAiService windAIService;
    @Resource
    private WindTurbineTestingPointDiService windDIService;
    @Autowired
    private WindturbineMapper windturbineMapper;
    @Autowired
    private RestfulClient restfulClient;


    /**
     * 查询报警规则中点的数据
     * @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) throws Exception {

        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);

        List<TestingPointAI> testingPointAIList = TestingPointCache.getTestingPointAIList(wtmid.getModelid());
        List<TestingPointDI> testingPointDIList = TestingPointCache.getTestingPointDIList(wtmid.getModelid());
        for (String code : codes) {
            Map<String, Object> map = new HashMap<>();
            List<TsData> tsDataList = null;
                //根据风机编号和编码获取点名
                String thingType = "windturbine";
                tsDataList = restfulClient.getHistorySnap("", wtId, thingType, code, startTs, endTs, interval.get());
                String pointName="";
                if(code.toUpperCase().contains("AI")){
                    Optional<TestingPointAI> first = testingPointAIList.stream().filter(s -> code.equals(s.getUniformCode())).findFirst();
                    pointName=first.isPresent()?first.get().getName():"";
                }
            if(code.toUpperCase().contains("DI")){
                Optional<TestingPointDI> first = testingPointDIList.stream().filter(s -> code.equals(s.getUniformCode())).findFirst();
                pointName=first.isPresent()?first.get().getName():"";
            }

            String unit = "--";
                map.put("unit", unit);
                map.put("name", code+"--"+pointName);
                if (tsDataList != null && tsDataList.size() != 0) {
                    map.put("data", tsDataList);
                }
                list.add(map);
        }
        return AjaxResult.successData(AjaxStatus.success.code, list);
    }
}
