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

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.LeakRecordService;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.plan.LeakRecord;
import lombok.AllArgsConstructor;
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 java.util.Arrays;
import java.util.List;

/**
 * 探漏记录表
 *
 * @author admin
 * @date Tue Oct 26 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/leakrecord")
@Api(tags="探漏记录表接口")
public class LeakRecordController {

    private LeakRecordService leakRecordService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param leakRecord 探漏记录表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getLeakRecordPage(Page page, @ModelAttribute LeakRecord leakRecord) {
      return ResultRespone.success(leakRecordService.page(page, Wrappers.query(leakRecord)));
    }


    /**
     * 通过id查询探漏记录表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(leakRecordService.getById(id));
    }

    /**
     * 新增探漏记录表
     * @param leakRecord 探漏记录表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增探漏记录表" )
    @ApiOperation(value = "新增探漏记录表")
    @PostMapping
    public ResultRespone save(@RequestParam(value = "leakSourceFile", required = false) List<MultipartFile> leakSourceFile, @RequestParam(value = "excavationFile", required = false) List<MultipartFile> excavationFile, LeakRecord leakRecord) {
        String currentDate = DateTimeUtil.getCurrentDate(DateTimeUtil.DAY_FORMAT);
        List<LeakRecord> list = leakRecordService.list();
        String fir = currentDate;
        if(null!=list && list.size()>0){
            int size = list.size();
            int i = size + 1;
            fir = fir + Constant.DATE_SEPARATION +i;
        }else {
            fir = fir + Constant.DATE_SEPARATION +0;
        }
        leakRecord.setLeakSourceCode(fir);
        return ResultRespone.success(leakRecordService.savaLeakRecordInfo(leakSourceFile,excavationFile,leakRecord));
    }

    /**
     * 修改探漏记录表
     * @param leakRecord 探漏记录表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改探漏记录表" )
    @ApiOperation(value = "修改探漏记录表")
    @PutMapping
    public ResultRespone updateById(@RequestBody LeakRecord leakRecord) {
      return ResultRespone.success(leakRecordService.updateById(leakRecord));
    }


    /**
     * 通过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(leakRecordService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
