ConsultJob.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package com.yihu.wlyy.job;
  2. import com.yihu.wlyy.entity.doctor.team.admin.AdminTeam;
  3. import com.yihu.wlyy.entity.organization.Hospital;
  4. import com.yihu.wlyy.entity.address.Town;
  5. import com.yihu.wlyy.entity.consult.ConsultTeam;
  6. import com.yihu.wlyy.entity.doctor.profile.Doctor;
  7. import com.yihu.wlyy.entity.job.QuartzJobLog;
  8. import com.yihu.wlyy.entity.patient.SignFamily;
  9. import com.yihu.wlyy.entity.statistics.WlyyQuotaResult;
  10. import com.yihu.wlyy.repository.address.TownDao;
  11. import com.yihu.wlyy.repository.consult.ConsultTeamDao;
  12. import com.yihu.wlyy.repository.doctor.DoctorAdminTeamDao;
  13. import com.yihu.wlyy.repository.doctor.DoctorDao;
  14. import com.yihu.wlyy.repository.job.QuartzJobLogDao;
  15. import com.yihu.wlyy.repository.organization.HospitalDao;
  16. import com.yihu.wlyy.repository.statistics.WlyyQuotaResultDao;
  17. import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
  18. import com.yihu.wlyy.web.quota.WlyyQuotaVO;
  19. import org.quartz.Job;
  20. import org.quartz.JobDataMap;
  21. import org.quartz.JobExecutionContext;
  22. import org.quartz.JobExecutionException;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.jdbc.core.JdbcTemplate;
  25. import org.springframework.stereotype.Component;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import org.springframework.util.StringUtils;
  28. import org.springframework.web.context.support.SpringBeanAutowiringSupport;
  29. import java.util.*;
  30. /**
  31. * 咨询模块的指标执行类
  32. */
  33. @Component
  34. public class ConsultJob 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 DoctorDao doctorDao;
  43. @Autowired
  44. private HospitalDao hospitalDao;
  45. @Autowired
  46. private TownDao townDao;
  47. @Autowired
  48. private ConsultTeamDao consultTeamDao;
  49. @Autowired
  50. private JdbcTemplate jdbcTemplate;
  51. @Autowired
  52. private DoctorAdminTeamDao doctorAdminTeamDao;
  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"))?SignJob.getDayString(0):map.get("now").toString();
  64. yesterday= StringUtils.isEmpty(map.get("yesterday"))?SignJob.getDayString(-1):map.get("yesterday").toString(); //線刪除今天的数据
  65. //计算指标
  66. computequota();
  67. }
  68. @Transactional
  69. private void computequota() {
  70. jdbcTemplate.execute("delete from wlyy_quota_result where quota_date='"+yesterday+"' and quato_code='"+3+"'");
  71. //新建任务日志对象
  72. QuartzJobLog quartzJobLog = new QuartzJobLog();
  73. quartzJobLog.setJobStartTime(new Date());
  74. quartzJobLog.setJobId(wlyyJobConfig.getId());
  75. quartzJobLog.setJobName(wlyyJobConfig.getJobName());
  76. //找出全部的团队
  77. List<AdminTeam> adminTeams=doctorAdminTeamDao.findAllTeam();
  78. Map<String, AdminTeam> adminTeamMap = new HashMap<String, AdminTeam>();
  79. for (AdminTeam adminTeam : adminTeams) {
  80. adminTeamMap.put(adminTeam.getId()+"", adminTeam);
  81. }
  82. //查找出系统全部的机构
  83. List<Hospital> hospitals = hospitalDao.findHospital2();
  84. Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
  85. for (Hospital hospital : hospitals) {
  86. hospitalsMap.put(hospital.getCode(), hospital);
  87. }
  88. //查找出厦门市全部的区
  89. List<Town> towns = townDao.findByCityCode(Constant.city);
  90. Map<String, Town> townsMap = new HashMap<String, Town>();
  91. for (Town town : towns) {
  92. townsMap.put(town.getCode(), town);
  93. }
  94. //找出今天的咨询信息
  95. List<ConsultTeam> consultTeams = consultTeamDao.findByCzrqyYesterday(yesterday,now);
  96. Map<String, Long> tjTownMap = new HashMap<String, Long>();//区级的统计map key 是区行政区划350200
  97. Map<String, Long> tjOrgMap = new HashMap<String, Long>();//机构的统计map key 是机构的code
  98. Map<String, Long> tjAdminTeamMap = new HashMap<String, Long>();//团队级的统计map 目前没有团队 先用全科医生统一 key doctorCode
  99. Long cityCount = 0L; //统计到市的数目
  100. Long townCount = 0L; //统计到所有区的数目
  101. Long orgCount = 0L;//统计到所有机构的数目
  102. Long jkCount = 0L;//统计到所有健康医生的数目
  103. boolean isAll=true;//是否统计失败
  104. StringBuffer errorContent=new StringBuffer();
  105. //统计有咨询的
  106. for (ConsultTeam consultTeam : consultTeams) {
  107. String teamCode = consultTeam.getAdminTeamId()+"";//行政的团队的id
  108. AdminTeam adminTeam=adminTeamMap.get(teamCode);
  109. if(adminTeam == null) {
  110. errorContent.append("咨询Id:"+consultTeam.getId()+",咨询的团队为空或者不存在");
  111. isAll=false;
  112. continue;
  113. }
  114. String orgCode = adminTeam.getOrgCode();
  115. String orgCodeTemp="";
  116. //统计机构
  117. if(!"00".equals(orgCode.substring(orgCode.length()-2,orgCode.length()))){
  118. //统计站
  119. orgCodeTemp=orgCode.substring(0,orgCode.length()-2)+"00";
  120. }else{
  121. //统计社区
  122. orgCodeTemp=orgCode;
  123. }
  124. //统计区 ---------------start--------------
  125. Hospital hospital=hospitalsMap.get(orgCodeTemp);
  126. if(hospital == null) {
  127. errorContent.append("咨询Id:"+consultTeam.getId()+",咨询的健康医生(doctor字段)的所属机构(wlyy_doctor表中的hospital字段)为空或者所属的机构数据有问题");
  128. isAll=false;
  129. continue;
  130. }
  131. //统计市 ---------------start--------------
  132. cityCount++;
  133. //统计区 ---------------start--------------
  134. String townCode = hospital.getTown();
  135. compute(tjTownMap, townCode);
  136. townCount++;
  137. //统计机构 ---------------start--------------
  138. compute(tjOrgMap, orgCodeTemp);
  139. orgCount++;
  140. //统计团队 ---------------start--------------
  141. compute(tjAdminTeamMap, teamCode);
  142. jkCount++;
  143. }
  144. //保存统计的结果
  145. //保存全科医生的咨询统计
  146. for (Map.Entry<String, AdminTeam> entry : adminTeamMap.entrySet()) {
  147. AdminTeam adminTeam = adminTeamMap.get(entry.getKey());//得到团队
  148. String orgCodeTemp=getOrg(adminTeam.getOrgCode());
  149. Hospital hospital = hospitalsMap.get(orgCodeTemp);//得到全科医生的机构
  150. if(hospital == null) continue;
  151. String city=Constant.city;
  152. String cityName=Constant.cityName;
  153. String town=hospital.getTown();
  154. String townName=hospital.getTownName();
  155. String org=hospital.getCode();
  156. if(!"00".equals(hospital.getCode().substring(hospital.getCode().length()-2,hospital.getCode().length()))){
  157. //统计站
  158. org=hospital.getCode().substring(0,hospital.getCode().length()-2)+"00";
  159. }
  160. String orgName=hospital.getName();
  161. String doctorCode=adminTeam.getId()+"";
  162. String doctorName=adminTeam.getName();
  163. String doctorJob="";
  164. String level="1";
  165. String key=adminTeam.getId()+"";
  166. save(tjAdminTeamMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  167. }
  168. //保存机构的咨询统计
  169. for (Map.Entry<String, Hospital> entry : hospitalsMap.entrySet()) {
  170. Hospital hospital = hospitalsMap.get(entry.getKey());
  171. if(hospital == null) continue;
  172. String city=Constant.city;
  173. String cityName=Constant.cityName;
  174. String town=hospital.getTown();
  175. String townName=hospital.getTownName();
  176. String org=hospital.getCode();
  177. String orgName=hospital.getName();
  178. String doctorCode="";
  179. String doctorName="";
  180. String doctorJob="";
  181. String level="2";
  182. String key=hospital.getCode();
  183. save(tjOrgMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  184. }
  185. //保存区级的咨询统计
  186. for (Map.Entry<String, Town> entry : townsMap.entrySet()) {
  187. Town townObj = townsMap.get(entry.getKey());
  188. String city=Constant.city;
  189. String cityName=Constant.cityName;
  190. String town=townObj.getCode();
  191. String townName=townObj.getName();
  192. String org="";
  193. String orgName="";
  194. String doctorCode="";
  195. String doctorName="";
  196. String doctorJob="";
  197. String level="3";
  198. String key=townObj.getCode();
  199. save(tjTownMap, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level, key);
  200. }
  201. //保存市级的统计
  202. {
  203. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  204. wlyyQuotaResult.setDel("1");
  205. wlyyQuotaResult.setCity(Constant.city);
  206. wlyyQuotaResult.setCityName(Constant.cityName);
  207. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  208. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  209. wlyyQuotaResult.setQuotaDate(getYesterday());
  210. wlyyQuotaResult.setCreateTime(new Date());
  211. wlyyQuotaResult.setLevel1Type("4");
  212. //判断全科医生是否有咨询量
  213. if (cityCount > 0) {
  214. wlyyQuotaResult.setResult(cityCount + "");
  215. } else {
  216. wlyyQuotaResult.setResult("0");
  217. }
  218. wlyyQuotaResultDao.save(wlyyQuotaResult);
  219. }
  220. quartzJobLog.setJobEndTime(new Date());
  221. quartzJobLog.setJobContent(saveContent(consultTeams,jkCount,orgCount,townCount,cityCount,isAll,errorContent));
  222. quartzJobLog.setJobType(isAll?"1":"0");
  223. quartzJobLogDao.save(quartzJobLog);
  224. }
  225. /**
  226. * 保存方案
  227. * @param tjTownMap
  228. * @param city
  229. * @param cityName
  230. * @param town
  231. * @param townName
  232. * @param org
  233. * @param orgName
  234. * @param doctorCode
  235. * @param doctorName
  236. * @param doctorJob
  237. * @param level
  238. * @param key
  239. */
  240. 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) {
  241. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  242. wlyyQuotaResult.setDel("1");
  243. wlyyQuotaResult.setCity(city);
  244. wlyyQuotaResult.setCityName(cityName);
  245. wlyyQuotaResult.setTown(town);
  246. wlyyQuotaResult.setTownName(townName);
  247. wlyyQuotaResult.setOrgCode(org);
  248. wlyyQuotaResult.setOrgName(orgName);
  249. wlyyQuotaResult.setQkdoctorJobName(doctorJob);
  250. wlyyQuotaResult.setQkdoctorName(doctorName);
  251. wlyyQuotaResult.setQkdoctorCode(doctorCode);
  252. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  253. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  254. wlyyQuotaResult.setQuotaDate(getYesterday());
  255. wlyyQuotaResult.setCreateTime(new Date());
  256. wlyyQuotaResult.setLevel1Type(level);
  257. //判断全科医生是否有咨询量
  258. if (tjTownMap.containsKey(key)) {
  259. wlyyQuotaResult.setResult(tjTownMap.get(key) + "");
  260. } else {
  261. wlyyQuotaResult.setResult("0");
  262. }
  263. wlyyQuotaResultDao.save(wlyyQuotaResult);
  264. }
  265. /**
  266. * 统计方案
  267. * @param tjQkdoctorMap
  268. * @param doctorCode
  269. */
  270. private void compute(Map<String, Long> tjQkdoctorMap, String doctorCode) {
  271. if (tjQkdoctorMap.containsKey(doctorCode)) {
  272. tjQkdoctorMap.put(doctorCode, tjQkdoctorMap.get(doctorCode) + 1);
  273. } else {
  274. tjQkdoctorMap.put(doctorCode, 1L);
  275. }
  276. }
  277. private String getOrg(String org){
  278. if(!"00".equals(org.substring(org.length()-2,org.length()))){
  279. return org.substring(0,org.length()-2)+"00";
  280. }else{
  281. return org;
  282. }
  283. }
  284. private String saveContent(List<ConsultTeam> consultTeams, Long qkCount, Long orgCount, Long townCount, Long cityCount, boolean isAll, StringBuffer errorContent) {
  285. StringBuffer string=new StringBuffer("统计"+getYesterday()+" 的咨询数据完成 ,数据库查询到咨询数目:"+consultTeams.size());
  286. string.append(",统计到市的数据总数:"+cityCount);
  287. string.append(",统计到区的数据总数:"+townCount);
  288. string.append(",统计到机构的数据总数:"+orgCount);
  289. string.append(",统计到团队的数据总数:"+qkCount);
  290. string.append(",是否统计成功:"+isAll);
  291. if(!isAll){
  292. string.append(",失败原因:"+errorContent);
  293. }
  294. return string.toString();
  295. }
  296. public String getYesterday() {
  297. return yesterday;
  298. }
  299. }