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

import com.railway.business.catenary.domain.BusJcbDmcgyq;
import com.railway.business.catenary.service.IBusJcbDmcgyqService;
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.StringUtils;
import com.railway.common.utils.poi.EasyExcelUtil;
import com.railway.common.utils.poi.WordUtils;
import com.railway.system.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcaifeng
 */
@Api(value = "rest/bus/jcb/dmcgyq", tags = "检查表 - 地面磁感应器")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jcb/dmcgyq")
public class BusJcbDmcgyqController extends BaseController {

    private final IBusJcbDmcgyqService busJcbDmcgyqService;
    private final ISysFileService sysFileService;

    public BusJcbDmcgyqController(IBusJcbDmcgyqService busJcbDmcgyqService,
        ISysFileService sysFileService) {
        this.busJcbDmcgyqService = busJcbDmcgyqService;
        this.sysFileService = sysFileService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BusJcbDmcgyq busJcbDmcgyq) {
        return toAjax(busJcbDmcgyqService.create(busJcbDmcgyq));
    }

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

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Valid BusJcbDmcgyq busJcbDmcgyq) {
        return toAjax(busJcbDmcgyqService.update(busJcbDmcgyq));
    }

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

    @ApiOperation(value = "列表")
    @GetMapping(value = "/list")
    public TableDataInfo getList(BusJcbDmcgyq busJcbDmcgyq) {
        startPage();
        List<BusJcbDmcgyq> list = busJcbDmcgyqService.getList(busJcbDmcgyq);
        return getDataTable(list);
    }

    @ApiOperation(value = "获取上次检测值")
    @GetMapping(value = "/getLastOne")
    public AjaxResult getLastOne() {
        BusJcbDmcgyq info = busJcbDmcgyqService.getLastRecord();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("info",info);
        return ajax;
    }

    @ApiOperation(value = "提交")
    @PutMapping("/submit")
    public AjaxResult submit(@RequestBody BusJcbDmcgyq busJcbDmcgyq) {
        return toAjax(busJcbDmcgyqService.submit(busJcbDmcgyq));
    }

    @ApiOperation(value = "退回")
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody BusJcbDmcgyq busJcbDmcgyq) {
        return toAjax(busJcbDmcgyqService.reject(busJcbDmcgyq));
    }

    @ApiOperation(value = "确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody BusJcbDmcgyq busJcbDmcgyq) {
        return toAjax(busJcbDmcgyqService.confirm(busJcbDmcgyq));
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export/{id}")
    public AjaxResult export(@PathVariable(value = "id") String id) {

        BusJcbDmcgyq info = busJcbDmcgyqService.getInfo(id);
        WordUtils wordUtil = new WordUtils();
        //填充变量
        Map<String, Object> params = new HashMap<>();
        params.put("${fenxiang}", info.getFenxiang());
        params.put("${tianqi}", info.getTianqi() == null ? "" : info.getTianqi());
        params.put("${wendu}", info.getTemperature() == null ? "" : info.getTemperature());
        params.put("${zzhfw}", info.getPillarArea().toString());
        params.put("${xingbie}", info.getXingbie());
        params.put("${dgqwgAzlg}", StringUtils.yesOrNo(info.getDgqwgAzlg()));
        params.put("${dgqwgWzws}", StringUtils.yesOrNo(info.getDgqwgWzws()));
        params.put("${dgqwgBmqj}", StringUtils.yesOrNo(info.getDgqwgBmqj()));
        params.put("${bspwgBsqx}", StringUtils.yesOrNo(info.getBspwgBsqx()));
        params.put("${bspwgAzlg}", StringUtils.yesOrNo(info.getBspwgAzlg()));
        params.put("${dgqclG1}", info.getDgqclG1().toString());
        params.put("${dgqclG2}", info.getDgqclG2().toString());
        params.put("${dgqclG3}", info.getDgqclG3().toString());
        params.put("${dgqclG4}", info.getDgqclG4().toString());
        params.put("${jianchariqi}", info.getCheckDate().toString());
        params.put("${jcr}", info.getCheckUser());
        params.put("${sbzt}", StringUtils.YES_VALUE.equals(info.getSbzt()) ? "合格" : "不合格");
        params.put("${qrr}", info.getConfirmUser());
        params.put("${czwt}", info.getCzwt() == null ? "" : info.getCzwt());

        //生成word文件的文件名
        String fileName = "地面磁感应手册.docx";
        String template = "word/template/地面磁感应手册.docx";
        String localFilePath = wordUtil.getWord(params, template, fileName);
        return sysFileService.uploadFile(localFilePath);
    }

    @ApiOperation(value = "导出excel")
    @GetMapping(value = "exportExcel")
    public AjaxResult exportExcel(BusJcbDmcgyq busJcbDmcgyq) {
        List<BusJcbDmcgyq> list = busJcbDmcgyqService.getList(busJcbDmcgyq);
        String fileName = "地磁感应器检测记录.xls";
        String templateFileName = "excel/template/地磁感应器检测记录.xls";
        String localFilePath = new EasyExcelUtil().writeExcel(fileName, templateFileName, list);
        return sysFileService.uploadFile(localFilePath);
    }


}
