package thyyxxk.wxservice_server.service; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import thyyxxk.wxservice_server.config.exception.ExceptionEnum; import thyyxxk.wxservice_server.constant.Constants; import thyyxxk.wxservice_server.dao.CovidVaccinateAppointmentDao; import thyyxxk.wxservice_server.entity.ResultVo; import thyyxxk.wxservice_server.entity.covidvaccinate.CovidVaccinate; import thyyxxk.wxservice_server.entity.covidvaccinate.ZdCovidVaccinate; import thyyxxk.wxservice_server.entity.wxapi.PushMessageParam; import thyyxxk.wxservice_server.utils.DateUtil; import thyyxxk.wxservice_server.utils.IdCardUtil; import thyyxxk.wxservice_server.utils.ResultVoUtil; import thyyxxk.wxservice_server.utils.StringUtil; import java.util.Date; import java.util.List; /** * @author dj */ @Slf4j @Service public class CovidVaccinateAppointmentService { private final CovidVaccinateAppointmentDao dao; private final PushWxMessageService pushWxMessageService; @Autowired public CovidVaccinateAppointmentService(CovidVaccinateAppointmentDao dao, PushWxMessageService pushWxMessageService) { this.dao = dao; this.pushWxMessageService = pushWxMessageService; } public ResultVo> selectVaccinates() { List list = dao.selectVaccinates(); if (null != list && list.size() > 0) { String[] nextSevenDate = DateUtil.getDatesInOneWeek(); list.forEach(item -> { for (String date : nextSevenDate) { Integer max = dao.selectThresholdByDateAndCode(date, item.getVaccinateCode()); max = null == max ? 0 : max; Integer count = dao.selectTotalCountByDateAndCode(date, item.getVaccinateCode()); count = null == count ? 0 : count; int offset = max - count; item.setMinLeftNum(offset); if (offset > 0) { break; } } }); } return ResultVoUtil.success(list); } public ResultVo getPatientInfoAndJobCategories(String patientId) { CovidVaccinate info = dao.selectPatientInfo(patientId); if (null == info) { info = new CovidVaccinate(); } else { info.setPatientId(patientId); String phone = info.getPhone(); String socialNo = info.getSocialNo(); if (null != phone) { if (phone.length() != Constants.CommonLength.PHONE) { info.setPhone(null); } } if (null != socialNo) { if (socialNo.startsWith(Constants.Exception.ILLEGAL_ID_START) || socialNo.length() != Constants.CommonLength.ID_CARD) { info.setSocialNo(null); } } } info.setJobCategories(dao.selectJobCategories()); return ResultVoUtil.success(info); } public ResultVo getNextSevenDaysSources(int code) { int[] result = new int[7]; String[] nextSevenDate = DateUtil.getDatesInOneWeek(); for (int i = 0; i < nextSevenDate.length; i++) { String date = nextSevenDate[i]; Integer max = dao.selectThresholdByDateAndCode(date, code); max = null == max ? 0 : max; Integer count = dao.selectTotalCountByDateAndCode(date, code); count = null == count ? 0 : count; result[i] = max - count; } return ResultVoUtil.success(result); } public ResultVo submitVaccinateAppointment(CovidVaccinate param) { param.setCreateDatetime(new Date()); if (!IdCardUtil.isValidatedIdCard(param.getSocialNo())) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您在我院绑定的身份证号不是有效的身份证号," + "请前往【个人中心 - 我的就诊人 - 就诊人信息】进行修改。"); } int age = IdCardUtil.getAgeByIdCard(param.getSocialNo()); if (age < 18 || age > 70) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "可接种的年龄区间为【18岁 - 70岁】," + "您的年龄不满足此条件,敬请谅解。"); } param.setAge(age); param.setSex(IdCardUtil.getGenderByIdCard(param.getSocialNo()).getCode()); String dateFormatted = DateUtil.formatDatetime(param.getExecuteDate(), "yyyy-MM-dd"); if (dao.selectValidAppointment(param.getSocialNo(), param.getExecuteDate()) > 0) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您在【" + dateFormatted + "】已存在有效的预约,请勿重复预约。"); } Integer threshold = dao.selectThresholdByDateAndCode2(param.getExecuteDate(), param.getVaccinateCode()); if (null == threshold) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "【" + dateFormatted + "】没有可预约的疫苗,请选择其他预约日期。"); } Integer count = dao.selectTotalCountByDateAndCode2(param.getExecuteDate(), param.getVaccinateCode()); count = null == count ? 0 : count; if (count >= threshold) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "【" + dateFormatted + "】的预约名额已满,请选择其他预约日期。"); } dao.insertNewAppointment(param); if (StringUtil.notBlank(param.getPatientId())) { dao.updatePhoneAndSocialNo(param.getPatientId(), param.getSocialNo(), param.getPhone()); String msgContent = "{\"touser\":\"\",\"data\":" + "{\"keyword2\":{\"color\":\"#173177\",\"value\":\"" + dateFormatted + "\"}," + "\"keyword1\":{\"color\":\"#173177\",\"value\":\"" + param.getName() + "\"}," + "\"keyword3\":{\"color\":\"#173177\",\"value\":\"" + (++count) + "\"}," + "\"remark\":{\"color\":\"#FF0000\",\"value\":\"感谢您的使用,祝您健康!\"}," + "\"first\":{\"color\":\"#FF0000\",\"value\":\"您已成功预约新冠疫苗接种服务。" + "请于【" + dateFormatted + "】凭本人身份证在【泰和医院门诊大楼五楼体检中心】接种新冠疫苗。\"}}," + "\"template_id\":\"zVSzbYLmdqq_h1IcTPSwi6X4qFm0j9aTVeLZPxR03Bs\"," + "\"url\":\"\"}"; PushMessageParam pojo = new PushMessageParam(); pojo.setCardNo(param.getPatientId()); pojo.setCardNoPatientId(true); pojo.setMsgContext(JSONObject.parseObject(msgContent)); pushWxMessageService.pushMessage2(pojo); } log.info("预约新冠疫苗接种:{}", JSONObject.toJSONStringWithDateFormat(param, "yyyy-MM-dd HH:mm:ss")); return ResultVoUtil.success("您已成功预约新冠疫苗接种服务。请于【" + dateFormatted + "】" + "凭本人身份证在【泰和医院门诊大楼五楼体检中心】接种新冠疫苗。"); } }