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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.quality.entity.AssayConfigD;
import com.tofly.quality.entity.dto.AssayConfigDQuery;
import com.tofly.quality.service.AssayConfigDService;
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("/assayconfigd")
@Api(tags = "化验项配置接口")
public class AssayConfigDController {

    private final AssayConfigDService assayConfigDService;

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

        return ResultRespone.success(assayConfigDService.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(assayConfigDService.getById(id));
    }

    /**
     * 新增化验项配置
     *
     * @param assayConfigD 化验项配置
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增化验项配置")
    @ApiOperation(value = "新增化验项配置")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid AssayConfigD assayConfigD) {

        Boolean flag = assayConfigDService.selectByName(assayConfigD.getAssayConfigName(), 0L);
        if (flag) {
            return ResultRespone.success(assayConfigDService.saveConfig(assayConfigD,true));

        }
        return ResultRespone.failed("化验配置名称名称已存在，不能重复创建");

    }

    /**
     * 修改化验项配置
     *
     * @param assayConfigD 化验项配置
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改化验项配置")
    @ApiOperation(value = "修改化验项配置")
    @PutMapping
    public ResultRespone updateById(@RequestBody @Valid AssayConfigD assayConfigD) {
        boolean flag=true;

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

        if (!StringUtils.isNullOrEmpty(assayConfigD.getAssayConfigName())){
         flag = assayConfigDService.selectByName(assayConfigD.getAssayConfigName(), assayConfigD.getId());
      }
        if (flag) {
            return ResultRespone.success(assayConfigDService.saveConfig(assayConfigD,false));

        }
        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(assayConfigDService.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(assayConfigDService.deleteByIds(ids));
    }
}
