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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.MaterialHouse;
import com.elephant.framework.galaxy.aroad.module.emergency.service.MaterialHouseService;
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;

/**
 * @author songwenjie
 * @date 2019-03-21 14:48:31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/materialhouse")
public class MaterialHouseController extends BaseController {

  private final MaterialHouseService materialHouseService;

  /**
   * 分页查询
   *
   * @param page 分页对象
   * @param materialHouse
   * @return
   */
  @GetMapping("/page")
  public R getMaterialHousePage(Page page, MaterialHouse materialHouse) {
    return new R<>(materialHouseService.page(page, Wrappers.query(materialHouse)));
  }

  /**
   * @Description: 通过多ID查询物资库信息
   * @Param: ids
   *
   * @return: com.elephant.framework.galaxy.aroad.response.R
   * @Author: XingYuyang
   * @Date: 2019-04-04
   *     16:35:13
   */
  @RequestMapping(
      value = "/houseinfobyids/{ids}",
      method = {RequestMethod.GET})
  @ResponseBody
  public R getHouseInfoByIds(@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<>(materialHouseService.listByIds(idsList));
  }

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

  /**
   * @Description: 获取队伍列表
   * @Param:
   *
   * @return: @Author: XingYuyang
   * @Date: 2019-04-15 10:08:18
   */
  @GetMapping("/getteams")
  public R getTeams() {
    return new R<>(materialHouseService.getTeams());
  }

  /**
   * 新增
   *
   * @param materialHouse
   * @return R
   */
  @SysLog("新增")
  @PostMapping
  public R save(@RequestBody MaterialHouse materialHouse) {
    Integer rowId = materialHouse.getRowId();
    if (rowId==null) {
      return new R<>(materialHouseService.save(materialHouse));
    } else {
      return updateById(materialHouse);
    }
    // return new R<>(materialHouseService.save(materialHouse));
  }

  /**
   * @Description: 获取物资库的坐标信息
   * @Author: XingYuyang
   * @Date: 2019-04-04 10:39:49
   *
   * @param: []
   * @return: com.elephant.framework.galaxy.aroad.response.R
   */
  @GetMapping("/coordinates")
  public R getCoordinates() {
    return new R<>(materialHouseService.getCoordinates());
  }

  /**
   * 修改
   *
   * @param materialHouse
   * @return R
   */
  @SysLog("修改")
  @PutMapping
  public R updateById(@RequestBody MaterialHouse materialHouse) {
    return new R<>(materialHouseService.updateById(materialHouse));
  }

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