package com.yihu.wlyy.web.wx;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.service.app.sign.FamilyContractService;
import com.yihu.wlyy.service.common.account.PatientService;
import com.yihu.wlyy.util.SystemConf;
import com.yihu.wlyy.web.WeixinBaseController;

@Controller
@RequestMapping("/wx/self/management")
@Api(description = "自我管理")
public class SelfManagementController extends WeixinBaseController {

    @Autowired
    private PatientService patientService;
    @Autowired
    private FamilyContractService familyContractService;

    /**
     * 自我管理»三师签约
     *
     * @param code
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "ssqy", method = RequestMethod.GET)
    public void ssqy(HttpServletResponse response, String code) throws IOException {
        String message = getOpenid(code);
        JSONObject json = new JSONObject(message);
        if (Integer.parseInt(json.get("status").toString()) == 100) {
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/home/html/login.html?type=6&openid=" + json.get("msg").toString());
        } else if (Integer.parseInt(json.get("status").toString()) == 200) {
//			String json2 = json.get("data").toString();
//			String paramUrl = getParamUrl(json2, 6);
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/ssgg/html/teachers-sign.html");
        } else
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/health_service/404.html");
    }

    /**
     * 自我管理»家庭签约
     *
     * @param code
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "jtqy", method = RequestMethod.GET)
    public void jtqy(HttpServletResponse response, String code) throws IOException {
        String openid = getOpenidByCode(code);
        if (!StringUtils.isEmpty(openid)) {
            String doctor = null;
            Patient patient = patientService.findByOpenid(openid);
            if (patient != null) {
                doctor = familyContractService.checkPatient(patient.getCode());
            }
            if (StringUtils.isEmpty(doctor)) {
                response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/ssgg/html/choose-region.html?openid=" + openid);
            } else {
                response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/ssgg/html/doctor-home-page.html?openid=" + openid + "&doctor=" + doctor);
            }
        } else
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/health_service/404.html");
    }

    /**
     * 自我管理»健康指标
     *
     * @param code
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "jkzb", method = RequestMethod.GET)
    public void jkzb(HttpServletResponse response, String code) throws IOException {
        String message = getOpenid(code);
        JSONObject json = new JSONObject(message);
        if (Integer.parseInt(json.get("status").toString()) == 100) {
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/home/html/login.html?type=8&openid=" + json.get("msg").toString());
        } else if (Integer.parseInt(json.get("status").toString()) == 200) {
//			String json2 = json.get("data").toString();
//			String paramUrl = getParamUrl(json2, 8);
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/ssgg/html/health-index.html");
        } else
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/health_service/404.html");
    }

    /**
     * 自我管理»健康记录
     *
     * @param code
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "jkjl", method = RequestMethod.GET)
    public void jkjl(HttpServletResponse response, String code) throws IOException {
        String message = getOpenid(code);
        JSONObject json = new JSONObject(message);
        if (Integer.parseInt(json.get("status").toString()) == 100) {
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/home/html/login.html?type=9&openid=" + json.get("msg").toString());
        } else if (Integer.parseInt(json.get("status").toString()) == 200) {
//			String json2 = json.get("data").toString();
//			String paramUrl = getParamUrl(json2, 9);
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/ssgg/html/health-record.html");
        } else
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/health_service/404.html");
    }

    /**
     * 自我管理»健康知识
     *
     * @param code
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @RequestMapping(value = "jkzs", method = RequestMethod.GET)
    public void jkzs(HttpServletResponse response, String code) throws JSONException, IOException {
        String message = getOpenid(code);
        JSONObject json = new JSONObject(message);
        if (Integer.parseInt(json.get("status").toString()) == 100) {
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/home/html/login.html?type=10&openid=" + json.get("msg").toString());
        } else if (Integer.parseInt(json.get("status").toString()) == 200) {
//			String json2 = json.get("data").toString();
//			String paramUrl = getParamUrl(json2, 10);
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/html/ssgg/html/health-headline.html");
        } else
            response.sendRedirect(SystemConf.getInstance().getServerUrlStr() + "wx/health_service/404.html");
    }

}
