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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.eartag2.admin.entity.AccountGroup;
import com.huimv.eartag2.admin.entity.GroupMenu;
import com.huimv.eartag2.admin.entity.Menu;
import com.huimv.eartag2.admin.mapper.AccountGroupMapper;
import com.huimv.eartag2.admin.mapper.GroupMenuMapper;
import com.huimv.eartag2.admin.mapper.MenuMapper;
import com.huimv.eartag2.admin.service.IAuthorizeService;
import com.huimv.eartag2.admin.service.IGroupMenuService;
import com.huimv.eartag2.common.utils.Result;
import com.huimv.eartag2.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
    IGroupMenuService groupMenuService;
    @Autowired
    private MenuMapper menuMapper;
    
    @Override
    @Transactional
    public Result saveAccountGroup(String accountId, String groupIds) {
        /**
         * Step1:先删除原先的账号关联权限组数据；
         * Step2:再添加新的账号关联权限组数据；
         */
//        if (accountId == null||"".equals(accountId)){

//            accountGroupMapper.removeAccountGroup(accountId);
//            return new Result(10001, "保存账号关联权限组成功", true);
//        }else {
            String[] groupIdArray = accountId.split(",");
            accountGroupMapper.removeAccountGroup(accountId);
//        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,String isAll) {
        /**
         * Step1:先删除原先的权限组关联菜单数据；
         * Step2:再添加新的账号权限组关联菜单数据；
         */
        groupMenuMapper.removeGroupMenu(groupId);
        if (StringUtils.isNotBlank(menuIds)){
            String[] menuIdsArray = menuIds.split(",");
            String[] split = isAll.split(",");

            List<GroupMenu> list = new ArrayList();
            for (int a = 0; a < menuIdsArray.length; a++) {
                    GroupMenu groupMenu = new GroupMenu();
                    groupMenu.setGroupId(Integer.parseInt(groupId));
                    groupMenu.setMenuId(Integer.parseInt(menuIdsArray[a]));
                list.add(groupMenu);
            }
            if (StringUtils.isNotBlank(isAll)){
                for (GroupMenu groupMenu : list) {
                    for (String s : split) {
                        if (groupMenu.getMenuId()== Integer.parseInt(s)){
                            groupMenu.setIsAll(1);
                        }
                    }
                }
            }
            groupMenuService.saveBatch(list);
        }
        return new Result(10000, "保存权限组关联菜单成功", 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) {
                list.add(menu.getPermission());
            }
            return list;
        }
        return menuMapper.getMyButton(userId);
    }

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

    @Override
    public Result getMenuByGroup1(String groupId) {
        QueryWrapper<GroupMenu> groupMenuQueryWrapper = new QueryWrapper<>();
        groupMenuQueryWrapper.eq("group_id",groupId);
        groupMenuQueryWrapper.eq("is_all",0);
        List<GroupMenu> groupMenuEntityList = groupMenuMapper.selectList(groupMenuQueryWrapper);
        if (groupMenuEntityList.size() == 0) {
            return new Result(10005, "当前权限组暂未关联任何菜单.", false);
        }
        StringBuilder stringBuffer = new StringBuilder();

        for (GroupMenu groupMenu : groupMenuEntityList) {

            stringBuffer.append(groupMenu.getMenuId());
            stringBuffer.append(",");

        }
        stringBuffer.deleteCharAt(stringBuffer.length()-1);


        return new Result(ResultCode.SUCCESS, stringBuffer.toString());
    }

}
