package com.ebts.generator.controller;

import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.poi.ExcelUtil;
import com.ebts.generator.entity.ApiClass;
import com.ebts.generator.service.ApiClassService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 接口类名Controller
 *
 * @author binlin
 * @date 2021-01-24
 */
@RestController
@RequestMapping("/tool/apiclass")
public class ApiClassController extends BaseController {

    @Resource
    private ApiClassService apiClassService;


    /**
     * 获取到api类的select选项
     *
     * @return
     */
    @PreAuthorize("@ebts.hasAnyPermi('tool:apiclass:querylist')")
    @GetMapping("querylist")
    public Result<List<ApiClass>> queryList() {
        return apiClassService.selectApiclassList(new ApiClass());
    }

    /**
     * 查询接口类名列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:apiclass:list')")
    @GetMapping("/list")
    public Serializable list(ApiClass apiclass) {
        startPage();
        Result<List<ApiClass>> result = apiClassService.selectApiclassList(apiclass);
        if (result.isSuccess()) {
            return getDataTable(result.getData());
        } else {
            return result;
        }
    }

    /**
     * 导出接口类名列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:apiclass:export')")
    @Log(title = "接口类名", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Serializable export(ApiClass apiclass) {
        Result<List<ApiClass>> result = apiClassService.selectApiclassList(apiclass);
        ExcelUtil<ApiClass> util = new ExcelUtil<>(ApiClass.class);
        if (result.isSuccess()) {
            return util.exportExcel(result.getData(), "apiclass");
        } else {
            return result;
        }
    }

    /**
     * 获取接口类名详细信息
     */
    @PreAuthorize("@ebts.hasPermi('tool:apiclass:query')")
    @GetMapping(value = "/{id}")
    public Result<ApiClass> getInfo(@PathVariable("id") Long id) {
        return apiClassService.selectApiclassById(id);
    }

    /**
     * 新增接口类名
     */
    @PreAuthorize("@ebts.hasPermi('tool:apiclass:add')")
    @Log(title = "接口类名", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Integer> add(@RequestBody @Validated ApiClass apiclass) {
        return apiClassService.insertApiclass(apiclass);
    }

    /**
     * 修改接口类名
     */
    @PreAuthorize("@ebts.hasPermi('tool:apiclass:edit')")
    @Log(title = "接口类名", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Integer> edit(@RequestBody @Validated ApiClass apiclass) {
        if (apiclass.getmId() == null || apiclass.getmId() < 0) {
            return Result.error("模块id不能为空!");
        }
        return apiClassService.updateApiclass(apiclass);
    }

    /**
     * 删除接口类名
     */
    @PreAuthorize("@ebts.hasPermi('tool:apiclass:remove')")
    @Log(title = "接口类名", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable Long[] ids) {
        if (ids.length == 0) {
            return Result.error("id不能为空!");
        }
        return apiClassService.deleteApiclassByIds(ids);
    }
}
