WeiXinOpenIdUtils.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.yihu.wlyy.wechat.util;
  2. import com.yihu.wlyy.entity.patient.Patient;
  3. import com.yihu.wlyy.entity.patient.PatientFamilyMember;
  4. import com.yihu.wlyy.repository.patient.PatientDao;
  5. import com.yihu.wlyy.repository.patient.PatientFamilyMemberDao;
  6. import com.yihu.wlyy.service.app.family.FamilyMemberService;
  7. import com.yihu.wlyy.util.IdCardUtil;
  8. import org.json.JSONObject;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.jdbc.core.JdbcTemplate;
  11. import org.springframework.stereotype.Component;
  12. import org.apache.commons.lang3.StringUtils;
  13. import javax.annotation.PostConstruct;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. /**
  19. * Created by Trick on 2017/4/20.
  20. */
  21. @Component
  22. public class WeiXinOpenIdUtils {
  23. @Autowired
  24. private JdbcTemplate jdbcTemplate;
  25. @Autowired
  26. PatientDao patientDao;
  27. private Map<Integer, String> relations = new HashMap<>();
  28. @PostConstruct
  29. public void init() {
  30. relations.put(0, "其他");
  31. relations.put(1, "父亲");
  32. relations.put(2, "母亲");
  33. relations.put(3, "老公");
  34. relations.put(4, "老婆");
  35. relations.put(5, "儿子");
  36. relations.put(6, "女儿");
  37. relations.put(7, "未知");
  38. }
  39. //关系翻译
  40. public String getRelation(int relation) {
  41. return relations.get(relation);
  42. }
  43. /**
  44. * 先按照家庭关系“父亲→母亲→老公→老婆→儿子→女儿→其他”的顺序,获取openid
  45. *
  46. * @param code 推送人
  47. * @return
  48. */
  49. public JSONObject getFamilyOpenId(String code) {
  50. /**
  51. * 按关系和时间排序
  52. */
  53. StringBuffer sql = new StringBuffer("SELECT * FROM wlyy_patient_family_member t WHERE t.patient = '" + code + "' ORDER BY t.family_relation ASC ,t.czrq DESC");
  54. JSONObject result = new JSONObject();
  55. List<Map<String, Object>> members = jdbcTemplate.queryForList(sql.toString());
  56. List<Map<String, Object>> others = new ArrayList<>();
  57. if (members != null && members.size() > 0) {
  58. for (Map<String, Object> member : members) {
  59. if ((int) member.get("family_relation") == 0) {
  60. others.add(member);
  61. } else {
  62. String memberCode = (String) member.get("family_member");
  63. if (StringUtils.isNotBlank(memberCode)) {
  64. Patient p = patientDao.findByCode(memberCode);
  65. if (StringUtils.isNotBlank(p.getOpenid())) {
  66. result.put("member", p);
  67. result.put("relation", (int) member.get("family_relation"));
  68. return result;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. //查询为关系为其他的openid
  75. if (others != null && others.size() > 0) {
  76. for (Map<String, Object> other : others) {
  77. String memberCode = (String) other.get("family_member");
  78. if (StringUtils.isNotBlank(memberCode)) {
  79. Patient p = patientDao.findByCode(memberCode);
  80. if (StringUtils.isNotBlank(p.getOpenid())) {
  81. result.put("member", p);
  82. result.put("relation", (int) other.get("family_relation"));
  83. return result;
  84. }
  85. }
  86. }
  87. }
  88. result.put("member",new Patient());
  89. return result;
  90. }
  91. public String getTitleMes(Patient p, int ralation, String dealerName) throws Exception {
  92. return "因您的" + relations.get(familyRelationTrans(p, ralation)) + p.getName() + "未绑定微信,故请将该消息传达给" + dealerName + ":";
  93. }
  94. /**
  95. * 家庭关系转换
  96. *
  97. * @param patient 居民
  98. * @param relation 关系 1父亲 2母亲 3老公 4老婆 5儿子 6女儿 7其他
  99. * @return
  100. */
  101. public int familyRelationTrans(Patient patient, int relation) throws Exception {
  102. int relationTrans = 0;
  103. switch (relation) {
  104. case 1:
  105. case 2:
  106. if (patient.getSex() == 1) {
  107. relationTrans = 5;
  108. } else if (patient.getSex() == 2) {
  109. relationTrans = 6;
  110. } else {
  111. relationTrans = 0;
  112. }
  113. if (relationTrans == 0) {
  114. if (IdCardUtil.getSexForIdcard(StringUtils.isEmpty(patient.getIdcard()) ? "" : patient.getIdcard()).equals("1")) {
  115. relationTrans = 6;
  116. } else if (IdCardUtil.getSexForIdcard(StringUtils.isEmpty(patient.getIdcard()) ? "" : patient.getIdcard()).equals("2")) {
  117. relationTrans = 5;
  118. }
  119. }
  120. break;
  121. case 3:
  122. relationTrans = 4;
  123. break;
  124. case 4:
  125. relationTrans = 3;
  126. break;
  127. case 5:
  128. case 6:
  129. if (patient.getSex() == 1) {
  130. relationTrans = 1;
  131. } else if (patient.getSex() == 2) {
  132. relationTrans = 2;
  133. } else {
  134. relationTrans = 0;
  135. }
  136. if (relationTrans == 0) {
  137. if (IdCardUtil.getSexForIdcard(StringUtils.isEmpty(patient.getIdcard()) ? "" : patient.getIdcard()).equals("1")) {
  138. relationTrans = 2;
  139. } else if (IdCardUtil.getSexForIdcard(StringUtils.isEmpty(patient.getIdcard()) ? "" : patient.getIdcard()).equals("2")) {
  140. relationTrans = 1;
  141. }
  142. }
  143. break;
  144. }
  145. return relationTrans;
  146. }
  147. }