ProductTask.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package com.caimei365.tools.task;
  2. import com.caimei365.tools.mapper.BaseMapper;
  3. import lombok.RequiredArgsConstructor;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.commons.collections.CollectionUtils;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.scheduling.annotation.EnableScheduling;
  8. import org.springframework.scheduling.annotation.Scheduled;
  9. import javax.annotation.Resource;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Calendar;
  12. import java.util.Date;
  13. import java.util.List;
  14. /**
  15. * 商品定时器
  16. *
  17. * @author : Charles
  18. * @date : 2021/10/26
  19. */
  20. @Slf4j
  21. @Configuration
  22. @EnableScheduling
  23. @RequiredArgsConstructor
  24. public class ProductTask {
  25. @Resource
  26. private BaseMapper baseMapper;
  27. /**
  28. * 每天凌晨1点执行一次
  29. * todo 二手商品暂无,弃用二手商品自动下架 同时商品下架标记进入 cm_organize_product_info 恢复使用时需修改sql
  30. */
  31. // @Scheduled(cron = "0 0 1 * * ?")
  32. public void task() {
  33. long currentTime = System.currentTimeMillis();
  34. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  35. Date currentDate = new Date(currentTime);
  36. log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>【自动】采美二手商品到期下架,时间:" + formatter.format(currentDate));
  37. try {
  38. //得到日历
  39. Calendar calendar = Calendar.getInstance();
  40. //把当前时间赋给日历
  41. calendar.setTime(currentDate);
  42. //设置为day月前
  43. calendar.add(Calendar.MONTH, -3);
  44. //得到3月前的时间
  45. Date beforeDays = calendar.getTime();
  46. // 获取时间到期且未下架的二手商品Id
  47. List<Integer> secondIdList = baseMapper.getMaturitySecondProductIds(beforeDays);
  48. if (CollectionUtils.isNotEmpty(secondIdList)) {
  49. for (Integer productId : secondIdList) {
  50. baseMapper.updateProductValidFlag(productId, "3");
  51. }
  52. }
  53. } catch (Exception e) {
  54. log.error("try-catch:",e);
  55. log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>采美二手商品到期下架失败");
  56. }
  57. }
  58. }