package com.kiisoo.ic.job.detail;

import com.kiisoo.ic.customer.CustomerService;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.wx.service.QWMailListManageService;
import com.lmax.disruptor.RingBuffer;

public class DetailDTOMessageEventProducer {

    private final RingBuffer<DetailDTOMessage> ringBuffer;

    private QWMailListManageService qwMailListManageService;

    private CustomerService customerService;

    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    public DetailDTOMessageEventProducer(RingBuffer<DetailDTOMessage> ringBuffer, QWMailListManageService qwMailListManageService, CustomerService customerService, PoiStoreStaffDOMapper poiStoreStaffDOMapper) {
        this.ringBuffer = ringBuffer;
        this.qwMailListManageService =qwMailListManageService;
        this.customerService = customerService;
        this.poiStoreStaffDOMapper = poiStoreStaffDOMapper;
    }

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

    public void produceData(ProduceDTO produceDTO) {
        // 获得下一个Event槽的下标
        long seq = ringBuffer.next();
        try {
            // 给Event填充数据
            //给这个区块放入 数据  如果此处不理解，想想RingBuffer的结构图
            ringBuffer.get(seq).setIndex(produceDTO.getIndex());
            ringBuffer.get(seq).setCpUserId(produceDTO.getCpUserId());
            ringBuffer.get(seq).setStoreId(produceDTO.getStoreId());
            ringBuffer.get(seq).setQwMailListManageService(qwMailListManageService);
            ringBuffer.get(seq).setCustomerService(customerService);
            ringBuffer.get(seq).setPoiStoreStaffDOMapper(poiStoreStaffDOMapper);
            ringBuffer.get(seq).setSumCustomer(produceDTO.getSumCustomer());
//          ringBuffer.get(seq).setCustomers(customers);
        } finally {
            // 发布Event，激活观察者去消费， 将sequence传递给该消费者
            // 注意，最后的 ringBuffer.publish() 方法必须包含在 finally 中以确保必须得到调用；如果某个请求的 sequence 未被提交，将会堵塞后续的发布操作或者其它的 producer。
            ringBuffer.publish(seq);
        }
    }
}
