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

import com.railway.business.safetool.domain.BaseSafetyTool;
import com.railway.business.safetool.service.IBaseSafetyToolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;
import com.railway.common.core.controller.BaseController;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.core.page.TableDataInfo;
import javax.validation.Valid;
import java.util.List;

import javax.validation.Valid;

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

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BaseSafetyTool baseSafetyTool) {
        //校验安全工具有效编码唯一
        BaseSafetyTool validate = new BaseSafetyTool();
        validate.setToolCode(baseSafetyTool.getToolCode());
        validate.setState("1");
        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 String[] 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(String 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("1");
        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,"工具编号已存在");
        }
    }


}
