/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.quality.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.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.quality.entity.ReportTypeD;
import com.tofly.quality.service.ReportTypeDService;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
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;
import java.util.Arrays;

/**
 * 报告类别
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/reporttyped")
@Api(tags = "报告类别接口")
public class ReportTypeDController {

    private final ReportTypeDService reportTypeDService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param reportTypeD 报告类别
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getReportTypeDPage(Page page, @ModelAttribute ReportTypeD reportTypeD) {
        return ResultRespone.success(reportTypeDService.page(page, Wrappers.query(reportTypeD)));
    }


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

    /**
     * 新增报告类别
     *
     * @param reportTypeD 报告类别
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增报告类别")
    @ApiOperation(value = "新增报告类别")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid ReportTypeD reportTypeD) {
        if(StringUtil.isEmpty(reportTypeD.getReportTypeName())){
            return ResultRespone.failed("报告名称不能为空");
        }
        if(reportTypeDService.selectByName(reportTypeD)){
            return ResultRespone.failed("报告名称已存在，不能重复创建");
        }
        return ResultRespone.success(reportTypeDService.save(reportTypeD));
    }

    /**
     * 修改报告类别
     *
     * @param reportTypeD 报告类别
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改报告类别")
    @ApiOperation(value = "修改报告类别")
    @PutMapping
    public ResultRespone updateById(@RequestBody @Valid ReportTypeD reportTypeD) {
        if(reportTypeD.getId()==null || reportTypeD.getId()==0){
            return ResultRespone.failed("参数id不能为空");
        }

        if(StringUtil.isEmpty(reportTypeD.getReportTypeName())){
            return ResultRespone.failed("报告名称不能为空");
        }
        if(reportTypeDService.selectByName(reportTypeD)){
            return ResultRespone.failed("报告名称已存在，不能修改");
        }
        return ResultRespone.success(reportTypeDService.updateById(reportTypeD));
    }

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