/**
* 文件：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.service.impl.AbstractCRUDServiceImpl;
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 org.springframework.stereotype.Service;

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

/**
 * <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 {

    @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<String>();
        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);
    }
	
}