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

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceDeptUnReadCountVO;
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogReadDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
import cn.iocoder.yudao.module.system.dal.mysql.worklog.LogReadMapper;
import cn.iocoder.yudao.module.system.service.worklog.dto.LogReadUserRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

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

    @Resource
    private LogReadMapper logReadMapper;

    @Resource
    private DeptMapper deptMapper;

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

    @Override
    public List<LogReadDO> getReadListByReadUserId(Long userId) {

        QueryWrapper<LogReadDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT start_user_id");
        queryWrapper.eq("read_user_id", userId);

        return logReadMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean isReadByLogId(Long logId) {

        Long count = logReadMapper.selectCount(new LambdaQueryWrapperX<LogReadDO>()
                .eq(LogReadDO::getLogInstanceId, logId)
                .eq(LogReadDO::getReadStatus, 1));

        return count > 0L;
    }

    @Override
    public Long getUnRead(Long userId) {

        return logReadMapper.selectCount(new LambdaQueryWrapperX<LogReadDO>()
                .eq(LogReadDO::getReadUserId, userId)
                .eq(LogReadDO::getReadStatus, 0));
    }

    @Override
    public void setRead(Long loginUserId) {

        logReadMapper.update(new LogReadDO().setReadStatus(1), new LambdaQueryWrapperX<LogReadDO>()
                .eq(LogReadDO::getReadUserId, loginUserId)
                .eq(LogReadDO::getReadStatus, 0));
    }

    @Override
    public List<LogInstanceDeptUnReadCountVO> getDeptUnReadCount(List<Long> deptIds) {

        return logReadMapper.selectDeptUnReadCount(getLoginUserId(), deptIds);
    }
}
