package com.yihu.wlyy.wechat.util;

import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.patient.PatientDao;
import com.yihu.wlyy.util.IdCardUtil;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

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;
    }

    /**
     * 查找代理人的openId
     * @param code
     * @return
     */
    public JSONArray getAgentOpenId(String code,String openid) {
        JSONArray result = new JSONArray();
        StringBuffer sql = new StringBuffer("SELECT * FROM wlyy_patient_family_member t WHERE t.patient = '" + code + "' and t.is_authorize =1 ");

        List<Map<String, Object>> members = jdbcTemplate.queryForList(sql.toString());
        if (members != null && members.size() > 0) {
            Map<String,String> map = new HashMap<>();
            for (Map<String, Object> member : members) {
                String memberCode = (String) member.get("family_member");
                if (StringUtils.isNotBlank(memberCode)) {
                    Patient p = patientDao.findByCode(memberCode);
                    if (StringUtils.isNotBlank(p.getOpenid())&&!p.getOpenid().equals(openid)) {
                        if(!map.containsKey(p.getOpenid())){
                            map.put(p.getOpenid(),p.getOpenid());
                            JSONObject json = new JSONObject();
                            json.put("member", p);
                            json.put("relation", (int) member.get("family_relation"));
                            result.put(json);
                        }
                    }
                }
            }
        }

        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;
    }
}
