package com.elephant.framework.galaxy.aroad.module.emergency.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elephant.framework.galaxy.aroad.module.emergency.dto.TeamCoordinateDto;
import com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyTeam;
import com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyTeamEquipment;
import com.elephant.framework.galaxy.aroad.module.emergency.mapper.EmergencyTeamMapper;
import com.elephant.framework.galaxy.aroad.module.emergency.service.EmergencyTeamEquipmentService;
import com.elephant.framework.galaxy.aroad.module.emergency.service.EmergencyTeamService;
import com.elephant.framework.galaxy.aroad.module.emergency.vo.EmergencyMaterialAndEquipmentVo;
import com.elephant.framework.galaxy.aroad.util.LocationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author songwenjie
 * @date 2019-03-21 09:50:15
 */
@Slf4j
@Service
public class EmergencyTeamServiceImpl extends ServiceImpl<EmergencyTeamMapper, EmergencyTeam>
    implements EmergencyTeamService {

  @Autowired private EmergencyTeamService emergencyTeamService;

  @Autowired private EmergencyTeamEquipmentService emergencyTeamEquipmentService;

  @Override
  public IPage<EmergencyTeam> getEmergencyTeamPage(Page page, EmergencyTeam emergencyTeam) {
    QueryWrapper<EmergencyTeam> wrapper = Wrappers.query(emergencyTeam);

    //        if(emergencyTeam != null && emergencyTeam.getTeamName() != null ){
    //            wrapper = wrapper.like("team_name",emergencyTeam.getTeamName());
    //        }

    return page(page, wrapper);
  }

  /**
   * @Description: 根据队伍ID获取该队伍详情信息及关联的装备物资、应急物资库信息 @Param: [id] 队伍ID
   *
   * @return:
   *     com.elephant.framework.galaxy.aroad.module.emergency.vo.EmergencyMaterialAndEquipmentVo @Author:
   *     songwenjie @Date: 2019/3/21
   */
  public EmergencyMaterialAndEquipmentVo getEmergencyMaterialAndEquipmentVo(Long id) {
    return baseMapper.getEmergencyMaterialAndEquipmentVo(id);
  }

  /**
   * @Description: 获取应急队伍的坐标信息 @Param: []
   *
   * @return:
   *     java.util.List<com.elephant.framework.galaxy.aroad.module.emergency.dto.TeamCoordinateDto> @Author:
   *     songwenjie @Date: 2019/4/2
   */
  @Cacheable(value = "teamCoordinates")
  public List<TeamCoordinateDto> getCoordinates() {
    return baseMapper.getCoordinates();
  }

  /**
   * @Description: 计算并返回在目标范围内的应急队伍id @Param: [latitude, longitude, goalDistance] 纬度、经度、目标距离（千米）
   *
   * @return: java.util.List<java.lang.Integer> @Author: songwenjie @Date: 2019/4/2
   */
  public List<Integer> calculatedCoordinates(
      double latitude, double longitude, double goalDistance) {
    List<Integer> ids = new ArrayList<>();

    List<TeamCoordinateDto> teamCoordinates = emergencyTeamService.getCoordinates();
    if (teamCoordinates == null || teamCoordinates.size() <= 0) {
      return ids;
    }

    teamCoordinates.forEach(
        t -> {
          double distance =
              LocationUtil.calculateDistance(
                  t.getLatitude(), t.getLongitude(), latitude, longitude);
          if (distance <= goalDistance) { // 如果在目标范围内
            ids.add(t.getRowId());
          }
        });
    return ids;
  }

  /**
   * @Description: 查询在目标范围内的应急队伍及其物资信息 @Param: [latitude, longitude, goalDistance] 纬度，经度，目标距离
   *
   * @return:
   *     java.util.List<com.elephant.framework.galaxy.aroad.module.emergency.vo.EmergencyMaterialAndEquipmentVo> @Author:
   *     songwenjie @Date: 2019/4/2
   */
  public List<EmergencyMaterialAndEquipmentVo> getGoalTeamVo(
      double latitude, double longitude, double goalDistance) {
    List<Integer> ids = calculatedCoordinates(latitude, longitude, goalDistance);
    return baseMapper.getGoalTeamVo(ids);
  }

  /**
   * @Description: 根据队伍id删除相关信息
   * @Param:
   *
   * @return:
   * @Author: XingYuyang
   * @Date: 2019-04-15 15:53:02
   */
  public boolean removeById(String teamId) {
    return baseMapper.removeById(teamId);
  }

  /**
   * @Description: 新增队伍信息
   * @Author: XingYuyang
   * @Date: 2019-04-16 10:36:34
   * */
  @Transactional(propagation = Propagation.REQUIRED)
  public boolean save(EmergencyTeam emergencyTeam) {
    emergencyTeamEquipmentService.save(emergencyTeam);
    return baseMapper.save(emergencyTeam);
  }

  /**
   * @Description: 修改队伍信息
   * @Author: XingYuyang
   * @Date: 2019-04-16 10:36:34
   * */
  @Transactional(propagation = Propagation.REQUIRED)
  public boolean updateWithId(EmergencyTeam emergencyTeam) {
    emergencyTeamEquipmentService.updateWithId(emergencyTeam);
    return baseMapper.updateWithId(emergencyTeam);
  }
}
