SignAgeGroupDiseaseJob.java 15 KB

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