wjli
2023-05-11 a067fdbf6b1374a1402096c722257575916eab99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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;
 
    @Scheduled(cron="0 31 21 * * ?")//每晚下午9点半点执行
    public void execute(){
        String day = getDay(0);
        QueryWrapper<OrderTask> wrapper = new QueryWrapper<>();
        wrapper.eq("send_Date",day);
        List<OrderTask> list = iOrderTaskService.list(wrapper);
 
        List<OrderTask2> 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;
    }
}