package com.ywt.outpatient.service.web;

import com.ywt.biz.common.util.CacheUtil;
import com.ywt.gapi.ResultCode;
import com.ywt.gapi.doctor.*;
import com.ywt.outpatient.core.BaseResponse;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class DoctorSrv {
    private static Logger logger = LoggerFactory.getLogger(DoctorSrv.class);

    // 排班医生列表缓存键
    private final String DEPT_SCHEDULE_DOCTOR_LIST = "dept_schedule_doctor_list";

    @DubboReference
    private DoctorService doctorServiceBlockingStub;

    public List<TeamInfo> getTeamListByDeptId(int deptId) {
        GetTeamListByDeptIdRequest req = GetTeamListByDeptIdRequest.newBuilder()
                .setDeptId(deptId)
                .build();
        GetTeamListByDeptIdResponse res = doctorServiceBlockingStub.getTeamListByDeptId(req);

        if (res.getCode() == ResultCode.SUCCEED_VALUE) {
            return res.getTeamList();
        }

        return new LinkedList<>();
    }

    /**
     * 根据科室Id获取医生团队
     *
     * @param deptId
     * @return
     */
    public BaseResponse getTeamListByDeptIdRes(int deptId) {
        BaseResponse response = new BaseResponse();

        try {
            GetTeamListByDeptIdRequest req = GetTeamListByDeptIdRequest.newBuilder()
                    .setDeptId(deptId)
                    .build();
            GetTeamListByDeptIdResponse res = doctorServiceBlockingStub.getTeamListByDeptId(req);

            if (res.getCode() == ResultCode.SUCCEED_VALUE) {
                Map<String, Object> team;
                Map<String, Object> doctor;

                List<Map<String, Object>> doctorList = new LinkedList<>();
                List<Map<String, Object>> teamList = new LinkedList<>();

                for (TeamInfo info : res.getTeamList()) {
                    for (TeamDoctor teamDoctor : info.getTeamDoctorList()) {
                        doctor = new HashMap<>();
                        doctor.put("doctorId", teamDoctor.getDoctorId());
                        doctor.put("doctorName", teamDoctor.getName());
                        doctor.put("doctorTitle", teamDoctor.getTitle());
                        doctor.put("doctorAvatar", teamDoctor.getAvatar());
                        doctor.put("specialty", teamDoctor.getSpecialty());

                        doctorList.add(doctor);
                    }

                    team = new HashMap<>();
                    team.put("teamId", info.getTeamId());
                    team.put("teamName", info.getTeamName());
                    team.put("teamIntro", info.getTeamIntro());
                    team.put("doctorList", doctorList);

                    teamList.add(team);
                }

                Map<String, Object> data = new HashMap<>();
                data.put("list", teamList);
                return response.succeed(data);
            }

            return response.failed(BaseResponse.APP_ERROR, res.getMsg());
        } catch (Exception e) {
            logger.error("getTeamListByDeptId({}): {}", deptId, e.getMessage(), e);
            return response.error();
        }
    }

    public List<DeptScheduleDoctor> getDeptScheduleDoctorListFromCache() {
        try {
            return (List<DeptScheduleDoctor>) CacheUtil.getInstance().get(DEPT_SCHEDULE_DOCTOR_LIST,
                    () -> getDeptScheduleDoctorList());
        } catch (Exception e) {
            logger.error("getDeptScheduleDoctorListFromCache(): {}", e.getMessage(), e);
        }

        return new LinkedList<>();
    }

    public List<DeptScheduleDoctor> getDeptScheduleDoctorList() {
        GetDeptScheduleDoctorListRequest req = GetDeptScheduleDoctorListRequest.newBuilder().build();
        GetDeptScheduleDoctorListResponse res = doctorServiceBlockingStub.getDeptScheduleDoctorList(req);

        if (res.getCode() == ResultCode.SUCCEED_VALUE) {
            return res.getDeptDoctorList();
        }

        return new LinkedList<>();
    }
}
