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

import com.railway.business.catenary.domain.BusJcbJcxc;
import com.railway.business.catenary.service.IBusJcbJcxcService;
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.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangcaifeng
 */
@Api(value = "rest/catenary/bus/jcb/jcxc", tags = "检查表 - 交叉线岔")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jcb/jcxc")
public class BusJcbJcxcController extends BaseController {

    private final IBusJcbJcxcService busJcbJcxcService;
    private final ISysFileService sysFileService;

    public BusJcbJcxcController(IBusJcbJcxcService busJcbJcxcService,
        ISysFileService sysFileService) {
        this.busJcbJcxcService = busJcbJcxcService;
        this.sysFileService = sysFileService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BusJcbJcxc busJcbJcxc) {
        return toAjax(busJcbJcxcService.create(busJcbJcxc));
    }

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

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Valid BusJcbJcxc busJcbJcxc) {
        return toAjax(busJcbJcxcService.update(busJcbJcxc));
    }

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

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

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

    @ApiOperation(value = "提交")
    @PutMapping("/submit")
    public AjaxResult submit(@RequestBody BusJcbJcxc busJcbJcxc) {
        return toAjax(busJcbJcxcService.submit(busJcbJcxc));
    }

    @ApiOperation(value = "退回")
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody BusJcbJcxc busJcbJcxc) {
        return toAjax(busJcbJcxcService.reject(busJcbJcxc));
    }

    @ApiOperation(value = "确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody BusJcbJcxc busJcbJcxc) {
        return toAjax(busJcbJcxcService.confirm(busJcbJcxc));
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export/{id}")
    public AjaxResult export(@PathVariable(value = "id") String id) {
        BusJcbJcxc info = busJcbJcxcService.getInfo(id);
        WordUtils wordUtil = new WordUtils();
        //填充变量
        Map<String, Object> params = new HashMap<>();
        params.put("${xch}", info.getXch());
        params.put("${stationName()}", info.getStationName());
        params.put("${temperature}", info.getTemperature() == null ? "" : info.getTemperature());
        if(info.getCheckDate() != null) {
            LocalDate jcrqArr = info.getCheckDate();
            params.put("${year}", jcrqArr.getYear());
            params.put("${month}", jcrqArr.getMonthValue());
            params.put("${day}", jcrqArr.getDayOfMonth());
        }
        params.put("${gz1lcz}", info.getGz1lcz().toString());
        params.put("${gz2lcz}", info.getGz2lcz().toString());
        params.put("${zglcz}", info.getZglcz().toString());
        params.put("${qglcz}", info.getQglcz().toString());
        params.put("${ngj}", info.getNgj().toString());
        params.put("${plz}", info.getPlz().toString());
        params.put("${xcxzgazlg}", StringUtils.yesOrNo(info.getXcxzgazlg()));
        params.put("${hdjx}", StringUtils.yesOrNo(info.getHdjx()));
        params.put("${fhzzsq}", StringUtils.yesOrNo(info.getFhzzsq()));
        params.put("${dqssxx}", StringUtils.yesOrNo(info.getDqssxx()));
        params.put("${lswst}", StringUtils.yesOrNo(info.getLswst()));
        params.put("${xzgldydx}", StringUtils.yesOrNo(info.getXzgldydx()));
        params.put("${dxztlh}", StringUtils.yesOrNo(info.getDxztlh()));
        params.put("${czjj}", info.getCzjj().toString());
        params.put("${lgzzgc}", info.getLgzzgc().toString());
        params.put("${gzyfzgc}", info.getGzyfzgc().toString());
        params.put("${wxj}", StringUtils.yesOrNo(info.getWxj()));
        params.put("${hddxxjzt}", StringUtils.yesOrNo(info.getHddxxjzt()));
        params.put("${jcdxzt}", StringUtils.yesOrNo(info.getJcdxzt()));
        params.put("${dljxzt}", StringUtils.yesOrNo(info.getDljxzt()));
        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(BusJcbJcxc busJcbJcxc) {
        List<BusJcbJcxc> list = busJcbJcxcService.getList(busJcbJcxc);
        list = list.stream().map(x -> transData(x)).collect(Collectors.toList());
        Map<String, Object> params = new HashMap<>();
        String fileName = "交叉线岔检测（修）记录.xls";
        String templateFileName = "excel/template/交叉线岔检测（修）记录.xls";
        String localFilePath = new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
        return sysFileService.uploadFile(localFilePath);
    }

    private BusJcbJcxc transData(BusJcbJcxc x) {
        x.setDcxh("18");
        x.setXiangbie("检测");
        if((Integer.parseInt(x.getXch()) & 1) == 1){
            // xch奇数
            if(x.getLgzzgc() != null) {
                x.setXxfx(x.getLgzzgc());
            } else {
                x.setXxfx(x.getGzyfzgc());
            }
        } else {
            // xch偶数
            if(x.getLgzzgc() != null) {
                x.setSxfx(x.getLgzzgc());
            } else {
                x.setSxfx(x.getGzyfzgc());
            }
        }
        x.setWxj(StringUtils.YES_VALUE.equals(x.getWxj()) ? "无" : "有");
        if(StringUtils.YES_VALUE.equals(x.getXcxzgazlg()) &&
                StringUtils.YES_VALUE.equals(x.getHdjx()) &&
                StringUtils.YES_VALUE.equals(x.getFhzzsq()) &&
                StringUtils.YES_VALUE.equals(x.getDqssxx()) &&
                StringUtils.YES_VALUE.equals(x.getLswst()) &&
                StringUtils.YES_VALUE.equals(x.getXzgldydx()) &&
                StringUtils.YES_VALUE.equals(x.getDxztlh()) &&
                StringUtils.YES_VALUE.equals(x.getWxj()) &&
                StringUtils.YES_VALUE.equals(x.getHddxxjzt()) &&
                StringUtils.YES_VALUE.equals(x.getJcdxzt()) &&
                StringUtils.YES_VALUE.equals(x.getDljxzt())
        ) {
            x.setGlbjzt("良好");
        } else {
            x.setGlbjzt("故障");
        }
        return x;

    }

}
