package com.yida.data.device.camera.service.impl;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.camera.EduCamera;
import com.yida.data.common.core.entity.camera.EduCameraAreaGroup;
import com.yida.data.common.core.entity.camera.EduCameraAreaGroupCamera;
import com.yida.data.common.core.entity.camera.EduCameraAuthorityTeacher;
import com.yida.data.common.core.entity.user.EduStaff;
import com.yida.data.common.core.entity.user.EduStaffDept;
import com.yida.data.device.camera.mapper.EduCameraAuthorityTeacherMapper;
import com.yida.data.device.camera.mapper.EduCameraMapper;
import com.yida.data.device.camera.service.EduCameraAreaGroupCameraService;
import com.yida.data.device.camera.service.EduCameraAuthorityTeacherService;
import com.yida.data.device.vo.camera.AuthorityByGroupIdVO;
import com.yida.data.user.feign.RemoteStaffService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static com.yida.data.common.core.entity.constant.CachePrefixConstant.STAFF_DATA;

/**
 * @author ccl
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduCameraAuthorityTeacherServiceImpl extends ServiceImpl
        <EduCameraAuthorityTeacherMapper, EduCameraAuthorityTeacher> implements EduCameraAuthorityTeacherService {

    private final EduCameraAreaGroupCameraService groupCameraService;
    private final EduCameraMapper cameraMapper;
    private final RemoteStaffService staffService;
    private final RedisService redisService;

    @Override
    public List<AuthorityByGroupIdVO> findAuthorityByGroupId(Long groupId) {
        List<EduCameraAreaGroupCamera> groupCameraList = groupCameraService.list(Wrappers
                .query(new EduCameraAreaGroupCamera()).lambda().eq(EduCameraAreaGroupCamera::getGroupId, groupId));

        List<Long> cameraIdList = new ArrayList<>();
        for (EduCameraAreaGroupCamera eduCameraAreaGroupCamera : groupCameraList) {
            cameraIdList.add(eduCameraAreaGroupCamera.getCameraId());
        }
        if (CollectionUtil.isEmpty(cameraIdList)) {
            return new ArrayList<>();
        }
        List<EduCameraAuthorityTeacher> authList = list(Wrappers.query(new EduCameraAuthorityTeacher()).lambda().in(EduCameraAuthorityTeacher::getCameraId, cameraIdList));
        Map<Integer, List<EduCameraAuthorityTeacher>> groupBy = authList.stream().collect(Collectors.groupingBy(EduCameraAuthorityTeacher::getGroupIndex));

        List<AuthorityByGroupIdVO> voList = new ArrayList<>();


        for (Integer integer : groupBy.keySet()) {
            List<EduCameraAuthorityTeacher> authGroupList = groupBy.get(integer);
            List<EduCamera> cameraList = new ArrayList<>();
            List<EduStaffDept> staffList = new ArrayList<>();


            for (EduCameraAuthorityTeacher auth : authGroupList) {
                EduCamera camera = cameraMapper.selectById(auth.getCameraId());
                cameraList.add(camera);
                EduStaff staff;

                if (ObjectUtil.isNotEmpty(redisService.hHasKey(auth.getStaffId().toString(), STAFF_DATA))) {
                    staff = (EduStaff) redisService.hget(auth.getStaffId().toString(), STAFF_DATA);
                    if (ObjectUtil.isEmpty(staff)) {
                        staff = staffService.getStaff(auth.getStaffId()).getData();
                        redisService.hset(auth.getStaffId().toString(), STAFF_DATA, staff);
                    }
                } else {
                    staff = staffService.getStaff(auth.getStaffId()).getData();
                    redisService.hset(auth.getStaffId().toString(), STAFF_DATA, staff);
                }
                EduStaffDept staffDept = new EduStaffDept();
                staffDept.setStaffId(staff.getId());
                staffDept.setStaffName(staff.getName());
                staffList.add(staffDept);

            }
            AuthorityByGroupIdVO vo = new AuthorityByGroupIdVO();


            vo.setStaffs(staffList.stream().distinct().collect(Collectors.toList()));
            vo.setGroupIndex(integer);
            vo.setCameras(cameraList.stream().distinct().collect(Collectors.toList()));
            voList.add(vo);
        }

        return voList.stream().sorted(Comparator.comparing(AuthorityByGroupIdVO::getGroupIndex)).collect(Collectors.toList());
    }

    @Override
    public void saveAuthority(List<AuthorityByGroupIdVO> vos) {

        List<Integer> indexList = new ArrayList<>();
        List<Long> groupIdList = new ArrayList<>();
        List<EduCameraAuthorityTeacher> authorityTeachers = new ArrayList<>();


        for (AuthorityByGroupIdVO vo : vos) {
            indexList.add(vo.getGroupIndex());
            groupIdList.add(vo.getGroupId());
            for (EduCamera camera : vo.getCameras()) {
                for (EduStaffDept staff : vo.getStaffs()) {
                    EduCameraAuthorityTeacher auth = new EduCameraAuthorityTeacher();
                    auth.setCameraId(camera.getId());
                    auth.setStaffId(staff.getStaffId());
                    auth.setGroupIndex(vo.getGroupIndex());
                    auth.setGroupId(vo.getGroupId());
                    authorityTeachers.add(auth);
                }

            }

        }
        remove(Wrappers.query(new EduCameraAuthorityTeacher()).lambda().eq(EduCameraAuthorityTeacher::getGroupId, vos.get(0).getGroupId()));

        saveOrUpdateBatch(authorityTeachers);
    }
}
