package com.zd.laboratory.service.impl;

import com.zd.common.core.domain.R;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.laboratory.domain.LabBuilding;
import com.zd.laboratory.domain.LabHazard;
import com.zd.laboratory.domain.LabSubject;
import com.zd.laboratory.domain.LabSubjectLayout;
import com.zd.laboratory.domain.vo.LabBuildingInfoVO;
import com.zd.laboratory.domain.vo.LabBuildingVO;
import com.zd.laboratory.domain.vo.LabHazardVO;
import com.zd.laboratory.domain.vo.LabSubjectVO;
import com.zd.laboratory.mapper.LabBuildingMapper;
import com.zd.laboratory.mapper.LabHazardMapper;
import com.zd.laboratory.mapper.LabSubjectLayoutMapper;
import com.zd.laboratory.mapper.LabSubjectMapper;
import com.zd.laboratory.service.ILabBuildingService;
import com.zd.system.api.RemoteDeptService;
import com.zd.system.api.domain.SysDept;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 楼栋Service业务层处理
 *
 * @author zd
 * @date 2021-09-01
 */
@Service
public class LabBuildingServiceImpl implements ILabBuildingService {
    @Autowired
    private LabBuildingMapper labBuildingMapper;

    @Autowired
    private LabSubjectMapper labSubjectMapper;

    @Autowired
    private LabHazardMapper labHazardMapper;

    @Autowired
    private LabSubjectLayoutMapper labSubjectLayoutMapper;

    /**
     * 查询楼栋
     *
     * @param id 楼栋主键
     * @return 楼栋
     */
    @Override
    public LabBuilding selectLabBuildingById(Long id) {
        return labBuildingMapper.selectLabBuildingById(id);
    }

    /**
     * 查询楼栋列表
     *
     * @param labBuilding 楼栋
     * @return 楼栋
     */
    @Override
    public List<LabBuilding> selectLabBuildingList(LabBuilding labBuilding) {
        return labBuildingMapper.selectLabBuildingList(labBuilding);
    }

    /**
     * 新增楼栋
     *
     * @param labBuilding 楼栋
     * @return 结果
     */
    @Override
    public int insertLabBuilding(LabBuilding labBuilding) {
        labBuilding.setCreateTime(DateUtils.getNowDate());
        return labBuildingMapper.insertLabBuilding(labBuilding);
    }


    /**
     * 修改楼栋
     *
     * @param labBuilding 楼栋
     * @return 结果
     */
    @Override
    public int updateLabBuilding(LabBuilding labBuilding) {
        labBuilding.setUpdateTime(DateUtils.getNowDate());
        return labBuildingMapper.updateLabBuilding(labBuilding);
    }

    /**
     * 批量删除楼栋
     *
     * @param ids 需要删除的楼栋主键
     * @return 结果
     */
    @Override
    public int deleteLabBuildingByIds(Long[] ids) {
        return labBuildingMapper.deleteLabBuildingByIds(ids);
    }

    /**
     * 删除楼栋信息
     *
     * @param id 楼栋主键
     * @return 结果
     */
    @Override
    public int deleteLabBuildingById(Long id) {
        return labBuildingMapper.deleteLabBuildingById(id);
    }


    /**
     * 查询楼栋列表(不分页)
     *
     * @param labBuilding 楼栋
     * @return 楼栋
     */
    @Override
    public List<LabBuilding> getBuildLlist(LabBuilding labBuilding) {
        return labBuildingMapper.getBuildLlist(labBuilding);
    }


    /**
     * 实验室布局图列表
     *
     * @return 实验室布局图集合
     */
    @Override
    public List<Map<String, Object>> labLayout(LabBuildingVO labBuilding) {
        List<Map<String, Object>> newFloorList = floorByHazrdList(labBuilding);
        return newFloorList;
    }

    /**
     * 数据大屏实验室以及信息布局图
     *
     */
    @Override
    public List<LabBuildingInfoVO> labInfoLayout(LabBuildingVO labBuilding) {

        return labBuildingMapper.queryLabInfoLayout(labBuilding);
    }

    /**
     * 实验室布局图列表
     *
     * @return 实验室布局图集合
     */
    public List<Map<String, Object>> floorByHazrdList(LabBuildingVO build) {
        LabBuilding labBuilding = new LabBuilding();
        labBuilding.setDeptId(build.getDeptId());
//        labBuilding.setParentId(build.getId());
        List<LabBuilding> oldFloorList = labBuildingMapper.selectLabBuildingList(labBuilding);
        if (oldFloorList.size() > 0) {
            Long[] ids = new Long[oldFloorList.size()];
            for (int i = 0; i < ids.length; i++) {
                ids[i] = oldFloorList.get(i).getId();
            }
            if (build.getShowHazard()) {
                List<LabSubjectVO> oldSubjectList = labSubjectMapper.selectLabSubjectListByIds(ids);
                Map<Long, List<LabSubjectVO>> mapTable = getMapTable(oldSubjectList, build.getShowHazard());
                List<Map<String, Object>> newFloorList = oldFloorList.stream().map(a -> {
                    List<LabSubjectVO> labSubjects = mapTable.get(a.getId());
                    Map<String, Object> floorMap = new HashMap<>();
                    floorMap.put("id", a.getId());
                    floorMap.put("name", a.getName());
                    floorMap.put("remark", a.getRemark());
                    floorMap.put("url", a.getUrl());
                    floorMap.put("list", labSubjects);
                    return floorMap;
                }).collect(Collectors.toList());
                return newFloorList;
            } else {
                List<LabSubjectLayout> oldLayoutList = labSubjectLayoutMapper.selectLayoutListByIds(ids);
                List<Map<String, Object>> newFloorList = oldFloorList.stream().map(a -> {
                    List<LabSubjectLayout> newLayoutList = oldLayoutList.stream().filter(h -> h.getFloorId().equals(a.getId())).collect(Collectors.toList());
                    Map<String, Object> floorMap = new HashMap<>();
                    floorMap.put("id", a.getId());
                    floorMap.put("name", a.getName());
                    floorMap.put("remark", a.getRemark());
                    floorMap.put("url", a.getUrl());
                    floorMap.put("list", newLayoutList);
                    return floorMap;
                }).collect(Collectors.toList());
                return newFloorList;
            }
        }
        return new ArrayList<>();

    }

    /**
     * 楼层和房子的映射
     *
     * @param oldSubjectList
     * @return
     */
    public Map<Long, List<LabSubjectVO>> getMapTable(List<LabSubjectVO> oldSubjectList, boolean showHazard) {
        //查询所有实验室下的危险源
        Long[] ids = new Long[oldSubjectList.size()];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = oldSubjectList.get(i).getId();
        }
        List<LabHazardVO> hazardList = getHazardList(ids);
        Map<Long, List<LabSubjectVO>> collect = oldSubjectList.stream().map(a -> {
            LabSubjectVO subjectVo = new LabSubjectVO();
            BeanUtils.copyProperties(a, subjectVo);
            if (showHazard) {
                List<LabHazardVO> newHazardList = hazardList.stream().filter(h -> h.getSubjectId().equals(a.getId())).collect(Collectors.toList());
                subjectVo.setLabHazardList(newHazardList);
            }
            return subjectVo;
        }).collect(Collectors.groupingBy(LabSubjectVO::getFloorId));
        return collect;
    }

    /**
     * 楼层和房子的映射
     *
     * @param ids
     * @return
     */
    public List<LabHazardVO> getHazardList(Long[] ids) {
        return labHazardMapper.getHazardByIds(ids);
    }

}
