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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.attendance.mapper.EduAttendanceDeviceGroupMapper;
import com.yida.data.attendance.mapper.EduDeviceGroupMapper;
import com.yida.data.attendance.service.EduAttendanceDeviceGroupService;
import com.yida.data.attendance.vo.DeviceOrDeviceGroupVO;
import com.yida.data.common.core.entity.CurrentUser;
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.system.Dept;
import com.yida.data.common.core.entity.system.enums.RoleEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.core.utils.RoleUtil;
import com.yida.data.system.feign.RemoteDeptService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
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 15:27:11
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduAttendanceDeviceGroupServiceImpl extends ServiceImpl
    <EduAttendanceDeviceGroupMapper, EduAttendanceDeviceGroup> implements EduAttendanceDeviceGroupService {

    private final EduDeviceGroupMapper eduDeviceGroupMapper;

    private final RemoteDeptService remoteDeptService;

    @Resource
    private EduAttendanceDeviceGroupMapper eduAttendanceDeviceGroupMapper;

    /**
     * 分页查分组
     *
     * @param current
     * @param size
     * @return
     */

    @Override
    public IPage<EduAttendanceDeviceGroup> selectGroup(String name, Long current, Long size, Long schoolId)
        throws NoSuchMethodException {
        Page<EduAttendanceDeviceGroup> page = new Page<>(current, size);
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        // 获取登陆用户的最大角色
        RoleEnum roleEnum = RoleUtil.getMaxRole(currentUser);

        List<Long> schoolIds = new ArrayList<>();
        // 代理商相关账号
        if (RoleEnum.ROLE_AGENT_ADMIN.equals(roleEnum) || RoleEnum.ROLE_AGENT_NORMAL.equals(roleEnum)) {
            if (schoolId != null) {
                schoolIds.add(schoolId);
            } else {
                List<Dept> deptList = remoteDeptService.getSchoolByArea(null, null).getData();
                if (CollUtil.isNotEmpty(deptList)) {
                    schoolIds = deptList.stream().map(Dept::getDeptId).collect(Collectors.toList());
                }
            }
        } else {
            // 学校账号
            schoolIds.add(currentUser.getDeptId());
        }

        return baseMapper.selectGroup(page, name, schoolIds);
    }

    @Override
    public List<EduAttendanceDeviceGroup> selectGroupAll() {

        return eduAttendanceDeviceGroupMapper.selectList(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertGroup(EduAttendanceDeviceGroup eduAttendanceDeviceGroup) {

        baseMapper.insert(eduAttendanceDeviceGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGroup(EduAttendanceDeviceGroup eduAttendanceDeviceGroup) {
        baseMapper.updateById(eduAttendanceDeviceGroup);
    }

    @Override
    public void delGroup(Long id) {

        removeById(id);
    }

    @Override
    public List<DeviceOrDeviceGroupVO> listDeviceAndDevice(String keyword, Long schoolId) {
        return baseMapper.listDeviceAndGroup(keyword, schoolId);
    }

    @Override
    public List<EduAttendanceDevice> listDeviceByGroup(List<Long> groupIds) {
        return baseMapper.listDeviceByGroup(groupIds);
    }
}
