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.common.core.entity.user.EduStaff;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.common.core.entity.user.EduUserDevice;
import com.yida.data.user.mapper.EduStaffMapper;
import com.yida.data.user.mapper.EduStudentMapper;
import com.yida.data.user.mapper.EduUserDeviceMapper;
import com.yida.data.user.service.EduUserDeviceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EduUserDeviceServiceImpl extends ServiceImpl<EduUserDeviceMapper, EduUserDevice> implements EduUserDeviceService {

    @Resource
    private EduStudentMapper eduStudentMapper;
    @Resource
    private EduStaffMapper eduStaffMapper;

    @Override
    public void saveUserDevice(EduUserDevice userDevice) {
        EduUserDevice existStudentDevice =
                getOne(Wrappers.lambdaQuery(new EduUserDevice()).eq(EduUserDevice::getUserId,
                        userDevice.getUserId()).eq(EduUserDevice::getUserType, userDevice.getUserType()));
        // 已存在记录直接更新  不进行采集状态更改
        EduStudent student =
                eduStudentMapper.selectOne(Wrappers.lambdaQuery(new EduStudent()).eq(EduStudent::getId, userDevice.getUserId()));
        //student.setAvatar(userDevice.getPicPath());
        if (existStudentDevice != null) {
            userDevice.setId(existStudentDevice.getId());
            updateById(userDevice);
        } else {
            if (userDevice.getUserType() == 0) {
                student.setCollectType(1);
            } else {
                EduStaff staff =
                        eduStaffMapper.selectOne(Wrappers.lambdaQuery(new EduStaff()).eq(EduStaff::getId,
                                userDevice.getUserId()));
                staff.setCollectType(1);
                eduStaffMapper.updateById(staff);
            }
            save(userDevice);
        }
        eduStudentMapper.updateById(student);
    }

    @Override
    public EduUserDevice getUserDeviceByUser(Long studentId, Integer type) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(new EduUserDevice()).eq(EduUserDevice::getUserId,
                studentId).eq(EduUserDevice::getUserType, type));
    }

    @Override
    public void delUserDevice(Long[] userIds, Integer type, Long faceGroupId) {
        // 修改采集状态为未采集
        //if (type == 0) {
        //    EduStudent student = new EduStudent();
        //    student.setCollectType(0);
        //    student.setId(userId);
        //    eduStudentMapper.updateById(student);
        //} else {
        //    EduStaff staff = new EduStaff();
        //    staff.setId(userId);
        //    staff.setCollectType(0);
        //    eduStaffMapper.updateById(staff);
        //}
        baseMapper.delete(Wrappers.lambdaQuery(new EduUserDevice()).in(EduUserDevice::getUserId,
                userIds).eq(EduUserDevice::getUserType, type).eq(EduUserDevice::getFaceGroupId, faceGroupId));
    }

    @Override
    public void batchSaveUserDevice(List<EduUserDevice> list) {
        List<EduUserDevice> saveData = new ArrayList<>();
        for (EduUserDevice userDevice : list) {
            long count = count(Wrappers.<EduUserDevice>lambdaQuery()
                    .eq(EduUserDevice::getUserId, userDevice.getUserId())
                    .eq(EduUserDevice::getUserType, userDevice.getUserType())
                    .eq(EduUserDevice::getDeviceId, userDevice.getDeviceId())
                    .eq(EduUserDevice::getFaceGroupId, userDevice.getFaceGroupId()));
            if (count == 0) {
                saveData.add(userDevice);
            }
        }
        if (CollUtil.isNotEmpty(saveData)) {
            saveBatch(list);
        }
    }
}
