package com.hzs.system.sys.controller.manage;

import java.util.List;
import java.util.Map;

import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.domain.system.sys.SysMenu;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hzs.common.core.constant.UserConstants;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.service.ISysMenuService;

import javax.servlet.http.HttpServletResponse;

/**
 * 菜单信息
 */
@RestController
@RequestMapping("/manage/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService iSysMenuService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * 获取菜单列表
     *
     * @param param
     * @return
     */
    @AccessPermissions("menuRole")
    @Log(module = EOperationModule.MENU_CONFIG, business = EOperationBusiness.MENU_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public AjaxResult list(SysMenu param) {
        List<SysMenu> menus = iSysMenuService.selectMenuList(param, SecurityUtils.getUserId());
        return AjaxResult.success(menus);
    }

    /**
     * 导出
     *
     * @param response
     * @param
     */
    @Log(module = EOperationModule.MENU_CONFIG, business = EOperationBusiness.MENU_CONFIG, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMenu param) {
        List<SysMenu> list = iSysMenuService.queryAllMenuAndColumn(param);
        //枚举翻译
        Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EShowStatus.values());
        list.forEach(itm ->
                itm.setVisibleVal(transactionMap.get(EnumsPrefixConstants.SHOW_STATUS + itm.getVisible()))
        );
        ExcelUtil<SysMenu> util = new ExcelUtil<>(SysMenu.class);
        util.exportExcel(response, list, "菜单配置");
    }


    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return AjaxResult.success(iSysMenuService.selectMenuById(menuId));
    }

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @Log(module = EOperationModule.MENU_CONFIG, business = EOperationBusiness.MENU_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMenu menu) {
        if (UserConstants.NOT_UNIQUE.equals(iSysMenuService.checkMenuNameUnique(menu))) {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(iSysMenuService.checkMenuPermsUnique(menu.getPerms(), menu.getMenuId()))) {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，权限标识已存在");
        }

        menu.setPkCreator(SecurityUtils.getUserId());
        return toAjax(iSysMenuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     *
     * @param menu
     * @return
     */

    @Log(module = EOperationModule.MENU_CONFIG, business = EOperationBusiness.MENU_CONFIG, method = EOperationMethod.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMenu menu) {
        if (null == menu.getMenuId()) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        if (UserConstants.NOT_UNIQUE.equals(iSysMenuService.checkMenuNameUnique(menu))) {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(iSysMenuService.checkMenuPermsUnique(menu.getPerms(), menu.getMenuId()))) {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，权限标识已存在");
        }
        if (menu.getMenuId().equals(menu.getParentId())) {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }

        menu.setPkModified(SecurityUtils.getUserId());
        return toAjax(iSysMenuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */

    @Log(module = EOperationModule.MENU_CONFIG, business = EOperationBusiness.MENU_CONFIG, method = EOperationMethod.DELETE)
    @DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId) {
        if (iSysMenuService.hasChildByMenuId(menuId)) {
            return AjaxResult.error("存在子菜单,不允许删除");
        }
        return toAjax(iSysMenuService.deleteMenuById(menuId, SecurityUtils.getUserId()));
    }

    /**
     * 查询全部菜单
     *
     * @return
     */
    @GetMapping("/all")
    public AjaxResult all() {
        List<SysMenu> menus = iSysMenuService.selectAllMenuList(new SysMenu(), SecurityUtils.getUserId());
        return AjaxResult.success(iSysMenuService.buildMenus(menus));
    }


    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/get-routers")
    public AjaxResult getRouters() {
        // 用户类型
        String userType = SecurityUtils.getMemberType();
        // 用户ID
        Long userId = SecurityUtils.getUserId();

        if (ELoginType.USER.getValue().equals(userType)) {
            // 管理员
            return AjaxResult.success(iSysMenuService.selectMenuRoutersByUserId(userId));
        }
        return AjaxResult.success();
    }

}
