package com.hchyun.web.controller.system;

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


import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.core.controller.HcyBaseController;
import com.hchyun.common.utils.ServerResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
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.hchyun.common.annotation.Log;
import com.hchyun.common.core.entity.AjaxResult;
import com.hchyun.common.enums.BusinessType;
import com.hchyun.system.entity.Module;
import com.hchyun.system.service.ModuleService;
import com.hchyun.common.utils.poi.ExcelUtil;
import com.hchyun.common.core.page.TableDataInfo;

/**
 * 模块管理Controller
 * 
 * @author hchyun
 * @date 2021-01-24
 */
@Api(value = "模块管理管理",tags = "模块管理管理")
@RestController
@RequestMapping("/system/module")
public class ModuleController extends HcyBaseController {
    protected final Logger logger = LoggerFactory.getLogger(ModuleController.class);

    @Autowired
    private ModuleService moduleService;

    /**
     * 查询模块管理列表
     */
    @ApiOperation("查询模块管理列表")
    @PreAuthorize("@ss.hasPermi('system:module:list')")
    @GetMapping("/list")
    public Serializable list(Module module) {
        try {
            startPage();
            ServerResult<List<Module>> serverResult = moduleService.selectModuleList(module);
            if (serverResult.isStart()) {
                return getDataTable(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 导出模块管理列表
     */
    @ApiOperation("导出模块管理列表")
    @PreAuthorize("@ss.hasPermi('system:module:export')")
    @Log(title = "模块管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Module module) {
        try {
            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());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 获取模块管理详细信息
     */
    @ApiOperation("获取模块管理详细信息")
    @ApiImplicitParam(name = "id" , value = "模块管理id" , required = true, dataType = "Long" , paramType = "path")
    @PreAuthorize("@ss.hasPermi('system:module:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            ServerResult<Module> serverResult = moduleService.selectModuleById(id);
            if (serverResult.isStart()) {
                return AjaxResult.success(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 新增模块管理
     */
    @ApiOperation("新增模块管理")
    @ApiImplicitParam(name = "module" , value = "新增模块管理信息" , dataType = "Module")
    @PreAuthorize("@ss.hasPermi('system: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("模块描述不能为空!");
        }
        try {
            ServerResult<Integer> serverResult = moduleService.insertModule(module);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 修改模块管理
     */
    @ApiOperation("修改模块管理")
    @ApiImplicitParam(name = "module" , value = "修改模块管理信息" , dataType = "Module")
    @PreAuthorize("@ss.hasPermi('system:module:edit')")
    @Log(title = "模块管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Module module) {
        try {

            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());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 删除模块管理
     */
    @ApiOperation("删除模块管理")
    @ApiImplicitParam(name = "ids" , value = "模块管理ids" , required = true, dataType = "Long" , paramType = "path")
    @PreAuthorize("@ss.hasPermi('system:module:remove')")
    @Log(title = "模块管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        try {
            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());
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }
}
