package com.elephant.framework.galaxy.aroad.module.emergency.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elephant.framework.galaxy.aroad.module.common.annotation.SysLog;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyTeam;
import com.elephant.framework.galaxy.aroad.module.emergency.service.EmergencyTeamService;
import com.elephant.framework.galaxy.aroad.response.R;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 应急队伍控制器类
 *
 * @author songwenjie
 * @date 2019-03-21 09:50:15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/emergencyteam")
public class EmergencyTeamController extends BaseController {

  private final EmergencyTeamService emergencyTeamService;

  /**
   * 分页查询
   *
   * @param page 分页对象
   * @param emergencyTeam
   * @return
   */
  @GetMapping("/page")
  public R getEmergencyTeamPage(Page page, EmergencyTeam emergencyTeam) {
    return new R<>(emergencyTeamService.getEmergencyTeamPage(page, emergencyTeam));
  }

  /**
   * @Description: 获取队伍坐标信息
   *
   * @auther: XingYuyang
   * @date: 2019/4/4 10:20
   * @param: []
   * @return: com.elephant.framework.galaxy.aroad.response.R
   */
  @GetMapping("/coordinates")
  public R getCoordinates() {
    return new R<>(emergencyTeamService.getCoordinates());
  }

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

  /**
   * 通过id查询
   *
   * @param rowId id
   * @return R
   */
  @GetMapping("/{rowId}")
  public R getById(@PathVariable("rowId") Integer rowId) {
    return new R<>(emergencyTeamService.getById(rowId));
  }

  /**
   * @Description: 通过多ID查询应急队伍基础信息
   * @Param: ids
   * @return: com.elephant.framework.galaxy.aroad.response.R
   * @Author: XingYuyang
   * @Date: 2019-04-04
   *     16:41:26
   */
  @RequestMapping(
      value = "/teaminfobyids/{ids}",
      method = {RequestMethod.GET})
  @ResponseBody
  public R getTeamInfoByIds(@PathVariable("ids") String ids) {
    List<String> idsStringList = Arrays.asList(ids.split(","));
    List<Integer> idsList = new ArrayList<>();
    CollectionUtils.collect(
        idsStringList,
        new Transformer() {
          public Object transform(Object o) {
            return Integer.valueOf(o.toString());
          }
        },
        idsList);
    return new R<>(emergencyTeamService.listByIds(idsList));
  }

  /**
   * @Description: 根据坐标查询事件点目标距离内的应急队伍及其物资信息
   * @Param: [latitude, longitude, goalDistance] 纬度、经度、目标距离
   * @return: com.elephant.framework.galaxy.aroad.response.R
   * @Author: songwenjie
   * @Date: 2019/4/2
   */
  @GetMapping("/goaldistanceteam")
  public R getGoalTeams(double latitude, double longitude, double goalDistance) {
    return new R<>(emergencyTeamService.getGoalTeamVo(latitude, longitude, goalDistance));
  }

  /**
   * 新增
   *
   * @param emergencyTeam
   * @return R
   */
  @SysLog("新增")
  @PostMapping
  public R save(@RequestBody EmergencyTeam emergencyTeam) {
    String teamId = emergencyTeam.getTeamId();
    if (teamId.equals(null) || teamId.equals("")) {
      emergencyTeam.setTeamId(UUID.randomUUID().toString());
      return new R<>(emergencyTeamService.save(emergencyTeam));
    }else{
      return updateById(emergencyTeam);
    }
  }

  /**
   * 修改
   *
   * @param emergencyTeam
   * @return R
   */
  @SysLog("修改")
  @PutMapping
  public R updateById(@RequestBody EmergencyTeam emergencyTeam) {
    return new R<>(emergencyTeamService.updateWithId(emergencyTeam));
  }

  /**
   * 通过id删除
   *
   * @param teamId teamId
   * @return R
   */
  @SysLog("删除")
  @DeleteMapping("/{teamId}")
  public R removeById(@PathVariable String teamId) {
    return new R<>(emergencyTeamService.removeById(teamId));
  }
}
