package com.jiayue.ipfcst.dataexchange.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import com.jiayue.ipfcst.common.data.entity.EquipmentAttribute;
import com.jiayue.ipfcst.console.service.ElectricFieldService;
import com.jiayue.ipfcst.dataexchange.repository.EquipmentAttributeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 装置的状态描述表的操作service类
 *
 * @author 修唯xiuwei
 * @version 3.0
 */
@Service
public class EquipmentAttributeService {

	/**
	 * 操作仓库
	 */
	@Autowired
	EquipmentAttributeRepository equipmentAttributeRepository;
	@Autowired
	ElectricFieldService electricFieldService;

	/**
	 * 获取某类设备的状态数据描述属性
	 * 查询设备属性by设备类型
	 *
	 * @param dataObjectTypeEnum 设备类型
	 * @return EquipmentAttribute 设备属性
	 */
	public List<EquipmentAttribute> getByEquipmentType(EquipmentTypeEnum dataObjectTypeEnum) {
		EquipmentAttribute equipmentAttribute = new EquipmentAttribute();
		equipmentAttribute.setEquipmentType(dataObjectTypeEnum);
		return this.equipmentAttributeRepository.findAll(Example.of(equipmentAttribute));
	}


	/**
	 * 查找符合条件的设备属性
	 *
	 * @param equipmentAttribute 设备属性
	 * @return EquipmentAttribute 设备属性列表
	 */
	public List<EquipmentAttribute> get(EquipmentAttribute equipmentAttribute) {
		return this.equipmentAttributeRepository.findAll(Example.of(equipmentAttribute));
	}

	/**
	 * 查所有的设备属性
	 *
	 * @return EquipmentAttribute 设备属性列表
	 */
	public List<EquipmentAttribute> getAll() {
		return this.equipmentAttributeRepository.findAll();
	}

	/**
	 *  分页查询
	 */
	public Page getAllByPage(Integer page, Integer size) {
		Pageable pageable = PageRequest.of(page - 1, size);
		Page equipmentAttributePage = equipmentAttributeRepository.findAll(pageable);
		return equipmentAttributePage;
	}

	/**
	 * 获取某类设备的状态数据描述属性
	 * 查询设备属性by设备类型
	 * 分页
	 *
	 * @param equipmentType 设备类型
	 * @return EquipmentAttribute 设备属性
	 */
	public Page<EquipmentAttribute> findByEquipmentType(String equipmentType, Integer page, Integer size) {
		Pageable pageable = PageRequest.of(page - 1, size);
		EquipmentTypeEnum equipmentTypeEnum = EquipmentTypeEnum.valueOf(equipmentType);
		Page<EquipmentAttribute> equipmentAttributePage = equipmentAttributeRepository.findAllByEquipmentType(equipmentTypeEnum, pageable);
		return equipmentAttributePage;
	}

	/**
	 * 保存
	 */
	public void saveOrUpdateEquipmentAttribute(EquipmentAttribute equipmentAttribute) {
		equipmentAttribute.setStationCode(electricFieldService.get().getStationCode());
		equipmentAttributeRepository.save(equipmentAttribute);
	}

	/**
	 * 删除
	 * */
	public void delete(Integer id) throws BusinessException {
		if (StringUtils.isEmpty(id)) {
			throw new BusinessException("设备属性id不能为空!");
		} else {
			equipmentAttributeRepository.deleteById(id);
		}
	}

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void saveCloud(EquipmentAttribute bean) {
    equipmentAttributeRepository.save(bean);
  }

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveCloud(List<EquipmentAttribute> beans) {
		equipmentAttributeRepository.deleteAll();
		equipmentAttributeRepository.saveAll(beans);
	}
}
