package com.railway.web.controller.business.safetool;

import com.railway.business.safetool.domain.BaseSafetyTool;
import com.railway.business.safetool.enums.ToolStateEnum;
import com.railway.business.safetool.service.IBaseSafetyToolService;
import com.railway.common.annotation.Log;
import com.railway.common.core.controller.BaseController;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.core.page.TableDataInfo;
import com.railway.common.enums.BusinessType;
import com.railway.common.utils.poi.ExcelUtil;
import com.railway.system.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wuhonghao
 */
@Api(value = "rest/safetool/base/safety/tool", tags = "安全工具 - 基础数据")
@RestController
@Validated
@RequestMapping(value = "business/safetool/base/safety/tool")
public class BaseSafetyToolController extends BaseController {

  private final IBaseSafetyToolService baseSafetyToolService;
  private final ISysFileService sysFileService;

  public BaseSafetyToolController(IBaseSafetyToolService baseSafetyToolService,
      ISysFileService sysFileService) {
    this.baseSafetyToolService = baseSafetyToolService;
    this.sysFileService = sysFileService;
  }

  @ApiOperation(value = "新增")
  @PostMapping("/add")
  public AjaxResult add(@Validated @RequestBody BaseSafetyTool baseSafetyTool) {
    //校验安全工具有效编码唯一
    BaseSafetyTool validate = new BaseSafetyTool();
    validate.setToolCode(baseSafetyTool.getToolCode());
    validate.setState(ToolStateEnum.NORMAL.getCode());
    List<BaseSafetyTool> list = baseSafetyToolService.getList(validate);
    if (list.size() == 1) {
      return new AjaxResult(AjaxResult.Type.WARN, "工具编号已存在");
    } else {
      return toAjax(baseSafetyToolService.create(baseSafetyTool));
    }
  }

  @ApiOperation(value = "删除")
  @DeleteMapping("/{toolIds}")
  public AjaxResult delete(@PathVariable Long[] toolIds) {
    return toAjax(baseSafetyToolService.delete(toolIds));
  }

  @ApiOperation(value = "更新")
  @PutMapping("/update")
  public AjaxResult update(@RequestBody @Valid BaseSafetyTool baseSafetyTool) {
    return toAjax(baseSafetyToolService.update(baseSafetyTool));
  }

  @ApiOperation(value = "单个")
  @GetMapping(value = {"/", "/{toolId}"})
  public AjaxResult getInfo(Long toolId) {
    BaseSafetyTool info = baseSafetyToolService.getInfo(toolId);
    AjaxResult ajax = AjaxResult.success();
    ajax.put("info", info);
    return ajax;
  }

  @ApiOperation(value = "列表")
  @GetMapping(value = "list")
  public TableDataInfo getList(BaseSafetyTool baseSafetyTool) {
    startPage();
    List<BaseSafetyTool> list = baseSafetyToolService.getList(baseSafetyTool);
    return getDataTable(list);
  }

  @ApiOperation(value = "替换安全工具")
  @PostMapping("/replaceTool")
  public AjaxResult replaceTool(@Validated @RequestBody BaseSafetyTool baseSafetyTool) {
    //校验安全工具有效编码唯一
    BaseSafetyTool validate = new BaseSafetyTool();
    validate.setToolCode(baseSafetyTool.getToolCode());
    validate.setState(ToolStateEnum.NORMAL.getCode());
    List<BaseSafetyTool> list = baseSafetyToolService.getList(validate);
    if (list.size() == 1 && list.get(0).getToolId().equals(baseSafetyTool.getToolId())) {
      return toAjax(baseSafetyToolService.replaceTool(baseSafetyTool));
    } else {
      return new AjaxResult(AjaxResult.Type.WARN, "工具编号已存在");
    }
  }

  @ApiOperation(value = "导出安全工具台账")
  @Log(title = "安全工具台账", businessType = BusinessType.EXPORT)
  @PreAuthorize("@ss.hasPermi('system:dict:export')")
  @GetMapping("/export")
  public AjaxResult export(BaseSafetyTool safetyTool) {
    List<BaseSafetyTool> list = baseSafetyToolService.getList(safetyTool);
    ExcelUtil<BaseSafetyTool> util = new ExcelUtil<>(BaseSafetyTool.class);
    String localFilePath = util.exportExcel(list, "安全工具台账");
    return sysFileService.uploadFile(localFilePath);
  }

}
