StatisticsService.java 59 KB

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