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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.user.EduFaceGroupDevice;
import com.yida.data.user.dto.BindFaceGroupDeviceDTO;
import com.yida.data.user.mapper.EduFaceGroupDeviceMapper;
import com.yida.data.user.service.EduFaceGroupDeviceService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 人脸组、设备组关系 Service实现
 *
 * @author zhanghaijun
 * @date 2021-06-16 14:15:08
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduFaceGroupDeviceServiceImpl extends ServiceImpl
    <EduFaceGroupDeviceMapper, EduFaceGroupDevice> implements EduFaceGroupDeviceService {

    private final RemoteAttendanceDeviceService remoteAttendanceDeviceService;

    @Override
    @Transactional
    public void bindFaceGroupAndDevice(BindFaceGroupDeviceDTO dto) {
        remove(Wrappers.lambdaQuery(new EduFaceGroupDevice()).eq(EduFaceGroupDevice::getFaceGroupId, dto.getFaceGroupId()));
        List<EduFaceGroupDevice> saveList = dto.getDeviceList().stream().map(x -> {
            x.setFaceGroupId(dto.getFaceGroupId());
            return x;
        }).collect(Collectors.toList());
        saveBatch(saveList);
    }

    @Override
    public List<EduFaceGroupDevice> listFaceGroupDevice(Long faceGroupId) {
        List<EduFaceGroupDevice> list = list(
            Wrappers.lambdaQuery(new EduFaceGroupDevice()).eq(EduFaceGroupDevice::getFaceGroupId, faceGroupId));
        if (CollUtil.isNotEmpty(list)) {
            list = list.stream().map(x -> {
                EduFaceGroupDevice device = new EduFaceGroupDevice();
                device.setDeviceId(x.getDeviceId());
                device.setDeviceName(x.getDeviceName());
                device.setType(x.getType());
                return device;
            }).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public List<Long> listDeviceByFaceGroup(Long faceGroupId) {
        List<Long> res = new ArrayList<>();
        List<EduFaceGroupDevice> deviceAndGroup = listFaceGroupDevice(faceGroupId);
        if (CollUtil.isNotEmpty(deviceAndGroup)) {
            List<Long> deviceIds = deviceAndGroup.stream().filter(x -> x.getType() == 0).map(x -> x.getDeviceId())
                .collect(Collectors.toList());
            List<Long> deviceGroupIds = deviceAndGroup.stream().filter(x -> x.getType() == 1).map(x -> x.getDeviceId())
                .collect(Collectors.toList());
            if (CollUtil.isNotEmpty(deviceGroupIds)) {
                // 根据设备组查询设备
                List<EduAttendanceDevice> deviceListByGroup = remoteAttendanceDeviceService
                    .listDeviceByGroup(deviceGroupIds.toArray(new Long[0])).getData();
                res.addAll(deviceListByGroup.stream().map(x -> x.getId()).collect(Collectors.toList()));
            }
            if (CollUtil.isNotEmpty(deviceIds)) {
                res.addAll(deviceIds);
            }
        }
        return res;
    }
}
