package com.kiisoo.ic.job.behavior;

import com.kiisoo.ic.employee.mapper.PrivilageCpUserDOMapper;
import com.kiisoo.ic.generalize.mapper.PoiCustomerContactDataStatMapper;
import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper;
import com.kiisoo.ic.store.mapper.StorePromotionDataDOMapper;
import com.lmax.disruptor.RingBuffer;

import java.util.Date;

public class BehaviorDTOMessageEventProducer {

    private final RingBuffer<BehaviorDTOMessage> ringBuffer;

    private PrivilageCpUserStoreDOMapper privilageCpUserStoreDOMapper;
    private PrivilageCpUserDOMapper privilageCpUserDOMapper;
    private PoiCustomerContactDataStatMapper poiCustomerContactDataStatMapper;
    private StorePromotionDataDOMapper storePromotionDataDOMapper;


    public BehaviorDTOMessageEventProducer(RingBuffer<BehaviorDTOMessage> ringBuffer, PrivilageCpUserStoreDOMapper privilageCpUserStoreDOMapper,PoiCustomerContactDataStatMapper poiCustomerContactDataStatMapper,StorePromotionDataDOMapper storePromotionDataDOMapper,PrivilageCpUserDOMapper privilageCpUserDOMapper) {
        this.ringBuffer = ringBuffer;
        this.privilageCpUserStoreDOMapper =privilageCpUserStoreDOMapper;
        this.poiCustomerContactDataStatMapper =poiCustomerContactDataStatMapper;
        this.storePromotionDataDOMapper =storePromotionDataDOMapper;
        this.privilageCpUserDOMapper =privilageCpUserDOMapper;
    }

    public void produceData(int index, String cpUserId, Date statDate) {
        // 获得下一个Event槽的下标
        long seq = ringBuffer.next();
        try {
            // 给Event填充数据
            //给这个区块放入 数据  如果此处不理解，想想RingBuffer的结构图
            ringBuffer.get(seq).setIndex(index);
            ringBuffer.get(seq).setCpUserId(cpUserId);
            ringBuffer.get(seq).setStatDate(statDate);
            ringBuffer.get(seq).setPrivilageCpUserStoreDOMapper(privilageCpUserStoreDOMapper);
            ringBuffer.get(seq).setPrivilageCpUserDOMapper(privilageCpUserDOMapper);
            ringBuffer.get(seq).setPoiCustomerContactDataStatMapper(poiCustomerContactDataStatMapper);
            ringBuffer.get(seq).setStorePromotionDataDOMapper(storePromotionDataDOMapper);
        } finally {
            // 发布Event，激活观察者去消费， 将sequence传递给该消费者
            // 注意，最后的 ringBuffer.publish() 方法必须包含在 finally 中以确保必须得到调用；如果某个请求的 sequence 未被提交，将会堵塞后续的发布操作或者其它的 producer。
            ringBuffer.publish(seq);
        }
    }
}
