package com.yida.data.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.attendance.feign.RemoteAttendanceDeviceService;
import com.yida.data.common.core.entity.attendance.EduAttendanceDevice;
import com.yida.data.common.core.entity.attendance.EduAttendanceDeviceGroup;
import com.yida.data.common.core.entity.user.EduFaceGroup;
import com.yida.data.common.core.entity.user.EduFaceGroupDevice;
import com.yida.data.common.core.entity.user.EduFaceGroupUser;
import com.yida.data.user.dto.DelFaceGroupUserDTO;
import com.yida.data.user.mapper.EduFaceGroupDeviceMapper;
import com.yida.data.user.mapper.EduFaceGroupMapper;
import com.yida.data.user.service.EduFaceGroupService;
import com.yida.data.user.service.EduFaceGroupUserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 人脸组信息 Service实现
 *
 * @author zhanghaijun
 * @date 2021-06-16 14:09:44
 */
@Service
@RequiredArgsConstructor
@Transactional
public class EduFaceGroupServiceImpl extends ServiceImpl
    <EduFaceGroupMapper, EduFaceGroup> implements EduFaceGroupService {

    private final EduFaceGroupUserService eduFaceGroupUserService;
    private final EduFaceGroupDeviceMapper eduFaceGroupDeviceMapper;
    private final RemoteAttendanceDeviceService remoteAttendanceDeviceService;

    @Override
    public IPage<EduFaceGroup> listFaceGroupPage(Page page, String groupName, Long schoolId) {
        IPage<EduFaceGroup> res = baseMapper.listFaceGroupPage(page, groupName, schoolId);
        // 获取所有设备和设备组id
        List<Long> deviceIds = new ArrayList<>();
        List<Long> groupIds = new ArrayList<>();
        for (EduFaceGroup record : res.getRecords()) {
            if (CollUtil.isNotEmpty(record.getDeviceList())) {
                for (EduFaceGroupDevice groupDevice : record.getDeviceList()) {
                    if (groupDevice.getType() == 0) {
                        deviceIds.add(groupDevice.getDeviceId());
                    } else if (groupDevice.getType() == 1) {
                        groupIds.add(groupDevice.getDeviceId());
                    }
                }
            }
        }
        // 查出所有的设备和设备组
        List<EduAttendanceDevice> devices = remoteAttendanceDeviceService.listDeviceById(deviceIds.toArray(new Long[0]))
            .getData();
        List<EduAttendanceDeviceGroup> groups = remoteAttendanceDeviceService.listDeviceGroupById(groupIds.toArray(new Long[0]))
            .getData();
        // 按id分组方便拿数据
        Map<Long, List<EduAttendanceDevice>> deviceMap = devices.stream()
            .collect(Collectors.groupingBy(EduAttendanceDevice::getId));
        Map<Long, List<EduAttendanceDeviceGroup>> groupMap = groups.stream()
            .collect(Collectors.groupingBy(EduAttendanceDeviceGroup::getId));
        for (EduFaceGroup record : res.getRecords()) {
            if (CollUtil.isNotEmpty(record.getDeviceList())) {
                for (EduFaceGroupDevice groupDevice : record.getDeviceList()) {
                    if (groupDevice.getType() == 0
                        && CollUtil.isNotEmpty(deviceMap)
                        && deviceMap.containsKey(groupDevice.getDeviceId())) {
                        groupDevice.setDeviceName(deviceMap.get(groupDevice.getDeviceId()).get(0).getName());
                        groupDevice.setState(deviceMap.get(groupDevice.getDeviceId()).get(0).getState());
                    } else if (groupDevice.getType() == 1
                        && CollUtil.isNotEmpty(groupMap)
                        && groupMap.containsKey(groupDevice.getDeviceId())) {
                        groupDevice.setDeviceName(groupMap.get(groupDevice.getDeviceId()).get(0).getGroupName());
                    }
                }
            }
        }
        return res;
    }


    @Override
    public void removeFaceGroup(Long groupId) {
        // 删除人脸关联信息 删除下发记录  删除设备上的人脸
        List<EduFaceGroupUser> faceGroupUserList =
            eduFaceGroupUserService
                .list(Wrappers.lambdaQuery(new EduFaceGroupUser()).eq(EduFaceGroupUser::getFaceGroupId, groupId));
        if (CollUtil.isNotEmpty(faceGroupUserList)) {
            // 删除人脸相关信息
            DelFaceGroupUserDTO dto = new DelFaceGroupUserDTO();
            dto.setFaceGroupId(groupId);
            dto.setFaceIds(faceGroupUserList.stream().map(x -> x.getId()).collect(Collectors.toList()));
            eduFaceGroupUserService.delUserBatch(dto);
        }
        //删除组
        removeById(groupId);
    }
}
