123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package com.yihu.wlyy.wechat.util;
- import com.yihu.wlyy.entity.patient.Patient;
- import com.yihu.wlyy.entity.patient.PatientFamilyMember;
- import com.yihu.wlyy.repository.patient.PatientDao;
- import com.yihu.wlyy.repository.patient.PatientFamilyMemberDao;
- import com.yihu.wlyy.service.app.family.FamilyMemberService;
- import com.yihu.wlyy.util.IdCardUtil;
- import org.json.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Component;
- import org.apache.commons.lang3.StringUtils;
- import javax.annotation.PostConstruct;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by Trick on 2017/4/20.
- */
- @Component
- public class WeiXinOpenIdUtils {
- @Autowired
- private JdbcTemplate jdbcTemplate;
- @Autowired
- PatientDao patientDao;
- private Map<Integer, String> relations = new HashMap<>();
- @PostConstruct
- public void init() {
- relations.put(0, "其他");
- relations.put(1, "父亲");
- relations.put(2, "母亲");
- relations.put(3, "老公");
- relations.put(4, "老婆");
- relations.put(5, "儿子");
- relations.put(6, "女儿");
- relations.put(7, "未知");
- }
- //关系翻译
- public String getRelation(int relation) {
- return relations.get(relation);
- }
- /**
- * 先按照家庭关系“父亲→母亲→老公→老婆→儿子→女儿→其他”的顺序,获取openid
- *
- * @param code 推送人
- * @return
- */
- public JSONObject getFamilyOpenId(String code) {
- /**
- * 按关系和时间排序
- */
- StringBuffer sql = new StringBuffer("SELECT * FROM wlyy_patient_family_member t WHERE t.patient = '" + code + "' ORDER BY t.family_relation ASC ,t.czrq DESC");
- JSONObject result = new JSONObject();
- List<Map<String, Object>> members = jdbcTemplate.queryForList(sql.toString());
- List<Map<String, Object>> others = new ArrayList<>();
- if (members != null && members.size() > 0) {
- for (Map<String, Object> member : members) {
- if ((int) member.get("family_relation") == 0) {
- others.add(member);
- } else {
- String memberCode = (String) member.get("family_member");
- if (StringUtils.isNotBlank(memberCode)) {
- Patient p = patientDao.findByCode(memberCode);
- if (StringUtils.isNotBlank(p.getOpenid())) {
- result.put("member", p);
- result.put("relation", (int) member.get("family_relation"));
- return result;
- }
- }
- }
- }
- }
- //查询为关系为其他的openid
- if (others != null && others.size() > 0) {
- for (Map<String, Object> other : others) {
- String memberCode = (String) other.get("family_member");
- if (StringUtils.isNotBlank(memberCode)) {
- Patient p = patientDao.findByCode(memberCode);
- if (StringUtils.isNotBlank(p.getOpenid())) {
- result.put("member", p);
- result.put("relation", (int) other.get("family_relation"));
- return result;
- }
- }
- }
- }
- result.put("member",new Patient());
- return result;
- }
- public String getTitleMes(Patient p, int ralation, String dealerName) throws Exception {
- return "因您的" + relations.get(familyRelationTrans(p, ralation)) + p.getName() + "未绑定微信,故请将该消息传达给" + dealerName + ":";
- }
- /**
- * 家庭关系转换
- *
- * @param patient 居民
- * @param relation 关系 1父亲 2母亲 3老公 4老婆 5儿子 6女儿 7其他
- * @return
- */
- public int familyRelationTrans(Patient patient, int relation) throws Exception {
- int relationTrans = 0;
- switch (relation) {
- case 1:
- case 2:
- if (patient.getSex() == 1) {
- relationTrans = 5;
- } else if (patient.getSex() == 2) {
- relationTrans = 6;
- } else {
- relationTrans = 0;
- }
- if (relationTrans == 0) {
- if (IdCardUtil.getSexForIdcard(StringUtils.isEmpty(patient.getIdcard()) ? "" : patient.getIdcard()).equals("1")) {
- relationTrans = 6;
- } else if (IdCardUtil.getSexForIdcard(StringUtils.isEmpty(patient.getIdcard()) ? "" : patient.getIdcard()).equals("2")) {
- relationTrans = 5;
- }
- }
- break;
- case 3:
- relationTrans = 4;
- break;
- case 4:
- relationTrans = 3;
- break;
- case 5:
- case 6:
- if (patient.getSex() == 1) {
- relationTrans = 1;
- } else if (patient.getSex() == 2) {
- relationTrans = 2;
- } else {
- relationTrans = 0;
- }
- if (relationTrans == 0) {
- if (IdCardUtil.getSexForIdcard(StringUtils.isEmpty(patient.getIdcard()) ? "" : patient.getIdcard()).equals("1")) {
- relationTrans = 2;
- } else if (IdCardUtil.getSexForIdcard(StringUtils.isEmpty(patient.getIdcard()) ? "" : patient.getIdcard()).equals("2")) {
- relationTrans = 1;
- }
- }
- break;
- }
- return relationTrans;
- }
- }
|