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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elephant.framework.galaxy.aroad.module.emergencyplan.dto.EmergencyPlanDto;
import com.elephant.framework.galaxy.aroad.response.R;
import com.elephant.framework.galaxy.aroad.module.common.annotation.SysLog;
import com.elephant.framework.galaxy.aroad.module.emergencyplan.entity.EmergencyPlan;
import com.elephant.framework.galaxy.aroad.module.emergencyplan.service.EmergencyPlanService;
import com.elephant.framework.galaxy.aroad.response.UpFileResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * 应急预案表
 *
 * @author songwenjie
 * @date 2019-04-25 09:47:42
 */
@RestController
@AllArgsConstructor
@RequestMapping("/emergencyplan")
public class EmergencyPlanController extends BaseController  {

  private final  EmergencyPlanService emergencyPlanService;

  /**
   * 分页查询
   * @param page 分页对象
   * @param emergencyPlan 应急预案表
   * @return
   */
  @GetMapping("/page")
  public R getEmergencyPlanPage(Page page, EmergencyPlan emergencyPlan) {
    return  new R<>(emergencyPlanService.getPlanPage(page,emergencyPlan));
  }


  /**
   * 通过id查询应急预案表
   * @param planId id
   * @return R
   */
  @GetMapping("/{planId}")
  public R getById(@PathVariable("planId") Integer planId){
    return new R<>(emergencyPlanService.getById(planId));
  }

  /**
   * 新增应急预案表
   * @param emergencyPlan 应急预案表
   * @return R
   */
  @SysLog("新增应急预案表")
  @PostMapping
  public R save(@RequestBody EmergencyPlanDto emergencyPlan){
    return new R<>(emergencyPlanService.save(emergencyPlan));
  }

  /**
   * 修改应急预案表
   * @param emergencyPlan 应急预案表
   * @return R
   */
  @SysLog("修改应急预案表")
  @PutMapping
  public R updateById(@RequestBody EmergencyPlanDto emergencyPlan){
    return new R<>(emergencyPlanService.updateById(emergencyPlan));
  }

  /**
   * 通过id删除应急预案表
   * @param planId id
   * @return R
   */
  @SysLog("删除应急预案表")
  @DeleteMapping("/{planId}")
  public R removeById(@PathVariable Integer planId){
    return new R<>(emergencyPlanService.removeById(planId));
  }



  @PostMapping("/upload")
  @ResponseBody
  public UpFileResponse upload(@RequestParam("file") MultipartFile file) {
    if (file.isEmpty()) {
      return null;
    }
    // 获取文件名
    String fileName = file.getOriginalFilename();
    // 获取文件的后缀名
//    String suffixName = fileName.substring(fileName.lastIndexOf("."));
    // 文件上传路径
    String filePath = "./src/main/resources/images/";
    // 解决中文问题,liunx 下中文路径,图片显示问题
    //fileName = UUID.randomUUID() + suffixName;
    File dest = new File(filePath + fileName);
    // 检测是否存在目录
    if (!dest.getParentFile().exists()) {
      dest.getParentFile().mkdirs();
    }
    try {
//      file.transferTo(dest);
      FileOutputStream out = new FileOutputStream(filePath+fileName);
      out.write(file.getBytes());
      out.flush();
      out.close();
      return UpFileResponse.builder()
              .name(fileName)
              .url("/images/"+fileName)
              .build();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null; }

}
