package com.zt.plat.module.qms.resource.material.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.tenant.core.job.TenantJob;
import com.zt.plat.module.qms.resource.material.service.MaterialInfomationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 物料相关定时任务
 */
@Component
@Slf4j
public class MaterialJob {

    @Autowired
    private MaterialInfomationService materialInfomationService;

    /**
     * 更新物料过期状态定时任务
     * 每天执行一次，检查并更新已过期的物料状态
     *
     */
    @XxlJob("updateMaterialExpiredStatusJob")
    public void updateMaterialExpiredStatusJob() {
        log.info("[updateMaterialExpiredStatusJob] 开始执行物料过期状态更新任务");

        try {
            Integer updateCount = materialInfomationService.updateExpiredMaterialStatus();
            log.info("[updateMaterialExpiredStatusJob] 任务执行成功，共更新 {} 个物料", updateCount);
            XxlJobHelper.handleSuccess(String.format("任务执行成功，共更新 %d 个物料", updateCount));
        } catch (Exception e) {
            log.error("[updateMaterialExpiredStatusJob] 任务执行失败：{}", e.getMessage(), e);
            throw e;
        }
    }

}
