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

import com.railway.business.catenary.domain.BusJcbGlkg;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.service.IBusJcbGlkgService;
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.DateUtils;
import com.railway.common.utils.StringUtils;
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 fenghao
 */
@Api(value = "rest/catenary/bus/jcb/glkg", tags = "检查表 - 隔离开关检查表")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jcb/glkg")
public class BusJcbGlkgController extends BaseController {

    private final IBusJcbGlkgService busJcbGlkgService;
    private final ISysFileService sysFileService;

    public BusJcbGlkgController(IBusJcbGlkgService busJcbGlkgService,
        ISysFileService sysFileService) {
        this.busJcbGlkgService = busJcbGlkgService;
        this.sysFileService = sysFileService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BusJcbGlkg busJcbGlkg) {
        return toAjax(busJcbGlkgService.create(busJcbGlkg));
    }

    @ApiOperation(value = "提交")
    @PutMapping("/submit")
    public AjaxResult submit(@RequestBody BusJcbGlkg busJcbGlkg) {
        busJcbGlkg.setUpdateBy(getUsername());
        return toAjax(busJcbGlkgService.submit(busJcbGlkg));
    }

    @ApiOperation(value = "退回")
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody BusJcbGlkg busJcbGlkg) {
        busJcbGlkg.setUpdateBy(getUsername());
        return toAjax(busJcbGlkgService.reject(busJcbGlkg));
    }

    @ApiOperation(value = "确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody BusJcbGlkg busJcbGlkg) {
        busJcbGlkg.setUpdateBy(getUsername());
        return toAjax(busJcbGlkgService.confirm(busJcbGlkg));
    }

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

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Valid BusJcbGlkg busJcbGlkg) {
        return toAjax(busJcbGlkgService.update(busJcbGlkg));
    }

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

    @ApiOperation(value = "查看生成的检测表信息")
    @GetMapping(value = {"/", "/getJcebInfo/{id}"})
    public AjaxResult getJcebInfo(@PathVariable(value = "id") String id) {
        BusJcbGlkg info = busJcbGlkgService.getJcebInfo(id);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("info",info);
        return ajax;
    }

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

    @ApiOperation(value = "导出excel")
    @GetMapping(value = "exportExcel")
    public AjaxResult exportExcel(JcebQueryVo query) {
        String localFilePath = busJcbGlkgService.exportExcel(query);
        return sysFileService.uploadFile(localFilePath);
    }

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

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

        BusJcbGlkg info = busJcbGlkgService.getInfo(id);
        WordUtils wordUtil = new WordUtils();
        //填充变量
        Map<String, Object> params = new HashMap<>();
        params.put("${kgh}", info.getKgbh() == null ? "" : info.getKgbh());
        params.put("${qj}", info.getStationName() == null ? "" : info.getStationName());
        params.put("${wd}", info.getTemperature() == null ? "" : info.getTemperature());
        params.put("${jcrq}", DateUtils.parseCheckDate(info.getCheckDate()));
        params.put("${jgxA}", StringUtils.yesOrNo(info.getJgxA()));
        params.put("${jgxB}", StringUtils.yesOrNo(info.getJgxB()));
        params.put("${jgxC}", StringUtils.yesOrNo(info.getJgxC()));
        params.put("${qdzzA}", StringUtils.yesOrNo(info.getQdzzA()));
        params.put("${cdgA}", StringUtils.yesOrNo(info.getCdgA()));
        params.put("${cdgB}", StringUtils.yesOrNo(info.getCdgB()));
        params.put("${cdgC}", StringUtils.yesOrNo(info.getCdgC()));
        params.put("${gbA}", StringUtils.yesOrNo(info.getGbA()));
        params.put("${ctztA}", StringUtils.yesOrNo(info.getCtztA()));
        params.put("${ctmtztA}", StringUtils.yesOrNo(info.getCtmtztA()));
        params.put("${hjkztA}", StringUtils.yesOrNo(info.getHjkztA()));
        params.put("${zcjyzztA}", StringUtils.yesOrNo(info.getZcjyzztA()));
        params.put("${yxztA}", StringUtils.yesOrNo(info.getYxztA()));
        params.put("${yxztB}", StringUtils.yesOrNo(info.getYxztB()));
        params.put("${yxztC}", StringUtils.yesOrNo(info.getYxztC()));
        params.put("${swyx1Lcz}", StringUtils.getSlash(info.getSwyx1Lcz()));
        params.put("${swyx1Dg}", StringUtils.getSlash(info.getSwyx1Dg()));
        params.put("${swyx2Lcz}", StringUtils.getSlash(info.getSwyx2Lcz()));
        params.put("${swyx2Dg}", StringUtils.getSlash(info.getSwyx2Dg()));
        params.put("${zxjl}", StringUtils.getSlash(info.getZxjl()));
        params.put("${xjztA}", StringUtils.yesOrNo(info.getXjztA()));
        params.put("${xjztB}", StringUtils.yesOrNo(info.getXjztB()));
        params.put("${sdfhsyA}", StringUtils.yesOrNo(info.getSdfhsyA()));
        params.put("${ddfhsyA}", StringUtils.yesOrNo(info.getDdfhsyA()));
        params.put("${ydfhsyA}", StringUtils.yesOrNo(info.getYdfhsyA()));
        params.put("${kgztjdztA}", StringUtils.yesOrNo(info.getKgztjdztA()));
        params.put("${kgztjdztDzz}", StringUtils.getSlash(info.getKgztjdztDzz()));
        params.put("${kgjgxjdztA}", StringUtils.yesOrNo(info.getKgjgxjdztA()));
        params.put("${kgjgxjdztDzz}", StringUtils.getSlash(info.getKgjgxjdztDzz()));
        params.put("${sbzt}", StringUtils.YES_VALUE.equals(info.getSbzt()) ? "合格" : "不合格");
        params.put("${jcr}", info.getCheckUser() == null ? "" : info.getCheckUser());
        params.put("${qrr}", info.getConfirmUser() == null ? "" : 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);
    }

}
