package com.judong.chuanyiserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.judong.chuanyiserver.dao.RoleDao;
import com.judong.chuanyiserver.entity.Menu;
import com.judong.chuanyiserver.entity.Role;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.service.RoleService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.ConstantStr;
import com.judong.chuanyiserver.util.Result;
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.Iterator;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public synchronized Result addRole(Role role) {
        if (Blank.isNotEmpty(roleDao.getRoleByName(role.getRoleName()))) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "该角色名已被使用，请更换其他角色名");
        }
        if (roleDao.addRole(role) <= 0) {
            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "新增失败");
        }
        return Result.ok("添加角色成功");
    }

    @Override
    public synchronized Result updateRole(Role role) {
        if (Blank.isEmpty(role.getId(), role.getRoleName(), role.getRoleCode())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "角色Id和角色名称不能为空");
        }
        if (Blank.isNotEmpty(roleDao.getRoleByNoIdName(role.getId(), role.getRoleName()))) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "已存在该角色名，请更换角色名称");
        }
        if (roleDao.updateRole(role) <= 0) {
            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "修改角色失败");
        }
        return Result.ok("修改角色成功");
    }

    @Override
    public Result getRoleListByPage(Integer page, Integer limit, String roleName, String roleCode) {
        JSONObject jsonObject = new JSONObject();
        Long count = roleDao.getRoleCount(roleName, roleCode, ConstantStr.PUBLIC_NO_DELETE);
        Long startNum = Long.valueOf((page - 1) * limit);
        List<Role> roleList = roleDao.getRoleListByPage(startNum, Long.valueOf(limit), roleName, roleCode, ConstantStr.PUBLIC_NO_DELETE);
        jsonObject.put("count", count);
        jsonObject.put("roleList", roleList);
        return Result.ok(jsonObject);
    }

    @Override
    public Result getRoleById(Integer id) {
        return Result.ok(roleDao.getRoleById(id));
    }

    @Override
    public Result getMenuListByRoleList(List<Role> roleList) {
        return Result.ok(roleDao.getMenuListByroleList(roleList));
    }

    @Override
    public Result deleteRoleById(Integer roleId) {
        if (roleDao.deleteRoleById(roleId, ConstantStr.PUBLIC_IS_DELETE) <= 0) {
            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "删除角色失败");
        }
        return Result.ok("删除角色成功");
    }

    @Override
    public Result assignMenu(Integer roleId, List<Menu> menuList) {
        //查询出拥有新旧菜单集合拥有的公共菜单
        List<Menu> shareMenuList = roleDao.getShareMenuList(roleId, menuList);
        //增加新增的菜单信息
        List<Menu> newMenuList = new ArrayList<>();
        if (Blank.isEmpty(shareMenuList)) {
            newMenuList = menuList;
            //删除原来的菜单
            roleDao.deleteRoleMenuByRoleId(roleId);
        } else {
            for (int i = 0; i < menuList.size(); i++) {
                Boolean flage = false;
                for (int j = 0; j < shareMenuList.size(); j++) {
                    if (menuList.get(i).getId() == shareMenuList.get(j).getId()) {
                        flage = true;
                        break;
                    }
                }
                if (!flage) {
                    newMenuList.add(menuList.get(i));
                }
            }
            //删除不在公共角色的旧角色Id
            List<Menu> oldMenuList = roleDao.getNoShareOldMenuList(roleId, shareMenuList);
            if (Blank.isNotEmpty(oldMenuList)) {
                roleDao.deleteRoleMenu(roleId, oldMenuList);
            }
        }
        if (Blank.isNotEmpty(newMenuList)) {
            roleDao.addRoleMenuList(roleId, newMenuList);
        }
        return Result.ok("角色分配菜单成功");
    }

    @Override
    public Result getMenuTreeByRoleList(List<Integer> roleIdList) {
        JSONObject jsonObject = new JSONObject();
        List<Menu> menuList = roleDao.getMenuByRoleIdList(roleIdList, ConstantStr.MENU_AVAILABLE);
        jsonObject.put("menuTree", gennerMenuTree(menuList, ConstantStr.TOP_LEVEL_MENU));
        return Result.ok(jsonObject);
    }

    public List<JSONObject> gennerMenuTree(List<Menu> menuList, Integer parentId) {
        List<JSONObject> jsonObjectList = new ArrayList<>();
        if (Blank.isEmpty(menuList)) {
            return null;
        }
        for (Menu menu : menuList) {
            if (menu.getParentId() == parentId) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", parentId);
                jsonObject.put("label", menu.getMenuName());
                jsonObject.put("url", menu.getMenuUrl());
                jsonObject.put("icon", menu.getMenuIcon());
                jsonObject.put("children", gennerMenuTree(menuList, menu.getId()));
                jsonObjectList.add(jsonObject);
            }
        }
        return jsonObjectList;
    }

}
