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

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.quality.entity.AssayDicD;
import com.tofly.quality.entity.dto.AssayDicDQuery;
import com.tofly.quality.service.AssayDicDService;
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("/assaydicd")
@Api(tags = "化验项字典接口")
public class AssayDicDController {

    private final AssayDicDService assayDicDService;

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param query 化验项字典
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getAssayDicDPage(Page page, @ModelAttribute AssayDicDQuery query) {
        return ResultRespone.success(assayDicDService.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(assayDicDService.getById(id));
    }

    /**
     * 新增化验项字典
     *
     * @param assayDicD 化验项字典
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增化验项字典")
    @ApiOperation(value = "新增化验项字典")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid AssayDicD assayDicD) {
        Boolean flag = assayDicDService.selectByName(assayDicD.getAssayDicName(), 0L);
        if (flag) {
            return ResultRespone.success(assayDicDService.save(assayDicD));

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

    }

    /**
     * 修改化验项字典
     *
     * @param assayDicD 化验项字典
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改化验项字典")
    @ApiOperation(value = "修改化验项字典")
    @PutMapping
    public ResultRespone updateById(@RequestBody @Valid AssayDicD assayDicD) {
        if(assayDicD.getId()==null || assayDicD.getId()==0){
            return ResultRespone.failed("参数id不能为空");
        }
        Boolean flag = assayDicDService.selectByName(assayDicD.getAssayDicName(), assayDicD.getId());
        if (flag) {
            return ResultRespone.success(assayDicDService.updateById(assayDicD));

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