/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.quality.controller;
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.quality.entity.SamplePlaceD;
import com.tofly.quality.entity.dto.SamplePlaceDQuery;
import com.tofly.quality.service.SamplePlaceDService;
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 javax.validation.Valid;


/**
 * 采样地点配置表
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sampleplaced")
@Api(tags = "采样地点配置表接口")
public class SamplePlaceDController {

    private final SamplePlaceDService samplePlaceDService;

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param query 采样地点配置表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getSamplePlaceDPage(Page page, @ModelAttribute SamplePlaceDQuery query) {
        return ResultRespone.success(samplePlaceDService.getPageData(page, query));
    }


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

    /**
     * 新增采样地点配置表
     *
     * @param samplePlaceD 采样地点配置表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增采样地点配置表")
    @ApiOperation(value = "新增采样地点配置表")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid SamplePlaceD samplePlaceD) {

        Boolean flag = samplePlaceDService.selectByName(samplePlaceD.getSamplePlaceName(), 0L);
        if (flag) {
            return ResultRespone.success(samplePlaceDService.save(samplePlaceD));

        }
        return ResultRespone.failed("采样点名称名称已存在，不能重复创建");
    }

    /**
     * 修改采样地点配置表
     *
     * @param samplePlaceD 采样地点配置表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改采样地点配置表")
    @ApiOperation(value = "修改采样地点配置表")
    @PutMapping
    public ResultRespone updateById(@RequestBody @Valid SamplePlaceD samplePlaceD) {

        if(samplePlaceD.getId()==null || samplePlaceD.getId()==0){
            return ResultRespone.failed("参数id不能为空");
        }

        Boolean flag = samplePlaceDService.selectByName(samplePlaceD.getSamplePlaceName(), samplePlaceD.getId());
        if (flag) {
            return ResultRespone.success(samplePlaceDService.updateById(samplePlaceD));

        }
        return ResultRespone.failed("采样点名称名称已存在，不能修改");

    }

    /**
     * 通过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(samplePlaceDService.deleteByIds(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(samplePlaceDService.deleteByIds(ids));
    }
}
