package com.zd.laboratory.service.impl;

import cn.hutool.core.util.StrUtil;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.utils.DictUtils;
import com.zd.laboratory.domain.LabSubject;
import com.zd.laboratory.domain.bigview.HardwareState;
import com.zd.laboratory.domain.bigview.HazardTotal;
import com.zd.laboratory.domain.bigview.SenseState;
import com.zd.laboratory.domain.vo.LabOnlineSubVO;
import com.zd.laboratory.domain.vo.LabSubjectVO;
import com.zd.laboratory.mapper.LabHardwareStateMapper;
import com.zd.laboratory.mapper.LabSubjectMapper;
import com.zd.laboratory.service.ILabSubjectLayoutService;
import com.zd.system.api.domain.SysUser;
import com.zd.system.api.laboratory.domain.LabSubjectControllerVO;
import com.zd.system.api.laboratory.domain.SubAddrr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: zhoupan
 * @Date: 2021/09/13/15:09
 * @Description:
 */
@Service
public class BigViewServiceImpl {


    @Autowired
    LabHardwareStateMapper hardwareStateMapper;

    @Autowired
    LabSubjectManagerService labSubjectManagerService;

    @Autowired
    LabSubjectMapper labSubjectMapper;

    @Autowired
    ILabSubjectLayoutService subjectLayoutService;

//    /**
//     * 获取设备状态echaerts图表
//     * @return
//     */
//    public Option queryHardwareStateEcherts()
//    {
//        List<HardwareState> hardwareStates = queryHardwareState();
//
//
//    }

    /**
     * 查询硬件状态统计
     *
     * @return
     */
    public List<HardwareState> queryHardwareState() {
        return hardwareStateMapper.queryHardwareState();
    }


    /**
     * 查询传感器状态统计
     *
     * @return
     */
    public List<SenseState> querySenseState() {
        return hardwareStateMapper.querySenseState();
    }


    /**
     * 查询危险源统计
     *
     * @return
     */
    public List<HazardTotal> queryHazardRank() {
        return hardwareStateMapper.queryHazardRank();

    }

    /**
     * 查询重点实验室
     *
     * @param size
     * @return
     */
    public  List<LabSubjectControllerVO> queryFocusSub(int size, Long buildId) {
        LabSubject labSubject = new LabSubject();
        labSubject.setIsDel(false);
        labSubject.setBuildId(buildId);
        List <LabSubjectVO> labSubjectVOS = labSubjectMapper.selectLabSubjectVOList(labSubject);
//        List<LabSubject> labSubjects = hardwareStateMapper.queryFocusSub(size);

       return labSubjectManagerService.completeMainSubInfo(labSubjectVOS);
    }


    /**
     * 实验室在线人数
     * @return
     */
    public List<com.zd.system.api.laboratory.domain.LabSubject> querySubOnline(Map<String, Object> params)
    {

        List<com.zd.system.api.laboratory.domain.LabSubject> labSubjects = hardwareStateMapper.querySubOnline(params);

        //2.1.获取详细地址后面做(已完成)
        Long[] layoutIds = labSubjects.stream().map(a -> a.getLayoutId()).filter(a -> a != null).toArray(Long[]::new);
        List<SubAddrr> subAddrrs = subjectLayoutService.selectAddrrByLayoutIds(layoutIds);

        Map<Long, SubAddrr> subAddrrMap = Optional.ofNullable(subAddrrs)
                .orElse(Collections.emptyList())
                .stream()
                .collect(Collectors.toMap(SubAddrr::getId, Function.identity()));

        /*return labSubjects.stream()
                .map(a -> {
                    SubSimpleVO simpleVO = new SubSimpleVO();
                    simpleVO.setSubId(a.getId());
                    simpleVO.setAddress(Optional.ofNullable(subAddrrMap.get(a.getLayoutId())).map(SubAddrr::addrrPlus).orElse(StrUtil.EMPTY));
                    simpleVO.setOnline(a.getOnline());
                    simpleVO.setSubName(a.getName());
                    return simpleVO;
                })
                .collect(Collectors.toList());*/

        labSubjects.forEach(sub -> {
            sub.setAddress(Optional.ofNullable(subAddrrMap.get(sub.getLayoutId())).map(SubAddrr::addrrPlus).orElse(StrUtil.EMPTY));
            // subDept 实验室分类
            sub.setSubDept(DictUtils.getDictCacheLable("subject_class",sub.getSubDept()));
            // 风险等级
            //sub.setSubClassVO(labSubjectManagerService.getSubClassVO(sub.getId()));
            sub.setSubClassVO(null);
        });


        return labSubjects;

    }


    /**
     * 实验室在线人数
     * @return
     */
    @DataScope(deptAlias = "s", permi = PerPrefix.LABORATORY_SUBJECT)
    public List<LabOnlineSubVO> onlineByBigView(LabSubjectVO labSubjectVO)
    {
        List<LabOnlineSubVO> labSubjects = hardwareStateMapper.onlineByBigView(labSubjectVO);
        return labSubjects;
    }

    /**
     * 实验室在线人数
     * @return
     */
    @DataScope(deptAlias = "s", permi = PerPrefix.LABORATORY_SUBJECT)
    public LinkedHashMap<String,Object> onlineSumByBigView(LabSubjectVO labSubjectVO)
    {
        List<LabOnlineSubVO> labSubjects = hardwareStateMapper.onlineByBigView(labSubjectVO);
        LongAdder result = new LongAdder();
        Optional.ofNullable(labSubjects).orElseGet(Collections::emptyList).stream().forEach(a->{
            result.add(a.getOnline());
        });
        LinkedHashMap<String,Object> allList = new LinkedHashMap<>();
        allList.put("resultCount",result.sum());
        allList.put("list",labSubjects);
        return allList;
    }

    /**
     * 查询实验室人员信息
     * 实验室id subId
     */
    public List<SysUser> querySubOnlineUser(Map<String, Object> params) {
        return hardwareStateMapper.querySubOnlineUser(params);
    }

    @DataScope(deptAlias = "t", permi = PerPrefix.LABORATORY_SUBJECT)
    public List<LabSubjectControllerVO> queryMySubview(LabSubject labSubject) {
        List <LabSubjectVO> labSubjectVOS = labSubjectMapper.selectLabSubjectVOList(labSubject);

        return labSubjectManagerService.completeMainSubInfo(labSubjectVOS);
    }
}
