SignAgeGroupDiseaseJob.java 17 KB

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