package com.ruoyi.adapter.api;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.predict.domain.PvPowerStation;
import com.ruoyi.predict.domain.Windplant;
import com.ruoyi.predict.domain.Windplantproject;
import com.ruoyi.predict.service.IPvPowerStationService;
import com.ruoyi.predict.service.IWindplantService;
import com.ruoyi.predict.service.IWindplantprojectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hlf
 * @date 2022/11/11 9:11
 * 文件说明：图迹场站参数数据
 */
@Api(value = "图迹场站参数数据Controller", tags = {"图迹场站参数数据操作接口"})
@Controller
@RequestMapping("/adapter/api/tjspdc")
public class TuJiStationParametersDataController extends BaseController {

    @Autowired
    private IWindplantService windplantService;

    @Autowired
    private IPvPowerStationService pvPowerStationService;

    @Autowired
    private IWindplantprojectService windplantprojectService;

    @ApiOperation("场站参数")
    @GetMapping("/getTerminalCdqglycData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号或期次编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "queryType", value = "查询类型(1场站 2期次)", required = true),
            @ApiImplicitParam(name = "longitude", value = "经度"),
            @ApiImplicitParam(name = "latitude", value = "纬度"),
    })
    public AjaxResult getTerminalCdqglycData(@RequestParam(value = "stationNumber") String stationNumber,
                                             @RequestParam(value = "itemVal") int itemVal,
                                             @RequestParam(value = "queryType") int queryType) {
        JSONObject json = new JSONObject();
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        if (queryType == 1) {
            if (itemVal == 0) {
                Windplant windplant = windplantService.selectWindplantById(Convert.toLong(stationNumber));
                if (ObjectUtil.isNotNull(windplant)) {
                    TjDataFormat tjDataFormat = new TjDataFormat();
                    tjDataFormat.setLongitude(windplant.getBsP());
                    tjDataFormat.setLatitude(windplant.getBsQ());
                    tjDataFormatList.add(tjDataFormat);
                }
            } else if (itemVal == 4) {
                PvPowerStation pvPowerStation = pvPowerStationService.selectPvPowerStationById(Convert.toLong(stationNumber));
                if (ObjectUtil.isNotNull(pvPowerStation)) {
                    TjDataFormat tjDataFormat = new TjDataFormat();
                    tjDataFormat.setLongitude(pvPowerStation.getLongtitude());
                    tjDataFormat.setLatitude(pvPowerStation.getLatitude());
                    tjDataFormatList.add(tjDataFormat);
                }
            }
        } else if (queryType == 2) {
            if (itemVal == 0) {
                String[] sn = stationNumber.split(",");
                Windplantproject windplantproject = windplantprojectService.selectWindplantprojectByWpNoProjectNo(Convert.toLong(sn[0]), Convert.toLong(sn[1]));
                if (ObjectUtil.isNotNull(windplantproject)) {
                    Windplant windplant = windplantService.selectWindplantById(windplantproject.getWpNo());
                    if (ObjectUtil.isNotNull(windplant)) {
                        TjDataFormat tjDataFormat = new TjDataFormat();
                        tjDataFormat.setLongitude(windplant.getBsP());
                        tjDataFormat.setLatitude(windplant.getBsQ());
                        tjDataFormatList.add(tjDataFormat);
                    }
                }
            } else if (itemVal == 4) {
                //暂无 后补
            }
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    public static class TjDataFormat {

        //经度
        private Double longitude;

        //纬度
        private Double latitude;

        public Double getLongitude() {
            return longitude;
        }

        public void setLongitude(Double longitude) {
            this.longitude = longitude;
        }

        public Double getLatitude() {
            return latitude;
        }

        public void setLatitude(Double latitude) {
            this.latitude = latitude;
        }
    }
}
