package com.jiayue.ipfcst.dataexchange.controller;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.abst.equipmentinfo.AbstractEquipmentInfo;
import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.common.data.entity.EquipmentAttribute;
import com.jiayue.ipfcst.common.data.entity.ProtocolGatherDataPoint;
import com.jiayue.ipfcst.console.service.ElectricFieldService;
import com.jiayue.ipfcst.dataexchange.service.EquipmentAttributeService;
import com.jiayue.ipfcst.dataexchange.service.ProtocolGatherDataPointService;
import com.jiayue.ipfcst.dataexchange.service.equipmentstatusdata.EquipmentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备属性类的对外http接口
 *
 * @author 修唯xiuwei
 * @version 3.0
 */
@Slf4j
@RequestMapping("dataExchange")
@RestController
public class EquipmentAttributeController {

  /**
   * 设备属性服务
   */
  @Autowired
  EquipmentAttributeService equipmentAttributeService;
  @Autowired ProtocolGatherDataPointService protocolGatherDataPointService;
  /**
   * 设备信息服务
   */
  @Autowired
  EquipmentInfoService equipmentInfoService;
  @Autowired
  ElectricFieldService electricFieldService;

  /**
   * 根据条件查询设备属性
   *
   * @param equipmentAttribute 设备实体
   * @return ResponseVO 统一返回值
   */
  @GetMapping(value = "equipmentAttribute")
  public ResponseVO get(@RequestBody EquipmentAttribute equipmentAttribute) {
    return ResponseVO.success(equipmentAttributeService.get(equipmentAttribute));
  }

  /**
   * 根据设备类型查设备属性
   *
   * @param equipmentType 设备类型
   * @return ResponseVO 统一返回值
   */
  @GetMapping(value = "getByEquipmentType/{equipmentType}")
  public ResponseVO getByEquipmentType(@PathVariable String equipmentType) throws BusinessException {
    if (!StringUtils.isNotBlank(equipmentType)) {
      throw new BusinessException("必须传入设备类型");
    }
    EquipmentTypeEnum dataObjectTypeEnum;
    try {
      int i = Integer.parseInt(equipmentType);
      dataObjectTypeEnum = EquipmentTypeEnum.valueOf(i);
    } catch (NumberFormatException e) {
      dataObjectTypeEnum = EquipmentTypeEnum.valueOf(equipmentType);
    }
    return ResponseVO.success(equipmentAttributeService.getByEquipmentType(dataObjectTypeEnum));
  }

  /**
   * 获取到所有的设备信息
   *
   * @return ResponseVO 统一返回值
   */
  @GetMapping(value = "equipmentInfo")
  public ResponseVO getAllEquipmentInfo() {
    List list = new ArrayList();
    List<AbstractEquipmentInfo> info = equipmentInfoService.getAllEquipmentInfo();
    for (int i = 0; i < info.size(); i++) {
      list.add(info.get(i));
    }
    ElectricField electricFields = electricFieldService.get();
    HashMap map = new HashMap();
    map.put("id", electricFields.getStationCode());
    map.put("name", electricFields.getName());
    map.put("code", 0);
    list.add(map);
    return ResponseVO.success(list);
  }

  /**
   * 得到所有
   * 所有设备属性
   *
   * @return {@link ResponseVO}
   */
  @GetMapping(value = "getAll")
  public ResponseVO getAll() {
    try {
      return ResponseVO.success(this.equipmentAttributeService.getAll());
    } catch (Exception e) {
      e.printStackTrace();
      log.error("查询异常");
      return ResponseVO.fail();
    }
  }
  /**
   * 分页
   * 所有设备属性
   *
   * @return {@link ResponseVO}
   */
  @GetMapping(value = "getAllByPage/{page}/{size}")
  public ResponseVO getAllByPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
    try {
      return ResponseVO.success(this.equipmentAttributeService.getAllByPage(page,size));
    } catch (Exception e) {
      e.printStackTrace();
      log.error("查询异常");
      return ResponseVO.fail();
    }
  }

  /**
   * 获取设备信息
   * 根据设备类型找设备
   *
   * @param equipmentType 设备类型
   * @return {@link ResponseVO}
   * @throws BusinessException 业务异常
   */
  @GetMapping(value = "equipmentInfo/{equipmentType}")
  public ResponseVO getEquipmentInfo(@PathVariable String equipmentType) throws BusinessException {
    if (!StringUtils.isNotBlank(equipmentType)) {
      throw new BusinessException("必须传入设备类型");
    }
    EquipmentTypeEnum type;
    try {
      int i = Integer.parseInt(equipmentType);
      type = EquipmentTypeEnum.valueOf(i);
    } catch (NumberFormatException e) {
      type = EquipmentTypeEnum.valueOf(equipmentType);
    }
    try {
      return ResponseVO.success(equipmentInfoService.findByEqType(type));
    } catch (Exception e) {
      e.printStackTrace();
      log.error("查询异常");
      return ResponseVO.fail();
    }
  }

  /**
   * 获取设备类型状态枚举
   * @return
   */
  @GetMapping("/getEquipmentTypeEnum")
  public ResponseVO getEquipmentTypeEnum(){
    List list = new ArrayList<>();
    for (EquipmentTypeEnum equipmentTypeEnum : EquipmentTypeEnum.values()) {
      Map map = new HashMap();
      if(!equipmentTypeEnum.getCode().equals(5)){
        map.put("value",equipmentTypeEnum.name());
        map.put("label",equipmentTypeEnum.getMessage());
        map.put("code",equipmentTypeEnum.getCode());
        list.add(map);
      }
    }
    return ResponseVO.success(list);
  }


  /**
   * 获取设备类型状态枚举
   * @return
   */
  @GetMapping("/getSubsidiaryEquipmentTypeEnum")
  public ResponseVO getSubsidiaryEquipmentTypeEnum(){
    List list = new ArrayList<>();
    for (EquipmentTypeEnum equipmentTypeEnum : EquipmentTypeEnum.SUBSIDIARY_EQUIPMENT_TYPE) {
      Map map = new HashMap();
      if(!equipmentTypeEnum.getCode().equals(5)){
        map.put("value",equipmentTypeEnum.name());
        map.put("label",equipmentTypeEnum.getMessage());
        map.put("code",equipmentTypeEnum.getCode());
        list.add(map);
      }
    }
    return ResponseVO.success(list);
  }

  /**
   * 根据设备类型查设备属性 分页
   *
   * @param equipmentType 设备类型
   * @return ResponseVO 统一返回值
   */
  @GetMapping(value = "/{equipmentType}/{page}/{size}")
  public ResponseVO findByEquipmentType(@PathVariable String equipmentType,@PathVariable Integer page,@PathVariable Integer size) {
    return ResponseVO.success(equipmentAttributeService.findByEquipmentType(equipmentType,page,size));
  }

  @GetMapping(value = "/dataPointById/{id}")
  public ResponseVO dataPointById(@PathVariable Integer id) {
    ProtocolGatherDataPoint protocolGatherDataPoint = new ProtocolGatherDataPoint();
    EquipmentAttribute equipmentAttribute = new EquipmentAttribute();
    equipmentAttribute.setId(id);
    protocolGatherDataPoint.setEquipmentAttribute(equipmentAttribute);
    List<ProtocolGatherDataPoint> list = protocolGatherDataPointService.get(protocolGatherDataPoint);
    return ResponseVO.success(list);
  }

  @PostMapping
  public ResponseVO saveEquipmentAttribute(@RequestBody EquipmentAttribute equipmentAttribute){
    equipmentAttributeService.saveOrUpdateEquipmentAttribute(equipmentAttribute);
    return ResponseVO.success(1);
  }
  @PutMapping
  public ResponseVO UpdateEquipmentAttribute(@RequestBody EquipmentAttribute equipmentAttribute){
    equipmentAttributeService.saveOrUpdateEquipmentAttribute(equipmentAttribute);
    return ResponseVO.success(1);
  }
  /*
   * 删除
   * */
  @DeleteMapping("/{id}")
  public ResponseVO delete(@PathVariable String id) throws BusinessException {
    equipmentAttributeService.delete(Integer.parseInt(id));
    return ResponseVO.success(1);
  }
}
