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

import com.railway.business.catenary.domain.BusJcbWjcxc;
import com.railway.business.catenary.service.IBusJcbWjcxcService;
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.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/wjcxc", tags = "检查表 - 无交叉线岔")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jcb/wjcxc")
public class BusJcbWjcxcController extends BaseController {

    private final IBusJcbWjcxcService busJcbWjcxcService;
    private final ISysFileService sysFileService;

    public BusJcbWjcxcController(IBusJcbWjcxcService busJcbWjcxcService,
                                 ISysFileService sysFileService) {
        this.busJcbWjcxcService = busJcbWjcxcService;
        this.sysFileService = sysFileService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BusJcbWjcxc busJcbWjcxc) {
        return toAjax(busJcbWjcxcService.create(busJcbWjcxc));
    }

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

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Valid BusJcbWjcxc busJcbWjcxc) {
        return toAjax(busJcbWjcxcService.update(busJcbWjcxc));
    }

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

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

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

    @ApiOperation(value = "提交")
    @PutMapping("/submit")
    public AjaxResult submit(@RequestBody BusJcbWjcxc busJcbWjcxc) {
        return toAjax(busJcbWjcxcService.submit(busJcbWjcxc));
    }

    @ApiOperation(value = "退回")
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody BusJcbWjcxc busJcbWjcxc) {
        return toAjax(busJcbWjcxcService.reject(busJcbWjcxc));
    }

    @ApiOperation(value = "确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody BusJcbWjcxc busJcbWjcxc) {
        return toAjax(busJcbWjcxcService.confirm(busJcbWjcxc));
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export/{id}")
    public AjaxResult export(@PathVariable(value = "id") String id) {
        BusJcbWjcxc info = busJcbWjcxcService.getInfo(id);
        //填充变量
        Map<String, Object> params = new HashMap<>();
        params.put("xch", info.getXch());
        params.put("qujian", info.getStationName());
        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("jxdxzt", StringUtils.yesOrNo(info.getJcdxzt()));
        params.put("hdxjzt", StringUtils.yesOrNo(info.getHdxjzt()));
        params.put("wxjqzt", StringUtils.yesOrNo(info.getWxjqzt()));
        params.put("dljzt", StringUtils.yesOrNo(info.getDljzt()));
        params.put("azzxlcz", info.getAzzxlcz().toString());
        params.put("azzxdg", info.getAzzxdg().toString());
        params.put("azcxlcz", info.getAzcxlcz().toString());
        params.put("azcxdg", info.getAzcxdg().toString());
        params.put("bzzxlcz", info.getBzzxlcz().toString());
        params.put("bzzxdg", info.getBzzxdg().toString());
        params.put("bzcxlcz", info.getBzcxlcz().toString());
        params.put("bzcxdg", info.getBzcxdg().toString());
        params.put("czzxlcz", info.getCzzxlcz().toString());
        params.put("czzxdg", info.getCzzxdg().toString());
        params.put("czcxlcz", info.getCzcxlcz().toString());
        params.put("czcxdg", info.getCzcxdg().toString());
        params.put("zxlcz", info.getZxlcz().toString());
        params.put("zxdg", info.getZxdg().toString());
        params.put("cxlcz", info.getCxlcz().toString());
        params.put("ljcdxjj", info.getLjcdxjj().toString());
        params.put("cxdg", info.getCxdg().toString());
        params.put("sbzt", StringUtils.YES_VALUE.equals(info.getSbzt()) ? "合格" : "不合格");
        params.put("jcr", info.getCheckUser());
        params.put("qrr", info.getConfirmUser());
        params.put("czwt", info.getCzwt());

        String fileName = "无交叉线岔.xls";
        String template = "word/template/无交叉线岔.xls";
        EasyExcelUtil easyExcelUtil = new EasyExcelUtil();
        String localFilePath = easyExcelUtil.getExcel(params, null, template, fileName);
        return sysFileService.uploadFile(localFilePath);
    }

    @ApiOperation(value = "导出excel")
    @GetMapping(value = "exportExcel")
    public AjaxResult exportExcel(BusJcbWjcxc busJcbWjcxc) {
        List<BusJcbWjcxc> list = busJcbWjcxcService.getList(busJcbWjcxc);
        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 BusJcbWjcxc transData(BusJcbWjcxc x) {
        x.setXiangbie("检测");
        if(StringUtils.YES_VALUE.equals(x.getZxwxjqzt()) ||
                StringUtils.YES_VALUE.equals(x.getCxwxjqzt())
        ) {
            x.setYwxj("有");
        } else {
            x.setYwxj("无");
        }
        if(StringUtils.YES_VALUE.equals(x.getDljzt())){
            x.setDljzt("良好");
        } else {
            x.setDljzt("故障");
        }
        return x;
    }

}
