package com.yihu.jw.hospital.appointment.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.entity.hospital.appointment.WlyyAppointmentDO;
import com.yihu.jw.entity.hospital.mapping.DoctorMappingDO;
import com.yihu.jw.hospital.appointment.dao.WlyyAppointmentDao;
import com.yihu.jw.hospital.mapping.service.DoctorMappingService;
import com.yihu.jw.hospital.mapping.service.PatientMappingService;
import com.yihu.jw.hospital.prescription.service.PrescriptionService;
import com.yihu.jw.hospital.prescription.service.entrance.RegisterService;
import com.yihu.jw.restmodel.hospital.register.RegisterAmVO;
import com.yihu.mysql.query.BaseJpaService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by Trick on 2019/10/29.
 */
@Service
@Transactional
public class AppointmentService extends BaseJpaService<WlyyAppointmentDO, WlyyAppointmentDao> {

    private static final Logger logger = LoggerFactory.getLogger(PrescriptionService.class);

    @Autowired
    private RegisterService registerService;
    @Autowired
    private DoctorMappingService doctorMappingService;
    @Autowired
    private PatientMappingService patientMappingService;
    @Autowired
    private WlyyAppointmentDao wlyyAppointmentDao;
    @Autowired
    private ObjectMapper objectMapper;

    @Value("${demo.flag}")
    private boolean demoFlag;

    /**
     * 获取医生号源
     * @param dept
     * @param docMappingCode
     * @param specialCode
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public JSONArray getDoctorSchedule(String dept, String docMappingCode,String nameKey,String specialCode,String chargeType,String startDate,String endDate)throws Exception{
        logger.info("getDoctorSchedule :");
        return registerService.BS10045(dept,docMappingCode,nameKey,specialCode,chargeType,startDate,endDate,demoFlag);
    }

    /**
     * 获取医生具体号源时间
     * @param dept
     * @param docMappingCode
     * @param date
     * @return
     * @throws Exception
     */
    public JSONArray getDoctorScheduleTime(String dept,String docMappingCode,String date,String ampm)throws Exception{
        logger.info("getDoctorScheduleTime :");
        return registerService.BS10046(dept,docMappingCode,date,ampm,demoFlag);
    }

    /**
     * 获取预约记录列表
     * @param idcard
     * @param doctor
     * @param doctorName
     * @param dept
     * @param startDate
     * @param endDate
     * @param chargeType
     * @return
     * @throws Exception
     */
    public JSONArray getAppointmentList(String idcard,String doctor,String doctorName,String dept,String startDate,String endDate,String chargeType)throws Exception{
        logger.info("getAppointmentList :");

        if(StringUtils.isBlank(startDate)){
            throw new RuntimeException("startDate is not null");
        }

        if(StringUtils.isBlank(endDate)){
            throw new RuntimeException("endDate is not null");
        }

        String doctorMappingId = null;
        if(StringUtils.isNotBlank(doctor)){
            DoctorMappingDO doctorMappingDO = doctorMappingService.findMappingCode(doctor,"350211A1002");
            if(doctorMappingDO!=null){
                doctorMappingId = doctorMappingDO.getMappingCode();
                logger.info("getAppointmentList doctorMappingId:"+doctorMappingId);
            }
        }

        return registerService.BS10047(idcard,doctorMappingId,doctorName,dept,startDate,endDate,chargeType,demoFlag);
    }

    /**
     * 预约
     * @param appointmentJson
     * @return
     * @throws Exception
     */
    public WlyyAppointmentDO makeAppointment(String appointmentJson)throws Exception{
        logger.info("getAppointmentList :");

        WlyyAppointmentDO wlyyAppointmentDO = objectMapper.readValue(appointmentJson,WlyyAppointmentDO.class);

        RegisterAmVO registerAmVO = objectMapper.readValue(appointmentJson,RegisterAmVO.class);


        if(StringUtils.isNotBlank(registerAmVO.getDoctor())){
            DoctorMappingDO doctorMappingDO = doctorMappingService.findMappingCode(registerAmVO.getDoctor(),"350211A1002");
            if(doctorMappingDO!=null){
                String doctorMappingId = doctorMappingDO.getMappingCode();
                if(StringUtils.isBlank(doctorMappingId)){
                    throw new RuntimeException("未找医生映射信息");
                }
                registerAmVO.setDoctor(doctorMappingId);
            }else{
                throw new RuntimeException("未找医生映射信息");
            }
        }else{
            throw new RuntimeException("医生信息为空");
        }


        net.sf.json.JSONObject res = registerService.BS10048(registerAmVO,demoFlag);

        if(res!=null){
            logger.info(res.toString());
            wlyyAppointmentDO.setStatus(1);
        }else{
            wlyyAppointmentDO.setStatus(0);
        }

        wlyyAppointmentDO.setCreateTime(new Date());
        wlyyAppointmentDO.setType(1);
        wlyyAppointmentDO.setHttpLog(res.toString());
        wlyyAppointmentDao.save(wlyyAppointmentDO);

        return wlyyAppointmentDO;
    }

    /**
     * 取消预约
     * @param appointmentJson
     * @return
     * @throws Exception
     */
    public net.sf.json.JSONObject cancelAppointment(String appointmentJson)throws Exception{

        RegisterAmVO registerAmVO = objectMapper.readValue(appointmentJson,RegisterAmVO.class);

        //查找医生映射id
        if(StringUtils.isNotBlank(registerAmVO.getDoctor())){
            DoctorMappingDO doctorMappingDO = doctorMappingService.findMappingCode(registerAmVO.getDoctor(),"350211A1002");
            if(doctorMappingDO!=null){
                String doctorMappingId = doctorMappingDO.getMappingCode();
                if(StringUtils.isBlank(doctorMappingId)){
                    throw new RuntimeException("未找医生映射信息");
                }
                registerAmVO.setDoctor(doctorMappingId);
            }else{
                throw new RuntimeException("未找医生映射信息");
            }
        }else{
            throw new RuntimeException("医生信息为空");
        }

        net.sf.json.JSONObject res = registerService.BS10049(registerAmVO,demoFlag);
        logger.info(res.toString());

        return  res;
    }

    /**
     * 根据映射id查询互联网医生ID
     * @param mappingCode
     * @return
     */
    public DoctorMappingDO getDoctorByMappingCode(String mappingCode){
        return doctorMappingService.findByOrgCodeAndMappingCode("350211A1002",mappingCode);
    }
}
