/*
 * Author: wangjian
 * 生成controller
 */
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.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.commons.Constant;
import com.tofly.xrtygis.entity.TfViewPointManage;
import com.tofly.xrtygis.exception.BusinessException;
import com.tofly.xrtygis.service.TfViewPointManageService;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 视点管理
 *
 * @author JF
 * @date Tue May 10 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tfviewpointmanage")
@Api(tags="视点管理接口")
public class TfViewPointManageController extends BaseController {

    private final  TfViewPointManageService tfViewPointManageService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param tfViewPointManage 视点管理
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getTfViewPointManagePage(Page page, @ModelAttribute TfViewPointManage tfViewPointManage) {
      return ResultRespone.success(tfViewPointManageService.page(page, Wrappers.query(tfViewPointManage)));
    }


    /**
     * 通过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(tfViewPointManageService.getById(id));
    }

//    /**
//     * 新增视点管理
//     * @param tfViewPointManage 视点管理
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="新增视点管理" )
//    @ApiOperation(value = "新增视点管理")
//    @PostMapping
//    public ResultRespone save(@RequestBody TfViewPointManage tfViewPointManage) {
//      return ResultRespone.success(tfViewPointManageService.save(tfViewPointManage));
//    }

    /**
     * 修改视点管理
     * @param tfViewPointManage 视点管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改视点管理" )
    @ApiOperation(value = "修改视点管理")
    @PutMapping
    public ResultRespone updateById(@RequestBody TfViewPointManage tfViewPointManage) {
      return ResultRespone.success(tfViewPointManageService.updateById(tfViewPointManage));
    }

//    /**
//     * 通过id删除视点管理
//     * @param id id
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="通过id删除视点管理" )
//    @ApiOperation(value = "通过ID删除视点管理")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
//    })
//    @DeleteMapping("/{id}" )
//    public ResultRespone removeById(@PathVariable Long id) {
//      return ResultRespone.success(tfViewPointManageService.removeById(id));
//    }
//
//    /**
//     * 通过id批量删除视点管理
//     * @param ids id
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="通过id批量删除视点管理" )
//    @ApiOperation(value = "通过ID批量删除视点管理")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
//    })
//    @DeleteMapping("/deleteByIds" )
//    public ResultRespone removeById(String ids) {
//        return ResultRespone.success(tfViewPointManageService.removeByIds(Arrays.asList(ids.split(","))));
//    }

    /**
     * 新增视点管理
     * @param tfViewPointManage 视点管理
     * @return ResultRespone
     */
    @ApiOperation(value = "新增视点管理")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增视点管理")
    public ResultRespone<Object> saveTfViewPointManage(@Valid TfViewPointManage tfViewPointManage,
                                                      BindingResult result,
                                                      @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            tfViewPointManageService.saveTfViewPointManage(fileList, tfViewPointManage);
        }
        return rest;

    }

//    /**
//     * 修改水库保洁管理
//     * @param tfReservoirClean 水库保洁管理
//     * @return ResultRespone
//     */
//    @ApiOperation(value = "修改水库保洁管理")
//    @PutMapping("/update")
//    @ToFlyAppLog(title = "修改水库保洁管理")
//    public ResultRespone<Object> updateTfReservoirClean(@Valid TfReservoirClean tfReservoirClean,
//                                                             BindingResult result,
//                                                             @RequestParam(value = "fileListBefore", required = false) List<MultipartFile> fileListBefore,
//                                                             @RequestParam(value = "fileListAfter", required = false) List<MultipartFile> fileListAfter) {
//        ResultRespone<Object> rest = new ResultRespone<>();
//        // 数据校验 判空返回
//        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
//            try {
//                tfReservoirCleanService.updateTfReservoirClean(fileListBefore, fileListAfter, tfReservoirClean);
//            } catch (BusinessException e) {
//                rest.setCode(e.getRetCd());
//                rest.setMessage(e.getMsgDes());
//            }
//
//        }
//        return rest;
//
//    }

    /**
     * 通过id批量删除视点管理
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过id批量删除视点管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    @ToFlyAppLog(title = "通过id批量删除视点管理")
    public ResultRespone<Object> removeByIdsTfViewPointManage(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        try {
            tfViewPointManageService.deleteTfViewPointManage(removeId);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }
}
