package com.xunhong.erp.turbo.infra.app.executor.query;

import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.xunhong.erp.turbo.api.infra.dto.qry.DictionaryListQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.DictionaryVO;
import com.xunhong.erp.turbo.infra.app.assembler.DictionaryAssembler;
import com.xunhong.erp.turbo.infra.domain.entity.Dictionary;
import com.xunhong.erp.turbo.infra.domain.gateway.DictionaryGateway;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author shenyifei
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DictionaryListQryExe {

	private final DictionaryGateway dictionaryGateway;
	private final DictionaryAssembler dictionaryAssembler;

	@Cached(name = ":dictionary:cache:type:", expire = 60, localExpire = 10, timeUnit = TimeUnit.MINUTES, cacheType = CacheType.REMOTE, key = "#dictionaryListQry.type", cacheNullValue = true)
	@CacheRefresh(refresh = 50, timeUnit = TimeUnit.MINUTES)
	public List<DictionaryVO> execute(DictionaryListQry dictionaryListQry) {
		List<Dictionary> dictionaryList = dictionaryGateway.list(dictionaryListQry);
		return dictionaryList.stream().map(dictionaryAssembler::toDictionaryVO).toList();
	}
}

