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

import com.railway.business.catenary.domain.BusJcebXsjcjl;
import com.railway.business.catenary.service.IBusJcebXsjcjlService;
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.utils.poi.EasyExcelUtil;
import com.railway.system.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
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.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangcaifeng
 */
@Api(value = "rest/catenary/bus/jceb/xsjcjl", tags = "检测表 - 巡视检查记录")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jceb/xsjcjl")
public class BusJcebXsjcjlController extends BaseController {

    private final IBusJcebXsjcjlService busJcebXsjcjlService;
    private final ISysFileService sysFileService;

    public BusJcebXsjcjlController(IBusJcebXsjcjlService busJcebXsjcjlService, ISysFileService sysFileService) {
        this.busJcebXsjcjlService = busJcebXsjcjlService;
        this.sysFileService = sysFileService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BusJcebXsjcjl busJcebXsjcjl) {
        return toAjax(busJcebXsjcjlService.create(busJcebXsjcjl));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@RequestParam String[] ids) {
        return toAjax(busJcebXsjcjlService.delete(ids));
    }

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Valid BusJcebXsjcjl busJcebXsjcjl) {
        return toAjax(busJcebXsjcjlService.update(busJcebXsjcjl));
    }

    @ApiOperation(value = "单个")
    @GetMapping(value = {"/", "/{id}"})
    public AjaxResult getInfo(String id) {
        BusJcebXsjcjl info = busJcebXsjcjlService.getInfo(id);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("info",info);
        return ajax;
    }

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

    @ApiOperation(value = "提交")
    @PutMapping("/submit")
    public AjaxResult submit(@RequestBody BusJcebXsjcjl busJcebXsjcjl) {
        return toAjax(busJcebXsjcjlService.submit(busJcebXsjcjl));
    }

    @ApiOperation(value = "退回")
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody BusJcebXsjcjl busJcebXsjcjl) {
        return toAjax(busJcebXsjcjlService.reject(busJcebXsjcjl));
    }

    @ApiOperation(value = "确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody BusJcebXsjcjl busJcebXsjcjl) {
        return toAjax(busJcebXsjcjlService.confirm(busJcebXsjcjl));
    }

    @ApiOperation(value = "导出excel")
    @GetMapping(value = "exportExcel")
    public AjaxResult exportExcel(BusJcebXsjcjl busJcebXsjcjl) {
        List<BusJcebXsjcjl> list = busJcebXsjcjlService.getList(busJcebXsjcjl);
        Map<String, Object> params = new HashMap<>();
        if(!list.isEmpty()) {
            params.put("stationName", list.get(0).getStationName());
        }
        String fileName = "巡检记录.xls";
        String templateFileName = "excel/template/巡检记录.xls";
        String localFilePath = new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
        return sysFileService.uploadFile(localFilePath);
    }
}
