/**
 * 文件：RoleUserServiceImpl.java
 * 版本：1.0.0
 * 日期：
 * Copyright &reg;
 * All right reserved.
 */

package com.mortals.xhx.base.system.role.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.util.DataUtil;
import com.mortals.xhx.base.system.resource.service.ResourceService;
import com.mortals.xhx.base.system.role.dao.RoleUserDao;
import com.mortals.xhx.base.system.role.model.RoleUserEntity;
import com.mortals.xhx.base.system.role.model.RoleUserQuery;
import com.mortals.xhx.base.system.role.service.RoleUserService;
import com.mortals.xhx.common.key.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.mortals.xhx.common.utils.MenuEncodeUtil.generateMenuUrlCode;

/**
 * <p>Title: 角色用户</p>
 * <p>Description: RoleUserServiceImpl service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author
 * @version 1.0.0
 */
@Service("roleUserService")
public class RoleUserServiceImpl extends AbstractCRUDServiceImpl<RoleUserDao, RoleUserEntity, Long> implements RoleUserService {

    @Autowired
    private ICacheService cacheService;

    @Autowired
    private ResourceService resourceService;

    @Override
    public void doDistributionUser(RoleUserQuery query) {
        Long roleId = query.getRoleId();
        // 删除角色对应的用户信息
        Map<String, Object> condition = new HashMap<>(1);
        condition.put("roleId", roleId);
        dao.delete(condition);

        List<Long> userIdList = query.getUserIdList();
        List<RoleUserEntity> list = new ArrayList<>();
        for (Long userId : userIdList) {
            RoleUserEntity rolseUser = new RoleUserEntity();
            rolseUser.setRoleId(roleId);
            rolseUser.setUserId(userId);
            list.add(rolseUser);
        }
        this.dao.insertBatch(list);
        updateUserMenuUrlCache();
    }

    @Override
    public void doDistributionRole(RoleUserQuery query) {
        // 删除角色对应的用户信息
        Long userId = query.getUserId();
        Map<String, Object> condition = new HashMap<>(1);
        condition.put("userId", userId);
        dao.delete(condition);
        List<Long> roleIdList = query.getRoleIdList();
        List<RoleUserEntity> list = new ArrayList<>();
        for (Long roleId : roleIdList) {
            RoleUserEntity rolseUser = new RoleUserEntity();
            rolseUser.setRoleId(roleId);
            rolseUser.setUserId(userId);
            list.add(rolseUser);
        }
        this.dao.insertBatch(list);
        updateUserMenuUrlCache();
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        updateUserMenuUrlCache();
    }

    private void updateUserMenuUrlCache() {
        //更新用户菜单
        Set<String> hkeys = cacheService.hkeys(RedisKey.KEY_USER_MENU_CACHE);
        for (String userId : hkeys) {
            Set<String> urls = resourceService.findUrlSetByUserId(DataUtil.converStr2Long(userId, 0L));
            String menuUrlCode = generateMenuUrlCode(urls);
            cacheService.hset(RedisKey.KEY_USER_MENU_CACHE, userId, menuUrlCode);
        }
    }

}