package cn.superdesk.uniorder.executor.scheduler.task;

import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dubbo.consumer.operation.UnifiedOrderIssuesDubboConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutorService;


/**
 * @Description: 订单问题
 * @Author: linjinyu
 * @CreateDate: 2021/1/29 下午5:32
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/1/29 下午5:32
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class UnifiedOrderIssuesTask {

	private UnifiedOrderIssuesTask() {
		log.info("call UnifiedOrderIssuesTask 构建方法");
	}

	@Resource
	private UnifiedOrderIssuesDubboConsumerService unifiedOrderIssuesDubboConsumerService;

	@Resource
	private ExecutorService executorService;

//	@Resource
//	private StandardThreadExecutor standardThreadExecutor;

	public void run(String params) {

		log.info("String params {}", params);

		PageQueryParam pageQueryParam = AnJsonUtils.toObject(params, PageQueryParam.class);

		log.info("PageQueryParam {}", AnJsonUtils.toJson(pageQueryParam));

		List<UnifiedOrder> resultList = unifiedOrderIssuesDubboConsumerService.getToDoUnifiedOrderIssuesList(pageQueryParam.getPageNo(), pageQueryParam.getPageSize());

		log.info("UnifiedOrderIssuesTask 一层调度 , TODO size : {}", resultList.size());

		if (CollUtil.isNotEmpty(resultList)) {
			resultList.forEach(entity -> {
				executorService.execute(() -> runTask(entity));
			});
		}
	}

	private void runTask(UnifiedOrder entity) {

		log.info("\nUnifiedOrderIssuesTask 二层执行\nrunTask \n{}", AnJsonUtils.toJson(entity));

		unifiedOrderIssuesDubboConsumerService.runTaskUnifiedOrderIssues(entity);

		log.info("UnifiedOrderIssuesTask 任务完成");
	}
}
