StatisticsService.java 59 KB

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