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

package com.mortals.xhx.base.system.resource.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.framework.util.StringUtils;
import com.mortals.xhx.base.system.resource.dao.ResourceDao;
import com.mortals.xhx.base.system.resource.model.ResourceEntity;
import com.mortals.xhx.base.system.resource.model.ResourceQuery;
import com.mortals.xhx.base.system.resource.service.ResourceService;

import com.mortals.xhx.base.system.role.model.RoleAuthEntity;
import com.mortals.xhx.base.system.role.model.RoleAuthQuery;
import com.mortals.xhx.base.system.role.service.RoleAuthService;
import com.mortals.xhx.common.key.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

/**
 * <p>Title: 资源信息</p>
 * <p>Description: ResourceServiceImpl service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author
 * @version 1.0.0
 */
@Service("resourceService")
public class ResourceServiceImpl extends AbstractCRUDServiceImpl<ResourceDao, ResourceEntity, Long> implements ResourceService {

    @Autowired
    private ICacheService cacheService;

    @Autowired
    private RoleAuthService roleAuthService;

    @Override
    public List<ResourceEntity> findAllEnable() throws AppException {
        ResourceQuery params = new ResourceQuery();
        return dao.getList(params);
    }

    @Override
    public List<ResourceEntity> findListByUserId(Long userId) throws AppException {
        return dao.getListByUserId(userId);
    }

    @Override
    public String findUrlByUserId(Long userId) throws AppException {
        Set<String> urls = this.findUrlSetByUserId(userId);
        return StringUtils.converArray2Str(urls.toArray(new String[urls.size()]));
    }

    @Override
    public Set<String> findUrlSetByUserId(Long userId) throws AppException {
        Set<String> urls = new HashSet<>();
        List<ResourceEntity> resList = this.findListByUserId(userId);
        for (ResourceEntity res : resList) {
            String url = res.getUrl();
            if (StringUtils.isEmpty(url)) {
                continue;
            }
            url = url.replaceAll("，", ",");
            urls.addAll(StringUtils.converStr2Set(url));
        }
        return urls;
    }

    @Override
    public List<ResourceEntity> findAll(int userType) {
        return dao.getAll(userType);
    }


    @Override
    protected void updateAfter(ResourceEntity entity, Context context) throws AppException {
        updateUserMenuUrlCache();
    }

    @Override
    protected void saveAfter(ResourceEntity entity, Context context) throws AppException {
        updateUserMenuUrlCache();
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        //删除关联表中数据
        if (!ObjectUtils.isEmpty(ids)) {
            RoleAuthQuery query = new RoleAuthQuery();
            query.setResourceIdList(Arrays.asList(ids));
            List<RoleAuthEntity> roleAuthEntities = roleAuthService.find(query);
            if (!ObjectUtils.isEmpty(roleAuthEntities)) {
                roleAuthService.remove(roleAuthEntities.stream().map(m -> m.getId()).toArray(Long[]::new), context);
                updateUserMenuUrlCache();
            }
        }
    }

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