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

import com.railway.business.catenary.domain.BusJcbFdfxjyq;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.service.IBusJcbFdfxjyqService;
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.file.BASE64DecodedMultipartFile;
import com.railway.common.utils.http.HttpUtils;
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.io.InputStream;
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.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author wuhonghao
 */
@Api(value = "rest/catenary/bus/jcb/fdfxjyq", tags = "检查表 - dxf分选绝缘器检查表 ")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jcb/fdfxjyq")
public class BusJcbFdfxjyqController extends BaseController {

    private final IBusJcbFdfxjyqService busJcbFdfxjyqService;
    private final ISysFileService sysFileService;

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

    public BusJcbFdfxjyqController(IBusJcbFdfxjyqService busJcbFdfxjyqService,
        ISysFileService sysFileService) {
        this.busJcbFdfxjyqService = busJcbFdfxjyqService;
        this.sysFileService = sysFileService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BusJcbFdfxjyq busJcbFdfxjyq) throws Exception {
        //将文件转成base64
        if(busJcbFdfxjyq.getPicUrl()!=null&&!"".equals(busJcbFdfxjyq.getPicUrl())){
            MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(busJcbFdfxjyq.getPicUrl());
            String path = sysFileService.uploadFile(multipartFile);
            busJcbFdfxjyq.setPicUrl(path);
        }
        return toAjax(busJcbFdfxjyqService.create(busJcbFdfxjyq));
    }

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

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Valid BusJcbFdfxjyq busJcbFdfxjyq) throws Exception {
        //将文件转成base64
        if(busJcbFdfxjyq.getPicUrl()!=null&&!"".equals(busJcbFdfxjyq.getPicUrl())){
            MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(busJcbFdfxjyq.getPicUrl());
            String path = sysFileService.uploadFile(multipartFile);
            busJcbFdfxjyq.setPicUrl(path);
        }
        return toAjax(busJcbFdfxjyqService.update(busJcbFdfxjyq));
    }

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

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

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

    @ApiOperation(value = "提交")
    @PutMapping("/submit")
    public AjaxResult submit(@RequestBody BusJcbFdfxjyq busJcbFdfxjyq) throws Exception {
        busJcbFdfxjyq.setUpdateBy(getUsername());
        //将文件转成base64
        if(busJcbFdfxjyq.getPicUrl()!=null&&!"".equals(busJcbFdfxjyq.getPicUrl())){
            MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(busJcbFdfxjyq.getPicUrl());
            String path = sysFileService.uploadFile(multipartFile);
            busJcbFdfxjyq.setPicUrl(path);
        }
        return toAjax(busJcbFdfxjyqService.submit(busJcbFdfxjyq));
    }

    @ApiOperation(value = "退回")
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody BusJcbFdfxjyq busJcbFdfxjyq) throws Exception {
        busJcbFdfxjyq.setUpdateBy(getUsername());
        return toAjax(busJcbFdfxjyqService.reject(busJcbFdfxjyq));
    }

    @ApiOperation(value = "确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody BusJcbFdfxjyq busJcbFdfxjyq) throws Exception {
        busJcbFdfxjyq.setUpdateBy(getUsername());
        return toAjax(busJcbFdfxjyqService.confirm(busJcbFdfxjyq));
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public AjaxResult export(@RequestParam String id) throws InterruptedException {
        //获取基础数据
        BusJcbFdfxjyq info = busJcbFdfxjyqService.getInfo(id);
        WordUtils wordUtil=new WordUtils();
        //填充变量
        Map<String, Object> params = new HashMap<>();
        params.put("${fdh}", info.getFdh());
        params.put("${qj}", info.getStationName());
        params.put("${jcrq}", DateUtils.parseCheckDate(info.getCheckDate()));
        params.put("${wd}", info.getTemperature());
        params.put("${jcr}", info.getCheckUser());
        params.put("${pointB}", info.getPointB().toString());
        params.put("${pointG}", info.getPointG().toString());
        params.put("${fcd}", info.getFcd().toString());
        params.put("${fddmpsd}", info.getFddmpsd().toString());
        params.put("${jybjFdjyqzjyzt}", StringUtils.yesOrNo(info.getJybjFdjyqzjyzt()));
        params.put("${jybjClsjyzzt}", StringUtils.yesOrNo(info.getJybjClsjyzzt()));
        params.put("${lbjztLs}", StringUtils.yesOrNo(info.getLbjztLs()));
        params.put("${lbjztXdx}", StringUtils.yesOrNo(info.getLbjztXdx()));
        params.put("${lbjztPhzj}", StringUtils.yesOrNo(info.getLbjztPhzj()));
        params.put("${lbjztJcxjt}", StringUtils.yesOrNo(info.getLbjztJcxjt()));
        params.put("${lbjztXhj}", StringUtils.yesOrNo(info.getLbjztXhj()));
        params.put("${sbzt}", StringUtils.YES_VALUE.equals(info.getSbzt()) ? "合格" : "不合格");
        params.put("${xlfdXlpd}", info.getXlfdXlpd().toString());
        params.put("${xlfdWgdg}", info.getXlfdWgdg().toString());
        params.put("${xlfdFdsxlqj}", info.getXlfdFdsxlqj().toString());
        params.put("${xlfdFdczxlqj}", info.getXlfdFdczxlqj().toString());
        params.put("${sdgjcztSddg}", StringUtils.yesOrNo(info.getSdgjcztSddg()));
        params.put("${confirmUser}", info.getConfirmUser()==null?"":info.getConfirmUser());
        params.put("${czwt}", info.getCzwt());
        //填充图片信息
        Map<String,Object> header = new HashMap<>();
        header.put("width", 450);
        header.put("height", 110);
        header.put("type", "jpg");
        InputStream inputStream = HttpUtils.sendGetStream(info.getPicUrl(), "");
        //解决网络传输造成流读取不全问题
        Thread.sleep(200);
        header.put("content", WordUtils.inputStream2ByteArray(inputStream, true));
        params.put("${header}",header);



        //生成word文件的文件名
        String fileName = "DXF分段绝缘器手册.docx";
        String template = "word/template/DXF分段绝缘器手册.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 = busJcbFdfxjyqService.exportExcel(query);
        return sysFileService.uploadFile(localFilePath);
    }


}
