CovidVaccinateAppointmentService.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package thyyxxk.wxservice_server.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import thyyxxk.wxservice_server.config.exception.ExceptionEnum;
  7. import thyyxxk.wxservice_server.constant.Constants;
  8. import thyyxxk.wxservice_server.dao.CovidVaccinateAppointmentDao;
  9. import thyyxxk.wxservice_server.entity.ResultVo;
  10. import thyyxxk.wxservice_server.entity.covidvaccinate.CovidVaccinate;
  11. import thyyxxk.wxservice_server.entity.covidvaccinate.ZdCovidVaccinate;
  12. import thyyxxk.wxservice_server.entity.wxapi.PushMessageParam;
  13. import thyyxxk.wxservice_server.utils.DateUtil;
  14. import thyyxxk.wxservice_server.utils.IdCardUtil;
  15. import thyyxxk.wxservice_server.utils.ResultVoUtil;
  16. import thyyxxk.wxservice_server.utils.StringUtil;
  17. import java.util.Date;
  18. import java.util.List;
  19. /**
  20. * @author dj
  21. */
  22. @Slf4j
  23. @Service
  24. public class CovidVaccinateAppointmentService {
  25. private final CovidVaccinateAppointmentDao dao;
  26. private final PushWxMessageService pushWxMessageService;
  27. @Autowired
  28. public CovidVaccinateAppointmentService(CovidVaccinateAppointmentDao dao, PushWxMessageService pushWxMessageService) {
  29. this.dao = dao;
  30. this.pushWxMessageService = pushWxMessageService;
  31. }
  32. public ResultVo<List<ZdCovidVaccinate>> selectVaccinates() {
  33. List<ZdCovidVaccinate> list = dao.selectVaccinates();
  34. if (null != list && list.size() > 0) {
  35. String[] nextSevenDate = DateUtil.getDatesInOneWeek();
  36. list.forEach(item -> {
  37. for (String date : nextSevenDate) {
  38. Integer max = dao.selectThresholdByDateAndCode(date, item.getVaccinateCode());
  39. max = null == max ? 0 : max;
  40. Integer count = dao.selectTotalCountByDateAndCode(date, item.getVaccinateCode());
  41. count = null == count ? 0 : count;
  42. int offset = max - count;
  43. item.setMinLeftNum(offset);
  44. if (offset > 0) {
  45. break;
  46. }
  47. }
  48. });
  49. }
  50. return ResultVoUtil.success(list);
  51. }
  52. public ResultVo<CovidVaccinate> getPatientInfoAndJobCategories(String patientId) {
  53. CovidVaccinate info = dao.selectPatientInfo(patientId);
  54. if (null == info) {
  55. info = new CovidVaccinate();
  56. } else {
  57. info.setPatientId(patientId);
  58. String phone = info.getPhone();
  59. String socialNo = info.getSocialNo();
  60. if (null != phone) {
  61. if (phone.length() != Constants.CommonLength.PHONE) {
  62. info.setPhone(null);
  63. }
  64. }
  65. if (null != socialNo) {
  66. if (socialNo.startsWith(Constants.Exception.ILLEGAL_ID_START) ||
  67. socialNo.length() != Constants.CommonLength.ID_CARD) {
  68. info.setSocialNo(null);
  69. }
  70. }
  71. }
  72. info.setJobCategories(dao.selectJobCategories());
  73. return ResultVoUtil.success(info);
  74. }
  75. public ResultVo<int[]> getNextSevenDaysSources(int code) {
  76. int[] result = new int[7];
  77. String[] nextSevenDate = DateUtil.getDatesInOneWeek();
  78. for (int i = 0; i < nextSevenDate.length; i++) {
  79. String date = nextSevenDate[i];
  80. Integer max = dao.selectThresholdByDateAndCode(date, code);
  81. max = null == max ? 0 : max;
  82. Integer count = dao.selectTotalCountByDateAndCode(date, code);
  83. count = null == count ? 0 : count;
  84. result[i] = max - count;
  85. }
  86. return ResultVoUtil.success(result);
  87. }
  88. public ResultVo<String> submitVaccinateAppointment(CovidVaccinate param) {
  89. param.setCreateDatetime(new Date());
  90. if (!IdCardUtil.isValidatedIdCard(param.getSocialNo())) {
  91. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您在我院绑定的身份证号不是有效的身份证号," +
  92. "请前往【个人中心 - 我的就诊人 - 就诊人信息】进行修改。");
  93. }
  94. int age = IdCardUtil.getAgeByIdCard(param.getSocialNo());
  95. if (age < 18 || age > 70) {
  96. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "可接种的年龄区间为【18岁 - 70岁】," +
  97. "您的年龄不满足此条件,敬请谅解。");
  98. }
  99. param.setAge(age);
  100. param.setSex(IdCardUtil.getGenderByIdCard(param.getSocialNo()).getCode());
  101. String dateFormatted = DateUtil.formatDatetime(param.getExecuteDate(), "yyyy-MM-dd");
  102. if (dao.selectValidAppointment(param.getSocialNo(), param.getExecuteDate()) > 0) {
  103. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您在【" + dateFormatted + "】已存在有效的预约,请勿重复预约。");
  104. }
  105. Integer threshold = dao.selectThresholdByDateAndCode2(param.getExecuteDate(), param.getVaccinateCode());
  106. if (null == threshold) {
  107. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "【" + dateFormatted + "】没有可预约的疫苗,请选择其他预约日期。");
  108. }
  109. Integer count = dao.selectTotalCountByDateAndCode2(param.getExecuteDate(), param.getVaccinateCode());
  110. count = null == count ? 0 : count;
  111. if (count >= threshold) {
  112. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "【" + dateFormatted + "】的预约名额已满,请选择其他预约日期。");
  113. }
  114. dao.insertNewAppointment(param);
  115. if (StringUtil.notBlank(param.getPatientId())) {
  116. dao.updatePhoneAndSocialNo(param.getPatientId(), param.getSocialNo(), param.getPhone());
  117. String msgContent = "{\"touser\":\"\",\"data\":" +
  118. "{\"keyword2\":{\"color\":\"#173177\",\"value\":\"" + dateFormatted + "\"}," +
  119. "\"keyword1\":{\"color\":\"#173177\",\"value\":\"" + param.getName() + "\"}," +
  120. "\"keyword3\":{\"color\":\"#173177\",\"value\":\"" + (++count) + "\"}," +
  121. "\"remark\":{\"color\":\"#FF0000\",\"value\":\"感谢您的使用,祝您健康!\"}," +
  122. "\"first\":{\"color\":\"#FF0000\",\"value\":\"您已成功预约新冠疫苗接种服务。" +
  123. "请于【" + dateFormatted + "】凭本人身份证在【泰和医院门诊大楼五楼体检中心】接种新冠疫苗。\"}}," +
  124. "\"template_id\":\"zVSzbYLmdqq_h1IcTPSwi6X4qFm0j9aTVeLZPxR03Bs\"," +
  125. "\"url\":\"\"}";
  126. PushMessageParam pojo = new PushMessageParam();
  127. pojo.setCardNo(param.getPatientId());
  128. pojo.setCardNoPatientId(true);
  129. pojo.setMsgContext(JSONObject.parseObject(msgContent));
  130. pushWxMessageService.pushMessage2(pojo);
  131. }
  132. log.info("预约新冠疫苗接种:{}", JSONObject.toJSONStringWithDateFormat(param, "yyyy-MM-dd HH:mm:ss"));
  133. return ResultVoUtil.success("您已成功预约新冠疫苗接种服务。请于【" + dateFormatted + "】" +
  134. "凭本人身份证在【泰和医院门诊大楼五楼体检中心】接种新冠疫苗。");
  135. }
  136. }