package com.ms.biz.task;

import com.ms.biz.common.TaskBaseService;
import com.ms.api.dto.dsorder.ResultDTO;
import com.ms.biz.service.DsMessageConsumerQueueService;
import com.ms.biz.service.DsMessageService;
import com.ms.dal.entity.DsMessageConsumerQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

/**
 * @author elf
 */
@Configuration
@Component
@Slf4j
public class DsMessageConsumeMsgTaskService extends TaskBaseService {
    @Autowired
    private DsMessageConsumerQueueService dsMessageConsumerQueueService;

    @Autowired
    private DsMessageService dsMessageService;

    @Bean(name = "dsMessageConsumeMsgTaskPool")
    @Override
    public Executor getAsyncExecutor() {
        return super.getAsyncExecutor();
    }

    @Resource(name = "dsMessageConsumeMsgTaskPool")
    protected Executor taskPool;

    @Async("dsMessageConsumeMsgTaskPool")
    @Scheduled(fixedRate = 10000)
    @Override
    public void runTask() {
        super.runTask();
    }

    @Override
    public int getCorePoolSiz() {
        return 3;
    }

    @Override
    public String getTaskExecutorName() {
        return "dsMessageConsumeMsgTaskPool";
    }

    @Override
    protected Executor getTaskPool() {
        return taskPool;
    }

    @Override
    public Object getTask() {
        Long timerLockId = 0L;
        return dsMessageConsumerQueueService.lockSolidRedisQueue(timerLockId, false);
    }

    @Override
    public Object processTask(Object params) {
        if (params == null) {
            System.out.println("[dsMessageConsumeMsgTask]没有可执行的任务");
            return null;
        }
        System.out.println("[dsMessageConsumeMsgTask]执行任务" + params);
        Map<String, Object> res = new HashMap<>();
        res.put("queue", params);
        try {
            dsMessageService.processDsMessage(((DsMessageConsumerQueue)params).getDsMessageId());
            res.put("processResult", ResultDTO.success());
        } catch (RuntimeException e) {
            log.error("[dsMessageConsumeMsgTask]error: ", e);
            res.put("processResult", ResultDTO.fail(e.getMessage()));
        }
        return res;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void clearTask(Object params) {
        if (params == null) {
            return;
        }
        log.info("[dsMessageConsumeMsgTask]clearTask params: " + params);
        Map<String, Object> res = (Map<String, Object>)params;
        DsMessageConsumerQueue queue = (DsMessageConsumerQueue)res.get("queue");
        ResultDTO processResult = (ResultDTO)res.get("processResult");
        dsMessageService.clearDsMessageQueue(queue, processResult);
    }
}
