package com.huimv.manage.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.manage.sys.entity.AccountGroup;
import com.huimv.manage.sys.entity.GroupMenu;
import com.huimv.manage.sys.entity.Menu;
import com.huimv.manage.sys.mapper.AccountGroupMapper;
import com.huimv.manage.sys.mapper.GroupMenuMapper;
import com.huimv.manage.sys.mapper.MenuMapper;
import com.huimv.manage.sys.service.IAuthorizeService;
import com.huimv.manage.common.utils.Result;
import com.huimv.manage.common.utils.ResultCode;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AuthorizeServiceImpl implements IAuthorizeService {

    @Autowired
    AccountGroupMapper accountGroupMapper;
    @Autowired
    GroupMenuMapper groupMenuMapper;
    @Autowired
    private MenuMapper menuMapper;
    
    @Override
    @Transactional
    public Result saveAccountGroup(String accountId, String groupIds) {
        /**
         * Step1:先删除原先的账号关联权限组数据；
         * Step2:再添加新的账号关联权限组数据；
         */
        if (accountId == null||"".equals(accountId)){

            accountGroupMapper.removeAccountGroup(groupIds);
            return new Result(10001, "保存账号关联权限组成功", true);
        }else {
            String[] groupIdArray = accountId.split(",");
            accountGroupMapper.removeAccountGroup(groupIds);
//        for (int a = 0; a < groupIdArray.length; a++) {
//            accountGroupMapper.removeAccountGroup(accountId,groupIdArray[a]);
//        }
            for (int a = 0; a < groupIdArray.length; a++) {
                AccountGroup accountGroupEntity = new AccountGroup();
                accountGroupEntity.setAccountId(Integer.parseInt(groupIdArray[a]));
                accountGroupEntity.setGroupId(Integer.parseInt(groupIds));
                accountGroupMapper.insert(accountGroupEntity);
//            accountGroupMapper.insert(Integer.parseInt(accountId),Integer.parseInt(groupIdArray[a]));
            }
            return new Result(10001, "保存账号关联权限组成功", true);
        }


    }

    @Override
    @Transactional
    public Result saveGroupMenu(String groupId, String menuIds) {
        /**
         * Step1:先删除原先的权限组关联菜单数据；
         * Step2:再添加新的账号权限组关联菜单数据；
         */
        groupMenuMapper.removeGroupMenu(groupId);
        if (StringUtils.isNotEmpty(menuIds)){
            String[] menuIdsArray = menuIds.split(",");
            for (int a = 0; a < menuIdsArray.length; a++) {

                GroupMenu GroupMenu = new GroupMenu();
                GroupMenu.setGroupId(Integer.parseInt(groupId));
                GroupMenu.setMenuId(Integer.parseInt(menuIdsArray[a]));
                groupMenuMapper.insert(GroupMenu);
            }
        }

        return new Result(10001, "保存权限组关联菜单成功", true);
    }





    @Override
    public Result getMenuByGroup(String groupId) {
        /* 根据权限组读取已关联菜单 */
        QueryWrapper<GroupMenu> groupMenuQueryWrapper = new QueryWrapper<>();
        groupMenuQueryWrapper.eq("group_id",groupId);
        List<GroupMenu> groupMenuEntityList = groupMenuMapper.selectList(groupMenuQueryWrapper);
        if (groupMenuEntityList.size() == 0) {
            return new Result(10005, "当前权限组暂未关联任何菜单.", false);
        }
        /* 读取所有菜单 */
        List<Menu> allMenuList = menuMapper.selectList(null);
        if (allMenuList.size() == 0) {
            return new Result(10007, "当前无任何菜单.", false);
        }
        List<GroupMenu> newList = new ArrayList();
        StringBuilder idSb2 = new StringBuilder();

        for (int a = 0; a < groupMenuEntityList.size(); a++) {
            GroupMenu groupMenuEntity = groupMenuEntityList.get(a);
            for (Menu menuEntity : allMenuList) {
                if (menuEntity.getId().equals(groupMenuEntity.getMenuId())) {
                    if (menuEntity.getParentId() == 0) {
                        if (idSb2.length() > 0) {
                            idSb2.append(",");
                        }
                        idSb2.append(groupMenuEntity.getMenuId());
                        break;
                    } else {
                        boolean isChild = false;
                        for (Menu menuEntity2 : allMenuList) {
                            if (menuEntity2.getParentId().equals(groupMenuEntity.getMenuId())) {
                                isChild = true;
                                break;
                            }
                        }
                        if (!isChild) {
                            if (idSb2.length() > 0) {
                                idSb2.append(",");
                            }
                            idSb2.append(groupMenuEntity.getMenuId());
                        }
                        break;
                    }
                }
            }

        }

//        StringBuilder idSb = new StringBuilder();
//        for (GroupMenu groupMenuEntity : groupMenuEntityList) {
//            if (idSb.length() > 0) {
//                idSb.append(",");
//            }
//            idSb.append(groupMenuEntity.getMenuId());
//        }
        return new Result(ResultCode.SUCCESS, idSb2.toString());
    }

    /**
     * @param accountId
     * @return
     */
    @Override
    public Result getGroupByAccount(String accountId) {
        /* 根据账号id读取已关联权限组 */
        QueryWrapper<AccountGroup> accountGroupQueryWrapper = new QueryWrapper<>();
        accountGroupQueryWrapper.eq("account_id",accountId);
        List<AccountGroup> accountGroupEntityList = accountGroupMapper.selectList(accountGroupQueryWrapper);
        if (accountGroupEntityList.size() == 0) {
            return new Result(10006, "当前用户账号暂未关联任何权限组.", false);
        }
        StringBuilder idSb = new StringBuilder();
        for (AccountGroup accountGroupEntity : accountGroupEntityList) {
            if (idSb.length() > 0) {
                idSb.append(",");
            }
            idSb.append(accountGroupEntity.getGroupId());
        }
        return new Result(ResultCode.SUCCESS, idSb.toString());
    }

    @Override
    public Result getAccountByGroup(String groupId) {
        /* 根据账号id读取已关联权限组 */

        QueryWrapper<AccountGroup> accountGroupQueryWrapper = new QueryWrapper<>();
        accountGroupQueryWrapper.eq("group_id",groupId);
        List<AccountGroup> accountGroupEntityList = accountGroupMapper.selectList(accountGroupQueryWrapper);
        if (accountGroupEntityList.size() == 0) {
            return new Result(10006, "当前权限组暂未关联任何用户账号.", false);
        }
        StringBuilder idSb = new StringBuilder();
        for (AccountGroup accountGroupEntity : accountGroupEntityList) {
            if (idSb.length() > 0) {
                idSb.append(",");
            }
            idSb.append(accountGroupEntity.getAccountId());
        }
        return new Result(ResultCode.SUCCESS, idSb.toString());
    }

    @Override
    public List getMyMenuAll(String accountName) {
        if ("1".equals(accountName)){
            List<Menu> menus = menuMapper.selectList(new QueryWrapper<Menu>().orderByAsc("sort"));
            return menus;
        }
        return menuMapper.getMyMenu(accountName);
    }

    @Override
    public List<String> getMyButton(Integer userId) {
        if (userId == 1){
            List<Menu> menus = menuMapper.selectList(null);
            List<String> list = new ArrayList<>();
            for (Menu menu : menus) {
                if (menu.getWeight() == 1){
                    list.add(menu.getPermission());
                }

            }
            return list;
        }
        return menuMapper.getMyButton(userId);
    }

    @Override
    public List getMyMenuMultilevel(String userId) {
        return null;
    }

}
