package cokr.xit.ens.batch.sched;

import cokr.xit.ens.modules.common.code.StatCd;
import cokr.xit.ens.modules.common.domain.SendMast;
import cokr.xit.ens.modules.common.domain.repository.SendMastRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@Profile("!local & !local-maria & !local-oracle & !local-test")
//@Profile({"prod-traffic", "prod-iup"})
@RequiredArgsConstructor
public class EnsScheduler {

    private final SendMastRepository sendMastRepository;


    /**
     * 마감처리
     *  -. "열람(open)" 상태의 자료 중 마감기한이 경과한 자료의 상태정보를 "마감(close)" 으로 변경 한다.
     */
    @Scheduled(cron = "0 */5 9-18 * * *")
    @Transactional
    public void closed() {
        log.info("=======================================================");
        List<SendMast> sendMastList = sendMastRepository.findAllByStatCdInAndCloseDtBefore(Arrays.asList(StatCd.sendcmplt, StatCd.open), LocalDateTime.now());
        log.info("[ENS] 마감처리 - 총 {}건", sendMastList.size());
        log.info("SendMastId {}", sendMastList.stream()
                .map(row -> {
                    row.setStatCd(StatCd.close);
                    return row.getSendMastId();
                })
                .collect(Collectors.toList())
                .toString());
        log.info("=======================================================");
    }

}
