SignJob.java 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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.doctor.profile.Doctor;
  6. import com.yihu.wlyy.entity.doctor.team.sign.DoctorPatientGroupInfo;
  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.doctor.DoctorAdminTeamDao;
  12. import com.yihu.wlyy.repository.doctor.DoctorDao;
  13. import com.yihu.wlyy.repository.doctor.DoctorPatientGroupInfoDao;
  14. import com.yihu.wlyy.repository.job.QuartzJobLogDao;
  15. import com.yihu.wlyy.repository.organization.HospitalDao;
  16. import com.yihu.wlyy.repository.patient.SignFamilyDao;
  17. import com.yihu.wlyy.repository.statistics.WlyyQuotaResultDao;
  18. import com.yihu.wlyy.util.IdCardUtil;
  19. import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
  20. import com.yihu.wlyy.web.quota.WlyyQuotaVO;
  21. import org.json.JSONArray;
  22. import org.json.JSONObject;
  23. import org.quartz.Job;
  24. import org.quartz.JobDataMap;
  25. import org.quartz.JobExecutionContext;
  26. import org.quartz.JobExecutionException;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.data.redis.core.StringRedisTemplate;
  29. import org.springframework.jdbc.core.JdbcTemplate;
  30. import org.springframework.stereotype.Component;
  31. import org.springframework.transaction.annotation.Transactional;
  32. import org.springframework.util.StringUtils;
  33. import org.springframework.web.context.support.SpringBeanAutowiringSupport;
  34. import java.text.SimpleDateFormat;
  35. import java.util.*;
  36. /**
  37. * 签约的指标执行类
  38. */
  39. @Component
  40. public class SignJob implements Job {
  41. private WlyyQuotaVO wlyyQuota;//指标对象
  42. private WlyyJobConfigVO wlyyJobConfig;//配置对象
  43. @Autowired
  44. private WlyyQuotaResultDao wlyyQuotaResultDao;//指标结果Dao
  45. @Autowired
  46. private QuartzJobLogDao quartzJobLogDao;//执行日志Dao
  47. @Autowired
  48. private SignFamilyDao signFamilyDao;
  49. @Autowired
  50. private DoctorDao doctorDao;
  51. @Autowired
  52. private HospitalDao hospitalDao;
  53. @Autowired
  54. private TownDao townDao;
  55. @Autowired
  56. private DoctorPatientGroupInfoDao doctorPatientGroupInfoDao;
  57. @Autowired
  58. private JdbcTemplate jdbcTemplate;
  59. @Autowired
  60. private StringRedisTemplate redisTemplate;
  61. @Autowired
  62. private DoctorAdminTeamDao doctorAdminTeamDao;
  63. String yesterday;
  64. String now;
  65. @Override
  66. public void execute(JobExecutionContext context)
  67. throws JobExecutionException {
  68. SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
  69. //初始化参数
  70. JobDataMap map = context.getJobDetail().getJobDataMap();
  71. wlyyQuota = (WlyyQuotaVO) map.get("quota");
  72. wlyyJobConfig = (WlyyJobConfigVO) map.get("jobConfig");
  73. now = StringUtils.isEmpty(map.get("now")) ? SignJob.getDayString(0) : map.get("now").toString();
  74. yesterday = StringUtils.isEmpty(map.get("yesterday")) ? SignJob.getDayString(-1) : map.get("yesterday").toString();
  75. String level1 = wlyyQuota.getLevel1();//得到一级维度
  76. String level2 = wlyyQuota.getLevel2();//得到二级维度
  77. //如果一级维度为空 不进行统计
  78. if (!StringUtils.isEmpty(level1)) {
  79. if (StringUtils.isEmpty(level2)) {
  80. //線刪除今天的数据
  81. //2级维度是空按照一级的机构唯独计算指标
  82. computequota();
  83. } else {
  84. switch (level2) {
  85. case "1": {
  86. //線刪除今天的数据
  87. //患者性别
  88. computequotaByPatientSex();
  89. break;
  90. }
  91. case "2": {
  92. //患者分组
  93. computequotaByPatientGroup();
  94. break;
  95. }
  96. case "3": {
  97. //患者年龄
  98. computequotaByPatientAge();
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * 机构维度下患者性别维度计算指标
  107. */
  108. @Transactional
  109. private void computequotaByPatientSex() {
  110. jdbcTemplate.execute("delete from wlyy_quota_result where quota_date='" + yesterday + "' and quato_code='" + 6 + "'");
  111. try {
  112. //新建任务日志对象
  113. QuartzJobLog QuartzJobLog = new QuartzJobLog();
  114. QuartzJobLog.setJobStartTime(new Date());
  115. QuartzJobLog.setJobId(wlyyJobConfig.getId());
  116. QuartzJobLog.setJobName(wlyyJobConfig.getJobName());
  117. //查找出系统全部的机构
  118. List<Hospital> hospitals = hospitalDao.findHospitalzxFWZ();
  119. Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
  120. for (Hospital hospital : hospitals) {
  121. hospitalsMap.put(hospital.getCode(), hospital);
  122. }
  123. //查找出厦门市全部的区
  124. List<Town> towns = townDao.findByCityCode(Constant.city);
  125. Map<String, Town> townsMap = new HashMap<String, Town>();
  126. for (Town town : towns) {
  127. townsMap.put(town.getCode(), town);
  128. }
  129. //找出今天的签约信息
  130. List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(yesterday, now);
  131. //数组里面第一个是女 第二个是男 第三个是未知
  132. Map<String, Map<String, Long>> citySexMap = new HashMap<String, Map<String, Long>>();//key是市行政代码 目前只有厦门市
  133. citySexMap.put(Constant.city, new HashMap<String, Long>());
  134. Map<String, Map<String, Long>> townSexMap = new HashMap<String, Map<String, Long>>();//key是区行政代码
  135. Map<String, Map<String, Long>> orgSexMap = new HashMap<String, Map<String, Long>>();//key是机构代码
  136. //统计有签约的
  137. Long cityCount = 0L; //统计到市的数目
  138. Long townCount = 0L; //统计到所有区的数目
  139. Long orgCount = 0L;//统计到所有机构的数目
  140. Long qkCount = 0L;//统计到所有全科医生的数目
  141. boolean isAll=true;//是否统计失败
  142. StringBuffer errorContent=new StringBuffer();
  143. for (SignFamily signFamily : signFamilys) {
  144. String idcard = signFamily.getIdcard();//得到签约中患者的身份证号
  145. String orgCodeTemp=getOrg(signFamily.getHospital());
  146. Hospital hospital = hospitalsMap.get(orgCodeTemp);//得到患者签约的机构
  147. if(hospital == null) {
  148. errorContent.append("签约code:"+signFamily.getCode()+",签约的全科医生(doctor字段)的所属机构(wlyy_doctor表中的hospital字段)为空或者所属的机构数据有问题");
  149. isAll=false;
  150. continue;
  151. }
  152. String town = hospital.getTown();//得到患者签约的机构的区号
  153. String hospitalCode = hospital.getCode();//得到患者签约的机构号
  154. String sex = IdCardUtil.getSexForIdcard(idcard);//根据身份证得到性别
  155. //统计市 目前只统计厦门市 所以先写死 后期再改造
  156. compute_level2_1(citySexMap,Constant.city, sex);
  157. cityCount++;
  158. //统计区
  159. compute_level2_1(townSexMap,town, sex);
  160. townCount++;
  161. //统计机构
  162. //统计站
  163. String orgCode=hospital.getCode();
  164. if(!"00".equals(orgCode.substring(orgCode.length()-2,orgCode.length()))){
  165. //统计站
  166. orgCodeTemp=orgCode.substring(0,orgCode.length()-2)+"00";
  167. compute_level2_1(orgSexMap,orgCodeTemp, sex);
  168. } else {
  169. //统计社区
  170. compute_level2_1(orgSexMap,hospitalCode, sex);
  171. }
  172. orgCount++;
  173. }
  174. //保存统计数据
  175. //保存市统计数据
  176. for (Map.Entry<String, Map<String, Long>> entry : citySexMap.entrySet()) {
  177. for(int i=1;i<4;i++){
  178. String key=i+"";
  179. String city=Constant.city;
  180. String cityName=Constant.cityName;
  181. String town="";
  182. String townName="";
  183. String org="";
  184. String orgName="";
  185. String level="4";
  186. save_level2_1(entry.getValue(), key, city, cityName, town, townName, org, orgName, level);
  187. }
  188. }
  189. //保存区的统计数据
  190. for (Map.Entry<String, Town> townEntry : townsMap.entrySet()) {
  191. //判断该区是否有统计数据
  192. Map<String, Long> oneTownSexMap = townSexMap.get(townEntry.getKey());//得到当个区的统计数据
  193. Town townObj = townEntry.getValue();//得到区级信息
  194. for(int i=1;i<4;i++){
  195. String key=i+"";
  196. String city=Constant.city;
  197. String cityName=Constant.cityName;
  198. String town=townObj.getCode();
  199. String townName=townObj.getName();
  200. String org="";
  201. String orgName="";
  202. String level="3";
  203. save_level2_1(oneTownSexMap, key, city, cityName, town, townName, org, orgName, level);
  204. }
  205. }
  206. //统计机构
  207. for (Map.Entry<String, Hospital> hospitalEntry : hospitalsMap.entrySet()) {
  208. //判断该机构是否有统计数据
  209. Map<String, Long> oneorgSexMap = orgSexMap.get(hospitalEntry.getKey());//得到当个机构的统计数据
  210. Hospital hospital = hospitalEntry.getValue();//得到区级信息
  211. for(int i=1;i<4;i++){
  212. String key=i+"";
  213. String city=Constant.city;
  214. String cityName=Constant.cityName;
  215. String town=hospital.getTown();
  216. String townName=hospital.getTownName();
  217. String org=hospital.getCode();
  218. String orgName=hospital.getName();
  219. String level="2";
  220. save_level2_1(oneorgSexMap, key, city, cityName, town, townName, org, orgName, level);
  221. }
  222. }
  223. QuartzJobLog.setJobEndTime(new Date());
  224. QuartzJobLog.setJobContent(saveContent(signFamilys,qkCount,orgCount,townCount,cityCount,isAll,errorContent));
  225. QuartzJobLog.setJobType(isAll?"1":"0");
  226. quartzJobLogDao.save(QuartzJobLog);
  227. } catch (Exception e) {
  228. e.printStackTrace();
  229. }
  230. }
  231. /**
  232. * 机构维度下患者性别维度计算指标 公用保存方法
  233. * @param oneorgSexMap
  234. * @param key
  235. * @param city
  236. * @param cityName
  237. * @param town
  238. * @param townName
  239. * @param org
  240. * @param orgName
  241. * @param level
  242. */
  243. private void save_level2_1(Map<String, Long> oneorgSexMap, String key, String city, String cityName, String town, String townName, String org, String orgName, String level) {
  244. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  245. wlyyQuotaResult.setDel("1");
  246. wlyyQuotaResult.setCity(city);
  247. wlyyQuotaResult.setCityName(cityName);
  248. wlyyQuotaResult.setTown(town);
  249. wlyyQuotaResult.setTownName(townName);
  250. wlyyQuotaResult.setOrgCode(org);
  251. wlyyQuotaResult.setOrgName(orgName);
  252. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  253. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  254. wlyyQuotaResult.setQuotaDate(getYesterday());
  255. wlyyQuotaResult.setCreateTime(new Date());
  256. wlyyQuotaResult.setLevel1Type(level);//机构级
  257. wlyyQuotaResult.setLevel2Type(key);
  258. wlyyQuotaResult.setLevel2TypeName(Constant.getLevelSexName(key));
  259. if (oneorgSexMap != null && oneorgSexMap.containsKey(key)) {
  260. wlyyQuotaResult.setResult(oneorgSexMap.get(key) + "");
  261. } else {
  262. wlyyQuotaResult.setResult("0");
  263. }
  264. wlyyQuotaResultDao.save(wlyyQuotaResult);
  265. }
  266. private void compute_level2_1(Map<String, Map<String, Long>> citySexMap,String key, String sex) {
  267. if (citySexMap.containsKey(key)) {
  268. Map<String, Long> sexMap = citySexMap.get(key);
  269. sexMap.put(sex, (sexMap.get(sex) == null ? 0l : sexMap.get(sex)) + 1);
  270. citySexMap.put(key, sexMap);
  271. } else {
  272. Map<String, Long> sexMap = new HashMap<String, Long>();
  273. sexMap.put(sex, 1L);
  274. citySexMap.put(key, sexMap);
  275. }
  276. }
  277. /**
  278. * 机构维度患者分组维度计算指标
  279. */
  280. @Transactional
  281. private void computequotaByPatientGroup() {
  282. jdbcTemplate.execute("delete from wlyy_quota_result where quota_date='" + yesterday + "' and quato_code='" + 7 + "'");
  283. try {
  284. //数组里面第一个是健康人群 第二个是慢病人群 第三个是65岁以上人群
  285. Map<String, Map<String, Long>> cityGroupMap = new HashMap<String, Map<String, Long>>();//key是市行政代码 目前只有厦门市
  286. cityGroupMap.put(Constant.city, new HashMap<String, Long>());
  287. Map<String, Map<String, Long>> townGroupMap = new HashMap<String, Map<String, Long>>();//key是区行政代码
  288. Map<String, Map<String, Long>> orgGroupMap = new HashMap<String, Map<String, Long>>();//key是机构代码
  289. //新建任务日志对象
  290. QuartzJobLog QuartzJobLog = new QuartzJobLog();
  291. QuartzJobLog.setJobStartTime(new Date());
  292. QuartzJobLog.setJobId(wlyyJobConfig.getId());
  293. QuartzJobLog.setJobName(wlyyJobConfig.getJobName());
  294. //查找出系统全部的机构
  295. List<Hospital> hospitals = hospitalDao.findHospital2();
  296. Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
  297. for (Hospital hospital : hospitals) {
  298. hospitalsMap.put(hospital.getCode(), hospital);
  299. Map<String, Long> one = new HashMap<String, Long>();
  300. one.put("1", 0L);
  301. one.put("2", 0L);
  302. one.put("3", 0L);
  303. one.put("4", 0L);
  304. one.put("5", 0L);
  305. one.put("100", 0L);
  306. orgGroupMap.put(hospital.getCode(), one);
  307. }
  308. //查找出厦门市全部的区
  309. List<Town> towns = townDao.findByCityCode(Constant.city);
  310. Map<String, Town> townsMap = new HashMap<String, Town>();
  311. for (Town town : towns) {
  312. townsMap.put(town.getCode(), town);
  313. Map<String, Long> one = new HashMap<String, Long>();
  314. one.put("1", 0L);
  315. one.put("2", 0L);
  316. one.put("3", 0L);
  317. one.put("4", 0L);
  318. one.put("5", 0L);
  319. one.put("100", 0L);
  320. townGroupMap.put(town.getCode(), one);
  321. }
  322. //找出今天的签约信息
  323. List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(yesterday, now);
  324. Long cityCount = 0L; //统计到市的数目
  325. Long townCount = 0L; //统计到所有区的数目
  326. Long orgCount = 0L;//统计到所有机构的数目
  327. Long qkCount = 0L;//统计到所有全科医生的数目
  328. boolean isAll=true;//是否统计失败
  329. StringBuffer errorContent=new StringBuffer();
  330. //统计有签约的
  331. for (SignFamily signFamily : signFamilys) {
  332. String patient = signFamily.getPatient();
  333. //设置查看病人所在分组
  334. List<DoctorPatientGroupInfo> dctorPatientGroupInfo = doctorPatientGroupInfoDao.findByMorenPatient(patient);
  335. String group ="";//得到该签约病人的所在分组 1 普通 2 慢病 3是 65岁以上人群 4是 高血压 5是糖尿病 100是高血压和糖尿病
  336. if (dctorPatientGroupInfo != null && dctorPatientGroupInfo.size() > 0) {
  337. group=dctorPatientGroupInfo.get(0).getGroup();
  338. } else {
  339. group="1";
  340. }
  341. Hospital hospital = hospitalsMap.get(getOrg(signFamily.getHospital()));//得到患者签约的机构
  342. if(hospital == null) {
  343. errorContent.append("签约code:"+signFamily.getCode()+",签约的全科医生(doctor字段)的所属机构(wlyy_doctor表中的hospital字段)为空或者所属的机构数据有问题");
  344. isAll=false;
  345. continue;
  346. }
  347. String town = hospital.getTown();
  348. //统计市 现在只有厦门市 默认先写死 后面再改
  349. compute_level2_2(cityGroupMap, group,Constant.city);
  350. cityCount++;
  351. //统计区
  352. compute_level2_2(townGroupMap, group,town);
  353. townCount++;
  354. //统计站
  355. String orgCode=hospital.getCode();
  356. if(!"00".equals(orgCode.substring(orgCode.length()-2,orgCode.length()))){
  357. //统计站
  358. String orgCodeTemp=orgCode.substring(0,orgCode.length()-2)+"00";
  359. //统计机构
  360. compute_level2_2(orgGroupMap, group,orgCodeTemp);
  361. } else {
  362. //统计机构
  363. compute_level2_2(orgGroupMap, group,hospital.getCode());
  364. }
  365. orgCount++;
  366. boolean hasGXY = false;//有高血压
  367. boolean hasTNB = false;//有糖尿病
  368. if ("2".equals(group)) {
  369. //如果是慢病的 统计高血压的 糖尿病 1高血压,2糖尿病
  370. String jsonString = redisTemplate.opsForValue().get("disease:" + patient);
  371. if (StringUtils.isEmpty(jsonString)) {
  372. continue;
  373. }
  374. //排除数据 只留下高血压和糖尿病
  375. List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
  376. JSONArray redisValues = new JSONArray(jsonString);
  377. for (Object obj : redisValues) {
  378. JSONObject redisValue = (JSONObject)(obj);
  379. if(!redisValue.has("signType")){
  380. continue;
  381. }
  382. //排除掉三师签约
  383. if ("1".equals(redisValue.get("signType").toString())) {
  384. continue;
  385. }
  386. String disease = redisValue.getString("disease");
  387. if ("1".equals(disease)) {
  388. jsonObjects.add(redisValue);
  389. hasGXY = true;//设置有高血压
  390. }
  391. if ("2".equals(disease)) {
  392. jsonObjects.add(redisValue);
  393. hasTNB = true;//设置有糖尿病
  394. }
  395. }
  396. if(jsonObjects.size()==0){
  397. continue;//如果没有高血压和糖尿病 不统计
  398. }
  399. if (hasGXY && hasTNB) {
  400. group = "100";//有高血压又有糖尿病
  401. } else if(hasGXY){
  402. group = 4 + "";//4高血压,5糖尿病
  403. }else if(hasTNB){
  404. group = 5 + "";//4高血压,5糖尿病
  405. }
  406. //统计市 现在只有厦门市 默认先写死 后面再改
  407. compute_level2_2(cityGroupMap, group,Constant.city);
  408. //统计区
  409. compute_level2_2(townGroupMap, group,town);
  410. //统计站
  411. orgCode=hospital.getCode();
  412. if(!"00".equals(orgCode.substring(orgCode.length()-2,orgCode.length()))){
  413. //统计站
  414. String orgCodeTemp=orgCode.substring(0,orgCode.length()-2)+"00";
  415. //统计机构
  416. compute_level2_2(orgGroupMap, group,orgCodeTemp);
  417. } else {
  418. //统计机构
  419. compute_level2_2(orgGroupMap, group,hospital.getCode());
  420. }
  421. }
  422. }
  423. //保存统计数据
  424. //保存市的统计数据
  425. for (Map.Entry<String, Map<String, Long>> entry : cityGroupMap.entrySet()) {
  426. //保存健康人群
  427. Map<String, Long> oneMap=entry.getValue();
  428. for(Map.Entry<String, Long> entryCh: oneMap.entrySet()){
  429. String city=entry.getKey();
  430. String cityName=Constant.cityName;
  431. String town="";
  432. String townName="";
  433. String org="";
  434. String orgName="";
  435. String level="4";
  436. String key=entryCh.getKey();
  437. save_level2_2(oneMap, city, cityName, town, townName, org, orgName, level, key);
  438. }
  439. }
  440. //保存区级
  441. //保存区的统计数据
  442. for (Map.Entry<String, Town> townEntry : townsMap.entrySet()) {
  443. //判断该区是否有统计数据
  444. Map<String, Long> oneTownGroupMap = townGroupMap.get(townEntry.getKey());//得到当个区的统计数据
  445. Town townObj = townEntry.getValue();//得到区级信息
  446. for(Map.Entry<String, Long> entryCh: oneTownGroupMap.entrySet()){
  447. String city=entryCh.getKey();
  448. String cityName=Constant.cityName;
  449. String town=townObj.getCode();
  450. String townName=townObj.getName();
  451. String org="";
  452. String orgName="";
  453. String level="3";
  454. String key=entryCh.getKey();
  455. save_level2_2(oneTownGroupMap, city, cityName, town, townName, org, orgName, level, key);
  456. }
  457. }
  458. //保存机构
  459. for (Map.Entry<String, Hospital> hospitalEntry : hospitalsMap.entrySet()) {
  460. //判断该机构是否有统计数据
  461. Map<String, Long> oneOrgGroupMap = orgGroupMap.get(hospitalEntry.getKey());//得到当个机构的统计数据
  462. Hospital hospital = hospitalEntry.getValue();//得到区级信息
  463. for(Map.Entry<String, Long> entryCh: oneOrgGroupMap.entrySet()){
  464. String city=entryCh.getKey();
  465. String cityName=Constant.cityName;
  466. String town=hospital.getTown();
  467. String townName=hospital.getTownName();
  468. String org=hospital.getCode();
  469. String orgName=hospital.getName();
  470. String level="2";
  471. String key=entryCh.getKey();
  472. save_level2_2(oneOrgGroupMap, city, cityName, town, townName, org, orgName, level, key);
  473. }
  474. }
  475. QuartzJobLog.setJobEndTime(new Date());
  476. QuartzJobLog.setJobContent(saveContent(signFamilys,qkCount,orgCount,townCount,cityCount,isAll,errorContent));
  477. QuartzJobLog.setJobType(isAll?"1":"0");
  478. quartzJobLogDao.save(QuartzJobLog);
  479. } catch (Exception e) {
  480. e.printStackTrace();
  481. }
  482. }
  483. private void save_level2_2(Map<String, Long> oneMap, String city, String cityName, String town, String townName, String org, String orgName, String level, String key) {
  484. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  485. wlyyQuotaResult.setDel("1");
  486. wlyyQuotaResult.setCity(city);
  487. wlyyQuotaResult.setCityName(cityName);
  488. wlyyQuotaResult.setTown(town);
  489. wlyyQuotaResult.setTownName(townName);
  490. wlyyQuotaResult.setOrgCode(org);
  491. wlyyQuotaResult.setOrgName(orgName);
  492. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  493. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  494. wlyyQuotaResult.setQuotaDate(getYesterday());
  495. wlyyQuotaResult.setCreateTime(new Date());
  496. wlyyQuotaResult.setLevel1Type(level);//市级
  497. wlyyQuotaResult.setLevel2Type(key);
  498. wlyyQuotaResult.setLevel2TypeName(Constant.getLevelGroupName(key));
  499. if (oneMap != null && oneMap.containsKey(key)) {
  500. wlyyQuotaResult.setResult(oneMap.get(key) + "");
  501. } else {
  502. wlyyQuotaResult.setResult("0");
  503. }
  504. wlyyQuotaResultDao.save(wlyyQuotaResult);
  505. }
  506. private void compute_level2_2(Map<String, Map<String, Long>> cityGroupMap, String group,String key) {
  507. if (cityGroupMap.containsKey(key)) {
  508. Map<String, Long> groupMapTemp = cityGroupMap.get(key);
  509. groupMapTemp.put(group, (groupMapTemp.get(group) == null ? 0l : groupMapTemp.get(group)) + 1);
  510. cityGroupMap.put(key, groupMapTemp);
  511. } else {
  512. Map<String, Long> groupMapTemp = new HashMap<String, Long>();
  513. groupMapTemp.put(group, 1L);
  514. cityGroupMap.put(key, groupMapTemp);
  515. }
  516. }
  517. /**
  518. * 机构维度患者年龄维度计算指标
  519. */
  520. @Transactional
  521. private void computequotaByPatientAge() {
  522. try {
  523. jdbcTemplate.execute("delete from wlyy_quota_result where quota_date='" + yesterday + "' and quato_code='" + 8 + "'");
  524. //新建任务日志对象
  525. QuartzJobLog QuartzJobLog = new QuartzJobLog();
  526. QuartzJobLog.setJobStartTime(new Date());
  527. QuartzJobLog.setJobId(wlyyJobConfig.getId());
  528. QuartzJobLog.setJobName(wlyyJobConfig.getJobName());
  529. //查找出系统全部的机构
  530. List<Hospital> hospitals = hospitalDao.findHospital2();
  531. Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
  532. for (Hospital hospital : hospitals) {
  533. hospitalsMap.put(hospital.getCode(), hospital);
  534. }
  535. //查找出厦门市全部的区
  536. List<Town> towns = townDao.findByCityCode(Constant.city);
  537. Map<String, Town> townsMap = new HashMap<String, Town>();
  538. for (Town town : towns) {
  539. townsMap.put(town.getCode(), town);
  540. }
  541. //找出今天的签约信息
  542. List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(yesterday, now);
  543. //数组里面第一个是健康人群 第二个是慢病人群 第三个是65岁以上人群
  544. Map<String, Map<String, Long>> cityAgeMap = new HashMap<String, Map<String, Long>>();//key是市行政代码 目前只有厦门市
  545. cityAgeMap.put(Constant.city, new HashMap<String, Long>());
  546. Map<String, Map<String, Long>> townAgeMap = new HashMap<String, Map<String, Long>>();//key是区行政代码
  547. Map<String, Map<String, Long>> orgAgeMap = new HashMap<String, Map<String, Long>>();//key是机构代码
  548. //统计有签约的
  549. Long cityCount = 0L; //统计到市的数目
  550. Long townCount = 0L; //统计到所有区的数目
  551. Long orgCount = 0L;//统计到所有机构的数目
  552. Long qkCount = 0L;//统计到所有全科医生的数目
  553. boolean isAll=true;//是否统计失败
  554. StringBuffer errorContent=new StringBuffer();
  555. //统计有签约的
  556. for (SignFamily signFamily : signFamilys) {
  557. String orgCodeTemp=getOrg(signFamily.getHospital());
  558. Hospital hospital = hospitalsMap.get(orgCodeTemp);//得到患者签约的机构
  559. if(hospital == null) {
  560. errorContent.append("签约code:"+signFamily.getCode()+",签约的全科医生(doctor字段)的所属机构(wlyy_doctor表中的hospital字段)为空或者所属的机构数据有问题");
  561. isAll=false;
  562. continue;
  563. }
  564. String town = hospital.getTown();
  565. int age = IdCardUtil.getAgeForIdcard(signFamily.getIdcard());//根据card解析年龄
  566. String ageCode = getAgeCode(age);//得到年龄的code
  567. //统计市
  568. compute_level2_3(cityAgeMap, ageCode,Constant.city);
  569. cityCount++;
  570. //统计区
  571. compute_level2_3(townAgeMap, ageCode,town);
  572. townCount++;
  573. //统计站
  574. String orgCode=hospital.getCode();
  575. if(!"00".equals(orgCode.substring(orgCode.length()-2,orgCode.length()))){
  576. //统计站
  577. orgCodeTemp=orgCode.substring(0,orgCode.length()-2)+"00";
  578. //统计机构
  579. compute_level2_3(orgAgeMap, ageCode,orgCodeTemp);
  580. } else {
  581. //统计机构
  582. compute_level2_3(orgAgeMap, ageCode,hospital.getCode());
  583. }
  584. orgCount++;
  585. }
  586. //保存统计数据
  587. // 保存市的统计数据
  588. for (Map.Entry<String, Map<String, Long>> entry : cityAgeMap.entrySet()) {
  589. Map<String, Long> oneAgeMap = entry.getValue();
  590. for(int i=1;i<7;i++){
  591. String city=Constant.city;
  592. String cityName=Constant.cityName;
  593. String town="";
  594. String townName="";
  595. String org="";
  596. String orgName="";
  597. String level="4";
  598. String key=i+"";
  599. save_level2_3(oneAgeMap, city, cityName, town, townName, org, orgName, level, key);
  600. }
  601. }
  602. //保存区级
  603. for (Map.Entry<String, Town> entry : townsMap.entrySet()) {
  604. //判断该区是否有统计数据
  605. Map<String, Long> oneAgeMap = townAgeMap.get(entry.getKey());//得到当个区的统计数据
  606. Town townObj = entry.getValue();//得到区级信息
  607. for(int i=1;i<7;i++){
  608. String city=Constant.city;
  609. String cityName=Constant.cityName;
  610. String town=townObj.getCode();
  611. String townName=townObj.getName();
  612. String org="";
  613. String orgName="";
  614. String level="3";
  615. String key=i+"";
  616. save_level2_3(oneAgeMap, city, cityName, town, townName, org, orgName, level, key);
  617. }
  618. }
  619. //保存机构
  620. for (Map.Entry<String, Hospital> hospitalEntry : hospitalsMap.entrySet()) {
  621. //判断该机构是否有统计数据
  622. Map<String, Long> oneAgeMap = orgAgeMap.get(hospitalEntry.getKey());//得到当个机构的统计数据
  623. Hospital hospital = hospitalEntry.getValue();//得到区级信息
  624. for(int i=1;i<7;i++){
  625. String city=Constant.city;
  626. String cityName=Constant.cityName;
  627. String town=hospital.getTown();
  628. String townName=hospital.getTownName();
  629. String org=hospital.getCode();
  630. String orgName=hospital.getName();
  631. String level="2";
  632. String key=i+"";
  633. save_level2_3(oneAgeMap, city, cityName, town, townName, org, orgName, level, key);
  634. }
  635. }
  636. QuartzJobLog.setJobEndTime(new Date());
  637. QuartzJobLog.setJobContent(saveContent(signFamilys,qkCount,orgCount,townCount,cityCount,isAll,errorContent));
  638. QuartzJobLog.setJobType(isAll?"1":"0");
  639. quartzJobLogDao.save(QuartzJobLog);
  640. } catch (Exception e) {
  641. e.printStackTrace();
  642. }
  643. }
  644. private void save_level2_3(Map<String, Long> oneAgeMap, String city, String cityName, String town, String townName, String org, String orgName, String level, String key) {
  645. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  646. wlyyQuotaResult.setDel("1");
  647. wlyyQuotaResult.setCity(city);
  648. wlyyQuotaResult.setCityName(cityName);
  649. wlyyQuotaResult.setTown(town);
  650. wlyyQuotaResult.setTownName(townName);
  651. wlyyQuotaResult.setOrgCode(org);
  652. wlyyQuotaResult.setOrgName(orgName);
  653. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  654. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  655. wlyyQuotaResult.setQuotaDate(getYesterday());
  656. wlyyQuotaResult.setCreateTime(new Date());
  657. wlyyQuotaResult.setLevel1Type(level);//市级
  658. wlyyQuotaResult.setLevel2Type(key);
  659. wlyyQuotaResult.setLevel2TypeName(Constant.getLevelAgeName(key));
  660. if (oneAgeMap != null && oneAgeMap.containsKey(key)) {
  661. wlyyQuotaResult.setResult(oneAgeMap.get(key) + "");
  662. } else {
  663. wlyyQuotaResult.setResult("0");
  664. }
  665. wlyyQuotaResultDao.save(wlyyQuotaResult);
  666. }
  667. private void compute_level2_3(Map<String, Map<String, Long>> cityAgeMap, String ageCode,String key) {
  668. if (cityAgeMap.containsKey(key)) {
  669. Map<String, Long> groupMapTemp = cityAgeMap.get(key);
  670. groupMapTemp.put(ageCode, (groupMapTemp.get(ageCode) == null ? 0l : groupMapTemp.get(ageCode)) + 1);
  671. cityAgeMap.put(key, groupMapTemp);
  672. } else {
  673. Map<String, Long> groupMapTemp = new HashMap<String, Long>();
  674. groupMapTemp.put(ageCode, 1L);
  675. cityAgeMap.put(key, groupMapTemp);
  676. }
  677. }
  678. /**
  679. * 机构维度计算指标
  680. */
  681. @Transactional
  682. private void computequota() {
  683. jdbcTemplate.execute("delete from wlyy_quota_result where quota_date='" + yesterday + "' and quato_code='" + 1 + "'");
  684. //新建任务日志对象
  685. QuartzJobLog QuartzJobLog = new QuartzJobLog();
  686. QuartzJobLog.setJobStartTime(new Date());
  687. QuartzJobLog.setJobId(wlyyJobConfig.getId());
  688. QuartzJobLog.setJobName(wlyyJobConfig.getJobName());
  689. //查找出系统全部的全科医生
  690. List<AdminTeam> adminTeams=doctorAdminTeamDao.findAllTeam();
  691. Map<String, AdminTeam> adminTeamMap = new HashMap<String, AdminTeam>();
  692. for (AdminTeam adminTeam : adminTeams) {
  693. adminTeamMap.put(adminTeam.getId()+"", adminTeam);
  694. }
  695. //查找出系统全部的机构
  696. List<Hospital> hospitals = hospitalDao.findHospitalzxFWZ();
  697. Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
  698. for (Hospital hospital : hospitals) {
  699. hospitalsMap.put(hospital.getCode(), hospital);
  700. }
  701. //查找出厦门市全部的区
  702. List<Town> towns = townDao.findByCityCode(Constant.city);
  703. Map<String, Town> townsMap = new HashMap<String, Town>();
  704. for (Town town : towns) {
  705. townsMap.put(town.getCode(), town);
  706. }
  707. //找出今天的签约信息 yesterday,now
  708. List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(yesterday, now);
  709. Map<String, Long> tjTownMap = new HashMap<String, Long>();//区级的统计map key 是区行政区划350200
  710. Map<String, Long> tjOrgMap = new HashMap<String, Long>();//机构的统计map key 是机构的code
  711. Map<String, Long> tjAdminTeamMap = new HashMap<String, Long>();//团队级的统计map 目前没有团队 先用全科医生统一 key doctorCode
  712. Long cityCount = 0L; //统计到市的数目
  713. Long townCount = 0L; //统计到所有区的数目
  714. Long orgCount = 0L;//统计到所有机构的数目
  715. Long qkCount = 0L;//统计到所有全科医生的数目
  716. boolean isAll=true;//是否统计失败
  717. StringBuffer errorContent=new StringBuffer();
  718. //统计有签约的
  719. for (SignFamily signFamily : signFamilys) {
  720. String orgCode = signFamily.getHospital();
  721. String orgCodeTemp="";
  722. if(!"00".equals(orgCode.substring(orgCode.length()-2,orgCode.length()))){
  723. //站
  724. orgCodeTemp=orgCode.substring(0,orgCode.length()-2)+"00";
  725. }else{
  726. //社区
  727. orgCodeTemp=orgCode;
  728. }
  729. Hospital hospital=hospitalsMap.get(orgCodeTemp);
  730. if(hospital == null) {
  731. errorContent.append("签约code:"+signFamily.getCode()+",签约表中(wlyy_sign_family)中的hospital数据异常");
  732. isAll=false;
  733. continue;
  734. }
  735. AdminTeam adminTeam=adminTeamMap.get(signFamily.getAdminTeamId()+"");
  736. if(adminTeam == null) {
  737. errorContent.append("签约code:"+signFamily.getCode()+",签约表中(wlyy_sign_family)中的Admin_Team_Id数据异常");
  738. isAll=false;
  739. continue;
  740. }
  741. //统计市 ---------------start--------------
  742. cityCount++;
  743. //统计区 ---------------start--------------
  744. String townCode = hospital.getTown();
  745. compute_level1(tjTownMap, townCode);
  746. townCount++;
  747. //统计机构---------------start--------------
  748. compute_level1(tjOrgMap, orgCodeTemp);
  749. orgCount++;
  750. //统计团队 ---------------start--------------
  751. String adminTeamId = signFamily.getAdminTeamId()+"";
  752. compute_level1(tjAdminTeamMap, adminTeamId);
  753. qkCount++;
  754. }
  755. //保存统计的结果
  756. for (Map.Entry<String, AdminTeam> entry : adminTeamMap.entrySet()) {
  757. AdminTeam adminTeam = adminTeamMap.get(entry.getKey());//得到全科医生
  758. String orgCodeTemp=getOrg(adminTeam.getOrgCode());
  759. Hospital hospital = hospitalsMap.get(orgCodeTemp);//得到全科医生的机构
  760. if(hospital == null) continue;
  761. String city=Constant.city;
  762. String cityName=Constant.cityName;
  763. String town=hospital.getTown();
  764. String townName=hospital.getTownName();
  765. String org=hospital.getCode();
  766. if(!"00".equals(hospital.getCode().substring(hospital.getCode().length()-2,hospital.getCode().length()))){
  767. //统计站
  768. org=hospital.getCode().substring(0,hospital.getCode().length()-2)+"00";
  769. }
  770. String orgName=hospital.getName();
  771. String doctorCode=adminTeam.getId()+"";
  772. String doctorName=adminTeam.getName();
  773. String doctorJob="";
  774. String level="1";
  775. String key=adminTeam.getId()+"";
  776. save_level1(tjAdminTeamMap, key, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level);
  777. }
  778. //保存机构的签约统计
  779. for (Map.Entry<String, Hospital> entry : hospitalsMap.entrySet()) {
  780. Hospital hospital = hospitalsMap.get(entry.getKey());//得到全科医生
  781. if(hospital == null) continue;
  782. String city=Constant.city;
  783. String cityName=Constant.cityName;
  784. String town=hospital.getTown();
  785. String townName=hospital.getTownName();
  786. String org=hospital.getCode();
  787. String orgName=hospital.getName();
  788. String doctorCode="";
  789. String doctorName="";
  790. String doctorJob="";
  791. String level="2";
  792. String key=hospital.getCode();
  793. save_level1(tjOrgMap, key, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level);
  794. }
  795. //保存区级的签约统计
  796. for (Map.Entry<String, Town> entry : townsMap.entrySet()) {
  797. Town townObj = townsMap.get(entry.getKey());//得到全科医生
  798. String city=Constant.city;
  799. String cityName=Constant.cityName;
  800. String town=townObj.getCode();
  801. String townName=townObj.getName();
  802. String org="";
  803. String orgName="";
  804. String doctorCode="";
  805. String doctorName="";
  806. String doctorJob="";
  807. String level="3";
  808. String key=townObj.getCode();
  809. save_level1(tjTownMap, key, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level);
  810. }
  811. //保存市级的统计
  812. {
  813. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  814. wlyyQuotaResult.setDel("1");
  815. wlyyQuotaResult.setCity(Constant.city);
  816. wlyyQuotaResult.setCityName(Constant.cityName);
  817. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  818. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  819. wlyyQuotaResult.setQuotaDate(getYesterday());
  820. wlyyQuotaResult.setCreateTime(new Date());
  821. wlyyQuotaResult.setLevel1Type("4");
  822. //判断全科医生是否有签约量
  823. if (cityCount > 0) {
  824. wlyyQuotaResult.setResult(cityCount + "");
  825. } else {
  826. wlyyQuotaResult.setResult("0");
  827. }
  828. wlyyQuotaResultDao.save(wlyyQuotaResult);
  829. }
  830. QuartzJobLog.setJobEndTime(new Date());
  831. QuartzJobLog.setJobContent(saveContent(signFamilys,qkCount,orgCount,townCount,cityCount,isAll,errorContent));
  832. QuartzJobLog.setJobType(isAll?"1":"0");
  833. quartzJobLogDao.save(QuartzJobLog);
  834. }
  835. /**
  836. * 统计签约的公用保存方案
  837. *
  838. */
  839. private void save_level1(Map<String, Long> countMap, String key, String city, String cityName, String town, String townName, String org, String orgName, String doctorCode, String doctorName, String doctorJob, String level) {
  840. WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
  841. wlyyQuotaResult.setDel("1");
  842. wlyyQuotaResult.setOrgCode(org);
  843. wlyyQuotaResult.setOrgName(orgName);
  844. wlyyQuotaResult.setCity(city);
  845. wlyyQuotaResult.setCityName(cityName);
  846. wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
  847. wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
  848. wlyyQuotaResult.setQuotaDate(getYesterday());
  849. wlyyQuotaResult.setCreateTime(new Date());
  850. wlyyQuotaResult.setQkdoctorJobName(doctorJob);
  851. wlyyQuotaResult.setQkdoctorName(doctorName);
  852. wlyyQuotaResult.setQkdoctorCode(doctorCode);
  853. wlyyQuotaResult.setTown(town);
  854. wlyyQuotaResult.setTownName(townName);
  855. wlyyQuotaResult.setLevel1Type(level);
  856. //判断全科医生是否有解约量
  857. if (countMap.containsKey(key)) {
  858. wlyyQuotaResult.setResult(countMap.get(key) + "");
  859. } else {
  860. wlyyQuotaResult.setResult("0");
  861. }
  862. wlyyQuotaResultDao.save(wlyyQuotaResult);
  863. }
  864. /**
  865. * 统计签约的公用统计方案
  866. * @param tjQkdoctorMap
  867. * @param doctorCode
  868. */
  869. private void compute_level1(Map<String, Long> tjQkdoctorMap, String doctorCode) {
  870. if (tjQkdoctorMap.containsKey(doctorCode)) {
  871. tjQkdoctorMap.put(doctorCode, tjQkdoctorMap.get(doctorCode) + 1);
  872. } else {
  873. tjQkdoctorMap.put(doctorCode, 1L);
  874. }
  875. }
  876. /*
  877. 得到昨天的日期字符串 yyyy-MM-dd
  878. */
  879. public String getYesterday() {
  880. return yesterday;
  881. }
  882. /**
  883. * 根据年龄得到对应的code
  884. *
  885. * @param age
  886. * @return
  887. */
  888. public String getAgeCode(int age) {
  889. if (age < 7) {
  890. return Constant.level_age_1;
  891. } else if (age >= 7 && age < 18) {
  892. return Constant.level_age_2;
  893. } else if (age >= 18 && age < 30) {
  894. return Constant.level_age_3;
  895. } else if (age >= 30 && age < 50) {
  896. return Constant.level_age_4;
  897. } else if (age >= 50 && age < 65) {
  898. return Constant.level_age_5;
  899. } else {
  900. return Constant.level_age_6;
  901. }
  902. }
  903. public static String getDayString(Integer size) {
  904. Date date = new Date();//取时间
  905. Calendar calendar = new GregorianCalendar();
  906. calendar.setTime(date);
  907. calendar.add(calendar.DATE, size);//把日期往后增加一天.整数往后推,负数往前移动
  908. date = calendar.getTime(); //这个时间就是日期往后推一天的结果
  909. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  910. String dateString = formatter.format(date);
  911. return dateString;
  912. }
  913. private String getOrg(String org){
  914. //if(!"00".equals(org.substring(org.length()-2,org.length()))){
  915. if(org.length() == 10 && !org.endsWith("00")){
  916. return org.substring(0,org.length()-2)+"00";
  917. }else{
  918. return org;
  919. }
  920. }
  921. private String saveContent(List<SignFamily> signFamilys, Long qkCount, Long orgCount, Long townCount, Long cityCount, boolean isAll, StringBuffer errorContent) {
  922. StringBuffer string=new StringBuffer("统计"+getYesterday()+" 的待签约数据完成 ,数据库查询到待签约数目:"+signFamilys.size());
  923. string.append(",统计到市的数据总数:"+cityCount);
  924. string.append(",统计到区的数据总数:"+townCount);
  925. string.append(",统计到机构的数据总数:"+orgCount);
  926. string.append(",统计到团队的数据总数:"+qkCount);
  927. string.append(",是否统计成功:"+isAll);
  928. if(!isAll){
  929. string.append(",失败原因:"+errorContent);
  930. }
  931. return string.toString();
  932. }
  933. }