package cn.exrick.xboot.your.schedulings;
|
import cn.exrick.xboot.your.entity.Area;
|
import cn.exrick.xboot.your.entity.AreaSection;
|
import cn.exrick.xboot.your.entity.Customer;
|
import cn.exrick.xboot.your.entity.OrderTask;
|
import cn.exrick.xboot.your.service.IAreaSectionService;
|
import cn.exrick.xboot.your.service.IAreaService;
|
import cn.exrick.xboot.your.service.ICustomerService;
|
import cn.exrick.xboot.your.service.IOrderTaskService;
|
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.io.IOException;
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.Collections;
|
import java.util.List;
|
|
//大屏配送分析
|
@Component
|
public class StatisticPc2ScheduleImpl {
|
|
@Autowired
|
private IAreaService iAreaService;
|
|
@Autowired
|
private IAreaSectionService iAreaSectionService;
|
|
@Autowired
|
private ICustomerService iCustomerService;
|
|
@Autowired
|
private IOrderTaskService iOrderTaskService;
|
|
@Scheduled(cron="0 0 20 * * ?")//每晚8点执行
|
public void execute(){
|
List<Area> list = iAreaService.list();
|
for(Area area:list){
|
QueryWrapper<Customer> wrapper = new QueryWrapper<>();
|
wrapper.eq("area_id",area.getId());
|
int count = iCustomerService.count(wrapper);
|
area.setCustomerSum(count);
|
iAreaService.saveOrUpdate(area);
|
}
|
|
List<AreaSection> list2 = iAreaSectionService.list();
|
for(AreaSection areaSection:list2){
|
String id = areaSection.getId();
|
QueryWrapper<Customer> wrapper = new QueryWrapper<>();
|
wrapper.eq("area_section_id",id);
|
int count = iCustomerService.count(wrapper);
|
areaSection.setCustomerNum(count);
|
|
List<OrderTask> orderTasks = iOrderTaskService.sumTime(id);
|
int sum = 0;
|
List<Integer> listTime = new ArrayList<>();
|
for(OrderTask orderTask:orderTasks){
|
sum+=orderTask.getTime();
|
listTime.add(orderTask.getTime());
|
}
|
|
Collections.sort(listTime);
|
|
double v = (sum * 1.0) / (1000 * 60 * 60) / listTime.size();
|
areaSection.setAvgTime((double) Math.round(v * 10) / 10);
|
if(listTime.size()>0){
|
double v1 = (listTime.get(0) * 1.0) / (1000 * 60 * 60);
|
areaSection.setMinTime((double) Math.round(v1 * 10) / 10);
|
|
double v2 = (listTime.get(listTime.size()-1) * 1.0) / (1000 * 60 * 60);
|
areaSection.setMaxTime((double) Math.round(v2 * 10) / 10);
|
}
|
|
double variance = Variance(listTime);
|
areaSection.setStability(variance);
|
iAreaSectionService.saveOrUpdate(areaSection);
|
}
|
|
}
|
|
public double Variance(List<Integer> x) {
|
List<Double> y = new ArrayList<>();
|
for(Integer s:x){
|
y.add(trans((s*1.0)/(3600*1000)));
|
}
|
|
int m = x.size();
|
double sum = 0;
|
for (int i = 0; i < m; i++) {
|
sum += y.get(i);
|
}
|
double dAve = sum / m;
|
double dVar = 0;
|
for (int i = 0; i < m; i++) {
|
dVar += (y.get(i) - dAve) * (y.get(i) - dAve);
|
}
|
double v = dVar / m;
|
return trans2(v);
|
}
|
|
public double trans(double v2){
|
return (double) Math.round(v2 * 10) / 10;
|
}
|
|
public double trans2(double v2){
|
return (double) Math.round(v2 * 100) / 100;
|
}
|
}
|