ChannelAnalyseServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package com.ruoyi.demo.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.github.pagehelper.PageHelper;
  4. import com.github.pagehelper.PageInfo;
  5. import com.ruoyi.demo.entity.Brand;
  6. import com.ruoyi.demo.entity.ManageType;
  7. import com.ruoyi.demo.entity.StoreWd;
  8. import com.ruoyi.demo.entity.WdInfo;
  9. import com.ruoyi.demo.entity.bo.Histogram;
  10. import com.ruoyi.demo.entity.bo.StoreWdCategoryCount;
  11. import com.ruoyi.demo.entity.bo.StoreWdCategoryCountBody;
  12. import com.ruoyi.demo.entity.vo.ChannelAnalyseAceeptVo;
  13. import com.ruoyi.demo.entity.vo.ChannelMapAceeptVo;
  14. import com.ruoyi.demo.entity.vo.TagAnalyse;
  15. import com.ruoyi.demo.mapper.BrandMapper;
  16. import com.ruoyi.demo.mapper.StoreWdDao;
  17. import com.ruoyi.demo.mapper.WdInfoDao;
  18. import com.ruoyi.demo.mapper.WdTopologicalInfoDao;
  19. import com.ruoyi.demo.service.ChannelAnalyseService;
  20. import com.ruoyi.demo.utils.InitMapUtil;
  21. import com.ruoyi.demo.utils.WdRedisStoreage;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Qualifier;
  24. import org.springframework.data.geo.Circle;
  25. import org.springframework.data.geo.Distance;
  26. import org.springframework.data.geo.GeoResults;
  27. import org.springframework.data.geo.Point;
  28. import org.springframework.data.redis.connection.RedisGeoCommands;
  29. import org.springframework.data.redis.core.RedisTemplate;
  30. import org.springframework.stereotype.Service;
  31. import org.springframework.web.client.RestTemplate;
  32. import java.math.BigDecimal;
  33. import java.util.*;
  34. import java.util.concurrent.CompletableFuture;
  35. import java.util.concurrent.ExecutionException;
  36. import java.util.concurrent.ExecutorService;
  37. import java.util.stream.Collectors;
  38. @Service
  39. public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
  40. @Autowired
  41. WdInfoDao wdInfoDao;
  42. @Autowired
  43. InitMapUtil initMapUtil;
  44. @Autowired
  45. BrandMapper brandMapper;
  46. @Autowired
  47. WdTopologicalInfoDao wdTopologicalInfoDao;
  48. @Autowired
  49. private WdRedisStoreage wdRedisStoreage;
  50. @Autowired
  51. private StoreWdDao storeWdDao;
  52. @Autowired
  53. @Qualifier("executor")
  54. ExecutorService executor;
  55. @Override
  56. public HashMap cityTier(ChannelMapAceeptVo channelMapAceeptVo) {
  57. //1.统计结果
  58. HashMap<String, Long> result = new HashMap<>();
  59. result.put("一线", 0L);
  60. result.put("新一线", 0L);
  61. result.put("二线", 0L);
  62. result.put("三线", 0L);
  63. result.put("四线", 0L);
  64. result.put("五线", 0L);
  65. result.put("其他", 0L);
  66. //1.获取所有符合条件的网点信息
  67. QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
  68. queryWrapper.select("addr_code","count(*) as audit");
  69. assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
  70. queryWrapper.groupBy("addr_code");
  71. List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
  72. for (WdInfo wdInfo : wdInfos) {
  73. String initCityTierMap = initMapUtil.getInitCityTierMap(wdInfo.getAddrCode());
  74. if (initCityTierMap != null) {
  75. result.put(initCityTierMap, result.get(initCityTierMap) + wdInfo.getAudit());
  76. } else
  77. result.put("其他", result.get("其他") + wdInfo.getAudit());
  78. }
  79. return result;
  80. }
  81. @Override
  82. public PageInfo<Brand> brandList(ChannelAnalyseAceeptVo channelAnalyseAceeptVo) {
  83. String text = "";
  84. if (channelAnalyseAceeptVo.getSearchText() != null)
  85. text = channelAnalyseAceeptVo.getSearchText();
  86. PageHelper.startPage(channelAnalyseAceeptVo.getPageNum(), channelAnalyseAceeptVo.getPageSize());
  87. List<Brand> brands = brandMapper.searchList(text);
  88. PageInfo<Brand> pageInfo = new PageInfo<>(brands);
  89. return pageInfo;
  90. }
  91. //TODO 待测试(redis版本)
  92. @Override
  93. public HashMap<String, Object> tagAnalyse(ChannelMapAceeptVo channelMapAceeptVo) {
  94. //1.根据条件的到位网点信息
  95. QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
  96. queryWrapper.select("wd_id");
  97. assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
  98. List<WdInfo> queryWd = wdInfoDao.selectList(queryWrapper);
  99. //2.获取网点周边标签,并进行统计
  100. Hashtable<String, Integer> hashtable = new Hashtable<>();
  101. hashtable.put("total",0);
  102. CompletableFuture<Void> future = null;
  103. for (WdInfo wdInfo : queryWd) {
  104. future = CompletableFuture.runAsync(()->{
  105. List<String> wdTag = wdRedisStoreage.getWdTag(wdInfo);
  106. if (wdTag != null){
  107. for (String s : wdTag) {
  108. s += "边";
  109. Integer integer = hashtable.get(s);
  110. if (integer == null)
  111. hashtable.put(s,1);
  112. else
  113. hashtable.put(s,integer+1);
  114. hashtable.put("total",hashtable.get("total")+1);
  115. }
  116. }
  117. },executor);
  118. }
  119. try {
  120. future.get();
  121. } catch (InterruptedException e) {
  122. throw new RuntimeException(e);
  123. } catch (ExecutionException e) {
  124. throw new RuntimeException(e);
  125. }
  126. //3.封装统计值
  127. HashMap<String, Object> result = new HashMap<>();
  128. List<TagAnalyse> list = new ArrayList<>();
  129. Integer total = hashtable.get("total");
  130. for (String s : hashtable.keySet()) {
  131. if (s.equals("total"))
  132. continue;
  133. TagAnalyse tagAnalyse = new TagAnalyse();
  134. tagAnalyse.setName(s);
  135. tagAnalyse.setCount(hashtable.get(s));
  136. BigDecimal bigDecimal = new BigDecimal((double) tagAnalyse.getCount() / total);
  137. double v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  138. tagAnalyse.setRadio(v);
  139. list.add(tagAnalyse);
  140. }
  141. //4.对结果集进行排序
  142. Collections.sort(list, new Comparator<TagAnalyse>() {
  143. @Override
  144. public int compare(TagAnalyse o1, TagAnalyse o2) {
  145. return o2.getCount() - o1.getCount();
  146. }
  147. });
  148. //5.分页返回结果集
  149. int i = 1;
  150. int start = 0,end = 6;
  151. while(start<list.size()){
  152. if(end>=list.size())
  153. end = list.size();
  154. result.put(""+i++,new ArrayList(list.subList(start,end)));
  155. start=end;
  156. end+=6;
  157. }
  158. result.put("pages",i-1);
  159. result.put("total",total);
  160. return result;
  161. }
  162. @Override
  163. public List<StoreWdCategoryCount> category(ChannelMapAceeptVo channelMapAceeptVo) {
  164. //2.组装条件构造器找到网点信息网点
  165. QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
  166. queryWrapper.select("wd_id");
  167. assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
  168. //门店
  169. queryWrapper.and(queryWrapper1 -> {
  170. queryWrapper1.eq("wd_type_code","1");
  171. });
  172. List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
  173. List<String> collect = wdInfos.stream().map(wdInfo -> {
  174. return wdInfo.getWdId();
  175. }).collect(Collectors.toList());
  176. QueryWrapper<StoreWd> queryWrapper1 = new QueryWrapper<>();
  177. queryWrapper1.select("manage_type_code","count(*) as comment_count")
  178. .in("wd_id",collect)
  179. .groupBy("manage_type_code");
  180. //5.查询周边网点结果,并统计
  181. List<StoreWd> storeWds = storeWdDao.selectList(queryWrapper1);
  182. HashMap<String,StoreWdCategoryCount> hashMap = new HashMap<>();
  183. for (StoreWd wdInfo : storeWds) {
  184. if(wdInfo.getManageTypeCode() == null || wdInfo.getManageTypeCode().equals(""))
  185. continue;
  186. String s = wdInfo.getManageTypeCode().substring(0, 4) + "00";
  187. StoreWdCategoryCount storeWdCategoryCount = hashMap.get(s);
  188. ManageType initManageType = initMapUtil.getInitManageType(wdInfo.getManageTypeCode());
  189. if (storeWdCategoryCount == null){
  190. StoreWdCategoryCountBody storeWdCategoryCountBody = new StoreWdCategoryCountBody();
  191. if(initManageType.getSubCategory() != null && !initManageType.getSubCategory().equals("")){
  192. storeWdCategoryCountBody.setName(initManageType.getSubCategory());
  193. }else if(initManageType.getMidCategory() != null && !initManageType.getMidCategory().equals("")){
  194. storeWdCategoryCountBody.setName(initManageType.getMidCategory());
  195. }else if (initManageType.getBigCategory() != null && !initManageType.getBigCategory().equals("")){
  196. storeWdCategoryCountBody.setName(initManageType.getBigCategory());
  197. }
  198. storeWdCategoryCountBody.setManageTypeCode(wdInfo.getManageTypeCode());
  199. storeWdCategoryCountBody.setCount(wdInfo.getCommentCount());
  200. StoreWdCategoryCount storeWdCategoryCount1 = new StoreWdCategoryCount();
  201. storeWdCategoryCount1.setManageTypeCode(s);
  202. storeWdCategoryCount1.setName(initMapUtil.getInitManageType(s).getMidCategory());
  203. storeWdCategoryCount1.setCount(wdInfo.getCommentCount());
  204. ArrayList<StoreWdCategoryCountBody> list = new ArrayList<>();
  205. list.add(storeWdCategoryCountBody);
  206. storeWdCategoryCount1.setStoreWdCategoryCountBodyList(list);
  207. hashMap.put(s,storeWdCategoryCount1);
  208. }else {
  209. StoreWdCategoryCountBody storeWdCategoryCountBody = new StoreWdCategoryCountBody();
  210. if(initManageType.getSubCategory() != null && !initManageType.getSubCategory().equals("")){
  211. storeWdCategoryCountBody.setName(initManageType.getSubCategory());
  212. }else if(initManageType.getMidCategory() != null && !initManageType.getMidCategory().equals("")){
  213. storeWdCategoryCountBody.setName(initManageType.getMidCategory());
  214. }else if (initManageType.getBigCategory() != null && !initManageType.getBigCategory().equals("")){
  215. storeWdCategoryCountBody.setName(initManageType.getBigCategory());
  216. }
  217. storeWdCategoryCountBody.setManageTypeCode(wdInfo.getManageTypeCode());
  218. storeWdCategoryCountBody.setCount(wdInfo.getCommentCount());
  219. storeWdCategoryCount.getStoreWdCategoryCountBodyList().add(storeWdCategoryCountBody);
  220. storeWdCategoryCount.setCount(storeWdCategoryCountBody.getCount()+storeWdCategoryCount.getCount());
  221. //hashMap.put(s,storeWdCategoryCount);
  222. }
  223. }
  224. //6.组装结果集
  225. List<StoreWdCategoryCount> storeWdCategoryCounts = new ArrayList<>();
  226. for (String s : hashMap.keySet()) {
  227. storeWdCategoryCounts.add(hashMap.get(s));
  228. }
  229. return storeWdCategoryCounts;
  230. }
  231. @Override
  232. public HashMap<String,Integer> businessStatusAnalyse(ChannelMapAceeptVo channelMapAceeptVo) {
  233. QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
  234. queryWrapper.select("wd_id");
  235. assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
  236. queryWrapper.and(queryWrapper1 -> {
  237. queryWrapper1.eq("wd_type_code","1");
  238. });
  239. List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
  240. List<String> collect = wdInfos.stream().map(wdInfo -> {
  241. return wdInfo.getWdId();
  242. }).collect(Collectors.toList());
  243. QueryWrapper<StoreWd> queryWrapper1 = new QueryWrapper<>();
  244. queryWrapper1.select("business_status","count(*) as comment_count")
  245. .in("wd_id",collect)
  246. .groupBy("business_status");
  247. List<StoreWd> storeWds = storeWdDao.selectList(queryWrapper1);
  248. HashMap<String,Integer> hashMap = new HashMap<>();
  249. int total = 0;
  250. for (StoreWd storeWd : storeWds) {
  251. total+=storeWd.getCommentCount();
  252. hashMap.put(storeWd.getBusinessStatus(),storeWd.getCommentCount());
  253. }
  254. hashMap.put("total",total);
  255. return hashMap;
  256. }
  257. @Override
  258. public ArrayList<Histogram> aroundBuildAnalyse(ChannelMapAceeptVo channelMapAceeptVo) {
  259. QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
  260. queryWrapper.select("type_code_by","count(*) as audit");
  261. assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
  262. queryWrapper.and(queryWrapper1 -> {
  263. queryWrapper1.eq("wd_type_code","3");
  264. });
  265. queryWrapper.groupBy("type_code_by");
  266. List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
  267. int total = 0;
  268. HashMap<String,Integer> hashMap = new HashMap<>();
  269. for (WdInfo wdInfo : wdInfos) {
  270. if(wdInfo.getTypeCodeBy() == null)
  271. continue;
  272. String typeCodeBy = wdInfo.getTypeCodeBy().substring(0,2);
  273. Integer integer = hashMap.get(typeCodeBy);
  274. if (integer == null){
  275. hashMap.put(typeCodeBy,wdInfo.getAudit());
  276. }else {
  277. hashMap.put(typeCodeBy,hashMap.get(typeCodeBy)+wdInfo.getAudit());
  278. }
  279. total+=wdInfo.getAudit();
  280. }
  281. ArrayList<Histogram> list = new ArrayList<>();
  282. for (String s : hashMap.keySet()) {
  283. Integer integer = hashMap.get(s);
  284. String initTypeByMap = initMapUtil.getInitTypeByMap(s + "0000").replace(":","");
  285. Histogram histogram = new Histogram();
  286. histogram.setName(initTypeByMap);
  287. histogram.setCount(integer);
  288. list.add(histogram);
  289. }
  290. return list;
  291. }
  292. @Override
  293. public ArrayList<Histogram> perCapitaConsumpAnalyse(ChannelMapAceeptVo channelMapAceeptVo) {
  294. QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
  295. queryWrapper.select("wd_id");
  296. assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
  297. queryWrapper.and(queryWrapper1 -> {
  298. queryWrapper1.eq("wd_type_code","1");
  299. });
  300. List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
  301. List<String> collect = wdInfos.stream().map(item -> {
  302. return item.getWdId();
  303. }).collect(Collectors.toList());
  304. HashMap<String, Integer> hashMap = new HashMap<>();
  305. hashMap.put("0~20",0);
  306. hashMap.put("20~50",0);
  307. hashMap.put("50~100",0);
  308. hashMap.put("100~200",0);
  309. hashMap.put("200以上",0);
  310. if (!collect.isEmpty()){
  311. QueryWrapper<StoreWd> queryWrapper1 = new QueryWrapper<>();
  312. queryWrapper1.select("per_capita_consumption");
  313. //queryWrapper1.in("wd_id",collect);
  314. for (String s : collect) {
  315. queryWrapper1.eq("wd_id",s).or();
  316. }
  317. List<StoreWd> storeWds = storeWdDao.selectList(queryWrapper1);
  318. for (StoreWd storeWd : storeWds) {
  319. if (storeWd.getPerCapitaConsumption() != null){
  320. if (storeWd.getPerCapitaConsumption() >= 0 && storeWd.getPerCapitaConsumption()<20){
  321. hashMap.put("0~20",hashMap.get("0~20")+1);
  322. }else if(storeWd.getPerCapitaConsumption() >= 20 && storeWd.getPerCapitaConsumption()<50){
  323. hashMap.put("20~50",hashMap.get("20~50")+1);
  324. }else if(storeWd.getPerCapitaConsumption() >= 50 && storeWd.getPerCapitaConsumption()< 100){
  325. hashMap.put("50~100",hashMap.get("50~1000")+1);
  326. }else if(storeWd.getPerCapitaConsumption() >= 100 && storeWd.getPerCapitaConsumption()< 200){
  327. hashMap.put("100~200",hashMap.get("100~200")+1);
  328. }else {
  329. hashMap.put("200以上",hashMap.get("200以上")+1);
  330. }
  331. }
  332. }
  333. }
  334. ArrayList<Histogram> list = new ArrayList<>();
  335. for (String s : hashMap.keySet()) {
  336. Histogram histogram = new Histogram();
  337. histogram.setName(s);
  338. histogram.setCount(hashMap.get(s));
  339. list.add(histogram);
  340. }
  341. return list;
  342. }
  343. public void assembleQueryWrapper(QueryWrapper<WdInfo> queryWrapper,ChannelMapAceeptVo channelMapAceeptVo){
  344. //1.根据不同级别得到需要查询的 地区码
  345. List<String> addrCodeList = new ArrayList<>();
  346. if ("province".equals(channelMapAceeptVo.getRankType())) {
  347. //省码
  348. for (String s : channelMapAceeptVo.getAddrCode()) {
  349. String substring = s.substring(0, 2);
  350. addrCodeList.add(substring);
  351. }
  352. } else if ("city".equals(channelMapAceeptVo.getRankType())) {
  353. //省的所有市
  354. for (String s : channelMapAceeptVo.getAddrCode()) {
  355. String substring = s.substring(0, 2);
  356. addrCodeList.add(substring);
  357. }
  358. } else if ("zone".equals(channelMapAceeptVo.getRankType())) {
  359. //市的所有区
  360. for (String s : channelMapAceeptVo.getAddrCode()) {
  361. String substring = s.substring(0, 4);
  362. addrCodeList.add(substring);
  363. }
  364. } else if ("district".equals(channelMapAceeptVo.getRankType())){
  365. //区的所有街道
  366. for (String s : channelMapAceeptVo.getAddrCode()) {
  367. String substring = s.substring(0, 6);
  368. addrCodeList.add(substring);
  369. }
  370. }else {
  371. //省码
  372. for (String s : channelMapAceeptVo.getAddrCode()) {
  373. String substring = s.substring(0, 2);
  374. addrCodeList.add(substring);
  375. }
  376. }
  377. queryWrapper.in("wd_type_code", channelMapAceeptVo.getChannel()).and(originWdInfoQueryWrapper -> {
  378. for (String s : addrCodeList) {
  379. originWdInfoQueryWrapper.likeRight("addr_code", s).or();
  380. }
  381. });
  382. //搜索字段
  383. if (channelMapAceeptVo.getSearchText() != null && !channelMapAceeptVo.getSearchText() .trim().equals("")) {
  384. queryWrapper.and(originWdInfoQueryWrapper -> {
  385. originWdInfoQueryWrapper.like("wd_name", channelMapAceeptVo.getSearchText() );
  386. });
  387. }
  388. //城市等级分类
  389. if(channelMapAceeptVo.getCityTier() != null && channelMapAceeptVo.getCityTier().length > 0){
  390. List<String> tierCode = new ArrayList<>();
  391. for (String s : channelMapAceeptVo.getCityTier()) {
  392. List<String> list = initMapUtil.getInitCityTierListMap(s);
  393. if (list != null){
  394. tierCode.addAll(list);
  395. }
  396. }
  397. queryWrapper.and(wdInfoQueryWrapper -> {
  398. for (String s : tierCode) {
  399. wdInfoQueryWrapper.likeRight("addr_code", s).or();
  400. }
  401. });
  402. }
  403. //排序字段查询
  404. if (channelMapAceeptVo.getOrderby() != null && !channelMapAceeptVo.getOrderby().trim().equals("")) {
  405. PageHelper.orderBy(channelMapAceeptVo.getOrderby());
  406. }
  407. }
  408. }