package com.huimv.admin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.system.entity.*;
import com.huimv.admin.system.mapper.*;
import com.huimv.admin.system.service.IAppAuthorizeService;
import com.huimv.common.utils.Result;
import com.huimv.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 AppAuthorizeServiceImpl implements IAppAuthorizeService {

    @Autowired
    private AppAccountGroupMapper accountGroupMapper;
    @Autowired
    private AppGroupMenuMapper groupMenuMapper;
    @Autowired
    private AppMenuMapper menuMapper;
    
    @Override
    @Transactional
    public Result saveAccountGroup(String accountId, String groupIds) {
        /**
         * Step1:先删除原先的账号关联权限组数据；
         * Step2:再添加新的账号关联权限组数据；
         */

        if (accountId == null||"".equals(accountId)){
            String[] groupIdArray = accountId.split(",");
            Integer groupId = accountGroupMapper.selectCount(new QueryWrapper<AppSysAccountGroup>().in("group_id", groupIdArray));
            if (groupId >0 ){
                accountGroupMapper.removeAccountGroup(groupIds);
            }

            return new Result(10001, "保存账号关联权限组成功", true);
        }else {
            String[] groupIdArray = accountId.split(",");
            Integer groupId = accountGroupMapper.selectCount(new QueryWrapper<AppSysAccountGroup>().in("group_id", groupIdArray));
            if (groupId >0 ){
                accountGroupMapper.removeAccountGroup(groupIds);
            }

//        for (int a = 0; a < groupIdArray.length; a++) {
//            accountGroupMapper.removeAccountGroup(accountId,groupIdArray[a]);
//        }
            for (int a = 0; a < groupIdArray.length; a++) {
                String[] split = groupIds.split(",");
                for (String s : split) {
                    AppSysAccountGroup accountGroupEntity = new AppSysAccountGroup();
                    accountGroupEntity.setAccountId(Integer.parseInt(groupIdArray[a]));
                    accountGroupEntity.setGroupId(Integer.parseInt(s));
                    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:再添加新的账号权限组关联菜单数据；
         */
        Integer integer = groupMenuMapper.selectCount(new QueryWrapper<AppSysGroupMenu>().eq("group_id", groupId));
        if (integer >0 ){
            groupMenuMapper.removeGroupMenu(groupId);
        }

        if (StringUtils.isNotBlank(menuIds)){
            String[] menuIdsArray = menuIds.split(",");
            for (int a = 0; a < menuIdsArray.length; a++) {

                AppSysGroupMenu GroupMenu = new AppSysGroupMenu();
                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<AppSysGroupMenu> groupMenuQueryWrapper = new QueryWrapper<>();
        groupMenuQueryWrapper.eq("group_id",groupId);
        List<AppSysGroupMenu> groupMenuEntityList = groupMenuMapper.selectList(groupMenuQueryWrapper);
        if (groupMenuEntityList.size() == 0) {
            return new Result(10005, "当前权限组暂未关联任何菜单.", false);
        }
        /* 读取所有菜单 */
        List<AppSysMenu> 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++) {
            AppSysGroupMenu groupMenuEntity = groupMenuEntityList.get(a);
            for (AppSysMenu 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 (AppSysMenu 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<AppSysAccountGroup> accountGroupQueryWrapper = new QueryWrapper<>();
        accountGroupQueryWrapper.eq("account_id",accountId);
        List<AppSysAccountGroup> accountGroupEntityList = accountGroupMapper.selectList(accountGroupQueryWrapper);
        if (accountGroupEntityList.size() == 0) {
            return new Result(10006, "当前用户账号暂未关联任何权限组.", false);
        }
        StringBuilder idSb = new StringBuilder();
        for (AppSysAccountGroup 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<AppSysAccountGroup> accountGroupQueryWrapper = new QueryWrapper<>();
        accountGroupQueryWrapper.eq("group_id",groupId);
        List<AppSysAccountGroup> accountGroupEntityList = accountGroupMapper.selectList(accountGroupQueryWrapper);
        if (accountGroupEntityList.size() == 0) {
            return new Result(10006, "当前权限组暂未关联任何用户账号.", false);
        }
        StringBuilder idSb = new StringBuilder();
        for (AppSysAccountGroup 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<AppSysMenu> menus = menuMapper.selectList(new QueryWrapper<AppSysMenu>().orderByAsc("sort"));
            return menus;
        }

        return menuMapper.getMyMenuApp(accountName);
    }

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

        return menuMapper.getMyButton(userId);
    }

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

    @Transactional
    @Override
    public void saveAccountGroupNew(Integer accountId, String collect) {
        accountGroupMapper.delete(new QueryWrapper<AppSysAccountGroup>().eq("account_id",accountId));
        String[] split = collect.split(",");
        for (String s : split) {
            AppSysAccountGroup accountGroup = new AppSysAccountGroup();
            accountGroup.setGroupId(Integer.parseInt(s));
            accountGroup.setAccountId(accountId);
            accountGroupMapper.insert(accountGroup);
        }
    }
}
