package com.gic.haoban.manage.service.service.out.impl.role;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.app.customer.service.api.service.ManageSettingApiService;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.RolesListUtils;
import com.gic.haoban.manage.api.dto.ClerkEditInfoDTO;
import com.gic.haoban.manage.api.dto.TemplateEnterpriseRelatedDTO;
import com.gic.haoban.manage.api.dto.qdto.role.HaobanRoleMenuQDTO;
import com.gic.haoban.manage.api.dto.qdto.role.HaobanRoleQDTO;
import com.gic.haoban.manage.api.dto.role.HaobanMenuDTO;
import com.gic.haoban.manage.api.dto.role.HaobanRoleDTO;
import com.gic.haoban.manage.api.enums.role.RoleClerkTypeEnum;
import com.gic.haoban.manage.api.service.ApplicationSettingApiService;
import com.gic.haoban.manage.api.service.role.HaobanRoleApiService;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanTemplateApplicationRelatedMapper;
import com.gic.haoban.manage.service.entity.TabHaobanWxEnterpriseRelated;
import com.gic.haoban.manage.service.entity.role.TabHaobanRole;
import com.gic.haoban.manage.service.entity.role.TabHaobanRoleMenu;
import com.gic.haoban.manage.service.pojo.bo.role.HaobanMenuBO;
import com.gic.haoban.manage.service.pojo.bo.role.HaobanRoleBO;
import com.gic.haoban.manage.service.service.TemplateEnterpriseRelatedService;
import com.gic.haoban.manage.service.service.WxEnterpriseRelatedService;
import com.gic.haoban.manage.service.service.role.HaobanMenuService;
import com.gic.haoban.manage.service.service.role.HaobanRoleMenuService;
import com.gic.haoban.manage.service.service.role.HaobanRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class HaobanRoleApiServiceImpl implements HaobanRoleApiService {

    private static final Logger logger = LogManager.getLogger(HaobanRoleApiServiceImpl.class);

    @Autowired
    private HaobanRoleService haobanRoleService;
    @Autowired
    private HaobanRoleMenuService haobanRoleMenuService;
    @Autowired
    private ManageSettingApiService manageSettingApiService;
    @Autowired
    private WxEnterpriseRelatedService wxEnterpriseRelatedService;
    @Autowired
    private ApplicationSettingApiService applicationSettingApiService;
    @Autowired
    private HaobanMenuService haobanMenuService;
    @Autowired
    private TabHaobanTemplateApplicationRelatedMapper tabHaobanTemplateApplicationRelatedMapper;
    @Autowired
    private TemplateEnterpriseRelatedService templateEnterpriseRelatedService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResponse add(HaobanRoleQDTO haobanRoleQDTO, WebLoginDTO login) {
        HaobanRoleMenuQDTO clerkMenus = haobanRoleQDTO.getClerkMenus();
        HaobanRoleMenuQDTO manegeClerkMenus = haobanRoleQDTO.getManegeClerkMenus();
        HaobanRoleMenuQDTO managerMenus = haobanRoleQDTO.getManagerMenus();
        String wxEnterpriseId = login.getWxEnterpriseId();
        String clerkId = login.getClerkId();
        String clerkName = login.getClerkName();
        List<TabHaobanRole> haobanRoles = new ArrayList<>();
        List<TabHaobanRoleMenu> tabHaobanRoleMenus = new ArrayList<>();
        Date now = new Date();
        //导购
        if (clerkMenus != null) {
            TabHaobanRole tabHaobanRole = getTabHaobanRole(haobanRoleQDTO, wxEnterpriseId, clerkId, clerkName, haobanRoles, RoleClerkTypeEnum.CLERK.getCode());
            List<String> menuCodes = clerkMenus.getMenuCodes();
            for (String menuCode : menuCodes) {
                getTabHaobanRoleMenu(wxEnterpriseId, tabHaobanRoleMenus, now, tabHaobanRole, menuCode);
            }
        }

        //店长
        if (manegeClerkMenus != null) {
            TabHaobanRole tabHaobanRole = getTabHaobanRole(haobanRoleQDTO, wxEnterpriseId, clerkId, clerkName, haobanRoles, RoleClerkTypeEnum.SHOP.getCode());
            List<String> menuCodes = manegeClerkMenus.getMenuCodes();
            for (String menuCode : menuCodes) {
                getTabHaobanRoleMenu(wxEnterpriseId, tabHaobanRoleMenus, now, tabHaobanRole, menuCode);
            }
        }

        //区经
        if (managerMenus != null) {
            TabHaobanRole tabHaobanRole = getTabHaobanRole(haobanRoleQDTO, wxEnterpriseId, clerkId, clerkName, haobanRoles, RoleClerkTypeEnum.MANAGE.getCode());
            List<String> menuCodes = managerMenus.getMenuCodes();
            for (String menuCode : menuCodes) {
                getTabHaobanRoleMenu(wxEnterpriseId, tabHaobanRoleMenus, now, tabHaobanRole, menuCode);
            }
        }
        haobanRoleService.insertBatch(haobanRoles);
        haobanRoleMenuService.insertBatch(tabHaobanRoleMenus);
        return ServiceResponse.success();
    }

    private void getTabHaobanRoleMenu(String wxEnterpriseId, List<TabHaobanRoleMenu> tabHaobanRoleMenus, Date now, TabHaobanRole tabHaobanRole, String menuCode) {
        TabHaobanRoleMenu tabHaobanRoleMenu = new TabHaobanRoleMenu();
        tabHaobanRoleMenu.setRoleMenuId(UniqueIdUtils.uniqueLong());
        tabHaobanRoleMenu.setRoleId(tabHaobanRole.getRoleId());
        tabHaobanRoleMenu.setWxEnterpriseId(wxEnterpriseId);
        tabHaobanRoleMenu.setMenuCode(menuCode);
        tabHaobanRoleMenu.setStatusFlag(1);
        tabHaobanRoleMenu.setCreateTime(now);
        tabHaobanRoleMenu.setUpdateTime(now);
        tabHaobanRoleMenus.add(tabHaobanRoleMenu);
    }

    private TabHaobanRole getTabHaobanRole(HaobanRoleQDTO haobanRoleQDTO, String wxEnterpriseId, String clerkId, String clerkName, List<TabHaobanRole> haobanRoles, int clerkType) {
        TabHaobanRole tabHaobanRole = new TabHaobanRole();
        tabHaobanRole.setRoleId(UniqueIdUtils.uniqueLong());
        tabHaobanRole.setRoleName(haobanRoleQDTO.getRoleName());
        tabHaobanRole.setClerkType(clerkType);
        tabHaobanRole.setWxEnterpriseId(wxEnterpriseId);
        tabHaobanRole.setStatusFlag(1);
        tabHaobanRole.setSystemFlag(haobanRoleQDTO.getSystemFlag());
        tabHaobanRole.setCreatorId(clerkId);
        tabHaobanRole.setCreatorName(clerkName);
        haobanRoles.add(tabHaobanRole);
        return tabHaobanRole;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResponse edit(HaobanRoleQDTO haobanRoleQDTO, WebLoginDTO login) {
        HaobanRoleMenuQDTO clerkMenus = haobanRoleQDTO.getClerkMenus();
        HaobanRoleMenuQDTO manegeClerkMenus = haobanRoleQDTO.getManegeClerkMenus();
        HaobanRoleMenuQDTO managerMenus = haobanRoleQDTO.getManagerMenus();
        Integer systemFlag = haobanRoleQDTO.getSystemFlag();
        String wxEnterpriseId = login.getWxEnterpriseId();
        String clerkId = login.getClerkId();
        String clerkName = login.getClerkName();
        List<TabHaobanRoleMenu> tabHaobanRoleMenus = new ArrayList<>();
        Date now = new Date();
        //导购
        if (clerkMenus != null) {
            TabHaobanRole tabHaobanRole = updateTabHaobanRole(haobanRoleQDTO, clerkMenus, wxEnterpriseId, clerkId, clerkName, now);
            List<String> menuCodes = clerkMenus.getMenuCodes();
            for (String menuCode : menuCodes) {
                getTabHaobanRoleMenu(wxEnterpriseId, tabHaobanRoleMenus, now, tabHaobanRole, menuCode);
            }
            if (systemFlag == 0 && !"-1".equals(wxEnterpriseId)) {
                List<String> delMenuCodes = clerkMenus.getDelMenuCodes();
                for (String delMenuCode : delMenuCodes) {
                    haobanRoleMenuService.deleteByMenuCode(delMenuCode, wxEnterpriseId);
                }
            }
        }

        //店长
        if (manegeClerkMenus != null) {
            TabHaobanRole tabHaobanRole = updateTabHaobanRole(haobanRoleQDTO, manegeClerkMenus, wxEnterpriseId, clerkId, clerkName, now);
            List<String> menuCodes = manegeClerkMenus.getMenuCodes();
            for (String menuCode : menuCodes) {
                getTabHaobanRoleMenu(wxEnterpriseId, tabHaobanRoleMenus, now, tabHaobanRole, menuCode);
            }
            if (systemFlag == 0 && !"-1".equals(wxEnterpriseId)) {
                List<String> delMenuCodes = manegeClerkMenus.getDelMenuCodes();
                for (String delMenuCode : delMenuCodes) {
                    haobanRoleMenuService.deleteByMenuCode(delMenuCode, wxEnterpriseId);
                }
            }
        }

        //区经
        if (managerMenus != null) {
            TabHaobanRole tabHaobanRole = updateTabHaobanRole(haobanRoleQDTO, managerMenus, wxEnterpriseId, clerkId, clerkName, now);
            List<String> menuCodes = managerMenus.getMenuCodes();
            for (String menuCode : menuCodes) {
                getTabHaobanRoleMenu(wxEnterpriseId, tabHaobanRoleMenus, now, tabHaobanRole, menuCode);
            }
            if (systemFlag == 0 && !"-1".equals(wxEnterpriseId)) {
                List<String> delMenuCodes = managerMenus.getDelMenuCodes();
                for (String delMenuCode : delMenuCodes) {
                    haobanRoleMenuService.deleteByMenuCode(delMenuCode, wxEnterpriseId);
                }
            }
        }
        haobanRoleMenuService.insertBatch(tabHaobanRoleMenus);
        return ServiceResponse.success();
    }

    private TabHaobanRole updateTabHaobanRole(HaobanRoleQDTO haobanRoleQDTO, HaobanRoleMenuQDTO managerMenus, String wxEnterpriseId, String clerkId, String clerkName, Date now) {
        Long roleId = managerMenus.getRoleId();
        if (roleId == null) {
            logger.error("roleId为空");
            return null;
        }
        //删除已删除的权限
        haobanRoleMenuService.deleteByRoleId(roleId);
        TabHaobanRole tabHaobanRole = new TabHaobanRole();
        tabHaobanRole.setRoleId(roleId);
        tabHaobanRole.setRoleName(haobanRoleQDTO.getRoleName());
        tabHaobanRole.setWxEnterpriseId(wxEnterpriseId);
        tabHaobanRole.setModifierId(clerkId);
        tabHaobanRole.setModifierName(clerkName);
        tabHaobanRole.setStatusFlag(1);
        tabHaobanRole.setUpdateTime(now);
        haobanRoleService.update(tabHaobanRole);
        return haobanRoleService.queryById(roleId);
    }


    @Override
    public ServiceResponse<HaobanRoleDTO> getMenuByWxEnterpriseIdAndClerkType(String wxEnterpriseId, Integer clerkType, Integer systemFlag) {
        HaobanRoleBO haobanRoleBO = haobanRoleService.getByWxEnterpriseIdAndClerkType(wxEnterpriseId, clerkType, systemFlag);
        if (haobanRoleBO != null) {
            List<String> menuCodes = haobanRoleMenuService.getByWxEnterpriseIdAndRoleId(wxEnterpriseId, haobanRoleBO.getRoleId());
            List<HaobanMenuBO> allMenuList = haobanMenuService.getAllMenuList();
            // 添加关联的权限
            List<HaobanMenuBO> haobanMenuList = new ArrayList<>() ;
            for(HaobanMenuBO item : allMenuList) {
                String code = item.getMenuCode() ;
                // 开启的权限
                if(menuCodes.contains(code)) {
                    haobanMenuList.add(item) ;
                    continue ;
                }
                // 无需授权的权限
                if(0==item.getAuthControl() && StringUtils.isEmpty(item.getBtnCodeRel())) {
                    haobanMenuList.add(item) ;
                    menuCodes.add(code) ;
                    continue ;
                }
            }
            for(HaobanMenuBO item : allMenuList) {
                String code = item.getBtnCodeRel() ;
                if(0==item.getAuthControl() && StringUtils.isNotEmpty(item.getBtnCodeRel()) && menuCodes.contains(code)) {
                    haobanMenuList.add(item) ;
                    menuCodes.add(code) ;
                    continue ;
                }
            }
            haobanRoleBO.setMenuCodes(menuCodes);
            haobanRoleBO.setMenuList(EntityUtil.changeEntityListNew(HaobanMenuDTO.class, haobanMenuList));
        }
        return ServiceResponse.success(EntityUtil.changeEntityNew(HaobanRoleDTO.class, haobanRoleBO));
    }

    @Override
    public ServiceResponse addHaobanRole(String wxEnterpriseId) {
        List<HaobanRoleBO> haobanRoleBOList = haobanRoleService.getListByWxEnterpriseId("-1", null);
        HaobanRoleQDTO haobanRoleQDTO = new HaobanRoleQDTO();
        HaobanRoleQDTO haobanRoleSystemQDTO = new HaobanRoleQDTO();

        int count = haobanRoleService.getCountByWxEnterpriseId(wxEnterpriseId, null);
        //清除历史权限
        if (count > 0) {
            haobanRoleService.deleteByWxEnterpriseId(wxEnterpriseId, null);
            haobanRoleMenuService.deleteByWxEnterpriseId(wxEnterpriseId);
        }
        for (HaobanRoleBO haobanRoleBO : haobanRoleBOList) {
            Long roleId = haobanRoleBO.getRoleId();
            Integer clerkType = haobanRoleBO.getClerkType();
            String roleName = haobanRoleBO.getRoleName();
            haobanRoleQDTO.setRoleName(roleName);
            haobanRoleSystemQDTO.setRoleName(roleName);
            haobanRoleQDTO.setSystemFlag(1);
            haobanRoleSystemQDTO.setSystemFlag(0);
            List<String> menuCodes = haobanRoleMenuService.getByWxEnterpriseIdAndRoleId("-1", roleId);

            if (RoleClerkTypeEnum.CLERK.getCode().equals(clerkType)) {
                HaobanRoleMenuQDTO clerkMenus = new HaobanRoleMenuQDTO();
                clerkMenus.setClerkType(clerkType);
                clerkMenus.setMenuCodes(menuCodes);
                haobanRoleQDTO.setClerkMenus(clerkMenus);
                haobanRoleSystemQDTO.setClerkMenus(clerkMenus);
            } else if (RoleClerkTypeEnum.SHOP.getCode().equals(clerkType)) {
                HaobanRoleMenuQDTO manegeClerkMenus = new HaobanRoleMenuQDTO();
                manegeClerkMenus.setClerkType(clerkType);
                manegeClerkMenus.setMenuCodes(menuCodes);
                haobanRoleQDTO.setManegeClerkMenus(manegeClerkMenus);
                haobanRoleSystemQDTO.setManegeClerkMenus(manegeClerkMenus);
            } else if (RoleClerkTypeEnum.MANAGE.getCode().equals(clerkType)) {
                HaobanRoleMenuQDTO managerMenus = new HaobanRoleMenuQDTO();
                managerMenus.setClerkType(clerkType);
                managerMenus.setMenuCodes(menuCodes);
                haobanRoleQDTO.setManagerMenus(managerMenus);
                haobanRoleSystemQDTO.setManagerMenus(managerMenus);
            }
        }
        WebLoginDTO login = new WebLoginDTO();
        login.setWxEnterpriseId(wxEnterpriseId);
        login.setClerkId("-1");
        login.setClerkName("系统");
        //商户权限
        add(haobanRoleQDTO, login);
        //运维系统权限
        add(haobanRoleSystemQDTO, login);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse addHaobanRoleInit(String wxEnterpriseId) {
        List<TabHaobanWxEnterpriseRelated> enterpriseRelateds = wxEnterpriseRelatedService.getByWxEnterpriseId(wxEnterpriseId);
        if (CollectionUtils.isEmpty(enterpriseRelateds)) {
            logger.error("无关联企业：{}", wxEnterpriseId);
            return ServiceResponse.success();
        }
        //全品牌搜索
        boolean searchFlag = false;
        //离职继承
        boolean handoverFlag = false;
        boolean addClerkFlag = false;
        boolean editClerkFlag = false;
        boolean delClerkFlag = false;
        for (TabHaobanWxEnterpriseRelated enterpriseRelated : enterpriseRelateds) {
            ClerkEditInfoDTO clerkEditInfoDTO = ClerkEditInfoDTO.info(enterpriseRelated.getClerkEditFlag());
            logger.info("导购状态：{}", JSON.toJSONString(clerkEditInfoDTO));
            addClerkFlag = clerkEditInfoDTO.getAddClerkFlag() == 1;
            editClerkFlag = clerkEditInfoDTO.getEditClerkFlag() == 1;
            delClerkFlag = clerkEditInfoDTO.getDelClerkFlag() == 1;
            boolean searchFlagEnterpriseId = manageSettingApiService.getEnterpriseIdByFlag(enterpriseRelated.getEnterpriseId(), 1, null);
            if (searchFlagEnterpriseId) {
                searchFlag = true;
                break;
            }
        }

        for (TabHaobanWxEnterpriseRelated enterpriseRelated : enterpriseRelateds) {
            boolean handoverFlagEnterpriseId = manageSettingApiService.getEnterpriseIdByFlag(enterpriseRelated.getEnterpriseId(), null, 1);
            if (handoverFlagEnterpriseId) {
                handoverFlag = true;
                break;
            }
        }

        List<HaobanRoleBO> haobanRoleBOList = haobanRoleService.getListByWxEnterpriseId("-1", null);
        //导购店长
        List<String> menuCodes = new ArrayList<>();
        //运维后台 导购店长
        List<String> ywMenuCodes = new ArrayList<>();
        //区经
        List<String> menuManageCodes = new ArrayList<>();
        if (searchFlag) {
            menuCodes.add("customerList_allSearch");
            menuManageCodes.add("customerList_allSearch");
        }
        if (handoverFlag) {
            menuCodes.add("customerList_inheritCustomer");
        }
        ywMenuCodes.add("customerList_allSearch");
        ywMenuCodes.add("customerList_inheritCustomer");

        if (addClerkFlag) {
            menuCodes.add("userCenter_addClerk");
        }
        if (editClerkFlag) {
            menuCodes.add("storeClerkList_edit");
        }
        if (delClerkFlag) {
            menuCodes.add("userCenter_delClerk");
        }

        menuCodes.add("workbench");
        menuCodes.add("userCenter");
        menuCodes.add("dataSummary");

        ywMenuCodes.add("workbench");
        ywMenuCodes.add("userCenter");
        ywMenuCodes.add("dataSummary");

        //导购和店长初始化权限以好办web为准
        List<String> menuCodesOld = applicationSettingApiService.findMenuCodes(wxEnterpriseId, 1);
        if (CollectionUtils.isEmpty(menuCodesOld)) {
            logger.error("无权限：{}", wxEnterpriseId);
            return ServiceResponse.success();
        }
        List<String> clerkMenuCodes = haobanMenuService.getHaobanMenuByPid(menuCodesOld);
        menuCodes.addAll(clerkMenuCodes);

        TemplateEnterpriseRelatedDTO templateEnterpriseRelatedDTO = templateEnterpriseRelatedService.selectByWxEnterpriseId(wxEnterpriseId);
        if (templateEnterpriseRelatedDTO == null) {
            logger.error("无模板权限：{}", wxEnterpriseId);
            return ServiceResponse.success();
        }

        List<String> ywMenuCodesOld = tabHaobanTemplateApplicationRelatedMapper.listMenuCodesByTemplateId(templateEnterpriseRelatedDTO.getApplicationTemplateId());
        List<String> ywClerkMenuCodes = haobanMenuService.getHaobanMenuByPid(ywMenuCodesOld);
        ywMenuCodes.addAll(ywClerkMenuCodes);

        HaobanRoleQDTO haobanRoleQDTO = new HaobanRoleQDTO();
        HaobanRoleQDTO haobanRoleSystemQDTO = new HaobanRoleQDTO();
        int count = haobanRoleService.getCountByWxEnterpriseId(wxEnterpriseId, null);
        //清除历史权限
        if (count > 0) {
            haobanRoleService.deleteByWxEnterpriseId(wxEnterpriseId, null);
            haobanRoleMenuService.deleteByWxEnterpriseId(wxEnterpriseId);
        }
        for (HaobanRoleBO haobanRoleBO : haobanRoleBOList) {
            Long roleId = haobanRoleBO.getRoleId();
            Integer clerkType = haobanRoleBO.getClerkType();
            String roleName = haobanRoleBO.getRoleName();
            haobanRoleQDTO.setRoleName(roleName);
            haobanRoleSystemQDTO.setRoleName(roleName);

            haobanRoleQDTO.setSystemFlag(1);
            haobanRoleSystemQDTO.setSystemFlag(0);

            if (RoleClerkTypeEnum.CLERK.getCode().equals(clerkType)) {
                ywMenuCodes.add("storeClerkList_edit");
                //导购
                //初始化我的
                List<String> userCenter = haobanMenuService.getHaobanMenuByPid(Collections.singletonList("userCenter"));
                List<String> menuList = haobanRoleMenuService.getMenuInMenuCodes("-1", roleId, userCenter);
                List<String> codes = RolesListUtils.unionList(menuList, menuCodes);
                List<String> ywCodes = RolesListUtils.unionList(menuList, ywMenuCodes);

                HaobanRoleMenuQDTO clerkMenus = new HaobanRoleMenuQDTO();
                clerkMenus.setClerkType(clerkType);
                clerkMenus.setMenuCodes(new ArrayList<>(new HashSet<>(codes)));
                haobanRoleQDTO.setClerkMenus(clerkMenus);

                HaobanRoleMenuQDTO ywClerkMenus = new HaobanRoleMenuQDTO();
                ywClerkMenus.setClerkType(clerkType);
                ywClerkMenus.setMenuCodes(new ArrayList<>(new HashSet<>(ywCodes)));
                haobanRoleSystemQDTO.setClerkMenus(ywClerkMenus);

            } else if (RoleClerkTypeEnum.SHOP.getCode().equals(clerkType)) {
                ywMenuCodes.add("userCenter_addClerk");
                ywMenuCodes.add("storeClerkList_edit");
                ywMenuCodes.add("userCenter_delClerk");
                //店长
                //我的
                List<String> userCenter = haobanMenuService.getHaobanMenuByPid(Collections.singletonList("userCenter"));
                List<String> menuList = haobanRoleMenuService.getMenuInMenuCodes("-1", roleId, userCenter);
                List<String> unionListcodes = RolesListUtils.unionList(menuList, menuCodes);
                List<String> ywUnionListcodes = RolesListUtils.unionList(menuList, ywMenuCodes);

                HaobanRoleMenuQDTO manegeClerkMenus = new HaobanRoleMenuQDTO();
                manegeClerkMenus.setClerkType(clerkType);
                manegeClerkMenus.setMenuCodes(new ArrayList<>(new HashSet<>(unionListcodes)));
                haobanRoleQDTO.setManegeClerkMenus(manegeClerkMenus);

                HaobanRoleMenuQDTO ywManegeClerkMenus = new HaobanRoleMenuQDTO();
                ywManegeClerkMenus.setClerkType(clerkType);
                ywManegeClerkMenus.setMenuCodes(new ArrayList<>(new HashSet<>(ywUnionListcodes)));
                haobanRoleSystemQDTO.setManegeClerkMenus(ywManegeClerkMenus);

            } else if (RoleClerkTypeEnum.MANAGE.getCode().equals(clerkType)) {
                List<String> manageCodes = haobanRoleMenuService.getByWxEnterpriseIdAndRoleId("-1", roleId);
                menuManageCodes.addAll(manageCodes);

                HaobanRoleMenuQDTO managerMenus = new HaobanRoleMenuQDTO();
                managerMenus.setClerkType(clerkType);
                managerMenus.setMenuCodes(menuManageCodes);
                haobanRoleQDTO.setManagerMenus(managerMenus);

                haobanRoleSystemQDTO.setManagerMenus(managerMenus);
            }
        }
        WebLoginDTO login = new WebLoginDTO();
        login.setWxEnterpriseId(wxEnterpriseId);
        login.setClerkId("-1");
        login.setClerkName("系统");
        //商户权限
        add(haobanRoleQDTO, login);
        //运维系统权限
        add(haobanRoleSystemQDTO, login);
        return ServiceResponse.success();
    }
}
