StatisticsService.java 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. package com.yihu.wlyy.service.app.statistics;
  2. import com.google.common.collect.Collections2;
  3. import com.google.common.collect.Lists;
  4. import com.google.common.collect.Maps;
  5. import com.yihu.wlyy.entity.WlyyPeopleNum;
  6. import com.yihu.wlyy.entity.address.City;
  7. import com.yihu.wlyy.entity.address.Hospital;
  8. import com.yihu.wlyy.entity.address.Town;
  9. import com.yihu.wlyy.entity.doctor.Doctor;
  10. import com.yihu.wlyy.repository.*;
  11. import com.yihu.wlyy.service.BaseService;
  12. import com.yihu.wlyy.util.DateUtil;
  13. import org.json.JSONArray;
  14. import org.json.JSONObject;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.data.redis.core.StringRedisTemplate;
  17. import org.springframework.jdbc.core.JdbcTemplate;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.util.StringUtils;
  20. import redis.clients.jedis.JedisPoolConfig;
  21. import java.math.BigDecimal;
  22. import java.text.DateFormat;
  23. import java.text.DecimalFormat;
  24. import java.text.ParseException;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. /**
  28. * Created by lyr on 2016/08/16.
  29. */
  30. @Service
  31. public class StatisticsService extends BaseService {
  32. @Autowired
  33. CityDao cityDao;
  34. @Autowired
  35. TownDao townDao;
  36. @Autowired
  37. DoctorDao doctorDao;
  38. @Autowired
  39. HospitalDao hospitalDao;
  40. /******************************************数据库统计指标***************************************/
  41. // 签约人数
  42. public static String SIGN = "1";
  43. // 解约人数
  44. public static String SURRENDER = "2";
  45. // 咨询数
  46. public static String CONSULT = "3";
  47. // 随访数
  48. public static String FOLLOWUP = "4";
  49. // 指导数
  50. public static String GUIDANCE = "5";
  51. // 咨询数
  52. public static String SEX = "6";
  53. // 随访数
  54. public static String GROUP = "7";
  55. // 指导数
  56. public static String AGE = "8";
  57. @Autowired
  58. JdbcTemplate jdbcTemplate;
  59. @Autowired
  60. PeopleNumDao peopleNumDao;
  61. @Autowired
  62. private StringRedisTemplate redisTemplate;
  63. /**
  64. * 查询截止某个日期累计量
  65. *
  66. * @param endDate 截止日期
  67. * @param area 区域或机构代码
  68. * @param level 级别
  69. * @return
  70. */
  71. public long getTotalAmount(String endDate, String area, int level, String index) {
  72. int todayAmount = 0;
  73. // 查询语句
  74. String sql = " select " +
  75. " sum(result) amount" +
  76. " from " +
  77. " wlyy_quota_result " +
  78. " where " +
  79. " quato_code = '" + index + "' " +
  80. " and level1_type = ? and del = '1'" +
  81. " and quota_date <= ? ";
  82. if (level == 4) {
  83. // 市级别
  84. sql += " and city = ? ";
  85. } else if (level == 3) {
  86. // 区、城镇级别
  87. sql += " and town = ? ";
  88. } else if (level == 2) {
  89. // 机构级别
  90. sql += " and org_code = ? ";
  91. } else if (level == 1) {
  92. sql += " and qkdoctor_code = ?";
  93. }
  94. Map<String, Object> result = jdbcTemplate.queryForMap(sql, new Object[]{level, endDate, area});
  95. // 截止日期包含当天,则从redis查询当天统计数据
  96. if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0) {
  97. String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
  98. if (!StringUtils.isEmpty(val)) {
  99. JSONObject valJson = new JSONObject(val);
  100. if (valJson.has("num") && valJson.getInt("num") > 0) {
  101. todayAmount = valJson.getInt("num");
  102. }
  103. }
  104. }
  105. if (result != null) {
  106. return (result.get("amount") != null ? Math.round((double) result.get("amount")) : 0) + todayAmount;
  107. } else {
  108. return 0 + todayAmount;
  109. }
  110. }
  111. /**
  112. * 查询截止某个日期累计签约率
  113. *
  114. * @param endDate 截止日期
  115. * @param area 区域或机构代码
  116. * @param level 级别
  117. * @return
  118. */
  119. public String getSignRate(String endDate, String area, int level) throws Exception {
  120. long signAmount = getTotalAmount(endDate, area, level, SIGN);
  121. long serviceNum = peopleNumDao.findPeopleNumByCode(area);
  122. DecimalFormat df = new DecimalFormat("0.00");
  123. if (serviceNum > 0) {
  124. return df.format((signAmount * 1.00) / serviceNum * 100);
  125. } else {
  126. throw new Exception("户籍人口为0");
  127. }
  128. }
  129. /**
  130. * 查询截止某个日期签约完成率
  131. *
  132. * @param endDate 截止日期
  133. * @param area 区域或机构代码
  134. * @param level 级别
  135. * @return
  136. */
  137. public String getSignTaskRate(String endDate, String area, int level) throws Exception {
  138. long signAmount = getTotalAmount(endDate, area, level, SIGN);
  139. WlyyPeopleNum peopleNum = peopleNumDao.findByCodeAndYear(area,"");
  140. DecimalFormat df = new DecimalFormat("0.00");
  141. if (peopleNum != null && peopleNum.getTaskNum() > 0) {
  142. return df.format((signAmount * 1.00) / peopleNum.getTaskNum() * 100);
  143. } else {
  144. throw new Exception("户籍人口任务数为0");
  145. }
  146. }
  147. /**
  148. * 查询截止某个日期累计建档率
  149. *
  150. * @param endDate 截止日期
  151. * @param area 区域或机构代码
  152. * @param level 级别
  153. * @return
  154. */
  155. public String getFilingRate(String endDate, String area, int level) {
  156. return "";
  157. }
  158. /**
  159. * 获取某个指标在某个期间的增长量
  160. *
  161. * @param startDate 起始时间
  162. * @param endDate 结束时间
  163. * @param area 区域或机构代码
  164. * @param level 级别
  165. * @return
  166. */
  167. public long getIntervalIncrement(String startDate, String endDate, String area, int level, String index) {
  168. int todayAmount = 0;
  169. // 查询语句
  170. String sql = " select " +
  171. " sum(result) amount" +
  172. " from " +
  173. " wlyy_quota_result " +
  174. " where " +
  175. " quato_code = '" + index + "' " +
  176. " and level1_type = ? and del = '1'" +
  177. " and quota_date >= ? " +
  178. " and quota_date <= ? ";
  179. if (level == 4) {
  180. // 市级别
  181. sql += " and city = ? ";
  182. } else if (level == 3) {
  183. // 区、城镇级别
  184. sql += " and town = ? ";
  185. } else if (level == 2) {
  186. // 机构级别
  187. sql += " and org_code = ? ";
  188. } else if (level == 1) {
  189. sql += " and qkdoctor_code = ?";
  190. }
  191. Map<String, Object> result = jdbcTemplate.queryForMap(sql, new Object[]{level, startDate, endDate, area});
  192. // 截止日期包含当天,则从redis查询当天统计数据
  193. if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0) {
  194. String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
  195. if (!StringUtils.isEmpty(val)) {
  196. JSONObject valJson = new JSONObject(val);
  197. if (valJson.has("num") && valJson.getInt("num") > 0) {
  198. todayAmount = valJson.getInt("num");
  199. }
  200. }
  201. }
  202. if (result != null) {
  203. return (result.get("amount") != null ? Math.round((double) result.get("amount")) : 0) + todayAmount;
  204. } else {
  205. return 0 + todayAmount;
  206. }
  207. }
  208. /**
  209. * 查询截止某个日期某个区域后机构各下级累计签约情况
  210. *
  211. * @param endDate 截止日期
  212. * @param area 区域或机构代码
  213. * @param level 级别
  214. * @return
  215. */
  216. public JSONArray getLowLevelSignDetail(String endDate, String area, int level) {
  217. String areaField = "";
  218. String lowLevelField = "";
  219. String lowLevelName = "";
  220. if (level == 4) {
  221. // 市级别
  222. areaField = "city";
  223. lowLevelField = "town";
  224. lowLevelName = "town_name";
  225. } else if (level == 3) {
  226. // 区、城镇级别
  227. areaField = "town";
  228. lowLevelField = "org_code";
  229. lowLevelName = "org_name";
  230. } else if (level == 2) {
  231. // 机构级别
  232. areaField = "org_code";
  233. lowLevelField = "qkdoctor_code";
  234. lowLevelName = "qkdoctor_name";
  235. } else if (level == 1) {
  236. }
  237. // 查询语句
  238. String sql = " select " +
  239. " ifnull(a." + lowLevelField + ",'') code " +
  240. " ,ifnull(a." + lowLevelName + ",'') 'name' " +
  241. " ,sum(a.result) amount ";
  242. // if(level > 3) {
  243. // sql += " ,sum(a.result)*1.00/b.num*100 rate";
  244. // }
  245. sql += " from " +
  246. " wlyy_quota_result a";
  247. // if(level > 3) {
  248. // sql += " inner join " +
  249. // " wlyy_people_num b" +
  250. // " on a." + lowLevelField + " = b.code ";
  251. // }
  252. sql += " where " +
  253. " a.quato_code = '" + SIGN + "' " +
  254. " and a.level1_type = ? and a.del = '1'" +
  255. " and a.quota_date <= ? " +
  256. " and a." + areaField + " = ? " +
  257. " group by a." + lowLevelField + ",a." + lowLevelName;
  258. List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{level - 1, endDate, area});
  259. if (resultList != null) {
  260. DecimalFormat df = new DecimalFormat("0.00");
  261. for (Map<String, Object> map : resultList) {
  262. map.put("amount", map.get("amount") != null ? Math.round((double) map.get("amount")) : 0);
  263. // if(level > 3){
  264. // map.put("rate",df.format(map.get("rate") != null ? map.get("rate") : 0));
  265. // }
  266. }
  267. return new JSONArray(resultList);
  268. } else {
  269. return new JSONArray();
  270. }
  271. }
  272. /**
  273. * 查询截止某个日期某个区域后机构各下级累计建档情况
  274. *
  275. * @param endDate 截止日期
  276. * @param area 区域或机构代码
  277. * @param level 级别
  278. * @return
  279. */
  280. public JSONArray getLowLevelFilingDetail(String endDate, String area, int level) {
  281. String areaField = "";
  282. String lowLevelField = "";
  283. String lowLevelName = "";
  284. if (level == 4) {
  285. // 市级别
  286. areaField = "city";
  287. lowLevelField = "town";
  288. lowLevelName = "town_name";
  289. } else if (level == 3) {
  290. // 区、城镇级别
  291. areaField = "town";
  292. lowLevelField = "org_code";
  293. lowLevelName = "org_name";
  294. } else if (level == 2) {
  295. // 机构级别
  296. areaField = "org_code";
  297. lowLevelField = "qkdoctor_code";
  298. lowLevelName = "qkdoctor_name";
  299. } else if (level == 1) {
  300. }
  301. // 查询语句
  302. String sql = " select " +
  303. " ifnull(" + lowLevelField + ",'') code " +
  304. " ,ifnull(" + lowLevelName + ",'') 'name' " +
  305. " ,sum(result) amount" +
  306. " from " +
  307. " wlyy_quota_result " +
  308. " where " +
  309. " quato_code = '' " +
  310. " and level1_type = '1' and del = '1'" +
  311. " and quota_date <= ? " +
  312. " and " + areaField + " = ? " +
  313. " group by " + lowLevelField + "," + lowLevelName;
  314. List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{endDate, area});
  315. if (resultList != null) {
  316. for (Map<String, Object> map : resultList) {
  317. map.put("amount", map.get("amount") != null ? Math.round((double) map.get("amount")) : 0);
  318. }
  319. return new JSONArray(resultList);
  320. } else {
  321. return new JSONArray();
  322. }
  323. }
  324. /**
  325. * 查询截止某个日期某个区域后机构各下级指标累计情况
  326. *
  327. * @param endDate 截止日期
  328. * @param area 区域或机构代码
  329. * @param level 级别
  330. * @return
  331. */
  332. public JSONArray getLowLevelTotalDetail(String endDate, String area, int level, String index, int sort) {
  333. String areaField = "";
  334. String lowLevelField = "";
  335. String lowLevelName = "";
  336. if (level == 4) {
  337. // 市级别
  338. areaField = "city";
  339. lowLevelField = "town";
  340. lowLevelName = "town_name";
  341. } else if (level == 3) {
  342. // 区、城镇级别
  343. areaField = "town";
  344. lowLevelField = "org_code";
  345. lowLevelName = "org_name";
  346. } else if (level == 2) {
  347. // 机构级别
  348. areaField = "org_code";
  349. lowLevelField = "qkdoctor_code";
  350. lowLevelName = "qkdoctor_name";
  351. } else if (level == 1) {
  352. }
  353. // 查询语句
  354. String sql = " select " +
  355. " ifnull(" + lowLevelField + ",'') code " +
  356. " ,ifnull(" + lowLevelName + ",'') 'name' " +
  357. " ,ifnull(sum(result),0) amount" +
  358. " from " +
  359. " wlyy_quota_result " +
  360. " where " +
  361. " quato_code = '" + index + "' " +
  362. " and level1_type = ? and del = '1'" +
  363. " and quota_date <= ? " +
  364. " and " + areaField + " = ? " +
  365. " group by " + lowLevelField + "," + lowLevelName;
  366. if (sort == 1) {
  367. sql += " order by amount desc";
  368. } else {
  369. sql += " order by amount asc ";
  370. }
  371. List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{level - 1, endDate, area});
  372. // 结果为空时,自建结果集
  373. if (resultList == null || resultList.size() < 1) {
  374. resultList = new ArrayList<>();
  375. if (level == 4) {
  376. List<Town> towns = townDao.findByCityCode(area);
  377. if (towns != null) {
  378. for (Town town : towns) {
  379. Map<String, Object> obj = new HashMap<>();
  380. obj.put("code", town.getCode());
  381. obj.put("name", town.getName());
  382. obj.put("amount", Double.valueOf("0.0"));
  383. }
  384. }
  385. } else if (level == 3) {
  386. List<Hospital> hospitals = hospitalDao.findByTownCode(area);
  387. if (hospitals != null) {
  388. for (Hospital hos : hospitals) {
  389. Map<String, Object> obj = new HashMap<>();
  390. obj.put("code", hos.getCode());
  391. obj.put("name", hos.getName());
  392. obj.put("amount", Double.valueOf("0.0"));
  393. }
  394. }
  395. } else if (level == 2) {
  396. List<Doctor> doctors = doctorDao.findDoctorByLevelAndHospital(area, 2);
  397. if (doctors != null) {
  398. for (Doctor doc : doctors) {
  399. Map<String, Object> obj = new HashMap<>();
  400. obj.put("code", doc.getCode());
  401. obj.put("name", doc.getName());
  402. obj.put("amount", Double.valueOf("0.0"));
  403. }
  404. }
  405. }
  406. }
  407. if (resultList != null) {
  408. for (Map<String, Object> map : resultList) {
  409. map.put("amount", map.get("amount") != null ? Math.round((double) map.get("amount")) : 0);
  410. // 截止日期包含当天,则从redis查询当天统计数据
  411. if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0) {
  412. String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
  413. String val = redisTemplate.opsForValue().get("quota:" + index + ":" + (level - 1) + ":" + code);
  414. if (!StringUtils.isEmpty(val)) {
  415. JSONObject valJson = new JSONObject(val);
  416. if (valJson.has("num") && valJson.getInt("num") > 0) {
  417. map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
  418. }
  419. }
  420. }
  421. }
  422. Collections.sort(resultList, new Comparator<Map<String, Object>>() {
  423. public int compare(Map<String, Object> o1, Map<String, Object> o2) {
  424. long map1value = (long) o1.get("amount");
  425. long map2value = (long) o2.get("amount");
  426. if (map1value - map2value > 0) {
  427. return sort == 1 ? -1 : 1;
  428. } else if (map1value - map2value < 0) {
  429. return sort == 1 ? 1 : -1;
  430. } else {
  431. return 0;
  432. }
  433. }
  434. });
  435. return new JSONArray(resultList);
  436. } else {
  437. return new JSONArray();
  438. }
  439. }
  440. /**
  441. * 查询某个日期范围内某个区域或机构各下级指标增量情况
  442. *
  443. * @param startDate 起始日期
  444. * @param endDate 结束日期
  445. * @param area 区域或机构代码
  446. * @param level 级别
  447. * @param index 指标
  448. * @return
  449. */
  450. public JSONArray getLowLevelIncrementDetail(String startDate, String endDate, String area, int level, String index, int sort) {
  451. String areaField = "";
  452. String lowLevelField = "";
  453. String lowLevelName = "";
  454. if (level == 4) {
  455. // 市级别
  456. areaField = "city";
  457. lowLevelField = "town";
  458. lowLevelName = "town_name";
  459. } else if (level == 3) {
  460. // 区、城镇级别
  461. areaField = "town";
  462. lowLevelField = "org_code";
  463. lowLevelName = "org_name";
  464. } else if (level == 2) {
  465. // 机构级别
  466. areaField = "org_code";
  467. lowLevelField = "qkdoctor_code";
  468. lowLevelName = "qkdoctor_name";
  469. } else if (level == 1) {
  470. }
  471. // 查询语句
  472. String sql = " select " +
  473. " ifnull(" + lowLevelField + ",'') code " +
  474. " ,ifnull(" + lowLevelName + ",'') 'name' " +
  475. " ,ifnull(sum(result),0) amount" +
  476. " from " +
  477. " wlyy_quota_result " +
  478. " where " +
  479. " quato_code = '" + index + "' " +
  480. " and level1_type = ? and del = '1'" +
  481. " and quota_date >= ? " +
  482. " and quota_date <= ? " +
  483. " and " + areaField + " = ? " +
  484. " group by " + lowLevelField + "," + lowLevelName;
  485. if (sort == 1) {
  486. sql += " order by amount desc";
  487. } else {
  488. sql += " order by amount asc ";
  489. }
  490. List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{level - 1, startDate, endDate, area});
  491. // 结果为空时,自建结果集
  492. if (resultList == null || resultList.size() < 1) {
  493. resultList = new ArrayList<>();
  494. if (level == 4) {
  495. List<Town> towns = townDao.findByCityCode(area);
  496. if (towns != null) {
  497. for (Town town : towns) {
  498. Map<String, Object> obj = new HashMap<>();
  499. obj.put("code", town.getCode());
  500. obj.put("name", town.getName());
  501. obj.put("amount", Double.valueOf("0.0"));
  502. resultList.add(obj);
  503. }
  504. }
  505. } else if (level == 3) {
  506. List<Hospital> hospitals = hospitalDao.findByTownCode(area);
  507. if (hospitals != null) {
  508. for (Hospital hos : hospitals) {
  509. Map<String, Object> obj = new HashMap<>();
  510. obj.put("code", hos.getCode());
  511. obj.put("name", hos.getName());
  512. obj.put("amount", Double.valueOf("0.0"));
  513. resultList.add(obj);
  514. }
  515. }
  516. } else if (level == 2) {
  517. List<Doctor> doctors = doctorDao.findDoctorByLevelAndHospital(area, 2);
  518. if (doctors != null) {
  519. for (Doctor doc : doctors) {
  520. Map<String, Object> obj = new HashMap<>();
  521. obj.put("code", doc.getCode());
  522. obj.put("name", doc.getName());
  523. obj.put("amount", Double.valueOf("0.0"));
  524. resultList.add(obj);
  525. }
  526. }
  527. }
  528. }
  529. if (resultList != null) {
  530. for (Map<String, Object> map : resultList) {
  531. map.put("amount", map.get("amount") != null ? Math.round((double) map.get("amount")) : 0);
  532. // 截止日期包含当天,则从redis查询当天统计数据
  533. if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0) {
  534. String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
  535. String val = redisTemplate.opsForValue().get("quota:" + index + ":" + (level - 1) + ":" + code);
  536. if (!StringUtils.isEmpty(val)) {
  537. JSONObject valJson = new JSONObject(val);
  538. if (valJson.has("num") && valJson.getInt("num") > 0) {
  539. map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
  540. }
  541. }
  542. }
  543. }
  544. Collections.sort(resultList, new Comparator<Map<String, Object>>() {
  545. public int compare(Map<String, Object> o1, Map<String, Object> o2) {
  546. long map1value = (long) o1.get("amount");
  547. long map2value = (long) o2.get("amount");
  548. if (map1value - map2value > 0) {
  549. return sort == 1 ? -1 : 1;
  550. } else if (map1value - map2value < 0) {
  551. return sort == 1 ? 1 : -1;
  552. } else {
  553. return 0;
  554. }
  555. }
  556. });
  557. return new JSONArray(resultList);
  558. } else {
  559. return new JSONArray();
  560. }
  561. }
  562. /**
  563. * 获取时间范围内按间隔统计指标增量
  564. *
  565. * @param startDate 起始时间
  566. * @param endDate 结束时间
  567. * @param interval 时间间隔
  568. * @param area 区域或机构代码
  569. * @param level 级别
  570. * @param index 指标
  571. * @return
  572. */
  573. public JSONArray getDateIncrementDetail(String startDate, String endDate, int interval, String area, int level, String index) throws Exception {
  574. if (interval == 1) {
  575. return dateStatistics(startDate, endDate, area, level, index);
  576. } else if (interval == 2) {
  577. return weekStatistics(startDate, endDate, area, level, index);
  578. } else if (interval == 3) {
  579. return monthStatistics(startDate, endDate, area, level, index);
  580. }
  581. return new JSONArray();
  582. }
  583. /**
  584. * 按日统计
  585. *
  586. * @param startDate
  587. * @param endDate
  588. * @param area
  589. * @param level
  590. * @param index
  591. * @return
  592. */
  593. private JSONArray dateStatistics(String startDate, String endDate, String area, int level, String index) {
  594. String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  595. String areaField = "";
  596. String sql = "";
  597. if (level == 4) {
  598. // 市级别
  599. areaField = "city";
  600. } else if (level == 3) {
  601. // 区、城镇级别
  602. areaField = "town";
  603. } else if (level == 2) {
  604. // 机构级别
  605. areaField = "org_code";
  606. } else if (level == 1) {
  607. // 机构级别
  608. areaField = "qkdoctor_code";
  609. }
  610. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  611. // 起始日期
  612. Calendar start = Calendar.getInstance();
  613. start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
  614. //结束日期
  615. Calendar end = Calendar.getInstance();
  616. end.setTime(DateUtil.strToDate(endDate, DateUtil.YYYY_MM_DD));
  617. // 日期集合
  618. List<Calendar> days = new ArrayList<>();
  619. days.add(start);
  620. boolean flag = true;
  621. if (startDate.compareTo(endDate) == 0) {
  622. flag = false;
  623. }
  624. // 计算统计日期
  625. while (flag) {
  626. Calendar next = Calendar.getInstance();
  627. next.setTime(days.get(days.size() - 1).getTime());
  628. next.add(Calendar.DATE, 1);
  629. if (df.format(next.getTime()).compareTo(endDate) < 0) {
  630. days.add(next);
  631. } else {
  632. days.add(end);
  633. flag = false;
  634. }
  635. }
  636. Map<String, JSONObject> countResult = new HashMap<>();
  637. // 统计预计构建
  638. for (int i = 0; i < days.size(); i++) {
  639. String startStr = "";
  640. long amount = 0;
  641. startStr = df.format(days.get(i).getTime());
  642. // 当前范围包含当天,则需添加当天的统计数据
  643. if (startStr.compareTo(today) == 0) {
  644. String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
  645. if (!StringUtils.isEmpty(val)) {
  646. JSONObject valJson = new JSONObject(val);
  647. if (valJson.has("num") && valJson.getInt("num") > 0) {
  648. amount = (long) valJson.getInt("num");
  649. }
  650. }
  651. }
  652. JSONObject range = new JSONObject();
  653. range.put("range", startStr);
  654. range.put("amount", amount);
  655. countResult.put(startStr, range);
  656. }
  657. sql = " select " +
  658. " ifnull(quota_date,'') as 'range' " +
  659. " ,ifnull(sum(result),0) amount " +
  660. " from " +
  661. " wlyy_quota_result " +
  662. " where " +
  663. " quato_code = '" + index + "' " +
  664. " and level1_type = '" + level + "' and del = '1' " +
  665. " and quota_date >= '" + startDate + "' " +
  666. " and quota_date <= '" + endDate + "' " +
  667. " and " + areaField + " = '" + area + "' " +
  668. " group by quota_date ";
  669. List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
  670. if (resultList != null) {
  671. for (Map<String, Object> map : resultList) {
  672. if (countResult.containsKey(map.get("range").toString())) {
  673. JSONObject range = (JSONObject) countResult.get(map.get("range").toString());
  674. long amount = range.getLong("amount");
  675. long resultAmount = map.get("amount") != null ? Math.round((double) map.get("amount")) : 0;
  676. range.put("amount", amount + resultAmount);
  677. }
  678. }
  679. List<JSONObject> result = new ArrayList<>(countResult.values());
  680. result.sort(new Comparator<JSONObject>() {
  681. @Override
  682. public int compare(JSONObject o1, JSONObject o2) {
  683. if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
  684. return 1;
  685. } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
  686. return -1;
  687. } else {
  688. return 0;
  689. }
  690. }
  691. });
  692. return new JSONArray(result);
  693. } else {
  694. return new JSONArray();
  695. }
  696. }
  697. /**
  698. * 按周统计
  699. *
  700. * @param startDate
  701. * @param endDate
  702. * @param area
  703. * @param level
  704. * @param index
  705. * @return
  706. */
  707. private JSONArray weekStatistics(String startDate, String endDate, String area, int level, String index) throws Exception {
  708. String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  709. String areaField = "";
  710. String sql = "";
  711. if (level == 4) {
  712. // 市级别
  713. areaField = "city";
  714. } else if (level == 3) {
  715. // 区、城镇级别
  716. areaField = "town";
  717. } else if (level == 2) {
  718. // 机构级别
  719. areaField = "org_code";
  720. } else if (level == 1) {
  721. // 机构级别
  722. areaField = "qkdoctor_code";
  723. }
  724. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  725. // 起始日期
  726. Calendar start = Calendar.getInstance();
  727. start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
  728. // 第一个统计周期结束日期
  729. String firstEnd = "";
  730. // 结束日期
  731. Calendar end = Calendar.getInstance();
  732. end.setTime(DateUtil.strToDate(endDate, DateUtil.YYYY_MM_DD));
  733. // 起始日期为周几
  734. int week = start.get(Calendar.DAY_OF_WEEK);
  735. int incre = 7 - week + 1;
  736. // 日期集合
  737. List<Calendar> days = new ArrayList<>();
  738. days.add(start);
  739. boolean flag = true;
  740. int i = 0;
  741. if (startDate.compareTo(endDate) == 0) {
  742. flag = false;
  743. days.add(end);
  744. firstEnd = df.format(end.getTime());
  745. }
  746. // 计算统计日期
  747. while (flag) {
  748. Calendar next = Calendar.getInstance();
  749. next.setTime(days.get(days.size() - 1).getTime());
  750. if (i == 0) {
  751. if (incre != 7) {
  752. next.add(Calendar.DATE, incre);
  753. }
  754. } else {
  755. next.add(Calendar.DATE, 7);
  756. }
  757. if (df.format(next.getTime()).compareTo(df.format(end.getTime())) < 0) {
  758. days.add(next);
  759. if (i == 0) {
  760. firstEnd = df.format(next.getTime());
  761. }
  762. } else {
  763. days.add(end);
  764. flag = false;
  765. if (i == 0) {
  766. firstEnd = df.format(end.getTime());
  767. }
  768. }
  769. i++;
  770. }
  771. // 结果集
  772. Map<String, JSONObject> countResult = new HashMap<>();
  773. // 算出每个查询周期
  774. for (int j = 0; j < days.size() - 1; j++) {
  775. String startStr = "";
  776. String endStr = df.format(days.get(j + 1).getTime());
  777. long amount = 0;
  778. // 起始、截止日期
  779. if (j == 0) {
  780. startStr = df.format(days.get(j).getTime());
  781. } else {
  782. Calendar cal = Calendar.getInstance();
  783. cal.setTime(days.get(j).getTime());
  784. cal.add(Calendar.DATE, 1);
  785. startStr = df.format(cal.getTime());
  786. }
  787. // 当前范围包含当天,则需添加当天的统计数据
  788. if (startStr.compareTo(today) <= 0 && endStr.compareTo(today) >= 0) {
  789. String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
  790. if (!StringUtils.isEmpty(val)) {
  791. JSONObject valJson = new JSONObject(val);
  792. if (valJson.has("num") && valJson.getInt("num") > 0) {
  793. amount = valJson.getInt("num");
  794. }
  795. }
  796. }
  797. JSONObject range = new JSONObject();
  798. range.put("range", startStr);
  799. range.put("amount", amount);
  800. countResult.put(startStr + ":" + endStr, range);
  801. }
  802. // 查询时间范围内所有记录
  803. sql = " select " +
  804. " ifnull(quota_date,'') as 'range' " +
  805. " ,ifnull(sum(result),0) amount " +
  806. " from " +
  807. " wlyy_quota_result " +
  808. " where " +
  809. " quato_code = '" + index + "' " +
  810. " and level1_type = '" + level + "' and del = '1' " +
  811. " and quota_date >= '" + startDate + "' " +
  812. " and quota_date <= '" + endDate + "' " +
  813. " and " + areaField + " = '" + area + "' " +
  814. " group by quota_date ";
  815. List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
  816. if (resultList != null) {
  817. // 计算结果
  818. for (Map<String, Object> map : resultList) {
  819. long resultAmount = map.get("amount") != null ? Math.round((double) map.get("amount")) : 0;
  820. String range = map.get("range").toString();
  821. if (org.apache.commons.lang3.StringUtils.isNotEmpty(range)) {
  822. // 起始日期
  823. String startStr = "";
  824. // 结束日期
  825. String endStr = "";
  826. if (range.compareTo(startDate) >= 0 && range.compareTo(firstEnd) <= 0) {
  827. startStr = startDate;
  828. endStr = firstEnd;
  829. } else {
  830. Calendar cal = Calendar.getInstance();
  831. cal.setTime(df.parse(range));
  832. int calWeek = cal.get(Calendar.DAY_OF_WEEK) - 2;
  833. if (calWeek == -1) {
  834. cal.add(Calendar.DATE, -6);
  835. } else {
  836. cal.add(Calendar.DATE, -calWeek);
  837. }
  838. startStr = df.format(cal.getTime());
  839. cal.add(Calendar.DATE, 6);
  840. endStr = df.format(cal.getTime());
  841. if (endStr.compareTo(endDate) > 0) {
  842. endStr = endDate;
  843. }
  844. }
  845. JSONObject json = countResult.get(startStr + ":" + endStr);
  846. json.put("amount", json.getLong("amount") + resultAmount);
  847. }
  848. }
  849. List<JSONObject> result = new ArrayList<>(countResult.values());
  850. // 排序
  851. result.sort(new Comparator<JSONObject>() {
  852. @Override
  853. public int compare(JSONObject o1, JSONObject o2) {
  854. if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
  855. return 1;
  856. } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
  857. return -1;
  858. } else {
  859. return 0;
  860. }
  861. }
  862. });
  863. return new JSONArray(result);
  864. } else {
  865. return new JSONArray();
  866. }
  867. }
  868. /**
  869. * 按月统计
  870. *
  871. * @param startDate
  872. * @param endDate
  873. * @param area
  874. * @param level
  875. * @param index
  876. * @return
  877. * @throws Exception
  878. */
  879. private JSONArray monthStatistics(String startDate, String endDate, String area, int level, String index) throws Exception {
  880. String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  881. String areaField = "";
  882. String sql = "";
  883. if (level == 4) {
  884. // 市级别
  885. areaField = "city";
  886. } else if (level == 3) {
  887. // 区、城镇级别
  888. areaField = "town";
  889. } else if (level == 2) {
  890. // 机构级别
  891. areaField = "org_code";
  892. } else if (level == 1) {
  893. // 机构级别
  894. areaField = "qkdoctor_code";
  895. }
  896. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  897. // 起始日期
  898. Calendar start = Calendar.getInstance();
  899. start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
  900. // 结束日期
  901. Calendar end = Calendar.getInstance();
  902. end.setTime(DateUtil.strToDate(endDate, DateUtil.YYYY_MM_DD));
  903. // 第一个结束日期
  904. String firstEnd = "";
  905. // 日期集合
  906. List<Calendar> days = new ArrayList<>();
  907. days.add(start);
  908. boolean flag = true;
  909. int k = 0;
  910. if (startDate.compareTo(endDate) == 0) {
  911. flag = false;
  912. days.add(end);
  913. firstEnd = df.format(end.getTime());
  914. }
  915. // 统计日期计算
  916. while (flag) {
  917. Calendar next = Calendar.getInstance();
  918. next.setTime(days.get(days.size() - 1).getTime());
  919. if (k == 0) {
  920. next.add(Calendar.MONTH, 1);
  921. } else {
  922. next.add(Calendar.MONTH, 2);
  923. }
  924. next.set(Calendar.DAY_OF_MONTH, 1);
  925. next.add(Calendar.DAY_OF_MONTH, -1);
  926. if (df.format(next.getTime()).compareTo(df.format(end.getTime())) < 0) {
  927. days.add(next);
  928. if (k == 0) {
  929. firstEnd = df.format(next.getTime());
  930. }
  931. } else {
  932. days.add(end);
  933. flag = false;
  934. if (k == 0) {
  935. firstEnd = df.format(end.getTime());
  936. }
  937. }
  938. k++;
  939. }
  940. // 统计结果
  941. Map<String, JSONObject> countResult = new HashMap<>();
  942. for (int i = 0; i < days.size() - 1; i++) {
  943. String startStr = "";
  944. String endStr = df.format(days.get(i + 1).getTime());
  945. int amount = 0;
  946. // 起始时间计算
  947. if (i == 0) {
  948. startStr = df.format(days.get(i).getTime());
  949. } else {
  950. Calendar cal = Calendar.getInstance();
  951. cal.setTime(days.get(i).getTime());
  952. cal.add(Calendar.DATE, 1);
  953. startStr = df.format(cal.getTime());
  954. }
  955. // 当天数据计算
  956. if (startStr.compareTo(today) <= 0 && endStr.compareTo(today) >= 0) {
  957. String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
  958. if (!StringUtils.isEmpty(val)) {
  959. JSONObject valJson = new JSONObject(val);
  960. if (valJson.has("num") && valJson.getInt("num") > 0) {
  961. amount = valJson.getInt("num");
  962. }
  963. }
  964. }
  965. JSONObject range = new JSONObject();
  966. range.put("range", startStr);
  967. range.put("amount", amount);
  968. countResult.put(startStr + ":" + endStr, range);
  969. }
  970. // 查询时间范围内所有记录
  971. sql = " select " +
  972. " ifnull(quota_date,'') as 'range' " +
  973. " ,ifnull(sum(result),0) amount " +
  974. " from " +
  975. " wlyy_quota_result " +
  976. " where " +
  977. " quato_code = '" + index + "' " +
  978. " and level1_type = '" + level + "' and del = '1' " +
  979. " and quota_date >= '" + startDate + "' " +
  980. " and quota_date <= '" + endDate + "' " +
  981. " and " + areaField + " = '" + area + "' " +
  982. " group by quota_date ";
  983. List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
  984. if (resultList != null) {
  985. // 计算结果
  986. for (Map<String, Object> map : resultList) {
  987. long resultAmount = map.get("amount") != null ? Math.round((double) map.get("amount")) : 0;
  988. String range = map.get("range").toString();
  989. if (org.apache.commons.lang3.StringUtils.isNotEmpty(range)) {
  990. // 起始日期
  991. String startStr = "";
  992. // 结束日期
  993. String endStr = "";
  994. if (range.compareTo(startDate) >= 0 && range.compareTo(firstEnd) <= 0) {
  995. startStr = startDate;
  996. endStr = firstEnd;
  997. } else {
  998. Calendar cal = Calendar.getInstance();
  999. cal.setTime(df.parse(range));
  1000. cal.set(Calendar.DATE, 1);
  1001. startStr = df.format(cal.getTime());
  1002. cal.add(Calendar.MONTH, 1);
  1003. cal.set(Calendar.DATE, 1);
  1004. cal.add(Calendar.DATE, -1);
  1005. endStr = df.format(cal.getTime());
  1006. if (endStr.compareTo(endDate) > 0) {
  1007. endStr = endDate;
  1008. }
  1009. }
  1010. JSONObject json = countResult.get(startStr + ":" + endStr);
  1011. json.put("amount", json.getLong("amount") + resultAmount);
  1012. }
  1013. }
  1014. List<JSONObject> result = new ArrayList<>(countResult.values());
  1015. // 排序
  1016. result.sort(new Comparator<JSONObject>() {
  1017. @Override
  1018. public int compare(JSONObject o1, JSONObject o2) {
  1019. if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
  1020. return 1;
  1021. } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
  1022. return -1;
  1023. } else {
  1024. return 0;
  1025. }
  1026. }
  1027. });
  1028. return new JSONArray(result);
  1029. } else {
  1030. return new JSONArray();
  1031. }
  1032. }
  1033. /**
  1034. * 获取二级维度下指标时间范围内增量
  1035. *
  1036. * @param startDate 起始时间
  1037. * @param endDate 结束时间
  1038. * @param area 区域或机构代码
  1039. * @param level 级别
  1040. * @param index 指标
  1041. * @return
  1042. */
  1043. public JSONArray getLevelTwoIndexIncrement(String startDate, String endDate, String area, int level, String index) {
  1044. String areaField = "";
  1045. if (level == 4) {
  1046. // 市级别
  1047. areaField = "city";
  1048. } else if (level == 3) {
  1049. // 区、城镇级别
  1050. areaField = "town";
  1051. } else if (level == 2) {
  1052. // 机构级别
  1053. areaField = "org_code";
  1054. } else if (level == 1) {
  1055. // 团队
  1056. areaField = "qkdoctor_code";
  1057. }
  1058. // 查询语句
  1059. String sql = " select " +
  1060. " ifnull(level2_type,'') code " +
  1061. " ,ifnull(level2_type_name,'') 'name' " +
  1062. " ,ifnull(sum(result),0) amount" +
  1063. " from " +
  1064. " wlyy_quota_result " +
  1065. " where " +
  1066. " quato_code = '" + index + "' " +
  1067. " and level1_type = ? and del = '1'";
  1068. if (!org.apache.commons.lang3.StringUtils.isEmpty(startDate)) {
  1069. sql += " and quota_date >= ? ";
  1070. }
  1071. sql += " and quota_date <= ? " +
  1072. " and " + areaField + " = ? " +
  1073. " group by level2_type,level2_type_name";
  1074. List<Map<String, Object>> resultList = null;
  1075. if (org.apache.commons.lang3.StringUtils.isEmpty(startDate)) {
  1076. resultList = jdbcTemplate.queryForList(sql, new Object[]{level, endDate, area});
  1077. } else {
  1078. resultList = jdbcTemplate.queryForList(sql, new Object[]{level, startDate, endDate, area});
  1079. }
  1080. if (resultList == null || resultList.size() < 1) {
  1081. resultList = new ArrayList<>();
  1082. if (index.equals(SEX)) {
  1083. Map<String, Object> women = new HashMap<>();
  1084. women.put("code", "1");
  1085. women.put("name", "女");
  1086. women.put("amount", Double.valueOf("0.0"));
  1087. resultList.add(women);
  1088. Map<String, Object> man = new HashMap<>();
  1089. man.put("code", "2");
  1090. man.put("name", "男");
  1091. man.put("amount", Double.valueOf("0.0"));
  1092. resultList.add(man);
  1093. Map<String, Object> unknown = new HashMap<>();
  1094. unknown.put("code", "3");
  1095. unknown.put("name", "未知");
  1096. unknown.put("amount", Double.valueOf("0.0"));
  1097. resultList.add(unknown);
  1098. } else if (index.equals(GROUP)) {
  1099. Map<String, Object> normal = new HashMap<>();
  1100. normal.put("code", "1");
  1101. normal.put("name", "普通人群");
  1102. normal.put("amount", Double.valueOf("0.0"));
  1103. resultList.add(normal);
  1104. Map<String, Object> manbing = new HashMap<>();
  1105. manbing.put("code", "2");
  1106. manbing.put("name", "慢病人群");
  1107. manbing.put("amount", Double.valueOf("0.0"));
  1108. resultList.add(manbing);
  1109. Map<String, Object> upsixfive = new HashMap<>();
  1110. upsixfive.put("code", "3");
  1111. upsixfive.put("name", "65岁以上人群");
  1112. upsixfive.put("amount", Double.valueOf("0.0"));
  1113. resultList.add(upsixfive);
  1114. Map<String, Object> gxy = new HashMap<>();
  1115. gxy.put("code", "4");
  1116. gxy.put("name", "高血压");
  1117. gxy.put("amount", Double.valueOf("0.0"));
  1118. resultList.add(gxy);
  1119. Map<String, Object> tnb = new HashMap<>();
  1120. tnb.put("code", "5");
  1121. tnb.put("name", "糖尿病");
  1122. tnb.put("amount", Double.valueOf("0.0"));
  1123. resultList.add(tnb);
  1124. } else if (index.equals(AGE)) {
  1125. Map<String, Object> map1 = new HashMap<>();
  1126. map1.put("code", "1");
  1127. map1.put("name", "0~6");
  1128. map1.put("amount", Double.valueOf("0.0"));
  1129. resultList.add(map1);
  1130. Map<String, Object> map2 = new HashMap<>();
  1131. map2.put("code", "2");
  1132. map2.put("name", "7~18");
  1133. map2.put("amount", Double.valueOf("0.0"));
  1134. resultList.add(map2);
  1135. Map<String, Object> map3 = new HashMap<>();
  1136. map3.put("code", "3");
  1137. map3.put("name", "18~30");
  1138. map3.put("amount", Double.valueOf("0.0"));
  1139. resultList.add(map3);
  1140. Map<String, Object> map4 = new HashMap<>();
  1141. map4.put("code", "4");
  1142. map4.put("name", "30~50");
  1143. map4.put("amount", Double.valueOf("0.0"));
  1144. resultList.add(map4);
  1145. Map<String, Object> map5 = new HashMap<>();
  1146. map5.put("code", "5");
  1147. map5.put("name", "50~65");
  1148. map5.put("amount", Double.valueOf("0.0"));
  1149. resultList.add(map5);
  1150. Map<String, Object> map6 = new HashMap<>();
  1151. map6.put("code", "6");
  1152. map6.put("name", "50~65");
  1153. map6.put("amount", Double.valueOf("0.0"));
  1154. resultList.add(map6);
  1155. }
  1156. }
  1157. if (resultList != null) {
  1158. long total = 0;
  1159. for (Map<String, Object> map : resultList) {
  1160. map.put("amount", map.get("amount") == null ? 0 : Math.round((double) map.get("amount")));
  1161. // 当天数据统计
  1162. if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0) {
  1163. String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
  1164. String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + code + ":" + area);
  1165. if (!StringUtils.isEmpty(val)) {
  1166. JSONObject valJson = new JSONObject(val);
  1167. if (valJson.has("num") && valJson.getInt("num") > 0) {
  1168. map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
  1169. }
  1170. }
  1171. }
  1172. if (index.equals(GROUP)) {
  1173. // 分组指标总数算法
  1174. String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
  1175. if (!code.equals("2")) {
  1176. total += (long) map.get("amount");
  1177. }
  1178. } else {
  1179. total += (long) map.get("amount");
  1180. }
  1181. }
  1182. if (!index.equals(AGE)) {
  1183. DecimalFormat df = new DecimalFormat("0.00");
  1184. double rateTotal = 0.00;
  1185. for (Map<String, Object> map : resultList) {
  1186. double rateG = (total > 0 ? ((long) map.get("amount")) * 1.00 / total * 100 : 0);
  1187. map.put("rate", df.format(rateG));
  1188. rateTotal += rateG;
  1189. }
  1190. if (1 - rateTotal > 0) {
  1191. for (Map<String, Object> map : resultList) {
  1192. if ((long) map.get("amount") > 0) {
  1193. map.put("rate", df.format(Double.valueOf((String) map.get("rate")) + (1 - rateTotal)));
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. if (index.equals(SEX) && resultList.size() > 0) {
  1200. int i = 0;
  1201. boolean flag = false;
  1202. for (; i < resultList.size(); i++) {
  1203. if (resultList.get(i).get("code") != null && String.valueOf(resultList.get(i).get("code")).equals("3")
  1204. && String.valueOf(resultList.get(i).get("amount")).equals("0")) {
  1205. flag = true;
  1206. break;
  1207. }
  1208. }
  1209. if (flag) {
  1210. resultList.remove(i);
  1211. }
  1212. }
  1213. return new JSONArray(resultList);
  1214. } else {
  1215. return new JSONArray();
  1216. }
  1217. }
  1218. }