package com.zd.laboratory.service.impl;

import cn.hutool.core.util.StrUtil;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.laboratory.domain.vo.LabSubjectVO;
import com.zd.laboratory.domain.vo.ToDoWork;
import com.zd.laboratory.service.ILabSubjectService;
import com.zd.system.api.laboratory.domain.LabMessageUser;
import com.zd.laboratory.domain.LabSubjectAccessRecord;
import com.zd.laboratory.domain.vo.LabSubjectAccessRecordVo;
import com.zd.laboratory.domain.vo.LabWorkBenchVO;
import com.zd.laboratory.mapper.LabMessageUserMapper;
import com.zd.laboratory.mapper.LabSubjectAccessRecordMapper;
import com.zd.laboratory.mapper.LabWorkBenchMapper;
import com.zd.laboratory.service.ILabWorkBenchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 工作台Service业务层处理
 *
 * @author zd
 * @date 2021-08-30
 */
@Service
public class LabWorkBenchServiceImpl implements ILabWorkBenchService {

    @Autowired
    private LabMessageUserMapper labMessageUserMapper;

    @Autowired
    private LabSubjectAccessRecordMapper labSubjectAccessRecordMapper;

    @Autowired
    private LabWorkBenchMapper labWorkBenchMapper;

    @Autowired
    private ILabSubjectService labSubjectService;

    /**
     * 查询未读消息统计
     *
     * @param messageUser 消息对象
     * @return 未读消息统计数量
     */
    @Override
    public int unreadMessage(LabMessageUser messageUser) {
        List<LabMessageUser> messageUserList = labMessageUserMapper.selectLabMessageUserList(messageUser);
        return messageUserList.size();
    }


    /**
     * 实验室进出记录前五条
     *
     * @param access 对象
     * @return 实验室进出记录前五条
     */
    @Override
    public List<LabSubjectAccessRecordVo> inoutFirstFive(LabSubjectAccessRecord access) {
        List<LabSubjectAccessRecordVo> accessList = labSubjectAccessRecordMapper.selectLabInoutFirstFiveList(access);
        return accessList;
    }


    /**
     * 工作台实验室风险统计
     *
     * @return 工作台实验室风险统计
     */
    @Override
    public LabWorkBenchVO riskStatistics() {
        LabWorkBenchVO workBench = labWorkBenchMapper.riskStatistics();
        return workBench;
    }

    /**
     * 查询待办工作
     *
     * @return
     */
    @Override
    public List<ToDoWork> queryDoWork(Long size) {
        //获取当前用户负责的实验室
        Long userId = SecurityUtils.getUserId();
        List<LabSubjectVO> labSubjectVOS = labSubjectService.selectLabSubjectListByAdminIds(userId);
        String ids = Optional.ofNullable(labSubjectVOS).orElse(Arrays.asList())
                .stream()
                .map(a -> a.getId().toString())
                .collect(Collectors.joining(","));
        if (StrUtil.isEmpty(ids)) return Arrays.asList();

        return labWorkBenchMapper.toDoWorkStatistics(ids, size);

    }
}
