package cn.iocoder.yudao.module.system.service.worklog;

import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogReadDo;
import cn.iocoder.yudao.module.system.dal.mysql.worklog.LogReadMapper;
import cn.iocoder.yudao.module.system.service.worklog.dto.LogReadUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 日志查看 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class LogReadServiceImpl implements LogReadService{

    @Resource
    private LogReadMapper logReadMapper;

    @Override
    public void createLogRule(List<LogReadUserRespDTO> createReqVO, Long logInstanceId, Long startUserId) {

        List<LogReadDo> userInfoList = createReqVO.stream()
                .map(user -> {
                    LogReadDo logReadDo = new LogReadDo();
                    logReadDo.setLogInstanceId(logInstanceId);
                    logReadDo.setStartUserId(startUserId);
                    logReadDo.setReadUserDept(user.getDeptId());
                    logReadDo.setReadUserId(user.getUserId());
                    logReadDo.setReadStatus(0);
                    logReadDo.setCreator(String.valueOf(startUserId));
                    return logReadDo;
                })
                .collect(Collectors.toList());

        logReadMapper.insertBatch(userInfoList);
    }

    @Override
    public Map<Integer, List<Long>> getUserId(Long logId) {

        Map<Integer, List<Long>> readDoMap = new HashMap<>();

        List<LogReadDo> UnReadDos = logReadMapper.selectUserByLogId(logId, 0);
        List<LogReadDo> readDos = logReadMapper.selectUserByLogId(logId, 1);

        readDoMap.put(0, convertList(UnReadDos, LogReadDo::getReadUserId));
        readDoMap.put(1, convertList(readDos, LogReadDo::getReadUserId));

        return readDoMap;
    }

    @Override
    public void updateReadStatus(Long logId, Long userId) {

        logReadMapper.updateReadStatus(logId, userId);
    }
}
