package cn.ezhizao.project.system.controller;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.system.domain.SysModule;
import cn.ezhizao.project.system.domain.SysModuleDetail;
import cn.ezhizao.project.system.service.ISysMenuService;
import cn.ezhizao.project.system.service.ISysModuleDetailService;
import cn.ezhizao.project.system.service.ISysModuleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 模块管理Controller
 *
 * @author ruoyi
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/system/module")
public class SysModuleController extends BaseController
{
    @Resource
    private ISysModuleService sysModuleService;
    @Resource
    private ISysModuleDetailService sysModuleDetailService;
    @Resource
    private ISysMenuService sysMenuService;

    /**
     * 查询模块管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysModule sysModule) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(sysModule);
        startPage();
        List<SysModule> list = sysModuleService.getList(sysModule);
        return getDataTable(list);
    }

    /**
     * 导出模块管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:export')")
    @Log(title = "模块管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysModule sysModule) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(sysModule);
        List<SysModule> list = sysModuleService.getList(sysModule);
        ExcelUtil<SysModule> util = new ExcelUtil<SysModule>(SysModule.class);
        util.exportExcel(response, list, "模块管理数据");
    }

    /**
     * 获取模块管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:module:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysModule sysModule = sysModuleService.getById(id);
        SysModuleDetail query = new SysModuleDetail();
        query.setModuleId(sysModule.getId());
        List<SysModuleDetail> sysModuleDetail = sysModuleDetailService.getList(query);
        sysModule.setDetails(sysModuleDetail);
        return success(sysModule);
    }

    /**
     * 新增模块管理
     */
    @PreAuthorize("@ss.hasPermi('system:module:add')")
    @Log(title = "模块管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysModule sysModule) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(sysModule);
        return toAjax(sysModuleService.save(sysModule));
    }

    /**
     * 修改模块管理
     */
    @PreAuthorize("@ss.hasPermi('system:module:edit')")
    @Log(title = "模块管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysModule sysModule) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(sysModule);
        return toAjax(sysModuleService.updateById(sysModule));
    }

    /**
     * 删除模块管理
     */
    @PreAuthorize("@ss.hasPermi('system:module:remove')")
    @Log(title = "模块管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(sysModuleService.removeBatchByIds(ids));
    }

    @PostMapping("/getMenus")
    public AjaxResult getMenus() {
        return toAjax(true);
    }
}
