package cn.exrick.xboot.your.schedulings; import cn.exrick.xboot.core.entity.User; import cn.exrick.xboot.core.service.UserService; import cn.exrick.xboot.your.entity.*; import cn.exrick.xboot.your.service.*; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; //同步历史数据 @Component public class HistoryOrder{ @Autowired private IOrderTaskService iOrderTaskService; @Autowired private IOrderTask2Service iOrderTask2Service; @Autowired private UserService userService; @Autowired private ICustomerService iCustomerService; /** * 每天21点31分执行 * 将配送日期为今天的订单数据(t_order_task)存入历史订单数据表(t_order_task2) */ @Scheduled(cron="0 31 21 * * ?")//每晚下午9点半点执行 public void execute(){ String day = getDay(0); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("send_Date",day); List list = iOrderTaskService.list(wrapper); List list2 = new ArrayList<>(); for(OrderTask orderTask:list){ OrderTask2 orderTask2 = new OrderTask2(); BeanUtil.copyProperties(orderTask,orderTask2); orderTask2.setUserName(getUser(orderTask.getUserId())); //orderTask2.setCustomerName(getCustomer(orderTask.getCustomerId())); //orderTask2.setCustomerName(orderTask.getCustomerName()); list2.add(orderTask2); } iOrderTask2Service.saveBatch(list2); } private String getUser(String id){ if(StrUtil.isNotEmpty(id)){ User user = userService.get(id); if(user!=null){ return user.getNickname(); }else{ return null; } }else{ return null; } } private String getCustomer(String id){ if(StrUtil.isNotEmpty(id)){ Customer customer = iCustomerService.getById(id); if(customer!=null){ return customer.getName(); }else{ return null; } }else{ return null; } } private String getDay(int day){ Date dNow = new Date(); //当前时间 Calendar calendar = Calendar.getInstance(); //得到日历 calendar.setTime(dNow);//把当前时间赋给日历 calendar.add(Calendar.DAY_OF_MONTH, day); //设置为前n天 Date dBefore = calendar.getTime(); //得到前一天的时间 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式 String defaultStartDate = sdf.format(dBefore); return defaultStartDate; } }