package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.PersonResources;
import com.ruoyi.system.mapper.PersonResourcesMapper;
import com.ruoyi.system.service.IPersonResourcesService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 个人资源库Service业务层处理
 * 
 * @author mingxue
 * @date 2021-06-03
 */
@Service
public class PersonResourcesServiceImpl implements IPersonResourcesService 
{
    @Autowired
    private PersonResourcesMapper personResourcesMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ApplicationContext applicationContext;
    
    
    /**
     * 查询个人资源库
     * 
     * @param resourcesId 个人资源库ID
     * @return 个人资源库
     */
    @Override
    public PersonResources selectPersonResourcesById(Long resourcesId)
    {
        return personResourcesMapper.selectPersonResourcesById(resourcesId);
    }

    /**
     * 查询个人资源库列表
     * 
     * @param personResources 个人资源库
     * @return 个人资源库
     **/
    @Override
    public List<PersonResources> selectPersonResourcesList(PersonResources personResources)
    {
        List<PersonResources> list = personResourcesMapper.selectPersonResourcesList(personResources);
        if(list.size() > 0) {
            Map<Long, String> userNameNickMap = sysUserService.selectUsers(
                    list.stream().map(t -> t.getCreateId()).collect(Collectors.toSet()), null, null)
                    .stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName));
            list.forEach(t -> {
                t.setCreateName(userNameNickMap.get(t.getCreateId()));
                t.setCreateBy(t.getCreateName());
            });
        }

        return list;
    }

    /**
     * 新增个人资源库
     * 
     * @param personResources 个人资源库
     * @return 结果
     */
    @Override
    public int insertPersonResources(PersonResources personResources)
    {
        if(StringUtils.isBlank(personResources.getSourceId())){
            personResources.setSourceId(IdUtils.simpleUUID());
        }
        personResources.setCreateTime(DateUtils.getNowDate());
        personResources.setUpdateTime(new Date());
        int result = personResourcesMapper.insertPersonResources(personResources);
        personalResourceChanged();
        return result;
    }

    /**
     * 修改个人资源库
     * 
     * @param personResources 个人资源库
     * @return 结果
     */
    @Override
    public int updatePersonResources(PersonResources personResources)
    {
        personResources.setUpdateTime(new Date());
        int r = personResourcesMapper.updatePersonResources(personResources);
        personalResourceChanged();
        return r;
    }

    /**
     * 批量删除个人资源库
     * 
     * @param resourcesIds 需要删除的个人资源库ID
     * @return 结果
     */
    @Override
    public int deletePersonResourcesByIds(Long[] resourcesIds)
    {
        return personResourcesMapper.deletePersonResourcesByIds(resourcesIds);
    }

    /**
     * 删除个人资源库信息
     * 
     * @param resourcesId 个人资源库ID
     * @return 结果
     */
    @Override
    public int deletePersonResourcesById(Long resourcesId)
    {
        return personResourcesMapper.deletePersonResourcesById(resourcesId);
    }

    private void personalResourceChanged() {

    }
}
