package cn.com.poc.ai_software_copyright.scheduler;

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

import cn.com.poc.ai_software_copyright.contant.AiSoftWareCopyRightEnum;
import cn.com.poc.ai_software_copyright.entity.BizSoftwareCopyrightEntity;
import cn.com.poc.ai_software_copyright.service.BizSoftwareCopyrightService;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.message.entity.SoftwareCopyRightMessage;
import cn.com.poc.message.service.SoftwareCopyRightProduceService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author alex.yao
 * @date 2025/12/26
 */
@Component
public class SoftwareCopyRightScheduler {

    @Resource
    private SoftwareCopyRightProduceService produceService;

    @Resource
    private BizSoftwareCopyrightService bizSoftwareCopyrightService;

    @Scheduled(cron = "0 0 0/30 * * ?")
    public void checkSoftwareCopyRightTask() {
        BizSoftwareCopyrightEntity bizSoftwareCopyrightEntity = new BizSoftwareCopyrightEntity();
        bizSoftwareCopyrightEntity.setGeneratedStatus(AiSoftWareCopyRightEnum.GENERATED_STATUS.Daft.name());
        bizSoftwareCopyrightEntity.setIsDeleted(CommonConstant.IsDeleted.N);
        List<BizSoftwareCopyrightEntity> bizSoftwareCopyrightEntities =
                bizSoftwareCopyrightService.findByExample(bizSoftwareCopyrightEntity, null);
        if (CollectionUtils.isNotEmpty(bizSoftwareCopyrightEntities)) {
            for (BizSoftwareCopyrightEntity softwareCopyrightEntity : bizSoftwareCopyrightEntities) {
                SoftwareCopyRightMessage message = new SoftwareCopyRightMessage();
                message.setId(softwareCopyrightEntity.getId());
                message.setStatus(AiSoftWareCopyRightEnum.GENERATED_STATUS.Pending);
                message.setBaseInfo(bizSoftwareCopyrightEntity.getBaseInfo());
                message.setTechnicalContent(bizSoftwareCopyrightEntity.getTechnicalContent());
                message.setReferenceDocumentFileUrl(bizSoftwareCopyrightEntity.getReferenceDocumentFileUrl());
                message.setGeneratedDocType(bizSoftwareCopyrightEntity.getGeneratedDocType());
                produceService.submitTask(message);
            }
        }
    }
}
