/*
 * 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.entity.TfPipeMaterialConfig;
import com.tofly.xrtygis.service.TfPipeMaterialConfigService;
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 javax.validation.Valid;
import java.util.Arrays;

/**
 * 管线预警配置
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tfPipeMaterialConfig")
@Api(tags = "管线预警配置接口")
public class TfPipeMaterialConfigController {

    private final TfPipeMaterialConfigService tfPipeMaterialConfigService;

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param tfPipeMaterialConfig 化验项配置
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getAssayConfigDPage(Page page, @ModelAttribute TfPipeMaterialConfig tfPipeMaterialConfig) {

        return ResultRespone.success(tfPipeMaterialConfigService.page(page, Wrappers.query(tfPipeMaterialConfig)));

    }


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

    /**
     * 新增管线预警配置
     *
     * @param tfPipeMaterialConfig 管线预警配置
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增管线预警配置")
    @ApiOperation(value = "新增管线预警配置")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid TfPipeMaterialConfig tfPipeMaterialConfig) {
       int count=tfPipeMaterialConfigService.count(Wrappers.<TfPipeMaterialConfig> lambdaQuery()
                .eq(TfPipeMaterialConfig::getMaterialCode,tfPipeMaterialConfig.getMaterialCode()));
       if(count>0){
           return ResultRespone.failed("管线编码："+tfPipeMaterialConfig.getMaterialCode()+"已存在，不能新增");
       }
          return ResultRespone.success(tfPipeMaterialConfigService.save(tfPipeMaterialConfig));

    }

    /**
     * 修改管线预警配置
     *
     * @param tfPipeMaterialConfig 管线预警配置
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改管线预警配置")
    @ApiOperation(value = "修改管线预警配置")
    @PutMapping
    public ResultRespone updateById(@RequestBody @Valid TfPipeMaterialConfig tfPipeMaterialConfig) {
        int count=tfPipeMaterialConfigService.count(Wrappers.<TfPipeMaterialConfig> lambdaQuery()
                .eq(TfPipeMaterialConfig::getMaterialCode,tfPipeMaterialConfig.getMaterialCode())
                .ne(TfPipeMaterialConfig::getId,tfPipeMaterialConfig.getId()));
        if(count>0){
            return ResultRespone.failed("管线编码："+tfPipeMaterialConfig.getMaterialCode()+"已存在，不能修改");
        }
        return ResultRespone.success(tfPipeMaterialConfigService.updateById(tfPipeMaterialConfig));
    }

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