先建一个PortraitTask
1 @Component 2 @Slf4j 3 public class PortraitTask implements StatAble { 4 5 private static final String LOCK_ID = "sss:portrait_lock:portrait"; 6 7 @Resource 8 private PortraitAddTask portraitAddTask; 9 10 11 12 /** 13 * 定时同步任务 //TODO 14 */ 15 @Scheduled(cron = "${portrait.task.task-sync}") 16 @ClusterLock(lockId = LOCK_ID) 17 public void portraitTask() { 18 try { 19 //同步当天彩民画像 20 log.debug("start portrait ,,,"); 21 22 portraitAddTask.syncPortraitTask(); 23 24 } catch (Exception e) { 25 return; 26 } 27 } 28 }
此处为上面syncPortraitTask()方法的具体内容
1 @Component 2 @Slf4j 3 public class PortraitAddTask { 7 8 @Resource 9 private FeatureStatisticsMapper featureStatisticsMapper; 10 11 @Resource 12 private PortraitMapper portraitMapper; 13 14 @Resource 15 private PortraitDataService portraitDataService; 16 17 public void syncPortraitTask(){ 18 19 log.info("enter portrait syncPortraitTask"); 20 //获取到当前时间和前一天的时间 此处代码为借鉴其他博主(将时间格式转化为需要的格式) 21 Date dNow = new Date(); //当前时间 22 Date dBefore = new Date(); 23 Calendar calendar = Calendar.getInstance(); //得到日历 24 calendar.setTime(dNow);//把当前时间赋给日历 25 calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天 26 dBefore = calendar.getTime(); //得到前一天的时间 27 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式 28 SimpleDateFormat sdfStr=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式 29 String defaultStartDate = sdf.format(dBefore); //格式化前一天 30 String defaultEndDate = sdf.format(dNow); //格式化当前时间 31 String dayStr = sdfStr.format(dNow); //格式化当前时间 32 34 String lastDay =String.valueOf(yesterday); 37 String beginTime = defaultStartDate; 39 String endTime = defaultEndDate; 41 Date date =Date.from(yesterday.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); 43 44 addPortrait(beginTime,endTime,lastDay); 45 46 } 47 48 public void addPortrait(String beginTime, String endTime,String lastDay) { 49 PortraitInfo portraitEntity = new PortraitInfo(); 50 51 SimpleDateFormat sdfStr=new SimpleDateFormat("yyyy-MM-dd"); 52 String dayStr = sdfStr.format(new Date()); //格式化当前时间 54 55 List<String> shopIds = portraitDataService.getShopId(); 57 for (String shopId : shopIds) { 58 String shopName = portraitDataService.getShopNameByShopId(shopId); 60 SumMsgCustomerFlowInfo message = featureStatisticsMapper.queryOldNewBlackByShopIdAndDayStr(shopId, lastDay); 62 int orgId = portraitDataService.getOrgIdByShopId(shopId) ; 63 FeatureStatisticsEntity list = featureStatisticsMapper.queryAgeAndGenderByShopIdAndDayStr(shopId,lastDay); 65 int total = 0; 66 int female = 0; 67 int male = 0; 68 int underEighteen = 0; 69 int eighteenToThirty = 0; 70 int thirtyToFortyFive = 0; 71 int fortyFiveToSixty = 0; 72 int moreThanSixty = 0; 73 int unknownOfAge = 0; 74 int unknownOfGender = 0; 75 int blackCount =0; 76 int newCount = 0; 77 int oldCount = 0; 78 if (message != null) { 80 total = message.getTotalCount(); 81 blackCount =message.getBlackCount(); 82 newCount = message.getNewCount(); 83 oldCount = message.getOldCount(); 84 } 98 if(list != null){ 99 female = list.getNumOfFemale(); 100 male = list.getNumOfMale(); 101 underEighteen = list.getUnderEighteen(); 102 eighteenToThirty = list.getEighteenToThirty(); 103 thirtyToFortyFive = list.getThirtyToFortyFive(); 104 fortyFiveToSixty = list.getFortyFiveToSixty(); 105 moreThanSixty = list.getMoreThanSixty(); 106 unknownOfAge = list.getUnknownOfAge(); 107 unknownOfGender = list.getUnknownOfGender(); 108 109 } 110 portraitEntity.setUnderEighteen(underEighteen); 111 portraitEntity.setEighteenToThirty(eighteenToThirty); 112 portraitEntity.setThirtyToFortyFive(thirtyToFortyFive); 113 portraitEntity.setFortyFiveToSixty(fortyFiveToSixty); 114 portraitEntity.setMoreThanSixty(moreThanSixty); 115 portraitEntity.setNumOfFemale(female); 116 portraitEntity.setNumOfMale(male); 117 portraitEntity.setUnknownOfGender(unknownOfGender); 118 portraitEntity.setUnknownOfAge(unknownOfAge); 119 portraitEntity.setTotal(total); 120 portraitEntity.setBlackCustomerNum(blackCount); 121 portraitEntity.setNewCustomerNum(newCount); 122 portraitEntity.setOldCustomerNum(oldCount); 123 portraitEntity.setShopId(shopId); 124 portraitEntity.setShopName(shopName); 125 List<String> firstPeople =portraitDataService.getFirstPeopleTime(shopId,beginTime,endTime); 126 log.info("firstPeople:"+firstPeople); 127 if(CollectionUtils.isEmpty(firstPeople)){ 128 firstPeople.add(""); 129 }131 List<String> lastPeople = portraitDataService.getLastPeopleTime(shopId,beginTime,endTime);133 if(CollectionUtils.isEmpty(lastPeople)){ 134 lastPeople.add(""); 135 }137 portraitEntity.setBeginTime(lastPeople.get(0)); 138 portraitEntity.setEndTime(firstPeople.get(0)); 139 portraitEntity.setDayStr(dayStr); 140 portraitEntity.setOrgId(orgId); 141 portraitEntity.setUpdateTime(endTime);143 portraitMapper.insetPortraitInfo(portraitEntity); 144 } 145 } 146 }
1 @Repository 2 public interface FeatureStatisticsMapper extends BaseMapper<FeatureStatisticsEntity> 3 {20 21 FeatureStatisticsEntity queryAgeAndGenderByShopIdAndDayStr(@Param("shopId") String shopId, @Param("dayStr") String dayStr); 22 23 SumMsgCustomerFlowInfo queryOldNewBlackByShopIdAndDayStr(@Param("shopId") String shopId, @Param("dayStr") String dayStr);28 }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.allcam.abshop.service.statistics.mapper.FeatureStatisticsMapper"> <select id="queryAgeAndGenderByShopIdAndDayStr" resultType="com.allcam.abshop.service.statistics.entity.FeatureStatisticsEntity"> select * from tbl_sss_passenger_flow_feature where shopId = #{shopId} and dayStr =#{dayStr} </select> <select id="queryOldNewBlackByShopIdAndDayStr" resultType="com.allcam.abshop.service.message.model.SumMsgCustomerFlowInfo"> select sum(total) totalCount, sum(countOfOld) oldCount, sum(countOfNew) newCount, sum(countOfBlack) blackCount from tbl_sss_passenger_flow_statistics where shopId = #{shopId} and dayStr =#{dayStr} </select> </mapper>
代码还有很多需要优化的地方,仅供学习参考