package com.yihu.wlyy.web.doctor.family;

import com.yihu.wlyy.service.app.family.FamilyMemberService;
import com.yihu.wlyy.service.app.family.FamilyService;
import com.yihu.wlyy.web.WeixinBaseController;
import io.swagger.annotations.Api;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016.08.20.
 */
@Controller
@RequestMapping(value = "/doctor/family", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "医生端-家庭查询")
public class DoctorFamilyController extends WeixinBaseController {
    @Autowired
    private FamilyService familyService;
    @Autowired
    FamilyMemberService familyMemberService;

    //通過患者身份證查找患者家庭成员
    @RequestMapping(value = "/getfamilyMember")
    @ResponseBody
    public String getfamilyMember(
            String patientCode,//患者code
            String doctorCode,//医生code
            String idcard//患者身份证号
    ) {
        try {
            Map<String, JSONObject> result = new HashMap<>();
            JSONArray patients = familyService.getfamilyMember(patientCode, idcard, doctorCode);
            JSONArray patientsCustom = familyMemberService.getPatientFamilyMembers(patientCode, doctorCode);

            if (patients != null) {
                for(int i = 0;i < patients.length(); i++){
                    if(patients.getJSONObject(i).get("idcard") != null) {
                        result.put(patients.getJSONObject(i).getString("idcard"), patients.getJSONObject(i));
                    }
                }
            }
            if (patientsCustom != null) {
                for(int i = 0;i < patientsCustom.length(); i++){
                    if(patientsCustom.getJSONObject(i).get("idcard") != null) {
                        result.put(patientsCustom.getJSONObject(i).getString("idcard"), patientsCustom.getJSONObject(i));
                    }
                }
            }

            return write(200, "查询成功", "list", result.size() > 0 ? new JSONArray(result.values()) : new JSONArray());
        } catch (Exception e) {
            return error(-1, "查询失败！");
        }
    }

    //邀请家庭成员签约
    @RequestMapping(value = "/inviteFamilyMember")
    @ResponseBody
    public String inviteFamilyMember(
            String patient,//邀请的患者code
            String currentPatient,//患者code
            String doctor,//医生code
            String relation//关系
    ) {
        try {
            String returnCode = familyService.inviteFamilyMember(patient, currentPatient, doctor, relation);
            if ("-1".equals(returnCode)) {
                return write(-1, "邀请失败，患者已经签约");
            } else if ("-2".equals(returnCode)) {
                return write(-2, "邀请失败，医生已经邀请过该患者");
            } else if ("-3".equals(returnCode)) {
                return write(-3, "邀请失败，与患者签约的三师医生不一致");
            }
            return write(200, "查询成功");
        } catch (Exception e) {
            error(e);
            return error(-1, "邀请失败！");
        }
    }
}
