package com.ruoyi.demo.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.ruoyi.demo.constant.RedisContant; import com.ruoyi.demo.entity.Brand; import com.ruoyi.demo.entity.ManageType; import com.ruoyi.demo.entity.StoreWd; import com.ruoyi.demo.entity.WdInfo; import com.ruoyi.demo.entity.bo.Histogram; import com.ruoyi.demo.entity.bo.StoreWdCategoryCount; import com.ruoyi.demo.entity.bo.StoreWdCategoryCountBody; import com.ruoyi.demo.entity.vo.ChannelAnalyseAceeptVo; import com.ruoyi.demo.entity.vo.ChannelMapAceeptVo; import com.ruoyi.demo.entity.vo.TagAnalyse; import com.ruoyi.demo.mapper.BrandMapper; import com.ruoyi.demo.mapper.StoreWdDao; import com.ruoyi.demo.mapper.WdInfoDao; import com.ruoyi.demo.mapper.WdTopologicalInfoDao; import com.ruoyi.demo.service.ChannelAnalyseService; import com.ruoyi.demo.utils.InitMapUtil; import com.ruoyi.demo.utils.WdRedisStoreage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.geo.Circle; import org.springframework.data.geo.Distance; import org.springframework.data.geo.GeoResults; import org.springframework.data.geo.Point; import org.springframework.data.redis.connection.RedisGeoCommands; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.client.RestTemplate; import java.math.BigDecimal; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; @Service @Transactional public class ChannelAnalyseServiceImpl implements ChannelAnalyseService { @Autowired WdInfoDao wdInfoDao; @Autowired InitMapUtil initMapUtil; @Autowired BrandMapper brandMapper; @Autowired WdTopologicalInfoDao wdTopologicalInfoDao; @Autowired private WdRedisStoreage wdRedisStoreage; @Autowired private StoreWdDao storeWdDao; @Autowired @Qualifier("executor") ExecutorService executor; @Override public HashMap cityTier(ChannelMapAceeptVo channelMapAceeptVo) { //1.统计结果 HashMap result = new HashMap<>(); result.put("一线", 0L); result.put("新一线", 0L); result.put("二线", 0L); result.put("三线", 0L); result.put("四线", 0L); result.put("五线", 0L); result.put("其他", 0L); //1.获取所有符合条件的网点信息 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("addr_code","count(*) as audit"); assembleQueryWrapper(queryWrapper,channelMapAceeptVo); queryWrapper.groupBy("addr_code"); List wdInfos = wdInfoDao.selectList(queryWrapper); for (WdInfo wdInfo : wdInfos) { String initCityTierMap = initMapUtil.getInitCityTierMap(wdInfo.getAddrCode()); if (initCityTierMap != null) { result.put(initCityTierMap, result.get(initCityTierMap) + wdInfo.getAudit()); } else result.put("其他", result.get("其他") + wdInfo.getAudit()); } return result; } @Override public PageInfo brandList(ChannelAnalyseAceeptVo channelAnalyseAceeptVo) { String text = ""; if (channelAnalyseAceeptVo.getSearchText() != null) text = channelAnalyseAceeptVo.getSearchText(); PageHelper.startPage(channelAnalyseAceeptVo.getPageNum(), channelAnalyseAceeptVo.getPageSize()); List brands = brandMapper.searchList(text); PageInfo pageInfo = new PageInfo<>(brands); return pageInfo; } //TODO 待测试(redis版本) @Override public HashMap tagAnalyse(ChannelMapAceeptVo channelMapAceeptVo) { //1.根据条件的到位网点信息 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("wd_id"); assembleQueryWrapper(queryWrapper,channelMapAceeptVo); queryWrapper.and(queryWrapper2 ->{ queryWrapper2.eq("show_delete",0); }); List queryWd = wdInfoDao.selectList(queryWrapper); //2.获取周边标签 List collect = queryWd.stream().map(item -> { return RedisContant.WD_TAG + "_" + item.getWdId(); }).collect(Collectors.toList()); //切割获取redis数据 int split = 60000; int splitCount = collect.size()/split; int p1 = 0,p2 = split; ArrayList>>> splitList = new ArrayList<>(); for (int i=1;i<=splitCount;i++){ if (p2 > collect.size()) p2 = collect.size(); List list2 = collect.subList(p1, p2); CompletableFuture>> future = CompletableFuture.supplyAsync(()->{ return wdRedisStoreage.getWdTagList(list2); },executor); splitList.add(future); p1 = p2; p2+=split; } //3.统计 Hashtable hashtable = new Hashtable<>(); hashtable.put("total",0); ArrayList> list1 = new ArrayList<>(); for (CompletableFuture>> listCompletableFuture : splitList) { try { List> lists = listCompletableFuture.get(); for (List list : lists) { if (list != null && !list.isEmpty()){ CompletableFuture future = CompletableFuture.runAsync(()->{ for (String s : list) { s += "边"; Integer integer = hashtable.get(s); if (integer == null) hashtable.put(s,1); else hashtable.put(s,integer+1); } hashtable.put("total",hashtable.get("total")+1); },executor); list1.add(future); } } CompletableFuture.allOf(list1.toArray(new CompletableFuture[list1.size()])).join(); lists = null; } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } } collect = null; queryWd = null; //4.封装统计值 HashMap result = new HashMap<>(); List list = new ArrayList<>(); Integer total = hashtable.get("total"); for (String s : hashtable.keySet()) { if (s.equals("total")) continue; TagAnalyse tagAnalyse = new TagAnalyse(); tagAnalyse.setName(s); tagAnalyse.setCount(hashtable.get(s)); BigDecimal bigDecimal = new BigDecimal((double) tagAnalyse.getCount() / total); double v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); tagAnalyse.setRadio(v); list.add(tagAnalyse); } //5.对结果集进行排序 Collections.sort(list, new Comparator() { @Override public int compare(TagAnalyse o1, TagAnalyse o2) { return o2.getCount() - o1.getCount(); } }); //5.分页返回结果集 int i = 1; int start = 0,end = 6; while(start=list.size()) end = list.size(); result.put(""+i++,new ArrayList(list.subList(start,end))); start=end; end+=6; } result.put("pages",i-1); result.put("total",total); return result; } @Override public List category(ChannelMapAceeptVo channelMapAceeptVo) { //2.组装条件构造器找到网点信息网点 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("wd_id"); assembleQueryWrapper(queryWrapper,channelMapAceeptVo); queryWrapper.and(queryWrapper2 ->{ queryWrapper2.eq("show_delete",0); }); //门店 queryWrapper.and(queryWrapper1 -> { queryWrapper1.eq("wd_type_code","1"); }); //5.查询周边网点结果,并统计 List storeWds = storeWdDao.category(queryWrapper); HashMap hashMap = new HashMap<>(); for (StoreWd wdInfo : storeWds) { if(wdInfo.getManageTypeCode() == null || wdInfo.getManageTypeCode().equals("")) continue; String s = wdInfo.getManageTypeCode().substring(0, 4) + "00"; StoreWdCategoryCount storeWdCategoryCount = hashMap.get(s); ManageType initManageType = initMapUtil.getInitManageType(wdInfo.getManageTypeCode()); if (storeWdCategoryCount == null){ StoreWdCategoryCountBody storeWdCategoryCountBody = new StoreWdCategoryCountBody(); if(initManageType.getSubCategory() != null && !initManageType.getSubCategory().equals("")){ storeWdCategoryCountBody.setName(initManageType.getSubCategory()); }else if(initManageType.getMidCategory() != null && !initManageType.getMidCategory().equals("")){ storeWdCategoryCountBody.setName(initManageType.getMidCategory()); }else if (initManageType.getBigCategory() != null && !initManageType.getBigCategory().equals("")){ storeWdCategoryCountBody.setName(initManageType.getBigCategory()); } storeWdCategoryCountBody.setManageTypeCode(wdInfo.getManageTypeCode()); storeWdCategoryCountBody.setCount(wdInfo.getCommentCount()); StoreWdCategoryCount storeWdCategoryCount1 = new StoreWdCategoryCount(); storeWdCategoryCount1.setManageTypeCode(s); storeWdCategoryCount1.setName(initMapUtil.getInitManageType(s).getMidCategory()); storeWdCategoryCount1.setCount(wdInfo.getCommentCount()); ArrayList list = new ArrayList<>(); list.add(storeWdCategoryCountBody); storeWdCategoryCount1.setStoreWdCategoryCountBodyList(list); hashMap.put(s,storeWdCategoryCount1); }else { StoreWdCategoryCountBody storeWdCategoryCountBody = new StoreWdCategoryCountBody(); if(initManageType.getSubCategory() != null && !initManageType.getSubCategory().equals("")){ storeWdCategoryCountBody.setName(initManageType.getSubCategory()); }else if(initManageType.getMidCategory() != null && !initManageType.getMidCategory().equals("")){ storeWdCategoryCountBody.setName(initManageType.getMidCategory()); }else if (initManageType.getBigCategory() != null && !initManageType.getBigCategory().equals("")){ storeWdCategoryCountBody.setName(initManageType.getBigCategory()); } storeWdCategoryCountBody.setManageTypeCode(wdInfo.getManageTypeCode()); storeWdCategoryCountBody.setCount(wdInfo.getCommentCount()); storeWdCategoryCount.getStoreWdCategoryCountBodyList().add(storeWdCategoryCountBody); storeWdCategoryCount.setCount(storeWdCategoryCountBody.getCount()+storeWdCategoryCount.getCount()); //hashMap.put(s,storeWdCategoryCount); } } //6.组装结果集 List storeWdCategoryCounts = new ArrayList<>(); for (String s : hashMap.keySet()) { storeWdCategoryCounts.add(hashMap.get(s)); } return storeWdCategoryCounts; } @Override public HashMap businessStatusAnalyse(ChannelMapAceeptVo channelMapAceeptVo) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("wd_id"); assembleQueryWrapper(queryWrapper,channelMapAceeptVo); queryWrapper.and(queryWrapper1 -> { queryWrapper1.eq("wd_type_code","1"); }); queryWrapper.and(queryWrapper2 ->{ queryWrapper2.eq("show_delete",0); }); List storeWds = storeWdDao.businessStatusAnalyse(queryWrapper); HashMap hashMap = new HashMap<>(); int total = 0; for (StoreWd storeWd : storeWds) { total+=storeWd.getCommentCount(); hashMap.put(storeWd.getBusinessStatus(),storeWd.getCommentCount()); } ArrayList list = new ArrayList<>(); for (String s : hashMap.keySet()) { Histogram histogram = new Histogram(); histogram.setName(s); histogram.setCount(hashMap.get(s)); list.add(histogram); } HashMap result = new HashMap<>(); result.put("total",total); result.put("data",list); return result; } @Override public ArrayList aroundBuildAnalyse(ChannelMapAceeptVo channelMapAceeptVo) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("type_code_by","count(*) as audit"); assembleQueryWrapper(queryWrapper,channelMapAceeptVo); queryWrapper.and(queryWrapper1 -> { queryWrapper1.eq("wd_type_code","3"); }); queryWrapper.and(queryWrapper1 -> { queryWrapper1.isNotNull("type_code_by"); }); queryWrapper.groupBy("type_code_by"); List wdInfos = wdInfoDao.selectList(queryWrapper); int total = 0; HashMap hashMap = new HashMap<>(); for (WdInfo wdInfo : wdInfos) { String typeCodeBy = wdInfo.getTypeCodeBy().substring(0,2); Integer integer = hashMap.get(typeCodeBy); if (integer == null){ hashMap.put(typeCodeBy,wdInfo.getAudit()); }else { hashMap.put(typeCodeBy,hashMap.get(typeCodeBy)+wdInfo.getAudit()); } total+=wdInfo.getAudit(); } ArrayList list = new ArrayList<>(); for (String s : hashMap.keySet()) { Integer integer = hashMap.get(s); String initTypeByMap = initMapUtil.getInitTypeByMap(s + "0000").replace(":",""); Histogram histogram = new Histogram(); histogram.setName(initTypeByMap); histogram.setCount(integer); list.add(histogram); } return list; } @Override public ArrayList perCapitaConsumpAnalyse(ChannelMapAceeptVo channelMapAceeptVo) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("wd_id"); assembleQueryWrapper(queryWrapper,channelMapAceeptVo); queryWrapper.and(queryWrapper1 -> { queryWrapper1.eq("wd_type_code","1"); }); queryWrapper.and(queryWrapper2 ->{ queryWrapper2.eq("show_delete",0); }); List storeWds = storeWdDao.perCapitaConsumpAnalyse(queryWrapper); LinkedHashMap linkedHashMap = new LinkedHashMap<>(); linkedHashMap.put("0~20",0); linkedHashMap.put("20~50",0); linkedHashMap.put("50~100",0); linkedHashMap.put("100~200",0); linkedHashMap.put("200以上",0); for (StoreWd storeWd : storeWds) { if (storeWd.getPerCapitaConsumption() != null){ if (storeWd.getPerCapitaConsumption() >= 0 && storeWd.getPerCapitaConsumption()<20){ linkedHashMap.put("0~20",linkedHashMap.get("0~20")+1); }else if(storeWd.getPerCapitaConsumption() >= 20 && storeWd.getPerCapitaConsumption()<50){ linkedHashMap.put("20~50",linkedHashMap.get("20~50")+1); }else if(storeWd.getPerCapitaConsumption() >= 50 && storeWd.getPerCapitaConsumption()< 100){ linkedHashMap.put("50~100",linkedHashMap.get("50~100")+1); }else if(storeWd.getPerCapitaConsumption() >= 100 && storeWd.getPerCapitaConsumption()< 200){ linkedHashMap.put("100~200",linkedHashMap.get("100~200")+1); }else { linkedHashMap.put("200以上",linkedHashMap.get("200以上")+1); } } } ArrayList list = new ArrayList<>(); for (String s : linkedHashMap.keySet()) { Histogram histogram1 = new Histogram(s,linkedHashMap.get(s)); list.add(histogram1); } return list; } @Override public ArrayList operateTimeAnalyse(ChannelMapAceeptVo channelMapAceeptVo) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("wd_id"); assembleQueryWrapper(queryWrapper,channelMapAceeptVo); queryWrapper.and(queryWrapper1 -> { queryWrapper1.eq("wd_type_code","1"); }); queryWrapper.and(queryWrapper2 ->{ queryWrapper2.eq("show_delete",0); }); List storeWds = storeWdDao.operateTimeAnalyse(queryWrapper); HashMap hashMap = new HashMap<>(); for (StoreWd storeWd : storeWds) { Integer opentime = Integer.valueOf(storeWd.getOpentime().substring(0,2)); Integer closetime = Integer.valueOf(storeWd.getClosetime().substring(0,2)); int i = closetime - opentime; Integer integer = hashMap.get(i); if (integer == null) hashMap.put(i,1); else hashMap.put(i,integer+1); } ArrayList list = new ArrayList<>(); for (Integer s : hashMap.keySet()) { Histogram histogram = new Histogram(); histogram.setName(s+""); histogram.setCount(hashMap.get(s)); list.add(histogram); } return list; } public void assembleQueryWrapper(QueryWrapper queryWrapper,ChannelMapAceeptVo channelMapAceeptVo){ //1.根据不同级别得到需要查询的 地区码 List addrCodeList = new ArrayList<>(); if ("province".equals(channelMapAceeptVo.getRankType())) { //省码 for (String s : channelMapAceeptVo.getAddrCode()) { String substring = s.substring(0, 2); addrCodeList.add(substring); } } else if ("city".equals(channelMapAceeptVo.getRankType())) { //省的所有市 for (String s : channelMapAceeptVo.getAddrCode()) { String substring = s.substring(0, 2); addrCodeList.add(substring); } } else if ("zone".equals(channelMapAceeptVo.getRankType())) { //市的所有区 for (String s : channelMapAceeptVo.getAddrCode()) { String substring = s.substring(0, 4); addrCodeList.add(substring); } } else if ("district".equals(channelMapAceeptVo.getRankType())){ //区的所有街道 for (String s : channelMapAceeptVo.getAddrCode()) { String substring = s.substring(0, 6); addrCodeList.add(substring); } }else { //省码 for (String s : channelMapAceeptVo.getAddrCode()) { String substring = s.substring(0, 2); addrCodeList.add(substring); } } queryWrapper.in("wd_type_code", channelMapAceeptVo.getChannel()).and(originWdInfoQueryWrapper -> { for (String s : addrCodeList) { originWdInfoQueryWrapper.likeRight("addr_code", s).or(); } }); //搜索字段 if (channelMapAceeptVo.getSearchText() != null && !channelMapAceeptVo.getSearchText() .trim().equals("")) { queryWrapper.and(originWdInfoQueryWrapper -> { originWdInfoQueryWrapper.like("wd_name", channelMapAceeptVo.getSearchText() ); }); } //城市等级分类 if(channelMapAceeptVo.getCityTier() != null && channelMapAceeptVo.getCityTier().length > 0){ List tierCode = new ArrayList<>(); for (String s : channelMapAceeptVo.getCityTier()) { List list = initMapUtil.getInitCityTierListMap(s); if (list != null){ tierCode.addAll(list); } } queryWrapper.and(wdInfoQueryWrapper -> { for (String s : tierCode) { wdInfoQueryWrapper.likeRight("addr_code", s).or(); } }); } //排序字段查询 if (channelMapAceeptVo.getOrderby() != null && !channelMapAceeptVo.getOrderby().trim().equals("")) { PageHelper.orderBy(channelMapAceeptVo.getOrderby()); } } }