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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.EmergencyCar;
import com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyEquipment;
import com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyMaterial;
import com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyTeam;
import com.elephant.framework.galaxy.aroad.module.emergency.service.EmergencyCarService;
import com.elephant.framework.galaxy.aroad.module.emergency.service.EmergencyEquipmentService;
import com.elephant.framework.galaxy.aroad.module.emergency.service.EmergencyMaterialService;
import com.elephant.framework.galaxy.aroad.module.emergency.service.EmergencyTeamEquipmentService;
import com.elephant.framework.galaxy.aroad.response.R;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/** @Author: XingYuyang @Description: 判断新增信息分类（物资、装备、车辆） @Date: Created in 2019/4/16 */
@RestController
@AllArgsConstructor
@RequestMapping("/emergency_add")
public class EmergencyAddCheckController extends BaseController {
  private final EmergencyCarService emergencyCarService;
  private final EmergencyEquipmentService emergencyEquipmentService;
  private final EmergencyMaterialService emergencyMaterialService;
  private final EmergencyTeamEquipmentService emergencyTeamEquipmentService;

  @SysLog("新增")
  @PostMapping
  public boolean check(@RequestBody JSONObject jsons) {
    boolean flag = false;
    JSONObject json = JSONObject.parseObject(jsons.toString());

    List<EmergencyCar> carList =
        JSON.parseArray(json.getString("EmergencyCar"), EmergencyCar.class);
    List<EmergencyEquipment> equipList =
        JSON.parseArray(json.getString("EmergencyEquipment"), EmergencyEquipment.class);
    List<EmergencyMaterial> materialList =
        JSON.parseArray(json.getString("EmergencyMaterial"), EmergencyMaterial.class);

    try {
      for (int i = 0; i < carList.size(); i++) {
        if (!carList.get(i).getCarName().equals(null) && !carList.get(i).getCarName().equals("")) {
          new R<>(emergencyCarService.save(carList.get(i)));
        }
      }

      for (int i = 0; i < equipList.size(); i++) {
        if (!equipList.get(i).getEquipmentName().equals(null)
            && !equipList.get(i).getEquipmentName().equals("")) {
          new R<>(emergencyEquipmentService.save(equipList.get(i)));
        }
      }

      for (int i = 0; i < materialList.size(); i++) {
        if (!materialList.get(i).getMaterialName().equals(null)
            && !materialList.get(i).getMaterialName().equals("")) {
          new R<>(emergencyMaterialService.save(materialList.get(i)));
        }
      }
      flag = true;
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      String teamName = json.getString("teamName");
      String teamId = json.getString("teamId");
      String address = json.getString("address");
      String longitude = json.getString("longitude");
      String latitude = json.getString("latitude");

      EmergencyTeam emergencyTeam = new EmergencyTeam();

      emergencyTeam.setTeamName(teamName);
      emergencyTeam.setTeamId(teamId);
      emergencyTeam.setEquipmentAdress(address);
      emergencyTeam.setLongitude(new BigDecimal(longitude));
      emergencyTeam.setLatitude(new BigDecimal(latitude));
      new R<>(emergencyTeamEquipmentService.updateWithId(emergencyTeam));

      flag = true;
    } catch (Exception e) {
      e.printStackTrace();
      flag = false;
    }
    return flag;
  }
}
