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

import com.railway.business.catenary.domain.BusJcebJddz;
import com.railway.business.catenary.enums.JcebStatusEnum;
import com.railway.business.catenary.service.IBusJcebJddzService;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangcaifeng
 */
@Api(value = "rest/catenary/bus/jceb/jddz", tags = "检测表 - 接地电阻")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jceb/jddz")
public class BusJcebJddzController extends BaseController {

    private final IBusJcebJddzService busJcebJddzService;
    private final ISysFileService sysFileService;

    public BusJcebJddzController(IBusJcebJddzService busJcebJddzService, ISysFileService sysFileService) {
        this.busJcebJddzService = busJcebJddzService;
        this.sysFileService = sysFileService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody BusJcebJddz busJcebJddz) {
        return toAjax(busJcebJddzService.create(busJcebJddz));
    }

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

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Valid BusJcebJddz busJcebJddz) {
        return toAjax(busJcebJddzService.update(busJcebJddz));
    }

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

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

    @ApiOperation(value = "提交")
    @PutMapping("/submit")
    public AjaxResult submit(@RequestBody BusJcebJddz busJcebJddz) {
        return toAjax(busJcebJddzService.submit(busJcebJddz));
    }

    @ApiOperation(value = "退回")
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody BusJcebJddz busJcebJddz) {
        return toAjax(busJcebJddzService.reject(busJcebJddz));
    }

    @ApiOperation(value = "确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody BusJcebJddz busJcebJddz) {
        return toAjax(busJcebJddzService.confirm(busJcebJddz));
    }

    @ApiOperation(value = "导出excel")
    @GetMapping(value = "exportExcel")
    public AjaxResult exportExcel(BusJcebJddz busJcebJddz) {
        List<BusJcebJddz> list = busJcebJddzService.getList(busJcebJddz);
        Map<String, Object> params = new HashMap<>();
        if(!list.isEmpty()) {
            params.put("operator", list.get(0).getOperator());
            if(JcebStatusEnum.COMPLETED.getCode().equals(list.get(0).getSubmitState()))
                params.put("confirmUser", list.get(0).getUpdateBy());
        }
        list = list.stream()
                .map(x -> {x.setSfhg(StringUtils.YES_VALUE.equals(x.getSfhg()) ? "合格" : "不合格"); return x;})
                .collect(Collectors.toList());
        String fileName = "接地电阻测量记录.xls";
        String templateFileName = "excel/template/接地电阻测量记录.xls";
        String localFilePath = new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
        return sysFileService.uploadFile(localFilePath);
    }
}
