package com.bonus.system.controller;

import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.InnerAuth;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.annotation.RequiresPermissionsOrInnerAuth;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.SysMenu;
import com.bonus.system.api.domain.SysTemplate;
import com.bonus.system.api.domain.SysTemplateMenu;
import com.bonus.system.service.ISysMenuService;
import com.bonus.system.service.ISysTemplateMenuService;
import com.bonus.system.service.ISysTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 权限模板Controller
 *
 * @author bonus
 * @date 2025-01-05
 */
@RestController
@RequestMapping("/template")
@Slf4j
public class SysTemplateController extends BaseController {

    @Autowired
    private ISysTemplateService sysTemplateService;

    @Autowired
    private ISysTemplateMenuService sysTemplateMenuService;

    @Autowired
    private ISysMenuService menuService;

    /**
     * 查询权限模板列表
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:template:list"))
    @SysLog(title = "权限模板管理", businessType = OperaType.EXPORT,logType = 0,module = "公司管理->权限模板管理")
    @GetMapping("/list")
    public TableDataInfo list(SysTemplate sysTemplate) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ServiceException("抱歉,您无权查看公司权限列表!");
        }
        startPage();
        List<SysTemplate> list = sysTemplateService.selectSysTemplateList(sysTemplate);
        return getDataTable(list);
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/templateMenuTreeselect/{tempId}")
    public AjaxResult templateMenuTreeselect(@PathVariable("tempId") Long tempId) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ServiceException("抱歉,您无权查看公司权限菜单列表!");
        }
        try{
            Long userId = SecurityUtils.getUserId();
            List<SysMenu> menus = menuService.selectMenuList(userId);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("checkedKeys", sysTemplateMenuService.queryMenuIdByTemplateId(tempId));
            ajax.put("menus", menuService.buildMenuTreeSelect(menus));
            return ajax;
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 导出权限模板列表
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:template:list"))
    @SysLog(title = "权限模板管理", businessType = OperaType.EXPORT,logType = 0,module = "公司管理->权限模板管理")
    @GetMapping("/export")
    public void export(HttpServletResponse response, SysTemplate sysTemplate) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ServiceException("抱歉,您无权查看公司权限列表!");
        }
        List<SysTemplate> list = sysTemplateService.selectSysTemplateList(sysTemplate);
        ExcelUtil<SysTemplate> util = new ExcelUtil<SysTemplate>(SysTemplate.class);
        util.exportExcel(response, list, "template");
    }

    /**
     * 获取权限模板详细信息
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:template:list"))
    @SysLog(title = "权限模板管理", businessType = OperaType.EXPORT,logType = 0,module = "公司管理->权限模板管理")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ServiceException("抱歉,您无权查看公司权限列表!");
        }
        return AjaxResult.success(sysTemplateService.selectSysTemplateById(id));
    }

    /**
     * 新增权限模板
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:template:add"))
    @SysLog(title = "权限模板管理", businessType = OperaType.INSERT,logType = 0,module = "公司管理->权限模板管理")
    @PostMapping
    public AjaxResult add(@RequestBody SysTemplate sysTemplate) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ServiceException("抱歉,您无权添加公司权限列表!");
        }
        return toAjax(sysTemplateService.insertSysTemplate(sysTemplate));
    }

    /**
     * 修改权限模板
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:template:edit"))
    @SysLog(title = "权限模板管理", businessType = OperaType.UPDATE,logType = 0,module = "公司管理->权限模板管理")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysTemplate sysTemplate) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ServiceException("抱歉,您无权更新公司权限列表!");
        }
        return toAjax(sysTemplateService.updateSysTemplate(sysTemplate));
    }

    /**
     * 删除权限模板
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:template:edit"))
    @SysLog(title = "权限模板管理", businessType = OperaType.DELETE,logType = 0,module = "公司管理->权限模板管理")
    @PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ServiceException("抱歉,您无权删除公司权限列表!");
        }
        return toAjax(sysTemplateService.deleteSysTemplateByIds(ids));
    }
}
