package com.yanzhu.jh.work.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yanzhu.jh.work.domain.WorkFileUserLog;
import com.yanzhu.jh.work.mapper.WorkFileUserMapper;
import com.yanzhu.jh.work.domain.WorkFileUser;
import com.yanzhu.jh.work.service.IWorkFileUserService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 文件访问用户Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-10-22
 */
@Service
public class WorkFileUserServiceImpl implements IWorkFileUserService 
{
    @Autowired
    private WorkFileUserMapper workFileUserMapper;

    /**
     * 查询文件访问用户
     * 
     * @param id 文件访问用户主键
     * @return 文件访问用户
     */
    @Override
    public WorkFileUser selectWorkFileUserById(Long id)
    {
        return workFileUserMapper.selectWorkFileUserById(id);
    }

    /**
     * 查询文件访问用户列表
     * 
     * @param workFileUser 文件访问用户
     * @return 文件访问用户
     */
    @Override
    public List<WorkFileUser> selectWorkFileUserList(WorkFileUser workFileUser)
    {
        return workFileUserMapper.selectWorkFileUserList(workFileUser);
    }

    /**
     * 统计文件访问用户列表
     *
     * @param workFileUser 文件访问用户
     * @return 文件访问用户集合
     */
    public List<Map<String,Object>> queryCountByDeptType(WorkFileUser workFileUser) {
        return workFileUserMapper.queryCountByDeptType(workFileUser);
    }

    /**
     * 新增文件访问用户
     * 
     * @param workFileUser 文件访问用户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkFileUser(WorkFileUser workFileUser)
    {
        workFileUser.setCreateBy(SecurityUtils.getUsername());
        workFileUser.setCreateTime(DateUtils.getNowDate());
        int rows = workFileUserMapper.insertWorkFileUser(workFileUser);
        insertWorkFileUserLog(workFileUser);
        return rows;
    }

    /**
     * 修改文件访问用户
     * 
     * @param workFileUser 文件访问用户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkFileUser(WorkFileUser workFileUser)
    {
        workFileUser.setUpdateBy(SecurityUtils.getUsername());
        workFileUser.setUpdateTime(DateUtils.getNowDate());
        workFileUserMapper.deleteWorkFileUserLogByFileUserId(workFileUser.getId());
        insertWorkFileUserLog(workFileUser);
        return workFileUserMapper.updateWorkFileUser(workFileUser);
    }

    /**
     * 批量删除文件访问用户
     * 
     * @param ids 需要删除的文件访问用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkFileUserByIds(Long[] ids)
    {
        workFileUserMapper.deleteWorkFileUserLogByFileUserIds(ids);
        return workFileUserMapper.deleteWorkFileUserByIds(ids);
    }

    /**
     * 删除文件访问用户信息
     * 
     * @param id 文件访问用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkFileUserById(Long id)
    {
        workFileUserMapper.deleteWorkFileUserLogByFileUserId(id);
        return workFileUserMapper.deleteWorkFileUserById(id);
    }

    /**
     * 新增文件访问用户记录信息
     * 
     * @param workFileUser 文件访问用户对象
     */
    public void insertWorkFileUserLog(WorkFileUser workFileUser)
    {
        List<WorkFileUserLog> workFileUserLogList = workFileUser.getWorkFileUserLogList();
        Long id = workFileUser.getId();
        if (StringUtils.isNotNull(workFileUserLogList))
        {
            List<WorkFileUserLog> list = new ArrayList<WorkFileUserLog>();
            for (WorkFileUserLog workFileUserLog : workFileUserLogList)
            {
                workFileUserLog.setFileUserId(id);
                list.add(workFileUserLog);
            }
            if (list.size() > 0)
            {
                workFileUserMapper.batchWorkFileUserLog(list);
            }
        }
    }
}
