package com.tofly.xrtygis.controller;

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.xrtygis.entity.ValvePit;
import com.tofly.xrtygis.service.ValvePitService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/valvePit")
@Api(tags = "阀井照片信息管理")
public class ValvePitController {

    private final ValvePitService valvePitService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone page(Page page, @ModelAttribute ValvePit valvePit) {
        return ResultRespone.success(valvePitService.page(page, Wrappers.query(valvePit)));
    }

    @ApiOperation(value = "新增阀井照片信息管理")
    @PostMapping("/save")
    public ResultRespone save(@RequestParam(value = "fileList") List<MultipartFile> fileList, ValvePit valvePit) {
        try {
            return ResultRespone.success(valvePitService.saveValvePit(fileList, valvePit));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "单文件分片上传（阀井照片压缩包）")
    @PostMapping("/singleFilePartUpload")
    public ResultRespone singleFilePartUpload(@ApiParam("单次分片的文件") @RequestParam("filePart") MultipartFile filePart,
                                              @ApiParam("当前分片定位") @RequestParam("partIndex") Integer partIndex,
                                              @ApiParam("文件分片总数") @RequestParam("partNum") Integer partNum,
                                              @ApiParam("单次分片的文件名称") @RequestParam("fileName") String fileName,
                                              @ApiParam("单次分片的文件") @RequestParam("fileUid") String fileUid) {
        try {
            return ResultRespone.success(valvePitService.singleFilePartUpload(filePart, partIndex, partNum, fileName, fileUid));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "通过ID批量删除阀井照片信息管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone deleteByIds(String ids) {
        return ResultRespone.success(valvePitService.removeValvePitByIds(ids));
    }

}
