SignAgeGroupDiseaseJob.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package com.yihu.wlyy.job;
  2. import com.yihu.wlyy.entity.SignFamily;
  3. import com.yihu.wlyy.entity.WlyyJobLog;
  4. import com.yihu.wlyy.entity.WlyyQuotaResult;
  5. import com.yihu.wlyy.entity.address.Hospital;
  6. import com.yihu.wlyy.entity.address.Town;
  7. import com.yihu.wlyy.repository.*;
  8. import com.yihu.wlyy.util.IdCardUtil;
  9. import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
  10. import com.yihu.wlyy.web.quota.WlyyQuotaVO;
  11. import org.json.JSONArray;
  12. import org.json.JSONObject;
  13. import org.quartz.Job;
  14. import org.quartz.JobDataMap;
  15. import org.quartz.JobExecutionContext;
  16. import org.quartz.JobExecutionException;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.data.redis.core.StringRedisTemplate;
  19. import org.springframework.jdbc.core.JdbcTemplate;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.StringUtils;
  23. import org.springframework.web.context.support.SpringBeanAutowiringSupport;
  24. import java.text.SimpleDateFormat;
  25. import java.util.*;
  26. /**
  27. * 签约下按年龄分组后再按疾病统计
  28. */
  29. @Component
  30. public class SignAgeGroupDiseaseJob implements Job {
  31. private WlyyQuotaVO wlyyQuota;//指标对象
  32. private WlyyJobConfigVO wlyyJobConfig;//配置对象
  33. @Autowired
  34. private WlyyQuotaResultDao wlyyQuotaResultDao;//指标结果Dao
  35. @Autowired
  36. private WlyyJobLogDao wlyyJobLogDao;//执行日志Dao
  37. @Autowired
  38. private SignFamilyDao signFamilyDao;
  39. @Autowired
  40. private DoctorDao doctorDao;
  41. @Autowired
  42. private HospitalDao hospitalDao;
  43. @Autowired
  44. private TownDao townDao;
  45. @Autowired
  46. private DoctorPatientGroupInfoDao doctorPatientGroupInfoDao;
  47. @Autowired
  48. private PatientDao patientDao;
  49. @Autowired
  50. private JdbcTemplate jdbcTemplate;
  51. @Autowired
  52. private StringRedisTemplate redisTemplate;
  53. String yesterday;
  54. String now;
  55. @Override
  56. public void execute(JobExecutionContext context)
  57. throws JobExecutionException {
  58. SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
  59. //初始化参数
  60. JobDataMap map = context.getJobDetail().getJobDataMap();
  61. wlyyQuota = (WlyyQuotaVO) map.get("quota");
  62. wlyyJobConfig = (WlyyJobConfigVO) map.get("jobConfig");
  63. now = StringUtils.isEmpty(map.get("now")) ? SignAgeGroupDiseaseJob.getDayString(0) : map.get("now").toString();
  64. yesterday = StringUtils.isEmpty(map.get("yesterday")) ? SignAgeGroupDiseaseJob.getDayString(-1) : map.get("yesterday").toString();
  65. computequotaByPatientAge();
  66. }
  67. /**
  68. * 机构维度患者年龄维度计算指标
  69. */
  70. @Transactional
  71. private void computequotaByPatientAge() {
  72. try {
  73. jdbcTemplate.execute("delete from wlyy_quota_result where quota_date='"+yesterday+"' and quato_code='"+12+"'");
  74. //新建任务日志对象
  75. WlyyJobLog wlyyJobLog = new WlyyJobLog();
  76. wlyyJobLog.setJobStartTime(new Date());
  77. wlyyJobLog.setJobId(wlyyJobConfig.getId());
  78. wlyyJobLog.setJobName(wlyyJobConfig.getJobName());
  79. //查找出系统全部的机构
  80. List<Hospital> hospitals = hospitalDao.findHospital2();
  81. Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
  82. for (Hospital hospital : hospitals) {
  83. hospitalsMap.put(hospital.getCode(), hospital);
  84. }
  85. //查找出厦门市全部的区
  86. List<Town> towns = townDao.findByCityCode(Constant.city);
  87. Map<String, Town> townsMap = new HashMap<String, Town>();
  88. for (Town town : towns) {
  89. townsMap.put(town.getCode(), town);
  90. }
  91. //找出今天的签约信息
  92. List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(yesterday, now);
  93. //数组里面第一个是健康人群 第二个是慢病人群 第三个是65岁以上人群
  94. Map<String, Map<String, Map>> cityAgeMap = new HashMap<String, Map<String, Map>>();//key是市行政代码 目前只有厦门市
  95. Map<String,Map> temp =new HashMap<String,Map>();
  96. cityAgeMap.put(Constant.city, temp);
  97. Map<String, Map<String, Map>> townAgeMap = new HashMap<String, Map<String, Map>>();//key是区行政代码
  98. Map<String, Map<String, Map>> orgAgeMap = new HashMap<String, Map<String, Map>>();//key是机构代码
  99. //统计有签约的
  100. for (SignFamily signFamily : signFamilys) {
  101. Hospital hospital = hospitalsMap.get(signFamily.getHospital());//得到患者签约的机构
  102. String town = hospital.getTown();
  103. int age = IdCardUtil.getAgeForIdcard(signFamily.getIdcard());//根据card解析年龄
  104. String ageCode = getAgeCode(age);//得到年龄的code
  105. boolean hasGXY = false;//有高血压
  106. boolean hasTNB = false;//有糖尿病
  107. //如果是慢病的 统计高血压的 糖尿病 1高血压,2糖尿病 糖尿病和高血压是100
  108. String diseaseType="";
  109. String jsonString = redisTemplate.opsForValue().get("disease:" + signFamily.getPatient());
  110. if (StringUtils.isEmpty(jsonString)) {
  111. continue;
  112. }
  113. //排除数据 只留下高血压和糖尿病
  114. List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
  115. JSONArray redisValues = new JSONArray(jsonString);
  116. for (Object obj : redisValues) {
  117. JSONObject redisValue = new JSONObject(obj);
  118. //排除掉三师签约
  119. if ("1".equals(redisValue.get("signType").toString())) {
  120. continue;
  121. }
  122. String disease = redisValue.get("disease").toString();
  123. if (Integer.valueOf(disease).equals("1")) {
  124. jsonObjects.add(redisValue);
  125. hasGXY = true;//设置有高血压
  126. diseaseType="1";
  127. }
  128. if (Integer.valueOf(disease).equals("2")) {
  129. jsonObjects.add(redisValue);
  130. hasTNB = true;//设置有糖尿病
  131. diseaseType="2";
  132. }
  133. }
  134. if(hasGXY&&hasTNB){
  135. diseaseType="100";
  136. }
  137. //统计市
  138. compute(cityAgeMap, Constant.city, ageCode, diseaseType);
  139. //统计区
  140. compute(townAgeMap, town, ageCode, diseaseType);
  141. //统计机构
  142. //统计站
  143. if (!"00".equals(hospital.getCode().substring(8))) {
  144. String orgCodeTemp = hospital.getCode().substring(0, 8) + "00";
  145. //统计机构
  146. compute(orgAgeMap, orgCodeTemp, ageCode, diseaseType);
  147. } else {
  148. compute(orgAgeMap, hospital.getCode(), ageCode, diseaseType);
  149. }
  150. }
  151. //保存统计数据
  152. // 保存市的统计数据
  153. for (Map.Entry<String, Map<String, Map>> entry : cityAgeMap.entrySet()) {
  154. Map<String, Map> oneAgeMap = entry.getValue();
  155. for(int i=1;i<7;i++){
  156. for(int j=1;j<4;j++){
  157. String key_2=i+"";
  158. String key_3=j+"";
  159. String city=Constant.city;
  160. String cityName=Constant.cityName;
  161. String town="";
  162. String townName="";
  163. String org="";
  164. String orgName="";
  165. save(oneAgeMap, key_2, key_3, city, cityName, town, townName, org, orgName,"4");
  166. }
  167. }
  168. }
  169. //保存区级
  170. for (Map.Entry<String, Town> entry : townsMap.entrySet()) {
  171. //判断该区是否有统计数据
  172. Map<String, Map> oneAgeMap = townAgeMap.get(entry.getKey());//得到当个区的统计数据
  173. Town townObj = entry.getValue();//得到区级信息
  174. for(int i=1;i<7;i++){
  175. for(int j=1;j<4;j++){
  176. String key_2=i+"";
  177. String key_3=j+"";
  178. String city=Constant.city;
  179. String cityName=Constant.cityName;
  180. String town=townObj.getCode();
  181. String townName=townObj.getName();
  182. String org="";
  183. String orgName="";
  184. save(oneAgeMap, key_2, key_3, city, cityName, town, townName, org, orgName,"3");
  185. }
  186. }
  187. }
  188. for (Map.Entry<String, Hospital> entry : hospitalsMap.entrySet()) {
  189. //判断该机构是否有统计数据
  190. Map<String, Map> oneAgeMap = orgAgeMap.get(entry.getKey());//得到当个机构的统计数据
  191. Hospital hospital = entry.getValue();//得到机构信息
  192. for(int i=1;i<7;i++){
  193. for(int j=1;j<4;j++){
  194. String key_2=i+"";
  195. String key_3=j+"";
  196. String city=Constant.city;
  197. String cityName=Constant.cityName;
  198. String town=hospital.getTown();
  199. String townName=hospital.getTownName();
  200. String org=hospital.getCode();
  201. String orgName=hospital.getName();
  202. save(oneAgeMap, key_2, key_3, city, cityName, town, townName, org, orgName,"2");
  203. }
  204. }
  205. }
  206. wlyyJobLog.setJobEndTime(new Date());
  207. wlyyJobLog.setJobContent("统计" + getYesterday() + " 的签约患者年龄数据完成 ");
  208. wlyyJobLog.setJobType("1");
  209. wlyyJobLogDao.save(wlyyJobLog);
  210. } catch (Exception e) {
  211. e.printStackTrace();
  212. }
  213. }
  214. private void save(Map<String, Map> oneAgeMap, String key_2, String key_3, String city, String cityName, String town, String townName, String org, String orgName,String level) {
  215. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  216. wlyyQuotaResult.setDel("1");
  217. wlyyQuotaResult.setCity(city);
  218. wlyyQuotaResult.setCityName(cityName);
  219. wlyyQuotaResult.setTown(town);
  220. wlyyQuotaResult.setTownName(townName);
  221. wlyyQuotaResult.setOrgCode(org);
  222. wlyyQuotaResult.setOrgName(orgName);
  223. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  224. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  225. wlyyQuotaResult.setQuotaDate(getYesterday());
  226. wlyyQuotaResult.setCreateTime(new Date());
  227. wlyyQuotaResult.setLevel1Type(level);//等级
  228. wlyyQuotaResult.setLevel2Type(key_2);
  229. wlyyQuotaResult.setLevel2TypeName(Constant.getLevelAgeName(key_2));
  230. wlyyQuotaResult.setLevel3Type(key_3);
  231. wlyyQuotaResult.setLevel3TypeName(Constant.getLevelDiseaseName(key_3));
  232. if (oneAgeMap != null && oneAgeMap.containsKey(key_2)) {
  233. Map<String,Long> key3Map=oneAgeMap.get(key_2);
  234. if(key3Map!=null&&key3Map.containsKey(key_3)){
  235. wlyyQuotaResult.setResult(key3Map.get(key_3) + "");
  236. }else{
  237. wlyyQuotaResult.setResult("0");
  238. }
  239. } else {
  240. wlyyQuotaResult.setResult("0");
  241. }
  242. wlyyQuotaResultDao.save(wlyyQuotaResult);
  243. }
  244. private void compute(Map<String, Map<String, Map>> rootMap, String rootKey, String ageCode, String diseaseType) {
  245. if (rootMap.containsKey(rootKey)) {
  246. //得到市下面的所有的年龄map
  247. Map<String, Map> groupMapTemp = rootMap.get(rootKey);
  248. if(groupMapTemp.containsKey(ageCode)){
  249. //得到这个年龄下的患者map
  250. Map<String,Long> mape= groupMapTemp.get(ageCode);
  251. if(mape.containsKey(diseaseType)){
  252. mape.put(diseaseType,mape.get(diseaseType)+1L);
  253. }else{
  254. mape.put(diseaseType,1L);
  255. }
  256. }else{
  257. //新增疾病的统计map
  258. Map<String, Long> groupMapTemp1 = new HashMap<String, Long>();
  259. groupMapTemp1.put(diseaseType, 1L);
  260. groupMapTemp.put(ageCode,groupMapTemp1);
  261. }
  262. } else {
  263. //没有就新建统计数据 新增疾病的统计map
  264. Map<String, Long> groupMapTemp = new HashMap<String, Long>();
  265. groupMapTemp.put(diseaseType, 1L);
  266. //把统计疾病的map放入对应的年龄组map中
  267. Map<String, Map> groupMapTemp2 = new HashMap<String, Map>();
  268. groupMapTemp2.put(ageCode,groupMapTemp);
  269. //把年龄组map放入市的map里面
  270. rootMap.put(rootKey, groupMapTemp2);
  271. }
  272. }
  273. /*
  274. 得到昨天的日期字符串 yyyy-MM-dd
  275. */
  276. public String getYesterday() {
  277. return yesterday;
  278. }
  279. /**
  280. * 根据年龄得到对应的code
  281. *
  282. * @param age
  283. * @return
  284. */
  285. public String getAgeCode(int age) {
  286. if (age < 7) {
  287. return Constant.level_age_1;
  288. } else if (age >= 7 && age < 18) {
  289. return Constant.level_age_2;
  290. } else if (age >= 18 && age < 30) {
  291. return Constant.level_age_3;
  292. } else if (age >= 30 && age < 50) {
  293. return Constant.level_age_4;
  294. } else if (age >= 50 && age < 65) {
  295. return Constant.level_age_5;
  296. } else {
  297. return Constant.level_age_6;
  298. }
  299. }
  300. public static String getDayString(Integer size) {
  301. Date date = new Date();//取时间
  302. Calendar calendar = new GregorianCalendar();
  303. calendar.setTime(date);
  304. calendar.add(calendar.DATE, size);//把日期往后增加一天.整数往后推,负数往前移动
  305. date = calendar.getTime(); //这个时间就是日期往后推一天的结果
  306. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  307. String dateString = formatter.format(date);
  308. return dateString;
  309. }
  310. public static void main(String[] args) {
  311. getDayString(0);
  312. }
  313. }