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

import java.util.List;
import java.util.Map;

import com.yihu.wlyy.service.app.sign.FamilyContractService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
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 org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.service.app.account.DoctorInfoService;
import com.yihu.wlyy.service.app.team.DrHealthTeamService;
import com.yihu.wlyy.util.CommonUtil;
import com.yihu.wlyy.web.BaseController;

@Controller
@RequestMapping(value = "/doctor/team")
@Api(description = "医生签约团队")
public class SignTeamController extends BaseController {

    @Autowired
    private DoctorInfoService doctorInfoService;

    @Autowired
    private DrHealthTeamService drHealthTeamService;


    @Autowired
    private FamilyContractService familyContractService;

    /**
     * 查询医生所有的团队數目
     *
     * @return
     */
    @RequestMapping(value = "teamListCount", method = RequestMethod.GET)
    @ResponseBody
    public String teamListCount() {
        try {
            JSONObject json = new JSONObject();
            String doctorCode = super.getUID();
            Integer count = drHealthTeamService.teamListCountByDoctor(doctorCode);
            json.put("count", count);
            return write(200, "医生信息查询成功！", "data", json);
        } catch (Exception e) {
            error(e);
            return error(-1, "查询失败！");
        }
    }

    /**
     * 查询醫生所有的团队
     *
     * @return
     */
    @RequestMapping(value = "teamList", method = RequestMethod.GET)
    @ResponseBody
    public String teamList() {
        try {
            JSONObject json = new JSONObject();
            String doctorCode = super.getUID();
            json.put("count", drHealthTeamService.teamListByDoctorCode(doctorCode));
            return write(200, "医生信息查询成功！", "data", json);
        } catch (Exception e) {
            error(e);
            return error(-1, "查询失败！");
        }
    }

    /**
     * 查询指定医生的基本信息接口
     *
     * @param doctor 医生标识*
     * @param team   行政团队标识
     * @return
     */
    @RequestMapping(value = "info")
    @ResponseBody
    public String info(@RequestParam(required = true, value = "doctor") String doctor,
                       @RequestParam(required = false, value = "team") String team) {
        try {
            Doctor temp = doctorInfoService.findDoctorByCode(doctor);
            if (temp != null) {
                JSONObject json = new JSONObject();
                // 设置医生姓名
                json.put("name", temp.getName());
                // 设置医生头像
                json.put("photo", temp.getPhoto());
                // 设置医生性别
                json.put("sex", temp.getSex());
                // 设置省名称
                json.put("provinceName", temp.getProvinceName());
                // 设置城市名称
                json.put("cityName", temp.getCityName());
                // 设置医院名称
                json.put("hospitalName", temp.getHospitalName());
                // 设置部门名称
                json.put("deptName", temp.getDeptName());
                // 设置职称
                json.put("jobName", temp.getJobName());
                // 设置医生专长
                json.put("expertise", temp.getExpertise());
                // 设置医生简介
                json.put("introduce", temp.getIntroduce());
                // 二维码地址
                json.put("qrcode", temp.getQrcode());
                // 手机号
                json.put("mobile", temp.getMobile());
                // 级别
                json.put("level", temp.getLevel());
                if (!org.springframework.util.StringUtils.isEmpty(team)) {
                    //或者医生在某个行政团队的签约居民总数
                    Integer count = familyContractService.findPatientCountByAdminTeam(temp.getCode(), team);
                    // 级别
                    json.put("signCount", count);
                }
                return write(200, "医生信息查询成功！", "data", json);
            } else {
                return error(-1, "医生信息查询失败！");
            }
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "患者信息查询失败！");
        }
    }


    /**
     * 查询团队中健康管理师或全科医生
     *
     * @param type
     * @return
     */
    @RequestMapping(value = "doctors", method = RequestMethod.GET)
    @ResponseBody
    public String getTeamDoctors(
            String type,
            Integer page,
            Integer pageSize) {
        try {
            List<Object> doctors = drHealthTeamService.findTeamDoctors(getUID(), type, page, pageSize);

            return write(200, "查询成功！", "data", new JSONArray(doctors));
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "查询失败！");
        }
    }

    /**
     * 查询医生团队中的某个类别医生
     *
     * @param type
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "teamdoctors", method = RequestMethod.GET)
    @ResponseBody
    public String getTeamDoctorsByType(String type, Integer page,
                                       Integer pageSize) {
        try {
            List<Object> doctors = drHealthTeamService.findTeamDoctorsByType(getUID(), type, page, pageSize);
            return write(200, "查询成功！", "data", new JSONArray(doctors));
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "查询失败！");
        }
    }

    /**
     * 求助页面需要的医生查找
     *
     * @return
     */
    @RequestMapping(value = "teamDoctorsAndAdminTeamDoctors", method = RequestMethod.GET)
    @ResponseBody
    public String getTeamDoctorsAndAdminTeamDoctors(Long teamId) {
        try {
            Map<String, List<Doctor>> doctors = drHealthTeamService.getTeamDoctorsAndAdminTeamDoctors("64de930c-5b15-11e6-8344-fa163e8aee56", teamId);
            JSONObject jo = new JSONObject();
            jo.put("zr", addDoctor(doctors.get("zr")));
            jo.put("team", addDoctor(doctors.get("team")));
            return write(200, "查询成功！", "data", jo);
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "查询失败！");
        }
    }

    private JSONArray addDoctor(List<Doctor> zr) {
        JSONArray array = new JSONArray();
        for (Doctor doctor : zr) {
            if (doctor == null) {
                continue;
            }
            JSONObject json = new JSONObject();
            json.put("id", doctor.getId());
            // 医生标识
            json.put("code", doctor.getCode());
            // 医生姓名
            json.put("name", doctor.getName());
            // 所在医院名称
            json.put("hospital_name", doctor.getHospitalName());
            // 科室名称
            json.put("dept_name", StringUtils.isEmpty(doctor.getDeptName()) ? " " : doctor.getDeptName());
            // 职称名称
            json.put("job_name", StringUtils.isEmpty(doctor.getJobName()) ? " " : doctor.getJobName());
            // 头像
            json.put("photo", doctor.getPhoto());
            // 简介
            json.put("introduce", doctor.getIntroduce());
            // 专长
            json.put("expertise", doctor.getExpertise());
            // 是否求出
            json.put("isHelp", doctor.getIsHelp());
            array.put(json);
        }

        return array;
    }
}
