FollowUpJob.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. for (Map<String,Object> plan : plans) {
  105. String doctorCode = (String) plan.get("doctor_uid");//得到随访中健康医生的code
  106. //统计团队
  107. compute(tjQkdoctorMap, doctorCode);
  108. //判断医生属于哪个机构
  109. Doctor doctor = doctorsMap.get(doctorCode);
  110. String orgCode = doctor.getHospital();
  111. //统计机构
  112. if(!"00".equals(orgCode.substring(orgCode.length()-2,orgCode.length()))){
  113. //统计站
  114. String orgCodeTemp=orgCode.substring(0,orgCode.length()-2)+"00";
  115. compute(tjOrgMap, orgCodeTemp);
  116. }else{
  117. //统计社区
  118. compute(tjOrgMap, orgCode);
  119. }
  120. String townCode = doctor.getTown();
  121. //统计区
  122. compute(tjTownMap, townCode);
  123. //统计市
  124. cityCount++;
  125. }
  126. //保存统计的结果
  127. //保存全科医生的随访统计
  128. for (Map.Entry<String, Doctor> entry : doctorsMap.entrySet()) {
  129. Doctor doctor = doctorsMap.get(entry.getKey());//得到健康管理师
  130. Hospital hospital = hospitalsMap.get(doctor.getHospital());//得到全科医生的机构
  131. String city=Constant.city;
  132. String cityName=Constant.cityName;
  133. String town=hospital.getTown();
  134. String townName=hospital.getTownName();
  135. String org=doctor.getHospital();
  136. String orgName=doctor.getHosptialName();
  137. String doctorCode=doctor.getCode();
  138. String doctorName=doctor.getName();
  139. String doctorJob=doctor.getJob();
  140. String level="1";
  141. String key=doctor.getCode();
  142. save(tjQkdoctorMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  143. }
  144. //保存机构的随访统计
  145. for (Map.Entry<String, Hospital> entry : hospitalsMap.entrySet()) {
  146. Hospital hospital = hospitalsMap.get(entry.getKey());
  147. String city=Constant.city;
  148. String cityName=Constant.cityName;
  149. String town=hospital.getTown();
  150. String townName=hospital.getTownName();
  151. String org=hospital.getCode();
  152. String orgName=hospital.getName();
  153. String doctorCode="";
  154. String doctorName="";
  155. String doctorJob="";
  156. String level="2";
  157. String key=hospital.getCode();
  158. save(tjOrgMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  159. }
  160. //保存区级的随访统计
  161. for (Map.Entry<String, Town> entry : townsMap.entrySet()) {
  162. Town townObj = townsMap.get(entry.getKey());
  163. String city=Constant.city;
  164. String cityName=Constant.cityName;
  165. String town=townObj.getCode();
  166. String townName=townObj.getName();
  167. String org="";
  168. String orgName="";
  169. String doctorCode="";
  170. String doctorName="";
  171. String doctorJob="";
  172. String level="3";
  173. String key=townObj.getCode();
  174. save(tjTownMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  175. }
  176. //保存市级的统计
  177. {
  178. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  179. wlyyQuotaResult.setDel("1");
  180. wlyyQuotaResult.setCity(Constant.city);
  181. wlyyQuotaResult.setCityName(Constant.cityName);
  182. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  183. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  184. wlyyQuotaResult.setQuotaDate(getYesterday());
  185. wlyyQuotaResult.setCreateTime(new Date());
  186. wlyyQuotaResult.setLevel1Type("4");
  187. //判断全科医生是否有随访量
  188. if (cityCount > 0) {
  189. wlyyQuotaResult.setResult(cityCount + "");
  190. } else {
  191. wlyyQuotaResult.setResult("0");
  192. }
  193. wlyyQuotaResultDao.save(wlyyQuotaResult);
  194. }
  195. quartzJobLog.setJobEndTime(new Date());
  196. quartzJobLog.setJobContent("统计"+getYesterday()+" 的随访数据完成 ,得到隨訪数目:"+plans.size());
  197. quartzJobLog.setJobType("1");
  198. quartzJobLogDao.save(quartzJobLog);
  199. }
  200. /**
  201. * 统计方案
  202. * @param tjQkdoctorMap
  203. * @param doctorCode
  204. */
  205. private void compute(Map<String, Long> tjQkdoctorMap, String doctorCode) {
  206. if (tjQkdoctorMap.containsKey(doctorCode)) {
  207. tjQkdoctorMap.put(doctorCode, tjQkdoctorMap.get(doctorCode) + 1);
  208. } else {
  209. tjQkdoctorMap.put(doctorCode, 1L);
  210. }
  211. }
  212. public String getYesterday() {
  213. return yesterday;
  214. }
  215. public List<Map<String,Object>> getFvYesterday() {
  216. 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+"') ";
  217. return jdbcTemplate.queryForList(sql);
  218. }
  219. 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) {
  220. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  221. wlyyQuotaResult.setDel("1");
  222. wlyyQuotaResult.setCity(city);
  223. wlyyQuotaResult.setCityName(cityName);
  224. wlyyQuotaResult.setTown(town);
  225. wlyyQuotaResult.setTownName(townName);
  226. wlyyQuotaResult.setOrgCode(org);
  227. wlyyQuotaResult.setOrgName(orgName);
  228. wlyyQuotaResult.setQkdoctorJobName(doctorJob);
  229. wlyyQuotaResult.setQkdoctorName(doctorName);
  230. wlyyQuotaResult.setQkdoctorCode(doctorCode);
  231. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  232. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  233. wlyyQuotaResult.setQuotaDate(getYesterday());
  234. wlyyQuotaResult.setCreateTime(new Date());
  235. wlyyQuotaResult.setLevel1Type(level);
  236. //判断全科医生是否有随访量
  237. if (tjTownMap.containsKey(key)) {
  238. wlyyQuotaResult.setResult(tjTownMap.get(key) + "");
  239. } else {
  240. wlyyQuotaResult.setResult("0");
  241. }
  242. wlyyQuotaResultDao.save(wlyyQuotaResult);
  243. }
  244. public Properties getSystemProperties() {
  245. if (systemProperties == null) {
  246. InputStream is = null;
  247. try {
  248. is = this.getClass().getResourceAsStream("/application.properties");
  249. systemProperties = new Properties();
  250. systemProperties.load(is);
  251. } catch (IOException e1) {
  252. e1.printStackTrace();
  253. } finally {
  254. if (is != null) {
  255. try {
  256. is.close();
  257. } catch (IOException e) {
  258. e.printStackTrace();
  259. }
  260. }
  261. }
  262. }
  263. return systemProperties;
  264. }
  265. }