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.AjaxResult;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.ServerResult;
import com.ebts.common.utils.poi.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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 {
    protected final Logger logger = LoggerFactory.getLogger(ModuleController.class);

    @Autowired
    private ModuleService moduleService;


    /**
     * 获取到api类的select选项
     *
     * @return
     */
    @PreAuthorize("@ebts.hasAnyPermi('tool:module:querylist')")
    @GetMapping("querylist")
    public AjaxResult queryList() {
        ServerResult<List<Module>> ServerResult = moduleService.selectModuleList(new Module());
        if (ServerResult.isStart()) {
            return AjaxResult.success(ServerResult.getData());
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }


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

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

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

    /**
     * 新增模块管理
     */
    @PreAuthorize("@ebts.hasPermi('tool:module:add')")
    @Log(title = "模块管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Module module) {
        if (module.getmName() == null || module.getmName().equals("")) {
            return AjaxResult.error("模块名称不能为空!");
        }
        if (module.getmDescribe() == null || module.getmDescribe().equals("")) {
            return AjaxResult.error("模块描述不能为空!");
        }
        ServerResult<Integer> ServerResult = moduleService.insertModule(module);
        if (ServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }

    /**
     * 修改模块管理
     */
    @PreAuthorize("@ebts.hasPermi('tool:module:edit')")
    @Log(title = "模块管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Module module) {
        if (module.getmName() == null || module.getmName().equals("")) {
            return AjaxResult.error("模块名称不能为空!");
        }
        if (module.getmDescribe() == null || module.getmDescribe().equals("")) {
            return AjaxResult.error("模块描述不能为空!");
        }
        ServerResult<Integer> ServerResult = moduleService.updateModule(module);
        if (ServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }

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