FollowUpJob.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.doctor.profile.Doctor;
  5. import com.yihu.wlyy.entity.job.QuartzJobLog;
  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.job.QuartzJobLogDao;
  10. import com.yihu.wlyy.repository.organization.HospitalDao;
  11. import com.yihu.wlyy.repository.statistics.WlyyQuotaResultDao;
  12. import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
  13. import com.yihu.wlyy.web.quota.WlyyQuotaVO;
  14. import org.quartz.Job;
  15. import org.quartz.JobDataMap;
  16. import org.quartz.JobExecutionContext;
  17. import org.quartz.JobExecutionException;
  18. import org.springframework.beans.factory.annotation.Autowired;
  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.io.IOException;
  25. import java.io.InputStream;
  26. import java.util.*;
  27. /**
  28. * 随访的指标执行类
  29. */
  30. @Component
  31. public class FollowUpJob implements Job {
  32. private WlyyQuotaVO wlyyQuota;//指标对象
  33. private WlyyJobConfigVO wlyyJobConfig;//配置对象
  34. @Autowired
  35. private WlyyQuotaResultDao wlyyQuotaResultDao;//指标结果Dao
  36. @Autowired
  37. private QuartzJobLogDao quartzJobLogDao;//执行日志Dao
  38. @Autowired
  39. private DoctorDao doctorDao;
  40. @Autowired
  41. private HospitalDao hospitalDao;
  42. @Autowired
  43. private TownDao townDao;
  44. @Autowired
  45. private JdbcTemplate jdbcTemplate_wlyy;
  46. private org.apache.tomcat.jdbc.pool.DataSource fvdataSource=new org.apache.tomcat.jdbc.pool.DataSource();//随访数据源
  47. private JdbcTemplate jdbcTemplate = new JdbcTemplate();
  48. private Properties systemProperties;
  49. String yesterday;
  50. String now;
  51. @Override
  52. public void execute(JobExecutionContext context)
  53. throws JobExecutionException {
  54. SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
  55. getSystemProperties();
  56. fvdataSource.setDriverClassName( systemProperties.get("fv.jdbc.driver").toString());
  57. fvdataSource.setUrl( systemProperties.get("fv.jdbc.url").toString());
  58. fvdataSource.setUsername( systemProperties.get("fv.jdbc.username").toString());
  59. fvdataSource.setPassword( systemProperties.get("fv.jdbc.password").toString());
  60. //初始化参数
  61. JobDataMap map = context.getJobDetail().getJobDataMap();
  62. wlyyQuota = (WlyyQuotaVO) map.get("quota");
  63. wlyyJobConfig = (WlyyJobConfigVO) map.get("jobConfig");
  64. //计算指标
  65. jdbcTemplate.setDataSource(fvdataSource);//初始化随访的数据源
  66. //線刪除今天的数据
  67. now= StringUtils.isEmpty(map.get("now"))?SignJob.getDayString(0):map.get("now").toString();
  68. yesterday= StringUtils.isEmpty(map.get("yesterday"))?SignJob.getDayString(-1):map.get("yesterday").toString();
  69. computequota();
  70. }
  71. @Transactional
  72. private void computequota() {
  73. jdbcTemplate_wlyy.execute("delete from wlyy_quota_result where quota_date='"+yesterday+"' and quato_code='"+4+"'");
  74. //新建任务日志对象
  75. QuartzJobLog quartzJobLog = new QuartzJobLog();
  76. quartzJobLog.setJobStartTime(new Date());
  77. quartzJobLog.setJobId(wlyyJobConfig.getId());
  78. quartzJobLog.setJobName(wlyyJobConfig.getJobName());
  79. //查找出系统全部的医生
  80. List<Doctor> doctors = doctorDao.findAllCertifiedDoctors();
  81. Map<String, Doctor> doctorsMap = new HashMap<String, Doctor>();
  82. for (Doctor doctor : doctors) {
  83. doctorsMap.put(doctor.getCode(), doctor);
  84. }
  85. //查找出系统全部的机构
  86. List<Hospital> hospitals = hospitalDao.findHospital2();
  87. Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
  88. for (Hospital hospital : hospitals) {
  89. hospitalsMap.put(hospital.getCode(), hospital);
  90. }
  91. //查找出厦门市全部的区
  92. List<Town> towns = townDao.findByCityCode(Constant.city);
  93. Map<String, Town> townsMap = new HashMap<String, Town>();
  94. for (Town town : towns) {
  95. townsMap.put(town.getCode(), town);
  96. }
  97. //找出今天的随访信息
  98. List<Map<String,Object>> plans = getFvYesterday();
  99. Map<String, Long> tjTownMap = new HashMap<String, Long>();//区级的统计map key 是区行政区划350200
  100. Map<String, Long> tjOrgMap = new HashMap<String, Long>();//机构的统计map key 是机构的code
  101. Map<String, Long> tjQkdoctorMap = new HashMap<String, Long>();//团队级的统计map 目前没有团队 先用全科医生统一 key doctorCode
  102. Long cityCount = 0L;
  103. //统计有随访的
  104. String orgCodeTemp="";
  105. for (Map<String,Object> plan : plans) {
  106. String doctorCode = (String) plan.get("doctor_uid");//得到随访中健康医生的code
  107. //统计团队
  108. compute(tjQkdoctorMap, doctorCode);
  109. //判断医生属于哪个机构
  110. Doctor doctor = doctorsMap.get(doctorCode);
  111. String orgCode = doctor.getHospital();
  112. //统计机构
  113. if(!"00".equals(orgCode.substring(orgCode.length()-2,orgCode.length()))){
  114. //统计站
  115. orgCodeTemp=orgCode.substring(0,orgCode.length()-2)+"00";
  116. compute(tjOrgMap, orgCodeTemp);
  117. }else{
  118. //统计社区
  119. orgCodeTemp=orgCode;
  120. compute(tjOrgMap, orgCode);
  121. }
  122. Hospital hospital=hospitalsMap.get(orgCodeTemp);
  123. if(hospital == null) continue;
  124. String townCode = hospital.getTown();
  125. //统计区
  126. compute(tjTownMap, townCode);
  127. //统计市
  128. cityCount++;
  129. }
  130. //保存统计的结果
  131. //保存全科医生的随访统计
  132. for (Map.Entry<String, Doctor> entry : doctorsMap.entrySet()) {
  133. Doctor doctor = doctorsMap.get(entry.getKey());//得到健康管理师
  134. orgCodeTemp=getOrg(doctor.getHospital());
  135. Hospital hospital = hospitalsMap.get(orgCodeTemp);//得到全科医生的机构
  136. if(hospital == null) continue;
  137. String city=Constant.city;
  138. String cityName=Constant.cityName;
  139. String town=hospital.getTown();
  140. String townName=hospital.getTownName();
  141. String org=hospital.getCode();
  142. if(!"00".equals(hospital.getCode().substring(hospital.getCode().length()-2,hospital.getCode().length()))){
  143. //统计站
  144. org=hospital.getCode().substring(0,hospital.getCode().length()-2)+"00";
  145. }
  146. String orgName=hospital.getName();
  147. String doctorCode=doctor.getCode();
  148. String doctorName=doctor.getName();
  149. String doctorJob=doctor.getJob();
  150. String level="1";
  151. String key=doctor.getCode();
  152. save(tjQkdoctorMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  153. }
  154. //保存机构的随访统计
  155. for (Map.Entry<String, Hospital> entry : hospitalsMap.entrySet()) {
  156. Hospital hospital = hospitalsMap.get(entry.getKey());
  157. if(hospital == null) continue;
  158. String city=Constant.city;
  159. String cityName=Constant.cityName;
  160. String town=hospital.getTown();
  161. String townName=hospital.getTownName();
  162. String org=hospital.getCode();
  163. String orgName=hospital.getName();
  164. String doctorCode="";
  165. String doctorName="";
  166. String doctorJob="";
  167. String level="2";
  168. String key=hospital.getCode();
  169. save(tjOrgMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  170. }
  171. //保存区级的随访统计
  172. for (Map.Entry<String, Town> entry : townsMap.entrySet()) {
  173. Town townObj = townsMap.get(entry.getKey());
  174. String city=Constant.city;
  175. String cityName=Constant.cityName;
  176. String town=townObj.getCode();
  177. String townName=townObj.getName();
  178. String org="";
  179. String orgName="";
  180. String doctorCode="";
  181. String doctorName="";
  182. String doctorJob="";
  183. String level="3";
  184. String key=townObj.getCode();
  185. save(tjTownMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  186. }
  187. //保存市级的统计
  188. {
  189. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  190. wlyyQuotaResult.setDel("1");
  191. wlyyQuotaResult.setCity(Constant.city);
  192. wlyyQuotaResult.setCityName(Constant.cityName);
  193. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  194. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  195. wlyyQuotaResult.setQuotaDate(getYesterday());
  196. wlyyQuotaResult.setCreateTime(new Date());
  197. wlyyQuotaResult.setLevel1Type("4");
  198. //判断全科医生是否有随访量
  199. if (cityCount > 0) {
  200. wlyyQuotaResult.setResult(cityCount + "");
  201. } else {
  202. wlyyQuotaResult.setResult("0");
  203. }
  204. wlyyQuotaResultDao.save(wlyyQuotaResult);
  205. }
  206. quartzJobLog.setJobEndTime(new Date());
  207. quartzJobLog.setJobContent("统计"+getYesterday()+" 的随访数据完成 ,得到隨訪数目:"+plans.size());
  208. quartzJobLog.setJobType("1");
  209. quartzJobLogDao.save(quartzJobLog);
  210. }
  211. /**
  212. * 统计方案
  213. * @param tjQkdoctorMap
  214. * @param doctorCode
  215. */
  216. private void compute(Map<String, Long> tjQkdoctorMap, String doctorCode) {
  217. if (tjQkdoctorMap.containsKey(doctorCode)) {
  218. tjQkdoctorMap.put(doctorCode, tjQkdoctorMap.get(doctorCode) + 1);
  219. } else {
  220. tjQkdoctorMap.put(doctorCode, 1L);
  221. }
  222. }
  223. public String getYesterday() {
  224. return yesterday;
  225. }
  226. public List<Map<String,Object>> getFvYesterday() {
  227. String sql=" select * from fv_plan a where unix_timestamp(a.update_time)>=unix_timestamp('"+yesterday+"') and unix_timestamp(a.update_time)< unix_timestamp('"+now+"') ";
  228. return jdbcTemplate.queryForList(sql);
  229. }
  230. private void save(Map<String, Long> tjTownMap, String city, String cityName, String town, String townName, String org, String orgName, String doctorCode, String doctorName, String doctorJob, String level, String key) {
  231. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  232. wlyyQuotaResult.setDel("1");
  233. wlyyQuotaResult.setCity(city);
  234. wlyyQuotaResult.setCityName(cityName);
  235. wlyyQuotaResult.setTown(town);
  236. wlyyQuotaResult.setTownName(townName);
  237. wlyyQuotaResult.setOrgCode(org);
  238. wlyyQuotaResult.setOrgName(orgName);
  239. wlyyQuotaResult.setQkdoctorJobName(doctorJob);
  240. wlyyQuotaResult.setQkdoctorName(doctorName);
  241. wlyyQuotaResult.setQkdoctorCode(doctorCode);
  242. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  243. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  244. wlyyQuotaResult.setQuotaDate(getYesterday());
  245. wlyyQuotaResult.setCreateTime(new Date());
  246. wlyyQuotaResult.setLevel1Type(level);
  247. //判断全科医生是否有随访量
  248. if (tjTownMap.containsKey(key)) {
  249. wlyyQuotaResult.setResult(tjTownMap.get(key) + "");
  250. } else {
  251. wlyyQuotaResult.setResult("0");
  252. }
  253. wlyyQuotaResultDao.save(wlyyQuotaResult);
  254. }
  255. public Properties getSystemProperties() {
  256. if (systemProperties == null) {
  257. InputStream is = null;
  258. try {
  259. is = this.getClass().getResourceAsStream("/application.properties");
  260. systemProperties = new Properties();
  261. systemProperties.load(is);
  262. } catch (IOException e1) {
  263. e1.printStackTrace();
  264. } finally {
  265. if (is != null) {
  266. try {
  267. is.close();
  268. } catch (IOException e) {
  269. e.printStackTrace();
  270. }
  271. }
  272. }
  273. }
  274. return systemProperties;
  275. }
  276. private String getOrg(String org){
  277. if(!"00".equals(org.substring(org.length()-2,org.length()))){
  278. return org.substring(0,org.length()-2)+"00";
  279. }else{
  280. return org;
  281. }
  282. }
  283. }