package com.bweb.controller;

import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.SecMenu;
import com.hfkj.entity.SecPermission;
import com.hfkj.entity.SecRole;
import com.hfkj.model.CommonTreeModel;
import com.hfkj.model.ResponseData;
import com.hfkj.service.SecMenuService;
import com.hfkj.service.SecPermissionService;
import com.hfkj.service.SecRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/secMenu")
@Api(value = "菜单管理")
public class SecMenuController {

    private static Logger log = LoggerFactory.getLogger(SecMenuController.class);

    @Resource
    private SecMenuService secMenuService;
    @Resource
    private SecRoleService secRoleService;
    @Resource
    private SecPermissionService secPermissionService;


    @RequestMapping(value = "/getMenuList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取菜单列表")
    public ResponseData getMenuList(String menuName, Integer pageNum, Integer pageSize) {
        try {
            if (pageNum == null || pageSize == null) {
                log.error("SecMenuController --> getMenuList() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            PageInfo<SecMenu> menuList = secMenuService.findPage(menuName,pageNum,pageSize);
            return ResponseMsgUtil.success(menuList);
        } catch (Exception e) {
            log.error("SecMenuController --> getMenuList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/addMenu", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加菜单")
    public ResponseData addMenu(@RequestBody SecMenu secMenu) {
        try {
            if (StringUtils.isBlank(secMenu.getMenuName())
                    || secMenu.getMenuSort() == null
                    || secMenu.getMenuType() == null
                    || secMenu.getShowOnMobile() == null
                    || secMenu.getShowOnMobile() == 1 && StringUtils.isBlank(secMenu.getMenuMobileUrl())
                    || secMenu.getShowOnMobile() == 0 && StringUtils.isBlank(secMenu.getMenuUrl())) {
                log.error("SecMenuController --> addMenu() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 未校验重复（名称和路由）
            // 按钮父级菜单
            if (secMenu.getMenuType() == 1){
                if(secMenu.getMenuPSid() == null) {
                    log.error("SecMenuController --> addMenu() error!", "参数错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }
            // 添加权限
            SecPermission permission = new SecPermission();
            permission.setSort(secMenu.getMenuSort());
            permission.setPermissionName(secMenu.getMenuName());
            permission.setPermissionDesc(secMenu.getMenuDesc());

            if(secMenu.getShowOnMobile() == 0){
                //判断电脑端菜单还是按钮
                if (secMenu.getMenuType() == 0){
                    permission.setPermissionCode("MENU_" + secMenu.getMenuUrl());
                }else if (secMenu.getMenuType() == 1){
                    permission.setPermissionCode("BTN_" + secMenu.getMenuUrl());
                }else {
                    log.error("SecMenuController --> addMenu() error!", "菜单类型错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }else if(secMenu.getShowOnMobile() == 1){
                //判断手机端菜单还是按钮
                if (secMenu.getMenuType() == 0){
                    permission.setPermissionCode("MENU_" + secMenu.getMenuMobileUrl());
                }else if (secMenu.getMenuType() == 1){
                    permission.setPermissionCode("BTN_" + secMenu.getMenuMobileUrl());
                }else {
                    log.error("SecMenuController --> addMenu() error!", "菜单类型错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }
            secMenuService.addMenu(secMenu,permission);
            return ResponseMsgUtil.success(null);
        } catch (Exception e) {
            log.error("SecMenuController --> addMenu() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/editMenu", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改菜单信息")
    public ResponseData editMenu(@RequestBody SecMenu secMenu) {
        try {
            if (secMenu.getId() == null
                    || StringUtils.isBlank(secMenu.getMenuName())
                    || secMenu.getMenuSort() == null
                    || secMenu.getMenuType() == null
                    || secMenu.getShowOnMobile() == null
                    || secMenu.getShowOnMobile() == 1 && StringUtils.isBlank(secMenu.getMenuMobileUrl())
                    || secMenu.getShowOnMobile() == 0 && StringUtils.isBlank(secMenu.getMenuUrl())) {
                log.error("SecMenuController --> addMenu() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询菜单
            SecMenu secMenu2 = secMenuService.findById(secMenu.getId());

            if (secMenu.getMenuType() != secMenu2.getMenuType()) {
                log.error("SecMenuController --> editMenu() error!", "菜单类型不能更改");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TYPE_IS_NOT_EDIT_ERROR, "");
            }

            // 按钮父级菜单
            if (secMenu.getMenuType() == 1){
                if(secMenu.getMenuPSid() == null) {
                    log.error("SecMenuController --> addMenu() error!", "参数错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }

            // 查询菜单对应权限
            String newCode = "";

            if(secMenu.getShowOnMobile() == 0){
                if (secMenu.getMenuType() == 0) {
                    newCode = "MENU_" + secMenu.getMenuUrl();
                }else if (secMenu.getMenuType() == 1) {
                    newCode = "BTN_" + secMenu.getMenuUrl();
                }else {
                    log.error("SecMenuController --> addMenu() error!", "菜单类型错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }else if(secMenu.getShowOnMobile() == 1){
                if (secMenu.getMenuType() == 0) {
                    newCode = "MENU_" + secMenu.getMenuMobileUrl();
                }else if (secMenu.getMenuType() == 1) {
                    newCode = "BTN_" + secMenu.getMenuMobileUrl();
                }else {
                    log.error("SecMenuController --> addMenu() error!", "菜单类型错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }

            SecPermission permission = secPermissionService.findByMenuId(secMenu.getId());
            permission.setSort(secMenu.getMenuSort());
            permission.setPermissionName(secMenu.getMenuName());
            permission.setPermissionDesc(secMenu.getMenuDesc());
            permission.setPermissionCode(newCode);
            permission.setMenuId(secMenu.getId());
            secMenuService.editMenu(secMenu,permission);
            return ResponseMsgUtil.success(null);
        } catch (Exception e) {
            log.error("SecMenuController --> editMenu() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getAllMenu", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取所有菜单")
    public ResponseData getAllMenu() {
        try {
            List<SecMenu> menuList = secMenuService.findAll();
            return ResponseMsgUtil.success(menuList);
        } catch (Exception e) {
            log.error("SecMenuController --> getAllMenu() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/deleteMenu", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除菜单")
    public ResponseData deleteMenu(String menuIds) {
        try {
            if (StringUtils.isBlank(menuIds)) {
                log.error("SecMenuController --> deleteMenu() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            secMenuService.deleteMenuList(menuIds);
            return ResponseMsgUtil.success(null);
        } catch (Exception e) {
            log.error("SecMenuController --> deleteMenu() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/findMenuTree",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询菜单按钮树")
    public ResponseData findMenuTree(Long roleId,Integer roleType,Long allotRoleId){
        try {
            if(roleType == null || allotRoleId == null) {
                log.error("SecMenuController --> findMenuTree() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            //判断是超级管理员还是普通用户
            if (roleType == 0) {
                //超级管理员查询所有菜单树
                List<CommonTreeModel> treeList = secPermissionService.findAllMenuTree(allotRoleId);
                return ResponseMsgUtil.success(treeList);
            }else {

                if(roleId == null){
                    log.error("SecMenuController --> findMenuTree() error!","参数错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }

                //根据角色id查询公司主角色
                SecRole secRole = secRoleService.findById(roleId);

                //普通用户根据用户所在公司主角色查询菜单树
                List<CommonTreeModel> treeList = secPermissionService.findMenuTreeByRoleId(secRole.getId(),allotRoleId);
                return ResponseMsgUtil.success(treeList);
            }
        } catch (Exception e) {
            log.error("chargeController --> chargeItem() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/findAllMenuTree",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询全部菜单按钮树")
    public ResponseData findAllMenuTree(@RequestParam(value = "showOnMobile", required = true) Integer showOnMobile){
        try {

            List<CommonTreeModel> treeList = secPermissionService.getAllMenuTree(showOnMobile);
            return ResponseMsgUtil.success(treeList);

        } catch (Exception e) {
            log.error("chargeController --> findAllMenuTree() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/findMenuById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据ID查询菜单信息")
    public ResponseData findMenuById(@RequestParam(name = "menuId", required = true) Long menuId){
        try {

            return ResponseMsgUtil.success(secMenuService.findById(menuId));

        } catch (Exception e) {
            log.error("chargeController --> findMenuById() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
