/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.plan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.service.paln.FaultService;
import com.tofly.gpsboot.service.paln.LeakRecordService;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.plan.Fault;
import com.tofly.pojo.plan.Hydrantrecord;
import com.tofly.pojo.plan.LeakRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 故障记录
 *
 * @author admin
 * @date Tue Oct 26 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fault")
@Api(tags="故障上报")
public class FaultController {

    private final FaultService faultService;


    @GetMapping("/page" )
    @ApiOperation(value = "分页查询故障信息")
    public ResultRespone getHydrantrecordPage(Page page, @ModelAttribute Fault fault) {

        return ResultRespone.success(faultService.getFaultPage(page,fault));
    }


    @GetMapping("/getFaultTypeList" )
    @ApiOperation(value = "获取故障类型列表")
    public ResultRespone getFaultTypeList() {
        return ResultRespone.success(faultService.list().stream().map(Fault::getFaultType).collect(Collectors.toSet()));
    }

    /**
     * 通过id查询消防栓记录上报表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
        return ResultRespone.success(faultService.getById(id));
    }

    /**
     * 新增消防栓记录上报表
     * @param fault 消防栓记录上报表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增故障信息")
    @PostMapping("/save")
    public ResultRespone save(@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList,Fault fault) {
        return ResultRespone.success(faultService.savaFaultInfo(fileList,fault));
    }

    /**
     * 修改消防栓记录上报表
     * @param fault 消防栓记录上报表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改故障信息")
    @PutMapping("/update")
    public ResultRespone updateById(@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList, Fault fault) {
        // 判断文件是否修改
        return ResultRespone.success(faultService.updateFaultInfo(fileList,fault));
    }


    /**
     * 通过id批量删除消防栓记录上报表
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID批量删除故障信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(faultService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
