StatisticsService.java 60 KB

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