package com.gyee.alarm.controller;

import com.alibaba.fastjson.JSONObject;
import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.model.auto.ProBasicEquipmentPoint;
import com.gyee.alarm.model.auto.ProBasicPowerstation;
import com.gyee.alarm.model.auto.ProBasicWindturbinePower;
import com.gyee.alarm.service.DeviceService;
import com.gyee.alarm.service.auto.IProBasicEquipmentPointService;
import com.gyee.alarm.service.auto.IProBasicEquipmentService;
import com.gyee.alarm.service.auto.IProBasicPowerstationService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/info")
@CrossOrigin
@Api(value = "获取基础数据，如风场，风机，测点等", tags = "获取基础数据，如风场，风机，测点等")
public class InfoController {
    @Resource
    private DeviceService deviceService;

    @Resource
    private IProBasicPowerstationService proBasicPowerstationService;

    @Resource
    private IProBasicEquipmentService proBasicEquipmentService;

    @Resource
    private IProBasicEquipmentPointService proBasicEquipmentPointService;

    @GetMapping(value = "/point")
    @ResponseBody
    public JSONObject getPointList(@RequestParam(value = "deviceId", required = true) String deviceid,
                                   @RequestParam(value = "structcode", required = false) String structCode,
                                   @RequestParam(value = "stationId", required = true) String stationId,
                                   @RequestParam(value = "windturbineId", required = true) String windturbineId,
                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                   @RequestParam(value = "pageNum", required = false) Integer pageNum
    ) {

        if (pageSize == null || pageSize == 0)
            pageSize = 100;
        if (pageNum == null || pageNum == 0)
            pageNum = 1;
        JSONObject testingPoint = deviceService.getTestingPointByStructCode(deviceid, structCode, windturbineId, pageSize, pageNum);
        return testingPoint;
    }

    /**
     * 获取风场
     * @param id
     * @return
     */
    @GetMapping(value = "/station")
    @ResponseBody
    public List<ProBasicPowerstation> getAllWindStation(@RequestParam(value = "id", required = false) String id) {

        if (StringUtils.isBlank(id)) {
            id = "_FDC";
        }
        List<ProBasicPowerstation> lst = proBasicPowerstationService.getStationAll(id);
        return lst;
    }

    /**
     * 获取风场和光电场
     * @param id
     * @return
     */
    @GetMapping(value = "/station/all")
    @ResponseBody
    public List<ProBasicPowerstation> getAllStation(@RequestParam(value = "id", required = false) String id) {
        List<ProBasicPowerstation> lst = proBasicPowerstationService.getStationAll(id);
        return lst;
    }

    /**
     * 根据场站名称查询所有风机编号
     * @return
     */
    @GetMapping(value = "/windturbine")
    @ResponseBody
    public List<ProBasicEquipment> getWindturbineByStation( @RequestParam(value = "stationId", required = false) String stationId) {
        List<ProBasicEquipment> lst = proBasicEquipmentService.selectWindturbineByStationid(stationId);
        lst = lst.stream().filter(s -> s.getWindpowerstationId().contains("_FDC")).collect(Collectors.toList());
        return lst;
    }

    @GetMapping(value = "/testing_point_ai")
    @ResponseBody
    public List<ProBasicEquipmentPoint> getTestingPointAIByStationIdAndModelId(
            @RequestParam(value = "stationId") String stationId,
            @RequestParam(value = "modelId") String modelId
    ) {

        List<ProBasicEquipmentPoint> lst = new ArrayList<>();
        if (StringUtils.isNotBlank(stationId) && StringUtils.isNotBlank(modelId)) {
//            String stationStr = stationId.replace("_FDC", "").replace("_GDC", "").trim();
            lst = proBasicEquipmentPointService.selectByStationAndModel(stationId, modelId);
        }
        return lst;
    }

    @GetMapping(value = "/testing_point_di")
    @ResponseBody
    public List<ProBasicEquipmentPoint> getTestingPointDIByStationIdAndModelId(
            @RequestParam(value = "stationId", required = false) String stationId,
            @RequestParam(value = "modelId", required = false) String modelId
    ) {
        List<ProBasicEquipmentPoint> lst = new ArrayList<>();
        if (StringUtils.isNotBlank(stationId) || StringUtils.isNotBlank(modelId)) {

            lst = proBasicEquipmentPointService.selectByStationAndModel(stationId, modelId);


        }
        return lst;

    }

}
