package com.ebts.generator.controller;

import java.io.Serializable;
import java.util.List;


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.StringUtils;
import com.ebts.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ebts.generator.entity.Module;
import com.ebts.generator.service.ModuleService;

/**
 * 模块管理Controller
 *
 * @author binlin
 * @date 2021-01-24
 */
@RestController
@RequestMapping("/tool/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;


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


    /**
     * 查询模块管理列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:module:list')")
    @GetMapping("/list")
    public Serializable list(Module module) {
        startPage();
        Result<List<Module>> result = moduleService.selectModuleList(module);
        if (result.isSuccess()) {
            return getDataTable(result.getData());
        } else {
            return result;
        }
    }

    /**
     * 导出模块管理列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:module:export')")
    @Log(title = "模块管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Serializable export(Module module) {
        Result<List<Module>> result = moduleService.selectModuleList(module);
        ExcelUtil<Module> util = new ExcelUtil<Module>(Module.class);
        if (result.isSuccess()) {
            return util.exportExcel(result.getData(), "module");
        } else {
            return result;
        }
    }

    /**
     * 获取模块管理详细信息
     */
    @PreAuthorize("@ebts.hasPermi('tool:module:query')")
    @GetMapping(value = "/{id}")
    public Result<Module> getInfo(@PathVariable("id") Long id) {
        return moduleService.selectModuleById(id);
    }

    /**
     * 新增模块管理
     */
    @PreAuthorize("@ebts.hasPermi('tool:module:add')")
    @Log(title = "模块管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Integer> add(@RequestBody @Validated Module module) {
        return moduleService.insertModule(module);
    }

    /**
     * 修改模块管理
     */
    @PreAuthorize("@ebts.hasPermi('tool:module:edit')")
    @Log(title = "模块管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Integer> edit(@RequestBody @Validated Module module) {
        if (StringUtils.isNull(module.getId())) {
            return Result.error("id不能为空!");
        }
        return moduleService.updateModule(module);
    }

    /**
     * 删除模块管理
     */
    @PreAuthorize("@ebts.hasPermi('tool:module:remove')")
    @Log(title = "模块管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable Long[] ids) {
        if (ids.length == 0) {
            return Result.error("id不能为空!");
        }
        return moduleService.deleteModuleByIds(ids);
    }
}
