|  | @ -0,0 +1,465 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.birthday;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.birthday.BirthDayWishesToPatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.birthday.BirthdayWishesTemplateDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorHospitalDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.birthday.BirthDayWishesToPatient;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.birthday.BirthdayWishesTemplate;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.IdCardUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | import org.json.JSONObject;
 | 
	
		
			
				|  |  | import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.data.redis.core.RedisTemplate;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | import java.util.concurrent.LinkedBlockingQueue;
 | 
	
		
			
				|  |  | import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | import java.util.stream.Collectors;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by Bing on 2021/7/16.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class BirthdayWishesService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     private static LinkedBlockingQueue<JSONObject> queue = new LinkedBlockingQueue<>();
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorDao doctorDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientDao patientDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BirthdayWishesTemplateDao bwTemplateDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BirthDayWishesToPatientDao bwToPatientDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorHospitalDao hospitalDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private RedisTemplate redisTemplate;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //    /**
 | 
	
		
			
				|  |  | //     * 发送到mq
 | 
	
		
			
				|  |  | //     *
 | 
	
		
			
				|  |  | //     */
 | 
	
		
			
				|  |  | //    public void sender(List<BirthDayWishesToPatientES> birthDayWishesToPatientES) {
 | 
	
		
			
				|  |  | //        //发送到队列
 | 
	
		
			
				|  |  | //        birthDayWishesToPatientES.stream().forEach(one -> {
 | 
	
		
			
				|  |  | //            jmsTemplate.send(channelName, new MessageCreator() {
 | 
	
		
			
				|  |  | //                @Override
 | 
	
		
			
				|  |  | //                public Message createMessage(Session session) throws JMSException {
 | 
	
		
			
				|  |  | //                    TextMessage textMessage = session.createTextMessage();
 | 
	
		
			
				|  |  | //                    textMessage.setText(net.sf.json.JSONObject.fromObject(one).toString());
 | 
	
		
			
				|  |  | //                    return textMessage;
 | 
	
		
			
				|  |  | //                }
 | 
	
		
			
				|  |  | //            });
 | 
	
		
			
				|  |  | //        });
 | 
	
		
			
				|  |  | //    }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 医生端--新建祝福语模板
 | 
	
		
			
				|  |  |      * @param doctor
 | 
	
		
			
				|  |  |      * @param content
 | 
	
		
			
				|  |  |      * @param applicableRange
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @param isDefault
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Long create(String doctor, String content, Integer applicableRange, Long id, Integer isDefault) {
 | 
	
		
			
				|  |  |         BaseDoctorDO doctorDO = doctorDao.findById(doctor);
 | 
	
		
			
				|  |  |         BirthdayWishesTemplate template = null;
 | 
	
		
			
				|  |  |         //判断是编辑还是创建
 | 
	
		
			
				|  |  |         if(id != null){
 | 
	
		
			
				|  |  |             template = bwTemplateDao.findOne(id);
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             template = new BirthdayWishesTemplate();
 | 
	
		
			
				|  |  |             template.setCode(UUID.randomUUID().toString().replace("-",""));
 | 
	
		
			
				|  |  |             template.setType(2);
 | 
	
		
			
				|  |  |             template.setDel(1);
 | 
	
		
			
				|  |  |             template.setCreateUser(doctor);
 | 
	
		
			
				|  |  |             template.setCreateUserName(doctorDO.getName());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         template.setApplicableRange(applicableRange);
 | 
	
		
			
				|  |  |         template.setContent(content);
 | 
	
		
			
				|  |  |         template.setCreateTime(new Date());
 | 
	
		
			
				|  |  |         if(isDefault != null) {
 | 
	
		
			
				|  |  |             if(isDefault == 1){
 | 
	
		
			
				|  |  |                 //先取消该医生自创的默认模板
 | 
	
		
			
				|  |  |                 BirthdayWishesTemplate docTemplate = bwTemplateDao.findByCreateUserAndApplicableRangeAndIsDefault(doctor, applicableRange, 1);
 | 
	
		
			
				|  |  |                 if(docTemplate != null){
 | 
	
		
			
				|  |  |                     bwTemplateDao.setDefalutTemplateById(docTemplate.getId(), 0);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             template.setIsDefault(isDefault);
 | 
	
		
			
				|  |  |         }else if(id == null || template.getIsDefault() == null){
 | 
	
		
			
				|  |  |             template.setIsDefault(0);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         bwTemplateDao.save(template);
 | 
	
		
			
				|  |  |         return template.getId();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取系统模板和医生创建模板
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<BirthdayWishesTemplate> getTemplateByDoctor(String doctor) {
 | 
	
		
			
				|  |  |         return bwTemplateDao.getTemplateByDoctor(doctor);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据id获取模板消息
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public BirthdayWishesTemplate getTemplateById(Long id) {
 | 
	
		
			
				|  |  |         return bwTemplateDao.findOne(id);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public void deleteTemplateById(Long id) {
 | 
	
		
			
				|  |  |         bwTemplateDao.delete(id);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据ID设置为默认模板
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param doctor
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Transactional
 | 
	
		
			
				|  |  |     public BirthdayWishesTemplate setDefalutTemplateById(String doctor, Long id) {
 | 
	
		
			
				|  |  |         BirthdayWishesTemplate template = bwTemplateDao.findOne(id);
 | 
	
		
			
				|  |  |         //先取消该医生自创的默认模板
 | 
	
		
			
				|  |  |         BirthdayWishesTemplate docTemplate = bwTemplateDao.findByCreateUserAndApplicableRangeAndIsDefault(doctor, template.getApplicableRange(), 1);
 | 
	
		
			
				|  |  |         if(docTemplate != null){
 | 
	
		
			
				|  |  |             bwTemplateDao.setDefalutTemplateById(docTemplate.getId(), 0);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         //如果不是系统模板,就添加默认模板标识
 | 
	
		
			
				|  |  |         if(template.getType() != 1){
 | 
	
		
			
				|  |  |             bwTemplateDao.setDefalutTemplateById(id, 1);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return template;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取医生当天未发送生日祝福居民信息
 | 
	
		
			
				|  |  |      * @param doctor
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<Map<String, Object>> getPatientByDoctor(String doctor) throws Exception{
 | 
	
		
			
				|  |  |         //es日期格式
 | 
	
		
			
				|  |  |         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 | 
	
		
			
				|  |  |         String createTime = sdf.format(new Date()) + " 00:00:00";
 | 
	
		
			
				|  |  |         //今日推送的居民
 | 
	
		
			
				|  |  |         Set<String> sendPatients = (Set<String>) redisTemplate.opsForValue().get("birthday:wish:sendPatient");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Date currentTime = new Date();
 | 
	
		
			
				|  |  |         SimpleDateFormat format = new SimpleDateFormat("MMdd");
 | 
	
		
			
				|  |  |         String dateString = format.format(currentTime);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //获取当天0点
 | 
	
		
			
				|  |  |         SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
 | 
	
		
			
				|  |  |         String now = f.format(new Date());
 | 
	
		
			
				|  |  |         String sql = "SELECT p.id patient, p.`name`, " +
 | 
	
		
			
				|  |  |                 "CASE p.sex WHEN 1 THEN '男' WHEN 2 THEN '女' END sex," +
 | 
	
		
			
				|  |  |                 "p.idcard, p.photo," +
 | 
	
		
			
				|  |  |                 " CASE LENGTH(p.idcard) WHEN  18 then SUBSTR(p.idcard,11,4) when 15 then SUBSTR(p.idcard, 9,4) END date " +
 | 
	
		
			
				|  |  |                 "from wlyy_archive a,base_patient p WHERE  a.patient = p.id and a.sign_status = 1 and EXISTS( " +
 | 
	
		
			
				|  |  |                 "SELECT DISTINCT sr.patient from base_service_package_sign_record sr,base_service_package_record r, " +
 | 
	
		
			
				|  |  |                 "base_service_package_item i,base_team_member m " +
 | 
	
		
			
				|  |  |                 "WHERE  sr.id = r.sign_id and sr.patient = a.patient  and r.service_package_id = i.service_package_id " +
 | 
	
		
			
				|  |  |                 "and i.del = 1 and m.team_code = i.team_code and m.doctor_code = '"+doctor+"' ) "+
 | 
	
		
			
				|  |  |                 " and (p.archive_status<>2 or p.archive_status is null) " +
 | 
	
		
			
				|  |  |                 "AND CASE LENGTH(p.idcard) WHEN  18 then SUBSTR(p.idcard,11,4) when 15 then SUBSTR(p.idcard, 9,4) END =  '" + dateString + "' " ;
 | 
	
		
			
				|  |  |         List<Map<String, Object>> patientInfos =  jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  |         //整理未发送居民信息
 | 
	
		
			
				|  |  |         for(Map<String, Object> map : patientInfos){
 | 
	
		
			
				|  |  |             if (sendPatients == null || !sendPatients.contains(map.get("patient"))) {
 | 
	
		
			
				|  |  |                 int age = IdCardUtil.getAgeForIdcard(map.get("idcard") + "");
 | 
	
		
			
				|  |  |                 Date birthday = IdCardUtil.getBirthdayForIdcard(map.get("idcard") + "");
 | 
	
		
			
				|  |  |                 map.put("age", age);
 | 
	
		
			
				|  |  |                 map.put("birthday", DateUtil.dateToStr(birthday, "yyyy-MM-dd"));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return patientInfos;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取医生生日列表
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Map<String, Object> getBirthdayPatientList(String doctorCode, String patientName, Integer signYear, String startDate, String endDate, Integer status, String currentUserRole, String currentUserRoleLevel,
 | 
	
		
			
				|  |  |                                                       Integer roleType, Integer page, Integer pageSize, Integer teamId, Integer isLeader) throws Exception {
 | 
	
		
			
				|  |  |         Map<String, Object> resultMap = new HashMap<>();
 | 
	
		
			
				|  |  |         List<Map<String, Object>> resultList = new ArrayList();
 | 
	
		
			
				|  |  |         Long allCount = 0l;
 | 
	
		
			
				|  |  |         //判断是否筛选推送条件,若未筛选则从数据库直接查生日居民,再进行匹配是否推送,若选择已推送则从推送记录查,若未发送则从未推送表查
 | 
	
		
			
				|  |  |         //助老员签约患者
 | 
	
		
			
				|  |  |         String sql = " SELECT Distinct p.id patient,p.idcard,p.mobile,p.name,p.sex,p.id,p.photo," +
 | 
	
		
			
				|  |  |                 "CASE LENGTH(p.idcard) WHEN 18 THEN SUBSTR(p.idcard, 11, 4) WHEN 15 THEN SUBSTR(p.idcard, 9, 4) END birthday  " ;
 | 
	
		
			
				|  |  |         String sqlCount = "SELECT count(distinct p.idcard) ";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String whereSql = "from wlyy_archive a,base_patient p WHERE  a.patient = p.id and a.sign_status = 1 and EXISTS( " +
 | 
	
		
			
				|  |  |                 "SELECT DISTINCT sr.patient from base_service_package_sign_record sr,base_service_package_record r, " +
 | 
	
		
			
				|  |  |                 "base_service_package_item i,base_team_member m " +
 | 
	
		
			
				|  |  |                 "WHERE  sr.id = r.sign_id and sr.patient = a.patient  and r.service_package_id = i.service_package_id " +
 | 
	
		
			
				|  |  |                 "and i.del = 1 and m.team_code = i.team_code ";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //根据权限获取生日居民列表
 | 
	
		
			
				|  |  |         if (roleType == 1) {
 | 
	
		
			
				|  |  |             whereSql += " and m.doctor_code = '"+doctorCode+"' ";
 | 
	
		
			
				|  |  |         } else {//管理员
 | 
	
		
			
				|  |  |             switch (currentUserRoleLevel) {
 | 
	
		
			
				|  |  |                 case "1": {//省
 | 
	
		
			
				|  |  |                     whereSql += " and i.org_code in ( select org.code from base_org org where org.province_code = '"+currentUserRole+"' and org.del=1 )  ";
 | 
	
		
			
				|  |  |                     break;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 case "2": {//市
 | 
	
		
			
				|  |  |                     whereSql += " and i.org_code in ( select org.code from base_org org where org.city_code = '"+currentUserRole+"' and org.del=1 )  ";
 | 
	
		
			
				|  |  |                     break;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 case "3": {//区
 | 
	
		
			
				|  |  |                     whereSql += " and i.org_code in ( select org.code from base_org org where org.town_code = '"+currentUserRole+"' and org.del=1 )  ";
 | 
	
		
			
				|  |  |                     break;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 case "4": {//社区机构
 | 
	
		
			
				|  |  |                     whereSql += " and i.org_code = '" + currentUserRole + "' ";
 | 
	
		
			
				|  |  |                     break;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(patientName)) {
 | 
	
		
			
				|  |  |             whereSql += "and p.name like '%" + patientName + "%' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         //根据签约年限来获取生日居民
 | 
	
		
			
				|  |  |         if ( null==  signYear|| signYear == DateUtil.getNowYear()) {//当前年度
 | 
	
		
			
				|  |  |             whereSql += " and sr.status=1 ";
 | 
	
		
			
				|  |  |         } else {//
 | 
	
		
			
				|  |  |             whereSql += " and sr.status=-1 and sr.start_time >='" + signYear+" 01-01:00:00:00" + "' and sr.start_time <='" + signYear+" 12-31:23:59:59" + "'  ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
 | 
	
		
			
				|  |  |             String start = startDate.substring(4).replace("-", "");
 | 
	
		
			
				|  |  |             String end = endDate.substring(4).replace("-", "");
 | 
	
		
			
				|  |  |             Integer birStart = Integer.parseInt(start);
 | 
	
		
			
				|  |  |             Integer birEnd = Integer.parseInt(end);
 | 
	
		
			
				|  |  |             //判断生日日期
 | 
	
		
			
				|  |  |             if(birEnd > birStart) {
 | 
	
		
			
				|  |  |                 whereSql += "AND CASE LENGTH(p.idcard) WHEN 18 THEN SUBSTR(p.idcard, 11, 4) WHEN 15 THEN SUBSTR(p.idcard, 9, 4) END >= '" + start + "' " +
 | 
	
		
			
				|  |  |                         "AND CASE LENGTH(p.idcard) WHEN 18 THEN SUBSTR(p.idcard, 11, 4) WHEN 15 THEN SUBSTR(p.idcard, 9, 4) END <= '" + end + "' ";
 | 
	
		
			
				|  |  |             }else if(birEnd < birStart){
 | 
	
		
			
				|  |  |                 whereSql += "AND CASE LENGTH(p.idcard) WHEN 18 THEN SUBSTR(p.idcard, 11, 4) WHEN 15 THEN SUBSTR(p.idcard, 9, 4) END >= '" + end + "' " +
 | 
	
		
			
				|  |  |                         "AND CASE LENGTH(p.idcard) WHEN 18 THEN SUBSTR(p.idcard, 11, 4) WHEN 15 THEN SUBSTR(p.idcard, 9, 4) END <= '" + start + "' ";
 | 
	
		
			
				|  |  |             }else {
 | 
	
		
			
				|  |  |                 whereSql += "AND CASE LENGTH(p.idcard) WHEN 18 THEN SUBSTR(p.idcard, 11, 4) WHEN 15 THEN SUBSTR(p.idcard, 9, 4) END = '" + end + "' ";
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         whereSql += "and DATE_FORMAT(sr.create_time,'%m%d')< CASE LENGTH(p.idcard) WHEN 18 THEN SUBSTR(p.idcard, 11, 4) WHEN 15 THEN SUBSTR(p.idcard, 9, 4) END ";
 | 
	
		
			
				|  |  |         sql = sql + whereSql + " ) order by birthday limit "+(page-1)*pageSize + "," + pageSize;
 | 
	
		
			
				|  |  |         sqlCount = sqlCount + whereSql+")";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  |         allCount = jdbcTemplate.queryForObject(sqlCount, Long.class);
 | 
	
		
			
				|  |  |         resultMap.put("total", allCount);
 | 
	
		
			
				|  |  |         //获取生日居民code
 | 
	
		
			
				|  |  |         String patientString = "";
 | 
	
		
			
				|  |  |         for (Map<String, Object> map : list) {
 | 
	
		
			
				|  |  |             patientString += "'"+ map.get("patient") + "',";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if(patientString.length() > 0) {
 | 
	
		
			
				|  |  |             //查询该居民是否有推送过
 | 
	
		
			
				|  |  |             String es = "SELECT patient_code patientCode,doctor_name doctorName,content,birthday,create_time createTime FROM birthday_wishes_to_patient where patient_code in (" + patientString.substring(0, patientString.length() - 1) + ") and user_type =1 and create_time > '" + startDate + " 00:00:00' and create_time < '" + endDate + " 23:59:59'";
 | 
	
		
			
				|  |  |             List<Map<String, Object>> sendPatientList = jdbcTemplate.queryForList(es);
 | 
	
		
			
				|  |  |             Set<String> patientSet = new HashSet<>();
 | 
	
		
			
				|  |  |             for (Map<String, Object> map : list) {
 | 
	
		
			
				|  |  |                 String patientCode = map.get("patient") + "";
 | 
	
		
			
				|  |  |                 //判断是否在推送记录里
 | 
	
		
			
				|  |  |                 if( null == status || 1 == status  ) {
 | 
	
		
			
				|  |  |                     for (Map<String, Object> esMap : sendPatientList) {
 | 
	
		
			
				|  |  |                         if (patientCode.equals(esMap.get("patientCode") + "")) {
 | 
	
		
			
				|  |  |                             map.put("birthday", esMap.get("birthday") + "");
 | 
	
		
			
				|  |  |                             map.put("doctorName", esMap.get("doctorName") + "");
 | 
	
		
			
				|  |  |                             map.put("content", esMap.get("content") + "");
 | 
	
		
			
				|  |  |                             map.put("createTime", (esMap.get("createTime") + "").replace("T", " ").replace("+0800", ""));
 | 
	
		
			
				|  |  |                             map.put("status", "已发送");
 | 
	
		
			
				|  |  |                             patientSet.add(patientCode);
 | 
	
		
			
				|  |  |                             resultList.add(map);
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if(null == status || 2 == status) {
 | 
	
		
			
				|  |  |                 //存取未发送居民
 | 
	
		
			
				|  |  |                 for (Map<String, Object> map : list) {
 | 
	
		
			
				|  |  |                     String patientCode = map.get("patient") + "";
 | 
	
		
			
				|  |  |                     if (!patientSet.contains(patientCode)) {
 | 
	
		
			
				|  |  |                         String birthday = map.get("birthday")+"日";
 | 
	
		
			
				|  |  |                         StringBuilder bir = new StringBuilder(birthday);
 | 
	
		
			
				|  |  |                         bir.insert(2, "月");
 | 
	
		
			
				|  |  | //                            int age = IdCardUtil.getAgeForIdcard(map.get("idcard") + "");
 | 
	
		
			
				|  |  |                         map.put("birthday", bir.toString());
 | 
	
		
			
				|  |  | //                        int age = IdCardUtil.getAgeForIdcard(map.get("idcard") + "");
 | 
	
		
			
				|  |  | //                        map.put("age", age);
 | 
	
		
			
				|  |  |                         map.put("status", "未发送");
 | 
	
		
			
				|  |  |                         resultList.add(map);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         resultMap.put("detailModelList", resultList);
 | 
	
		
			
				|  |  |         resultMap.put("total", allCount);
 | 
	
		
			
				|  |  |         resultMap.put("pageCount", allCount%pageSize == 0 ?allCount/pageSize : allCount/pageSize +1);
 | 
	
		
			
				|  |  |         return resultMap;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 将发送祝福的居民相关信息存到es
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<BirthDayWishesToPatient> saveBirthdayWishesES(JSONObject json) {
 | 
	
		
			
				|  |  |         List<BirthDayWishesToPatient> list = new ArrayList<>();
 | 
	
		
			
				|  |  |         List<BirthDayWishesToPatient> birthdayWishesList = new ArrayList<>();
 | 
	
		
			
				|  |  |         String p = json.get("patient") + "";
 | 
	
		
			
				|  |  |         String doctorCode = json.get("doctorCode") + "";
 | 
	
		
			
				|  |  |         Integer sendType = (Integer) json.get("sendType");
 | 
	
		
			
				|  |  |         String currentUserRole = json.has("currentUserRole")?json.get("currentUserRole") + "":null;
 | 
	
		
			
				|  |  |         String currentUserRoleLevel = json.has("currentUserRoleLevel")?json.get("currentUserRoleLevel") + "":null;
 | 
	
		
			
				|  |  |         String dateString = json.get("birthday") + "";
 | 
	
		
			
				|  |  |         Integer isSave = (Integer) json.get("isSave");
 | 
	
		
			
				|  |  |         Long templateId = null;
 | 
	
		
			
				|  |  |         String content = null;
 | 
	
		
			
				|  |  |         if(isSave == 1) {
 | 
	
		
			
				|  |  |             templateId = (Long) json.get("templateId");
 | 
	
		
			
				|  |  |             BirthdayWishesTemplate birthdayWishesTemplate = bwTemplateDao.findOne(templateId);
 | 
	
		
			
				|  |  |             content = birthdayWishesTemplate.getContent();
 | 
	
		
			
				|  |  |         }else if(isSave == 0){
 | 
	
		
			
				|  |  |             content = json.get("content") + "";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Set<String> patientSet = new HashSet<>();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 | 
	
		
			
				|  |  |         String createTime = f.format(new Date());
 | 
	
		
			
				|  |  |         //医生今日推送的居民
 | 
	
		
			
				|  |  |         List<BirthDayWishesToPatient> sendPatientList = findSendPatientByCreateTime(p.toString(),createTime);
 | 
	
		
			
				|  |  |         Set<String> sendPatient = sendPatientList.stream().map(BirthDayWishesToPatient::getPatientCode).collect(Collectors.toSet());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Set<String> openidSet = new HashSet<>();
 | 
	
		
			
				|  |  |         BaseDoctorDO doctor = doctorDao.findById(doctorCode);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sql = " select * from ( select p.id as patient,p.name,we.openid from base_patient p inner join  base_patient_wechat we on p.id = we.patient_id and we.patient_id in ( "+p.toString()+") " +
 | 
	
		
			
				|  |  |                 "  order by we.create_time desc )A group by A.patient  ";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<Map<String, Object>> patientInfos = jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //批量发送要属于同一批次
 | 
	
		
			
				|  |  |         String batchNo = UUID.randomUUID().toString();
 | 
	
		
			
				|  |  |         for (Map<String, Object> patientInfo : patientInfos) {
 | 
	
		
			
				|  |  |             String s = patientInfo.get("patient") + "";
 | 
	
		
			
				|  |  |             //已推送则不再重复推送
 | 
	
		
			
				|  |  |             if(!sendPatient.contains(s)) {
 | 
	
		
			
				|  |  |                 patientSet.add(s);
 | 
	
		
			
				|  |  |                 //获取已推送居民
 | 
	
		
			
				|  |  |                 sendPatient.add(s);
 | 
	
		
			
				|  |  |             }else {
 | 
	
		
			
				|  |  |                 continue;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             openidSet.add(patientInfo.get("openid") + "");
 | 
	
		
			
				|  |  |             BirthDayWishesToPatient es = new BirthDayWishesToPatient();
 | 
	
		
			
				|  |  |             es.setBatchNo(batchNo);
 | 
	
		
			
				|  |  |             es.setPatientCode(s);
 | 
	
		
			
				|  |  |             es.setPatientName(patientInfo.get("name") + "");
 | 
	
		
			
				|  |  |             es.setDoctorCode(doctor.getId());
 | 
	
		
			
				|  |  |             es.setDoctorName(doctor.getName());
 | 
	
		
			
				|  |  |             es.setUserType(1);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             List<BaseDoctorHospitalDO> hospitalDOs = hospitalDao.findByDoctorCode(doctorCode);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //存储发送人信息
 | 
	
		
			
				|  |  |             es.setSendPic(doctor.getPhoto());
 | 
	
		
			
				|  |  |             es.setSendSex(doctor.getSex()+"");
 | 
	
		
			
				|  |  | //            es.setAdminTeamName(patientInfo.get("teamName") + "");
 | 
	
		
			
				|  |  | //            es.setAdminTeamCode(Long.valueOf(patientInfo.get("admin_team_code")+""));
 | 
	
		
			
				|  |  |             if (hospitalDOs.size()>0){
 | 
	
		
			
				|  |  |                 es.setHospital(hospitalDOs.get(0).getOrgCode());
 | 
	
		
			
				|  |  |                 es.setHospitalName(hospitalDOs.get(0).getOrgName());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             es.setTown(doctor.getTownCode());
 | 
	
		
			
				|  |  |             es.setTownName(doctor.getTownName());
 | 
	
		
			
				|  |  |             es.setSendType(sendType);
 | 
	
		
			
				|  |  |             es.setSendLevel(doctor.getLevel());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             if (StringUtils.isNotBlank(currentUserRole)){
 | 
	
		
			
				|  |  |                 es.setCurrentUserRoleCode(currentUserRole);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (StringUtils.isNotBlank(currentUserRoleLevel)){
 | 
	
		
			
				|  |  |                 es.setCurrentUserRoleLevel(currentUserRoleLevel);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             es.setAllCount(patientSet.size());
 | 
	
		
			
				|  |  |             es.setOpenidSet(openidSet.toString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //生日祝福信息
 | 
	
		
			
				|  |  |             es.setBirthday(dateString);
 | 
	
		
			
				|  |  |             es.setTemplateId(templateId);
 | 
	
		
			
				|  |  |             es.setContent(content);
 | 
	
		
			
				|  |  |             es.setCreateTime(DateUtil.getStringDate());
 | 
	
		
			
				|  |  |             list.add(es);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(list.size()>0){
 | 
	
		
			
				|  |  |             //保存到ES中
 | 
	
		
			
				|  |  | //            elastricSearchSave.save(list, esIndex, esType);
 | 
	
		
			
				|  |  |             //另存一条发送祝福的医生信息,便于查询医生发送祝福的居民列表
 | 
	
		
			
				|  |  |             BirthDayWishesToPatient e = new BirthDayWishesToPatient();
 | 
	
		
			
				|  |  |             BeanUtils.copyProperties(list.get(0), e);
 | 
	
		
			
				|  |  |             e.setUserType(2);
 | 
	
		
			
				|  |  |             e.setPatientCode(null);
 | 
	
		
			
				|  |  |             e.setPatientName(null);
 | 
	
		
			
				|  |  |             list.add(e);
 | 
	
		
			
				|  |  |             bwToPatientDao.save(list);
 | 
	
		
			
				|  |  | //            System.out.println("ES保存成功!");
 | 
	
		
			
				|  |  |             //将发送生日祝福的居民code存起来
 | 
	
		
			
				|  |  |             Calendar today = Calendar.getInstance();
 | 
	
		
			
				|  |  |             today.set(Calendar.HOUR, 23);
 | 
	
		
			
				|  |  |             today.set(Calendar.MINUTE, 59);
 | 
	
		
			
				|  |  |             today.set(Calendar.SECOND, 59);
 | 
	
		
			
				|  |  |             today.set(Calendar.MILLISECOND, 999);
 | 
	
		
			
				|  |  |             redisTemplate.opsForValue().set("birthday:wish:sendPatient", sendPatient);
 | 
	
		
			
				|  |  |             redisTemplate.expire("birthday:wish:sendPatient", today.getTimeInMillis() - Calendar.getInstance().getTimeInMillis(), TimeUnit.MILLISECONDS);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return list;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取今日已推送居民
 | 
	
		
			
				|  |  |      * @param
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<BirthDayWishesToPatient> findSendPatientByCreateTime(String patient,String createTime) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         StringBuffer sql = new StringBuffer("select patient_code patientCode from  birthday_wishes_to_patient  "+
 | 
	
		
			
				|  |  |                 " where user_type=1 and create_time>'" + createTime + "' ");
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(patient)){
 | 
	
		
			
				|  |  |             sql.append(" and patient_code in ( "+patient+" ) ") ;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         sql.append(" limit 0,500000 ");
 | 
	
		
			
				|  |  |         List<BirthDayWishesToPatient> patientList = jdbcTemplate.queryForList(sql.toString(), BirthDayWishesToPatient.class);
 | 
	
		
			
				|  |  |         return patientList;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |