package com.gyee.runeconomy.controller;


import com.gyee.common.model.StringUtils;
import com.gyee.runeconomy.dto.R;
import com.gyee.runeconomy.dto.ResultMsg;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicEquipment;
import com.gyee.runeconomy.model.auto.ProBasicSubStation;
import com.gyee.runeconomy.service.TokenService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 调度场站名称 前端控制器
 * </p>
 *
 * @author wang
 * @since 2022-11-22
 */
@RestController
@RequestMapping("//substation")
public class ProBasicSubStationController {


    @Resource
    private TokenService tokenService;

    @GetMapping("/queryall")
    @ResponseBody
    @ApiOperation(value = "获得升压站信息", notes = "获得升压站信息")
    public R queryAll() throws Exception {

        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        List<ProBasicSubStation> sublist = tokenService.getSubwpls(request);

        if (StringUtils.isNotNull(sublist)) {
            return R.data(ResultMsg.ok(sublist));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @GetMapping("/querywtbysubid")
    @ResponseBody
    @ApiOperation(value = "通过升压站编号火车风机列表", notes = "通过升压站编号火车风机列表")
    public R queryWtBySubId(String subId) throws Exception {

        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        List<ProBasicSubStation> sublist = tokenService.getSubwpls(request);
        List<ProBasicEquipment> subwtls = new ArrayList<>();
        if (StringUtils.notEmp(subId)) {
            for (ProBasicSubStation sub : sublist) {
                if (sub.getId().equals(subId)) {

                    if (CacheContext.subWtsMap.containsKey(subId)) {
                        subwtls = CacheContext.subWtsMap.get(subId);
                    }
                    break;
                }
            }
        }


        if (StringUtils.isNotNull(subwtls)) {
            return R.data(ResultMsg.ok(subwtls));
        } else {
            return R.error(ResultMsg.error());
        }
    }
}
