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

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.xrtygis.entity.TfLossCensus;
import com.tofly.xrtygis.service.TfLossCensusService;
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 java.util.Arrays;

/**
 * 损耗统计记录表
 *
 * @author JF
 * @date Mon Jun 20 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tflosscensus")
@Api(tags="损耗统计记录表接口")
public class TfLossCensusController {

    private final  TfLossCensusService tfLossCensusService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param tfLossCensus 损耗统计记录表
    * @return
    */
    @ToFlyAppLog(title="分页查询" )
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getTfLossCensusPage(Page page, @ModelAttribute TfLossCensus tfLossCensus) {
      return ResultRespone.success(tfLossCensusService.page(page, Wrappers.query(tfLossCensus)));
    }


    /**
     * 通过id查询损耗统计记录表
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过ID查询" )
    @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(tfLossCensusService.getById(id));
    }

    /**
     * 新增损耗统计记录表
     * @param tfLossCensus 损耗统计记录表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增损耗统计记录表" )
    @ApiOperation(value = "新增损耗统计记录表")
    @PostMapping
    public ResultRespone save(@RequestBody TfLossCensus tfLossCensus) {
       int count=tfLossCensusService.count(
                new QueryWrapper<TfLossCensus>().lambda().
                        eq(TfLossCensus::getType,tfLossCensus.getType())
                        .eq(TfLossCensus::getName,tfLossCensus.getName())
                        .eq(TfLossCensus::getFillingType,tfLossCensus.getFillingType())
                        .eq(TfLossCensus::getFillingDate,tfLossCensus.getFillingDate()));
       if(count>0){
           String errMsg="名称："+tfLossCensus.getName()+"已存在，不能重复添加";
           return ResultRespone.failed(errMsg);
       }
      return ResultRespone.success(tfLossCensusService.save(tfLossCensus));
    }

    /**
     * 修改损耗统计记录表
     * @param tfLossCensus 损耗统计记录表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改损耗统计记录表" )
    @ApiOperation(value = "修改损耗统计记录表")
    @PutMapping
    public ResultRespone updateById(@RequestBody TfLossCensus tfLossCensus) {
        int count=tfLossCensusService.count(
                new QueryWrapper<TfLossCensus>().lambda().
                        eq(TfLossCensus::getType,tfLossCensus.getType())
                        .eq(TfLossCensus::getName,tfLossCensus.getName())
                        .eq(TfLossCensus::getFillingType,tfLossCensus.getFillingType())
                        .eq(TfLossCensus::getFillingDate,tfLossCensus.getFillingDate())
                        .ne(TfLossCensus::getId,tfLossCensus.getId()));
        if(count>0){
            String errMsg="名称："+tfLossCensus.getName()+"已存在，不能修改";
            return ResultRespone.failed(errMsg);
        }
      return ResultRespone.success(tfLossCensusService.updateById(tfLossCensus));
    }

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