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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.attendance.dto.BssidImportDTO;
import com.yida.data.attendance.excel.BssidExcelListener;
import com.yida.data.attendance.mapper.AttendanceRuleWifiMapper;
import com.yida.data.attendance.service.AttendanceRuleWifiService;
import com.yida.data.common.core.entity.attendance.attendance.AttendanceRule;
import com.yida.data.common.core.entity.attendance.attendance.AttendanceRuleObjUser;
import com.yida.data.common.core.entity.attendance.attendance.AttendanceRuleWifi;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.common.core.utils.FileUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.school.dto.smart.GuideRosterErrorExportDTO;
import com.yida.data.school.dto.smart.GuideRosterImportDTO;
import com.yida.data.school.vo.smart.ImportRosterDataVO;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StopWatch;

/**
 * 考勤规则关联wifi Service实现
 *
 * @date 2023-08-09 11:37:25
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class AttendanceRuleWifiServiceImpl extends ServiceImpl
    <AttendanceRuleWifiMapper, AttendanceRuleWifi> implements AttendanceRuleWifiService {

    private final CommonService commonService;

    @Override
    public void ImportBssid(String fileUrl, Long ruleId) {
        // 上传的excel文件
        File file = null;
        // 错误信息文件excel
        File errorFile = null;
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            // 调用导入方法
            List<GuideRosterErrorExportDTO> errorList = new ArrayList<>();
            file = new File(fileUrl);
            EasyExcel.read(file, BssidImportDTO.class,
                new BssidExcelListener(this, ruleId))
                .sheet().headRowNumber(1).doRead();
            stopWatch.stop();
            log.info("导入指南花名册数据总共耗时: {}", stopWatch.getTotalTimeSeconds() + "秒");
        } catch (Exception e) {
            log.error("导入指南花名册数据失败: {}", e.getMessage(), e);
            throw new FebsException("导入失败！请重新操作");
        } finally {
            if (Objects.nonNull(file)) {
                cn.hutool.core.io.FileUtil.del(file);
            }
            if (Objects.nonNull(errorFile)) {
                cn.hutool.core.io.FileUtil.del(errorFile);
            }
        }
    }

    @Override
    public void addBssidList(ConcurrentHashMap<Integer, BssidImportDTO> concurrentHashMap, Long ruleId) {
        List<AttendanceRuleWifi> wifiList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(concurrentHashMap)) {
            for (Map.Entry<Integer, BssidImportDTO> entry : concurrentHashMap.entrySet()) {
                AttendanceRuleWifi wifi = new AttendanceRuleWifi();
                if (ObjectUtil.isNotNull(entry.getValue().getWifiBssid()) && ObjectUtil
                    .isNotNull(entry.getValue().getWifiName())) {
                    wifi.setWifiBssid(entry.getValue().getWifiBssid());
                    wifi.setWifiName(entry.getValue().getWifiName());
                    wifi.setRuleId(ruleId);
                    wifiList.add(wifi);
                }
            }
        }
        if (CollUtil.isNotEmpty(wifiList)) {
            saveBatch(wifiList);

            //清除缓存信息
            commonService.clearAttendanceRuleData();
        }
    }
}
