package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.entity.InverterInfo;
import com.jiayue.ipfcst.common.data.entity.WindTowerInfo;
import com.jiayue.ipfcst.common.data.entity.WindTurbineInfo;
import com.jiayue.ipfcst.common.data.repository.WindTurbineInfoRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
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;
import java.util.Optional;

/**
 * 风机业务层
 *
 * @author zzy
 * @version 1.0
 * @since 2019/8/7 10:58
 */
@Service
@Slf4j
public class WindTurbineInfoService extends BaseService {
    private final WindTurbineInfoRepository windTurbineInfoRepository;

    @Autowired
    public WindTurbineInfoService(WindTurbineInfoRepository windTurbineInfoRepository) {
        this.windTurbineInfoRepository = windTurbineInfoRepository;
    }


    /**
     * 新增风机
     *
     * @param windTurbine 风机
     * @throws BusinessException 业务异常
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void add(WindTurbineInfo windTurbine) throws BusinessException {
        this.windTurbineInfoRepository.save(windTurbine);
//        boolean b = this.windTurbineRepository.existsById(windTurbine.getNo());
//        if (b) {// 编号已存在
//            throw new BusinessException("风机编号已存在!");
//        } else {
//            this.windTurbineRepository.save(windTurbine);
//        }
    }

    /**
     * 修改风机
     *
     * @param windTurbine 逆变器
     * @throws BusinessException 业务异常
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void update(WindTurbineInfo windTurbine) throws BusinessException {
        if (StringUtils.isEmpty(windTurbine.getId())) {
            throw new BusinessException("风机编号不能为空!");
        } else {
            this.windTurbineInfoRepository.save(windTurbine);
        }
    }

    /**
     * 删除风机
     *
     * @param no 逆变器编号
     * @throws BusinessException 业务异常
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void delete(final Integer no) throws BusinessException {
        if (StringUtils.isEmpty(no)) {
            throw new BusinessException("风机编号不能为空!");
        } else {
            this.windTurbineInfoRepository.deleteById(no);
        }
    }

    /**
     * 查询风机
     *
     * @param no 逆变器编号
     * @return 逆变器
     * @throws BusinessException 业务异常
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public WindTurbineInfo get(final Integer no) throws BusinessException {
        Optional<WindTurbineInfo> optional = this.windTurbineInfoRepository.findById(no);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            throw new BusinessException("风机不存在!");
        }
    }

    /**
     * 查询风机【分页查询】
     *
     * @param windTurbine 查询条件
     * @param page        页码
     * @param size        每页记录数
     * @return 分页结果
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Page<WindTurbineInfo> get(final WindTurbineInfo windTurbine, final Integer page, final Integer size) {
        ExampleMatcher matcher =
                ExampleMatcher.matching().withMatcher("modelNumber", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<WindTurbineInfo> example = Example.of(windTurbine, matcher);
        Pageable pageable = PageRequest.of(page - 1, size);
        return this.windTurbineInfoRepository.findAll(example, pageable);
    }

    /**
     * 查询风机
     *
     * @return 分页结果
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<WindTurbineInfo> getAll() {
        return this.windTurbineInfoRepository.findAll();
    }


    /**
     * 查询所有样板机风机 create by xiuwei
     *
     * @return 风机信息
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<WindTurbineInfo> getAllSample() {
        WindTurbineInfo windTurbineInfo = new WindTurbineInfo();
        windTurbineInfo.setSample(true);
        return windTurbineInfoRepository.findAll(Example.of(windTurbineInfo));
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCloud(WindTurbineInfo bean) {
//        WindTurbineInfo beanOld = windTurbineInfoRepository.findByName(bean.getName());
//        if (null != beanOld) {
//            bean.setId(beanOld.getId());
//        }
        windTurbineInfoRepository.save(bean);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCloud(List<WindTurbineInfo> beans) {
        if(beans != null && beans.size() > 0){
            windTurbineInfoRepository.deleteAll();
            windTurbineInfoRepository.saveAll(beans);
        }
    }

  /**
   * 批量新增逆变器
   *
   * @param windTurbineInfo 风机
   * @throws BusinessException 业务异常
   */
  @Transactional(propagation = Propagation.SUPPORTS)
  public void addALL(WindTurbineInfo windTurbineInfo, Integer startValue, Integer endValue) throws BusinessException {
    for (int i = startValue; i < startValue + endValue; i++) {
      WindTurbineInfo windTurbineInfo1 = new WindTurbineInfo();
      windTurbineInfo1.setManufacturer(windTurbineInfo.getManufacturer());
      windTurbineInfo1.setReport(windTurbineInfo.getReport());
      windTurbineInfo1.setInstallationTime(windTurbineInfo.getInstallationTime());
      windTurbineInfo1.setName(windTurbineInfo.getName() + "" + i);
      windTurbineInfo1.setAirDensity(windTurbineInfo.getAirDensity());
      windTurbineInfo1.setCutInSpeed(windTurbineInfo.getCutInSpeed());
      windTurbineInfo1.setCutOutSpeed(windTurbineInfo.getCutOutSpeed());
      windTurbineInfo1.setGrade(windTurbineInfo.getGrade());
      windTurbineInfo1.setHubHeight(windTurbineInfo.getHubHeight());
      windTurbineInfo1.setLifeLength(windTurbineInfo.getLifeLength());
      windTurbineInfo1.setRatedWindSpeed(windTurbineInfo.getRatedWindSpeed());
      windTurbineInfo1.setSample(windTurbineInfo.getSample());
      windTurbineInfo1.setModelNumber(windTurbineInfo.getModelNumber());
      windTurbineInfo1.setLiveWindSpeed(windTurbineInfo.getLiveWindSpeed());
      windTurbineInfo1.setMaxPower(windTurbineInfo.getMaxPower());
      windTurbineInfo1.setParallelInDate(windTurbineInfo.getParallelInDate());
      windTurbineInfo1.setWindWheelDiameter(windTurbineInfo.getWindWheelDiameter());
      windTurbineInfo1.setWindWheelMaxSpeed(windTurbineInfo.getWindWheelMaxSpeed());
      windTurbineInfo1.setWindWheelRatedSpeed(windTurbineInfo.getWindWheelRatedSpeed());
      windTurbineInfo1.setWindWheelMinSpeed(windTurbineInfo.getWindWheelMinSpeed());
      windTurbineInfo1.setInterval(windTurbineInfo.getInterval());
      windTurbineInfo1.setLatitude(windTurbineInfo.getLatitude());
      windTurbineInfo1.setLongitude(windTurbineInfo.getLongitude());
      windTurbineInfo1.setCollectorCircuit(windTurbineInfo.getCollectorCircuit());
      windTurbineInfo1.setBackupA(windTurbineInfo.getBackupA());
      windTurbineInfo1.setBackupB(windTurbineInfo.getBackupB());
      windTurbineInfo1.setCorrectState(windTurbineInfo.getCorrectState());
      windTurbineInfo1.setPowerCurveFunction(windTurbineInfo.getPowerCurveFunction());
      windTurbineInfo1.setDivideIntoDifferentPeriod(windTurbineInfo.getDivideIntoDifferentPeriod());
      this.windTurbineInfoRepository.save(windTurbineInfo1);
    }


//        boolean b = this.inverterRepository.existsById(inverter.getNo());
//        if (b) {// 逆变器编号已存在
//            throw new BusinessException("逆变器编号已存在!");
//        } else {
//
//        }
  }

  /**
   * 批量删除风机信息
   */
  @Transactional(propagation = Propagation.SUPPORTS)
  public boolean delete(String ids) {
    boolean flag = false;
    if (!StringUtils.isEmpty(ids)) {
      String[] idArray = ids.split(",");
      if (idArray != null && idArray.length > 0) {
        for (String id : idArray) {
          this.windTurbineInfoRepository.deleteById(Integer.valueOf(id));
          log.info("删除风机ID为[" + id + "]成功！");

        }
        flag = true;
      } else {
        this.windTurbineInfoRepository.deleteById(Integer.valueOf(ids));
        flag = true;
        log.info("删除风机ID为[" + ids + "]成功！");
      }
    }

    return flag;

  }
}
