package com.inspect.metadata.patrolpointmnt.controller;

import com.inspect.metadata.alg.domain.BasedataAlgSubtype;
import com.inspect.metadata.alg.service.IBasedataAlgSubtypeService;
import com.inspect.metadata.area.domain.BaseDataArea;
import com.inspect.metadata.area.service.IBaseDataAreaService;
import com.inspect.metadata.eqpbook.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookService;
import com.inspect.metadata.patrolpointmnt.domain.BasedataPatrolPoint;
import com.inspect.metadata.patrolpointmnt.domain.BasedataPatrolPointPreset;
import com.inspect.metadata.patrolpointmnt.domain.BasedataStdPatrolPoint;
import com.inspect.metadata.patrolpointmnt.domain.vo.PointModel;
import com.inspect.metadata.patrolpointmnt.service.IBasedataPatrolPointPresetService;
import com.inspect.metadata.patrolpointmnt.service.IBasedataPatrolPointService;
import com.inspect.metadata.patrolpointmnt.service.IBasedataStdPatrolPointService;
import com.inspect.metadata.prodevmnt.domain.BasedataDevice;
import com.inspect.metadata.prodevmnt.domain.BasedataDeviceType;
import com.inspect.metadata.prodevmnt.service.IBasedataDeviceService;
import com.inspect.metadata.prodevmnt.service.IBasedataDeviceTypeService;
import com.inspect.metadata.station.domain.BasedataStation;
import com.inspect.metadata.station.service.IBasedataStationService;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.common.security.annotation.RequiresPermissions;

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

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/queryPointModel"})
public class BasedataQueryPointModelController extends BaseController {
    private final IBasedataStdPatrolPointService basedataStdPatrolPointService;
    private final IBasedataDeviceTypeService basedataDeviceTypeService;
    private final IBasedataAlgSubtypeService basedataAlgSubtypeService;
    private final IBasedataStationService basedataStationService;
    private final IBaseDataAreaService baseDataAreaService;
    private final IBasedataPatrolPointService basedataPatrolPointService;
    private final IBasedataDeviceService basedataDeviceService;
    private final IBasedataPatrolPointPresetService basedataPatrolPointPresetService;
    private final IBasedataEqpBookService basedataEqpBookService;

    public BasedataQueryPointModelController(IBasedataStdPatrolPointService basedataStdPatrolPointService, IBasedataDeviceTypeService basedataDeviceTypeService, IBasedataAlgSubtypeService basedataAlgSubtypeService, IBasedataStationService basedataStationService, IBaseDataAreaService baseDataAreaService, IBasedataPatrolPointService basedataPatrolPointService, IBasedataDeviceService basedataDeviceService, IBasedataPatrolPointPresetService basedataPatrolPointPresetService, IBasedataEqpBookService basedataEqpBookService) {
        this.basedataStdPatrolPointService = basedataStdPatrolPointService;
        this.basedataDeviceTypeService = basedataDeviceTypeService;
        this.basedataAlgSubtypeService = basedataAlgSubtypeService;
        this.basedataStationService = basedataStationService;
        this.baseDataAreaService = baseDataAreaService;
        this.basedataPatrolPointService = basedataPatrolPointService;
        this.basedataDeviceService = basedataDeviceService;
        this.basedataPatrolPointPresetService = basedataPatrolPointPresetService;
        this.basedataEqpBookService = basedataEqpBookService;
    }

    @RequiresPermissions({"basedata/patrolpointmnt:stdpatrolpoint:list"})
    @GetMapping({"/list"})
    public AjaxResult list() {
        List<PointModel> pointModelList = new ArrayList<>();
        List<BasedataPatrolPoint> patrolPointList = this.basedataPatrolPointService.selectBasedataPatrolPointList(new BasedataPatrolPoint());
        Iterator<BasedataPatrolPoint> iterator = patrolPointList.iterator();

        while (true) {
            BasedataPatrolPoint basedataPatrolpoint;
            BasedataDeviceType devicetype;
            BasedataDevice device;
            BasedataDevice component;
            BasedataStdPatrolPoint stdpatrolPoint;
            BasedataAlgSubtype algSubtype;
            BasedataEqpBook eqpbook;
            PointModel pointModel;
            while (true) {
                BaseDataArea area;
                BasedataStation station;
                do {
                    BasedataPatrolPointPreset patrolpointPreset;
                    do {
                        do {
                            do {
                                do {
                                    do {
                                        do {
                                            do {
                                                do {
                                                    if (!iterator.hasNext()) {
                                                        return AjaxResult.success(pointModelList);
                                                    }

                                                    basedataPatrolpoint = iterator.next();
                                                    area = this.baseDataAreaService.selectAreaById(basedataPatrolpoint.getAreaId());
                                                } while (area == null);

                                                devicetype = this.basedataDeviceTypeService.selectBasedataDeviceTypeByDeviceTypeId(basedataPatrolpoint.getDeviceTypeId());
                                            } while (devicetype == null);

                                            device = this.basedataDeviceService.selectBasedataDeviceByDeviceId(basedataPatrolpoint.getMainDeviceId());
                                        } while (device == null);

                                        component = this.basedataDeviceService.selectBasedataDeviceByDeviceId(basedataPatrolpoint.getDeviceId());
                                    } while (component == null);

                                    station = this.basedataStationService.getStationTypeByPatrolPointId(basedataPatrolpoint.getPatrolPointId() + "");
                                } while (station == null);

                                stdpatrolPoint = this.basedataStdPatrolPointService.selectBasedataStdPatrolPointByStdPatrolPointId(basedataPatrolpoint.getStdParolPointId());
                            } while (stdpatrolPoint == null);

                            algSubtype = this.basedataAlgSubtypeService.selectBasedataAlgSubtypeByAlgSubtypeId(stdpatrolPoint.getAlgSubtypeId());
                        } while (algSubtype == null);

                        patrolpointPreset = this.basedataPatrolPointPresetService.selectBasedataPatrolPointPresetByPatrolPointId(basedataPatrolpoint.getPatrolPointId());
                    } while (patrolpointPreset == null);

                    eqpbook = this.basedataEqpBookService.selectBasedataEqpBookByEqpBookId(patrolpointPreset.getEqpBookId());
                } while (eqpbook == null);

                pointModel = new PointModel();
                pointModel.setStation_name(station.getStationName());
                pointModel.setStation_id(station.getStationId());
                if ("bayunit".equals(area.getAreaType())) {
                    pointModel.setBay_id(String.valueOf(basedataPatrolpoint.getAreaId()));
                    pointModel.setBay_name(area.getAreaName());
                    BaseDataArea areaPa = this.baseDataAreaService.selectAreaById(area.getParentId());
                    if (areaPa == null) {
                        continue;
                    }

                    pointModel.setArea_id(String.valueOf(areaPa.getAreaId()));
                    pointModel.setArea_name(areaPa.getAreaName());
                    break;
                }

                pointModel.setArea_id(String.valueOf(basedataPatrolpoint.getAreaId()));
                pointModel.setArea_name(area.getAreaName());
                break;
            }

            pointModel.setMain_device_id(basedataPatrolpoint.getMainDeviceId() + "");
            pointModel.setMain_device_name(device.getDeviceName());
            pointModel.setComponent_id(basedataPatrolpoint.getDeviceId() + "");
            pointModel.setComponent_name(component.getDeviceName());
            pointModel.setDevice_id(basedataPatrolpoint.getPatrolPointId() + "");
            pointModel.setDevice_name(basedataPatrolpoint.getPatrolPointName());
            pointModel.setDevice_type(devicetype.getDeviceTypeName());
            pointModel.setMeter_type("");
            pointModel.setAppearance_type("");
            pointModel.setSave_type_list(stdpatrolPoint.getDataFormat());
            pointModel.setRecognition_type_list(algSubtype.getAlgSubtypeName());
            pointModel.setPhase(device.getPhase());
            pointModel.setDevice_info(basedataPatrolpoint.getPatrolContent());
            pointModel.setData_type(eqpbook.getPatrolDeviceName());
            pointModel.setLower_value(stdpatrolPoint.getLowerValue());
            pointModel.setUpper_value(stdpatrolPoint.getUpperValue());
            String device_code = "";
            String device_pos = "";
            String robot_code = "";
            String robot_pos = "";
            String uav_code = "";
            String uav_pos = "";
            if ("1,2,3".contains(eqpbook.getType())) {
                robot_code = eqpbook.getPatrolDeviceCode();
                robot_pos = basedataPatrolpoint.getStdParolPointId() + "";
            }

            if ("13".equals(eqpbook.getType())) {
                uav_code = eqpbook.getPatrolDeviceCode();
                uav_pos = basedataPatrolpoint.getStdParolPointId() + "";
            }

            if ("10".equals(eqpbook.getType())) {
                device_code = eqpbook.getPatrolDeviceCode();
                device_pos = basedataPatrolpoint.getStdParolPointId() + "";
            }

            String video_pos = "[{\"device_code\":\"" + device_code + "\",\"device_pos\":\"" + device_pos + "\",\"robot_code\":\"" + robot_code + "\",\"robot_pos\":\"" + robot_pos + "\",\"uav_code\":\"" + uav_code + "\",\"uav_pos\":\"" + uav_pos + "\"}]";
            pointModel.setVideo_pos(video_pos);
            pointModelList.add(pointModel);
        }
    }
}
