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

import com.railway.business.catenary.domain.BusJcbBlq;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.service.IBusJcbBlqService;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author wuhonghao
 */
@Api(value = "rest/catenary/bus/jcb/blq", tags = "检查表 - 避雷器")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jcb/blq")
public class BusJcbBlqController extends BaseController {

    private final IBusJcbBlqService busJcbBlqService;
    private final ISysFileService sysFileService;

    @Value("${railway.filePath}")
    public String filePath;

    public BusJcbBlqController(IBusJcbBlqService busJcbBlqService,
        ISysFileService sysFileService) {
        this.busJcbBlqService = busJcbBlqService;
        this.sysFileService = sysFileService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BusJcbBlq busJcbBlq) {
        return toAjax(busJcbBlqService.create(busJcbBlq));
    }

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

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Valid BusJcbBlq busJcbBlq) {
        return toAjax(busJcbBlqService.update(busJcbBlq));
    }

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

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

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

    @ApiOperation(value = "提交")
    @PutMapping("/submit")
    public AjaxResult submit(@RequestBody BusJcbBlq busJcbBlq) {
        busJcbBlq.setUpdateBy(getUsername());
        return toAjax(busJcbBlqService.submit(busJcbBlq));
    }

    @ApiOperation(value = "退回")
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody BusJcbBlq busJcbBlq) {
        busJcbBlq.setUpdateBy(getUsername());
        return toAjax(busJcbBlqService.reject(busJcbBlq));
    }

    @ApiOperation(value = "确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody BusJcbBlq busJcbBlq) {
        busJcbBlq.setUpdateBy(getUsername());
        return toAjax(busJcbBlqService.confirm(busJcbBlq));
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public AjaxResult export(@RequestParam String id) {
        BusJcbBlq info = busJcbBlqService.getInfo(id);
        WordUtils wordUtil=new WordUtils();
        Map<String, Object> params = new HashMap<>();
        params.put("${qj}", info.getStationName());
        params.put("${zzh}", info.getPillarCode());
        params.put("${wd}", info.getTemperature());
        params.put("${jcrq}", DateUtils.parseCheckDate(info.getCheckDate()));
        params.put("${jcr}", info.getCheckUser());
        params.put("${tjwzBlqtjazsp}", StringUtils.yesOrNo(info.getTjwzBlqtjazsp()));
        params.put("${tjwzWxs}", StringUtils.yesOrNo(info.getTjwzWxs()));
        params.put("${tjwzGblsljjg}", StringUtils.yesOrNo(info.getTjwzGblsljjg()));
        params.put("${blqjyzBlqjzcjyzcszzt}", StringUtils.yesOrNo(info.getBlqjyzBlqjzcjyzcszzt()));
        params.put("${blqjyzBmqjazlg}", StringUtils.yesOrNo(info.getBlqjyzBmqjazlg()));
        params.put("${blqyxBlqyxwssdg}", StringUtils.yesOrNo(info.getBlqyxBlqyxwssdg()));
        params.put("${blqyxGycyxdjdtdtzxjl}", info.getBlqyxGycyxdjdtdtzxjl().toString());
        params.put("${tlqZtlhwpslh}", StringUtils.yesOrNo(info.getTlqZtlhwpslh()));
        params.put("${jsqDzjs}", info.getJsqDzjs());
        params.put("${blqjdztJddz}", info.getBlqjdztJddz().toString());
        params.put("${sbzt}", StringUtils.YES_VALUE.equals(info.getSbzt()) ? "合格" : "不合格");
        params.put("${confirmUser}", StringUtils.isEmpty(info.getConfirmUser())?"":info.getConfirmUser());
        params.put("${czwt}", 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(JcebQueryVo query) {
        String localFilePath = busJcbBlqService.exportExcel(query);
        return sysFileService.uploadFile(localFilePath);
    }

}
