package com.ruoyi.web.v1.service.impl;

import com.ruoyi.web.v1.DTO.NFIDReadGroupDTO;
import com.ruoyi.web.v1.model.NFIDReadRecord;
import com.ruoyi.web.v1.model.NFIDReaderEvent;
import com.ruoyi.web.v1.model.request.ReqValidNFIDRecord;
import com.ruoyi.web.v1.mapper.NFIDReadRecordMapper;
import com.ruoyi.web.v1.service.INFIDReadRecordService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 识别记录Service业务层处理
 */
@Service
public class NFIDReadRecordServiceImpl implements INFIDReadRecordService {
    @Autowired
    private NFIDReadRecordMapper NFIDReadRecordMapper;
    private final ApplicationEventPublisher eventPublisher;

    public NFIDReadRecordServiceImpl(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;

        //测试使用
//        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
//
//        scheduler.scheduleAtFixedRate(
//                this::testBatchAdd,
//                1, 10, TimeUnit.SECONDS
//        );
    }

    //测试批量新增
    private void testBatchAdd(){
        List<NFIDReadRecord> records = new ArrayList<>();
        NFIDReadRecord record = new NFIDReadRecord();
        record.setCreateTime(DateUtils.getNowDate());
        record.setHookNo("0002");
        records.add(record);
        insertNFIDReadRecordBatch(records);
    }

    /**
     * 查询nfid识别记录信息
     *
     * @return nfid识别记录
     */
    @Override
    public List<NFIDReadRecord> selectNFIDReadRecordList(){
        return NFIDReadRecordMapper.selectNFIDReadRecordList();
    }

    /**
     * 查询可用的nfid识别记录信息
     *
     * @return nfid识别记录
     */
    @Override
    public List<NFIDReadRecord> selectValidNFIDReadRecord(ReqValidNFIDRecord req){
        return NFIDReadRecordMapper.selectValidNFIDReadRecord(req);
    }

    /**
     * 通过ID查询识记录
     *
     * @param id 记录ID
     * @return nfid识别记录信息
     */
    @Override
    public NFIDReadRecord selectNFIDReadRecordById(Long id){
        return NFIDReadRecordMapper.selectNFIDReadRecordById(id);
    }

    /**
     * 新增nfid识别记录
     *
     * @param record nfid识别记录
     * @return 结果
     */
    @Override
    public int insertNFIDReadRecord(NFIDReadRecord record){
        record.setCreateTime(DateUtils.getNowDate());
        return NFIDReadRecordMapper.insertNFIDReadRecord(record);
    }

    /**
     * 批量新增
     *
     * @param list nfid识别记录
     * @return 结果
     */
    @Override
    public int insertNFIDReadRecordBatch(List<NFIDReadRecord> list){
        int row =  NFIDReadRecordMapper.insertNFIDReadRecordBatch(list);
        if(row >0){
            // 发布事件
            for(NFIDReadRecord record : list){
                eventPublisher.publishEvent(new NFIDReaderEvent(this, record));
            }
        }
        return row;
    }
    /**
     * 通过设备序列号查询识别器信息
     *
     * @param ids 需要修改的数据主键集合
     * @return nfid识别记录信息
     */
    @Override
    public int setBindByHook(String[] ids){
        return NFIDReadRecordMapper.setBindByHook(ids);
    }

    /**
     * 将识别记录按照数量分组
     *
     * @return nfid识别记录
     */
    @Override
    public List<NFIDReadGroupDTO> selectNFIDReadByGroup(){
        return NFIDReadRecordMapper.selectNFIDReadByGroup();
    }

    /**
     * 获取分组详情
     *
     * @return nfid识别记录
     */
    @Override
    public List<NFIDReadRecord> selectNFIDReadRecordListByGroup(ReqValidNFIDRecord req){
        return NFIDReadRecordMapper.selectNFIDReadRecordListByGroup(req);
    }
}
