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

import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.entity.base.patient.PatientMedicareCardDO;
import com.yihu.jw.entity.hospital.mapping.PatientMappingDO;
import com.yihu.jw.hospital.mapping.dao.PatientMappingDao;
import com.yihu.jw.hospital.prescription.service.entrance.EntranceService;
import com.yihu.jw.hospital.prescription.service.entrance.YkyyEntranceService;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.patient.dao.BasePatientMedicareCardDao;
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/5/27.
 * 互联网医院居民信息映射
 */
@Service
@Transactional
public class PatientMappingService {

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

    @Autowired
    private PatientMappingDao patientMappingDao;
    @Autowired
    private BasePatientDao basePatientDao;
    @Autowired
    private YkyyEntranceService ykyyEntranceService;
    @Autowired
    private EntranceService entranceService;
    @Autowired
    private BasePatientMedicareCardDao patientMedicareCardDao;

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

    public String findHisPatNoByIdCard(String idCard)throws Exception{
        PatientMappingDO patientMappingDO = patientMappingDao.findByIdcardAndSource(idCard,"1");
        if(patientMappingDO!=null){
            return patientMappingDO.getMappingCode();
        }
        JSONArray rs = entranceService.BS15018(null,idCard,demoFlag);
        if(rs!=null&&rs.size()>0){

            //获取居民信息
            JSONObject json = rs.getJSONObject(0);
            String mappingCode = json.getString("PAT_NO");
            BasePatientDO patientDO =  basePatientDao.findByIdcardAndDel(idCard,"1");

            //存储对应映射关系
            PatientMappingDO mappingDO = new PatientMappingDO();
            mappingDO.setSource("1");
            mappingDO.setPatientName(patientDO.getName());
            mappingDO.setPatient(patientDO.getId());
            mappingDO.setMappingCode(mappingCode);
            mappingDO.setCreateTime(new Date());
            mappingDO.setIdcard(patientDO.getIdcard());
            patientMappingDao.save(mappingDO);

            return mappingCode;
        }else{
            throw new RuntimeException("获取his居民信息失败！");
        }
    }

    /**
     * 眼科医院
     * @param patient
     * @return
     * @throws Exception
     */
    public String findYkyyPatNoByPatient(String patient,boolean demoFlag)throws Exception{
        logger.info("findYkyyPatNoByPatient:"+patient);
        BasePatientDO patientDO = basePatientDao.findById(patient);
        if(patientDO == null){
            return patient;
        }
        logger.info("patientDO:"+patientDO.toString());
        PatientMappingDO patientMappingDO = patientMappingDao.findByIdcardAndSource(patientDO.getIdcard(),"1");
        PatientMedicareCardDO patientMedicareCardDO = patientMedicareCardDao.findByTypeAndPatientCodeAndDel("A_01",patient,"1");
        if(patientMappingDO!=null){
            return patientMappingDO.getMappingCode();
        }
        com.alibaba.fastjson.JSONArray rs = ykyyEntranceService.findHisPatientBySscAndIdcard(patientMedicareCardDO.getCode(),patientDO.getIdcard(),demoFlag);
        if(rs!=null&&rs.size()>0){

            //获取居民信息
            com.alibaba.fastjson.JSONObject json = rs.getJSONObject(0);
            String mappingCode = json.getString("brid").trim();

            //存储对应映射关系
            PatientMappingDO mappingDO = new PatientMappingDO();
            mappingDO.setSource("1");
            mappingDO.setPatientName(patientDO.getName());
            mappingDO.setPatient(patientDO.getId());
            mappingDO.setMappingCode(mappingCode);
            mappingDO.setCreateTime(new Date());
            mappingDO.setIdcard(patientDO.getIdcard());
            patientMappingDao.save(mappingDO);

            return mappingCode;
        }else{
            return null;
        }
    }

    public String findHisPatNoByPatient(String patient,String cardNo)throws Exception{
        logger.info("findHisPatNoByPatient:"+patient);
        BasePatientDO patientDO = basePatientDao.findById(patient);
        logger.info("patientDO:"+patientDO.toString());
        JSONArray rs = entranceService.BS15018(patientDO.getIdcard(),null,demoFlag);
        PatientMappingDO patientMappingDO = patientMappingDao.findByIdcardAndSource(patientDO.getIdcard(),"1");
        if(rs!=null&&rs.size()>0){
            for (int i=0;i<rs.size();i++){
                //获取居民信息
                JSONObject json = rs.getJSONObject(i);
                if (cardNo.equalsIgnoreCase(json.getString("CARD_NO").trim())){
                    String mappingCode = json.getString("PAT_NO").trim();
                    if (patientMappingDO!=null){
                        patientMappingDO.setMappingCode(mappingCode);
                        patientMappingDO = patientMappingDao.save(patientMappingDO);
                    }else {
                        //存储对应映射关系
                        PatientMappingDO mappingDO = new PatientMappingDO();
                        mappingDO.setSource("1");
                        mappingDO.setPatientName(patientDO.getName());
                        mappingDO.setPatient(patientDO.getId());
                        mappingDO.setMappingCode(mappingCode);
                        mappingDO.setCreateTime(new Date());
                        mappingDO.setIdcard(patientDO.getIdcard());
                        patientMappingDO = patientMappingDao.save(mappingDO);
                    }

                }

            }
            return patientMappingDO.getMappingCode();
        }else{
            return null;
        }
    }


    public PatientMappingDO findPatientNoByPatientCode(String code){
        PatientMappingDO patientMappingDO = patientMappingDao.findByPatient(code);
        return patientMappingDO;
    }
    public PatientMappingDO findPatientNoByPatientCodeOrIdcard(String code,String idCard){
        if (StringUtils.isNotBlank(code)){
            PatientMappingDO patientMappingDO = patientMappingDao.findByPatient(code);
            return patientMappingDO;
        }else if (StringUtils.isNoneBlank(idCard)){
            PatientMappingDO patientMappingDO =  patientMappingDao.findByIdcard(idCard);
            return patientMappingDO;
        }
        return null;
    }
}
