# AutoPublishHkInventory.java

    package cloud.wconnect.workperformance.businesstask.hkinventory;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.service.MailInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.entity.hkinventory.HkInventory;
    import cloud.wconnect.workperformance.service.hkinventory.IHkInventoryService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import org.springframework.util.CollectionUtils;

    import java.text.MessageFormat;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.List;
    import java.util.concurrent.TimeUnit;

    /**
     * 自动发布上上个月未发布的Housekeeping Inventory
     *
     * @author lichen
     * @date 2025/4/25
     */
    @Slf4j
    @Component
    public class AutoPublishHkInventory {

        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
        @Autowired
        private IHkInventoryService hkInventoryService;
        @Autowired
        private MailInternalService mailInternalService;
        private static final String prefixKey = "wconnect:task:publish-hk-inventory:";

        /**
         * 自动发布上上个月未发布的Housekeeping Inventory
         *
         * @return void
         * @author lichen
         * @date 2025/4/25
         */
        @Scheduled(cron = "${schedule.cron.hk-inventory.publish-hk-inventory}", zone = "${default.timezone}")
        public void publish() {
            LocalDate currentLocalDate = LocalDate.now();
            String currentDate = currentLocalDate.format(DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYYMM));
            if (!preventConcurrency(prefixKey + currentDate)) {
                return;
            }
            //上上个月的日期
            LocalDate previousTwoMonthsLocalDate = currentLocalDate.minusMonths(2);
            int year = previousTwoMonthsLocalDate.getYear();
            int monthValue = previousTwoMonthsLocalDate.getMonthValue();
            String yearMonth = DateTimeUtils.localDateToStr(previousTwoMonthsLocalDate, BaseConstant.DATE_FORMAT_YYYYMM);
            try {
                //上上个月未发布的数据
                List<HkInventory> hkInventoryList = hkInventoryService.listPendingByYearMonth(year, monthValue);
                if (CollectionUtils.isEmpty(hkInventoryList)) {
                    log.info("AutoPublishHkInventory.publish end... not exist pending Housekeeping Inventory of the previous two months");
                    return;
                }
                hkInventoryList.forEach(hkInventory -> hkInventoryService.published(hkInventory, 0));
            } catch (Exception e) {
                log.error("AutoPublishHkInventory.publish error:" + e.getMessage(), e.getMessage());
                String mailContent = MessageFormat.format("Business:{0}<br/>Content:{1}", "AutoPublishHkInventory",
                        "There is something wrong when auto publish Housekeeping Inventory.Exception: " + e.getMessage());
                mailInternalService.sendAlertEmail("[Warning] - Publish Housekeeping Inventory - " + yearMonth, mailContent);
            }
            log.info("AutoPublishHkInventory.publish end...");
        }

        /**
         * 校验是否并发执行
         *
         * @param key 缓存前缀
         * @return java.lang.Boolean
         * @author lichen
         * @date 2025/4/25
         */
        private Boolean preventConcurrency(String key) {
            return redisTemplate.opsForValue().setIfAbsent(key, 0, 1, TimeUnit.DAYS);
        }

    }