package com.zehong.web.controller.supervise;
import com.zehong.system.domain.*;
import com.zehong.system.service.IPipeInterfaceService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

/**
 * 管道信息接口控制层
 */
@Controller
@RestController
@RequestMapping("/pipe/pipe")
public class PipeInterfaceController {

    @Autowired
    IPipeInterfaceService iPipeInterfaceService;


    /**
     * 查询管道数据列表
     */
    @RequestBody
    @RequestMapping("/selectPipeData")
    public PipeList selectPipeData() {
        //获取分组数据
        List<PipeDate> pipeDatesGroup = iPipeInterfaceService.selectGroupPipeData();
        //查询管道数据列表
        List<PipeDate> pipeDates = iPipeInterfaceService.selectPipeData();
        int k=1;
        //循环根据权属单位名称分组的数据
        for (int i=0;i<pipeDatesGroup.size();i++){
             //循环全部数据
            for (int n=0;n<pipeDates.size();n++){
                //判断如果名称一样就一组
                if (pipeDatesGroup.get(i).getPowerCompany().equals(pipeDates.get(n).getPowerCompany())){
//                    //设置分组类型
//                    pipeDates.get(n).setCompanyType(Integer.parseInt(pipeDates.get(n).getPipeId()));
                    pipeDates.get(n).setIconType(1);
                }
            }
            k++;
        }
        PipeList pipeList=new PipeList();
        pipeList.setData(pipeDates);
        return pipeList;
    }


    /**
     * 调压箱数据查询接口
     */
    @RequestBody
    @RequestMapping("/selectDeviceData")
    public DeviceList  selectDeviceData(){
        //调压箱数据查询方法
        List<DeviceData> deviceData = iPipeInterfaceService.selectDeviceData();
        //查询全部数据方法
        List<PressureFlow> pressureFlows1 = iPipeInterfaceService.selectPressureFlow("1");
        //最后返回的封装类型
        DeviceList deviceList=new DeviceList();
        //调压箱数据循环
        for (int i=0;i<deviceData.size();i++){
            //临时存储压力表数据数组
            List<PressureFlow> pressureList=new ArrayList<>();
            //临时存储流量计数据数组
            List<PressureFlow> flowmeterList=new ArrayList<>();
            //存储全部数据
            List<PressureFlow> List=new ArrayList<>();
            for (int s=0;s<pressureFlows1.size();s++){
                //判断调压箱id与下级数据调压箱id是否相等
                if (pressureFlows1.get(s).getDeviceId()==deviceData.get(i).getDeviceId()){
                    //下级数据添加进临时存储数据的数组中
                    List.add(pressureFlows1.get(s));
                    //判断是压力表还是流量计   1压力表  2流量计
                    if (pressureFlows1.get(s).getDeviceType()=="1"){
                        pressureList.add(pressureFlows1.get(s));
                    }else {
                        flowmeterList.add(pressureFlows1.get(s));
                    }
                }
            }
            //压力表设备数量添加
            deviceData.get(i).setNumberPressureGauges(pressureList.size());
            deviceData.get(i).setPressureFlows(List);
            //流量计设备数量添加
            deviceData.get(i).setNumberEquipment(flowmeterList.size());
            deviceData.get(i).setIconType(2);
        }
        deviceList.setData(deviceData);
        return deviceList;
    }


    /**
     * 场站信息查询接口
     */
    @RequestBody
    @RequestMapping("/selectTSiteStationInfoData")
    public TSiteStationInfoDataList selectTSiteStationInfoData(){
        //场站信息查询方法
        List<TSiteStationInfoData> tSiteStationInfoData = iPipeInterfaceService.selectTSiteStationInfoData();
        //查询场站下级设备数据
        List<PressureFlow> pressureFlows = iPipeInterfaceService.selecttSiteStationInfoDataList("1");
        TSiteStationInfoDataList tSiteStationInfoDataList=new TSiteStationInfoDataList();
        for (int i=0;i<tSiteStationInfoData.size();i++){
            //临时存储压力表数据数组
            List<PressureFlow> pressureList=new ArrayList<>();
            //临时存储流量计数据数组
            List<PressureFlow> flowmeterList=new ArrayList<>();
            //存储全部数据
            List<PressureFlow> List=new ArrayList<>();
            for (int s=0;s<pressureFlows.size();s++){
                 if (tSiteStationInfoData.get(i).getSiteStationId()==pressureFlows.get(s).getDeviceId()){
                     List.add(pressureFlows.get(s));
                     //判断是压力表还是流量计   1压力表  2流量计
                     if (pressureFlows.get(s).getDeviceType()=="1"){
                         pressureList.add(pressureFlows.get(s));
                     }else {
                         flowmeterList.add(pressureFlows.get(s));
                     }
                 }
            }
            //压力表设备数量添加
            tSiteStationInfoData.get(i).setNumberPressureGauges(pressureList.size());
            tSiteStationInfoData.get(i).setPressureFlows(List);
            //流量计设备数量添加
            tSiteStationInfoData.get(i).setNumberEquipment(flowmeterList.size());
            tSiteStationInfoData.get(i).setIconType(4);
        }
        tSiteStationInfoDataList.setData(tSiteStationInfoData);
        return tSiteStationInfoDataList;
    }

    /**
     * 监控信息查询
     */
    @RequestBody
    @RequestMapping("/selectTvideoManagerData")
    public List<TvideoManagerData> selectTvideoManagerData(){
        //查询监控信息
        List<TvideoManagerData> tvideoManagerData = iPipeInterfaceService.selectTvideoManagerData();
        for (int i=0;i<tvideoManagerData.size();i++){
            tvideoManagerData.get(i).setIconType(5);
            if("999".equals(tvideoManagerData.get(i).getCompanyType())){
                tvideoManagerData.get(i).setCompanyType(null);
            }else if("0".equals(tvideoManagerData.get(i).getCompanyType())){
                tvideoManagerData.get(i).setCompanyType(null);
            }
        }
        return tvideoManagerData;
    }

    /**
     * 燃气用户信息查询接口
     */
    @RequestBody
    @RequestMapping("/selectTdetectorUser")
    public TenterpriseInfoDataList selectTdetectorUser(){
        TenterpriseInfoDataList tenterpriseInfoDataList=new TenterpriseInfoDataList();
       //燃气用户信息查询方法
        List<TdetectorUserData> tdetectorUserData = iPipeInterfaceService.selectTdetectorUser();
        for (int i=0;i<tdetectorUserData.size();i++){
            tdetectorUserData.get(i).setIconType(6);
        }
        tenterpriseInfoDataList.setData(tdetectorUserData);
        return tenterpriseInfoDataList;
    }

    /**
     * 企业名称id查询接口
     */
    @RequestBody
    @RequestMapping("/selectTenterpriseInfoData")
    public List<TenterpriseInfoData> selectTenterpriseInfoData(){
        //查询企业名称id方法
        List<TenterpriseInfoData> tenterpriseInfoData = iPipeInterfaceService.selectTenterpriseInfoData();
        return tenterpriseInfoData;
    }

    /**
     * 阀门数据查询接口
     */
    @RequestBody
    @RequestMapping("/selectvalveData")
    public DeviceDataList selectvalveData(){
        //查询阀门井数据
        List<DeviceData> pipeDates = iPipeInterfaceService.selectvalveData();
        //查询下级数据
        List<PressureFlow> pressureFlows1 = iPipeInterfaceService.selectPressureFlow("1");
        //最后返回的封装类型
        DeviceDataList deviceDataList=new DeviceDataList();
        //调压箱数据循环
        for (int i=0;i<pipeDates.size();i++){
            //临时存储压力表数据数组
            List<PressureFlow> pressureList=new ArrayList<>();
            //临时存储流量计数据数组
            List<PressureFlow> flowmeterList=new ArrayList<>();
            //存储全部数据
            List<PressureFlow> List=new ArrayList<>();
            for (int s=0;s<pressureFlows1.size();s++){
                //判断调压箱id与下级数据调压箱id是否相等
                if (pressureFlows1.get(s).getDeviceId()==pipeDates.get(i).getDeviceId()){
                    List.add(pressureFlows1.get(s));
                    //判断是压力表还是流量计   1压力表  2流量计
                    if (pressureFlows1.get(s).getDeviceType()=="1"){
                        pressureList.add(pressureFlows1.get(s));
                    }else {
                        flowmeterList.add(pressureFlows1.get(s));
                    }
                }
            }
            //压力表设备数量添加
            pipeDates.get(i).setNumberPressureGauges(pressureList.size());
            pipeDates.get(i).setPressureFlows(List);
            //流量计设备数量添加
            pipeDates.get(i).setNumberEquipment(flowmeterList.size());
            pipeDates.get(i).setIconType(3);
        }
            deviceDataList.setData(pipeDates);
        return deviceDataList;
    }

}
