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.attendance.feign.RemoteAttendanceStrategyService;
import com.yida.data.common.core.entity.attendance.EduAttendanceDevice;
import com.yida.data.common.core.entity.user.EduFaceGroupDevice;
import com.yida.data.common.core.entity.user.EduFaceGroupStrategy;
import com.yida.data.common.core.entity.user.EduFaceGroupUser;
import com.yida.data.user.dto.BindFaceGroupDeviceDTO;
import com.yida.data.user.mapper.EduFaceGroupDeviceMapper;
import com.yida.data.user.service.EduFaceGroupDeviceService;
import com.yida.data.user.service.EduFaceGroupStrategyService;
import com.yida.data.user.service.EduFaceGroupUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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

    @Lazy
    @Resource
    private EduFaceGroupUserService eduFaceGroupUserService;

    @Lazy
    @Resource
    private EduFaceGroupStrategyService eduFaceGroupStrategyService;

    private final RemoteAttendanceDeviceService remoteAttendanceDeviceService;
    private final RemoteAttendanceStrategyService remoteAttendanceStrategyService;

    @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);
        // 查询是否有策略信息
        EduFaceGroupStrategy groupStrategy = eduFaceGroupStrategyService.getOne(Wrappers.lambdaQuery(new EduFaceGroupStrategy())
                .eq(EduFaceGroupStrategy::getFaceGroupId, dto.getFaceGroupId()));
        if (Objects.isNull(groupStrategy)) {
            return;
        }
        // 处理设备策略信息
        groupStrategy.setFaceGroupId(dto.getFaceGroupId());
        // 查询关联的设备信息
        List<EduFaceGroupDevice> list = list(Wrappers.lambdaQuery(new EduFaceGroupDevice())
                .eq(EduFaceGroupDevice::getFaceGroupId, dto.getFaceGroupId()));
        if (CollUtil.isEmpty(list)) {
            log.info("当前人脸组策略没有对应的设备信息: {}", dto.getFaceGroupId());
            return;
        }
        // 设置设备信息
        groupStrategy.setGroupDeviceList(list);
        List<EduFaceGroupUser> groupUserList = eduFaceGroupUserService.list(Wrappers.lambdaQuery(new EduFaceGroupUser())
                .eq(EduFaceGroupUser::getFaceGroupId, groupStrategy.getFaceGroupId())
                // 已下发人脸
                .eq(EduFaceGroupUser::getStatus, 1)
        );
        groupStrategy.setGroupUserList(groupUserList);
        // 设置人员信息
        remoteAttendanceStrategyService.deleteAndSaveStrategy(groupStrategy);
    }

    @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());
                device.setState(x.getState());
                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(EduFaceGroupDevice::getDeviceId)
                    .collect(Collectors.toList());
            List<Long> deviceGroupIds = deviceAndGroup.stream().filter(x -> x.getType() == 1).map(EduFaceGroupDevice::getDeviceId)
                    .collect(Collectors.toList());
            if (CollUtil.isNotEmpty(deviceGroupIds)) {
                // 根据设备组查询设备
                List<EduAttendanceDevice> deviceListByGroup = remoteAttendanceDeviceService
                        .listDeviceByGroup(deviceGroupIds.toArray(new Long[0])).getData();
                res.addAll(deviceListByGroup.stream().map(EduAttendanceDevice::getId).collect(Collectors.toList()));
            }
            if (CollUtil.isNotEmpty(deviceIds)) {
                res.addAll(deviceIds);
            }
        }
        return res;
    }

    @Override
    public List<Long> listFaceGroupOnlineDevice(Long faceGroupId) {
        List<Long> res = new ArrayList<>();
        
        return res;
    }
}
