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

import com.huimv.admin.domain.primary.SysAccountGroupEntity;
import com.huimv.admin.domain.primary.SysGroupMenuEntity;
import com.huimv.admin.domain.primary.SysMenuEntity;
import com.huimv.admin.repo.primary.SysAccountGroupEntityRepository;
import com.huimv.admin.repo.primary.SysGroupMenuEntityRepository;
import com.huimv.admin.repo.primary.SysMenuEntityRepository;
import com.huimv.admin.service.primary.IAuthorizeService;
import com.huimv.admin.utils.Result;
import com.huimv.admin.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
    SysAccountGroupEntityRepository SysAccountGroupRepo;
    @Autowired
    SysGroupMenuEntityRepository sysGroupMenuRepo;
    @Autowired
    private SysMenuEntityRepository sysMenuEntityRepo;
    @Autowired
    private SysMenuEntityRepository sysMenuEntityRepository;

 /*   @Override
    @Transactional
    public Result saveAccountGroup(String accountId, String groupIds) {
        *//**
         * Step1:先删除原先的账号关联权限组数据；
         * Step2:再添加新的账号关联权限组数据；
         *//*
        if (groupIds == null||"".equals(groupIds)){
            SysAccountGroupRepo.removeAccountGroup(accountId);
            return new Result(10001, "保存账号关联权限组成功", true);
        }
        String[] groupIdArray = groupIds.split(",");
        SysAccountGroupRepo.removeAccountGroup(accountId);
//        for (int a = 0; a < groupIdArray.length; a++) {
//            SysAccountGroupRepo.removeAccountGroup(accountId,groupIdArray[a]);
//        }
        for (int a = 0; a < groupIdArray.length; a++) {
            SysAccountGroupEntity accountGroupEntity = new SysAccountGroupEntity();
            accountGroupEntity.setAccountId(Integer.parseInt(accountId));
            accountGroupEntity.setGroupId(Integer.parseInt(groupIdArray[a]));
            SysAccountGroupRepo.saveAndFlush(accountGroupEntity);
//            SysAccountGroupRepo.insert(Integer.parseInt(accountId),Integer.parseInt(groupIdArray[a]));
        }
        return new Result(10001, "保存账号关联权限组成功", true);
    } */
    @Override
    @Transactional
    public Result saveAccountGroup(String accountId, String groupIds) {
        /**
         * Step1:先删除原先的账号关联权限组数据；
         * Step2:再添加新的账号关联权限组数据；
         */
        if (accountId == null||"".equals(accountId)){
            SysAccountGroupRepo.removeAccountGroup(groupIds);
            return new Result(10001, "保存账号关联权限组成功", true);
        }else {
            String[] groupIdArray = accountId.split(",");
            SysAccountGroupRepo.removeAccountGroup(groupIds);
//        for (int a = 0; a < groupIdArray.length; a++) {
//            SysAccountGroupRepo.removeAccountGroup(accountId,groupIdArray[a]);
//        }
            for (int a = 0; a < groupIdArray.length; a++) {
                SysAccountGroupEntity accountGroupEntity = new SysAccountGroupEntity();
                accountGroupEntity.setAccountId(Integer.parseInt(groupIdArray[a]));
                accountGroupEntity.setGroupId(Integer.parseInt(groupIds));
                SysAccountGroupRepo.saveAndFlush(accountGroupEntity);
//            SysAccountGroupRepo.insert(Integer.parseInt(accountId),Integer.parseInt(groupIdArray[a]));
            }
            return new Result(10001, "保存账号关联权限组成功", true);
        }


    }

    @Override
    @Transactional
    public Result saveGroupMenu(String groupId, String menuIds) {
        /**
         * Step1:先删除原先的权限组关联菜单数据；
         * Step2:再添加新的账号权限组关联菜单数据；
         */
        sysGroupMenuRepo.removeGroupMenu(groupId);
        String[] menuIdsArray = menuIds.split(",");
     /*   for (int a = 0; a < menuIdsArray.length; a++) {
            sysGroupMenuRepo.removeGroupMenu(groupId, menuIdsArray[a]);
        }*/
        for (int a = 0; a < menuIdsArray.length; a++) {
            SysGroupMenuEntity sysGroupMenuEntity = new SysGroupMenuEntity();
            sysGroupMenuEntity.setGroupId(Integer.parseInt(groupId));
            sysGroupMenuEntity.setMenuId(Integer.parseInt(menuIdsArray[a]));
            sysGroupMenuRepo.saveAndFlush(sysGroupMenuEntity);
        }
        return new Result(10001, "保存权限组关联菜单成功", true);
    }

    /**
     * @param accountName
     * @return
     */
    public List getMyMenu_old(String accountName) {
        System.out.println(" accountName>>" + accountName);
        List<SysMenuEntity> menuEntityList = sysMenuEntityRepo.getMyMenu(accountName);
        System.out.println(" menuEntityList.size>>" + menuEntityList.size());
        List<SysMenuEntity> topList = new ArrayList();
        for (SysMenuEntity sysMenuEntity : menuEntityList) {
            if (sysMenuEntity.getParentId() == 0) {
                topList.add(sysMenuEntity);
            }
        }
        return topList;
    }

    @Override
    public List getMyMenu(String accountName) {
        List<SysMenuEntity> allMenuEntityList = sysMenuEntityRepo.getMyMenu(accountName);
        System.out.println("123456");
        //
        List<SysMenuEntity> myRootMenuList = new ArrayList();
        //pid（上级Id）为0的是根菜单
        for (SysMenuEntity menuEntity : allMenuEntityList) {
            if (menuEntity.getParentId() == 0) {
                myRootMenuList.add(menuEntity);
            }
        }
        // 遍历，找到二级菜单（根菜单的id和所有菜单中的pid比较）
        for (SysMenuEntity myMenuEntity : myRootMenuList) {
            /*  */
            List<SysMenuEntity> child = getChild(myMenuEntity.getId(), allMenuEntityList);
            myMenuEntity.setChildren(child);
        }
        return myRootMenuList;
    }

    public List<SysMenuEntity> getChild(int pid, List<SysMenuEntity> allMenuList) {
        //子菜单列表
        List<SysMenuEntity> childList = new ArrayList<>();
        for (SysMenuEntity menuEntity : allMenuList) {
            if (pid == menuEntity.getParentId()) {
                childList.add(menuEntity);
            }
        }
        //遍历 获取子菜单的子菜单
        for (SysMenuEntity childMenu : childList) {
            List<SysMenuEntity> child = getChild(childMenu.getId(), allMenuList);
            childMenu.setChildren(child);
        }
        //递归出口 childList长度为0
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

    @Override
    public Result getMenuByGroup(String groupId) {
        /* 根据权限组读取已关联菜单 */
        List<SysGroupMenuEntity> groupMenuEntityList = sysGroupMenuRepo.getMenuByGroup(groupId);
        if (groupMenuEntityList.size() == 0) {
            return new Result(10005, "当前权限组暂未关联任何菜单.", false);
        }
        /* 读取所有菜单 */
        List<SysMenuEntity> allMenuList = sysMenuEntityRepository.findAll();
        if (allMenuList.size() == 0) {
            return new Result(10007, "当前无任何菜单.", false);
        }
        List<SysGroupMenuEntity> newList = new ArrayList();
        StringBuilder idSb2 = new StringBuilder();

        for (int a = 0; a < groupMenuEntityList.size(); a++) {
            SysGroupMenuEntity groupMenuEntity = groupMenuEntityList.get(a);
            for (SysMenuEntity 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 (SysMenuEntity 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 (SysGroupMenuEntity 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读取已关联权限组 */
        List<SysAccountGroupEntity> accountGroupEntityList = SysAccountGroupRepo.getGroupByAccount(accountId);
        if (accountGroupEntityList.size() == 0) {
            return new Result(10006, "当前用户账号暂未关联任何权限组.", false);
        }
        StringBuilder idSb = new StringBuilder();
        for (SysAccountGroupEntity 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读取已关联权限组 */
        List<SysAccountGroupEntity> accountGroupEntityList = SysAccountGroupRepo.getAccountByGroup(groupId);
        if (accountGroupEntityList.size() == 0) {
            return new Result(10006, "当前权限组暂未关联任何用户账号.", false);
        }
        StringBuilder idSb = new StringBuilder();
        for (SysAccountGroupEntity 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) {
        return sysMenuEntityRepo.getMyMenu(accountName);
    }
}
