package com.yihu.figure.service.drug;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.figure.dao.drug.MedicineDetailDao;
import com.yihu.figure.dao.drug.MedicineRecordDao;
import com.yihu.figure.dao.patient.PatientInfoDao;
import com.yihu.figure.model.drug.MedicineDetail;
import com.yihu.figure.model.drug.MedicineRecord;
import com.yihu.figure.model.patient.PatientInfo;
import com.yihu.figure.service.BaseService;
import com.yihu.figure.util.DateUtil;
import com.yihu.figure.util.HttpUtil;
import com.yihu.figure.util.MapUtill;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by yeshijie on 2017/3/13.
 */
@Component
@Transactional(rollbackOn = Exception.class)
public class MedicineRecordService extends BaseService{

    @Autowired
    private MedicineRecordDao medicineRecordDao;
    @Autowired
    private MedicineDetailDao medicineDetailDao;
    @Autowired
    private PatientInfoDao patientInfoDao;

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 获取基位用药记录
     */
    public void getJwDrugList() throws Exception{
        List<PatientInfo> patientInfoList = patientInfoDao.findAllPatient();
        List<MedicineDetail> medicineDetailList = new ArrayList<>();
        List<MedicineRecord> medicineRecordList = new ArrayList<>();
        MedicineDetail detail = null;
        MedicineRecord record = null;
        for (PatientInfo patientInfo:patientInfoList){
            String drugs = getJwDrugList(patientInfo.getSsc());
            System.out.println(drugs);
            if(!StringUtils.isEmpty(drugs)){
                JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Map.class);
                List<Map<String,Object>> list = objectMapper.readValue(drugs,javaType);
                if(list!=null&&list.size()>0){
                    for (Map<String, Object> map:list){
                        String XMAN_ID = MapUtill.getMapString(map,"XMAN_ID");
                        String EVENT = MapUtill.getMapString(map,"EVENT");
                        Integer ORG_ID = MapUtill.getMapInt(map,"ORG_ID");
                        Integer TYPE = MapUtill.getMapInt(map,"TYPE");
                        Integer SERIAL = MapUtill.getMapInt(map,"SERIAL");
                        String END_TIME = MapUtill.getMapString(map,"END_TIME");
                        String CATALOG_CODE = MapUtill.getMapString(map,"CATALOG_CODE");
                        String EHR_COMMIT_TIME = MapUtill.getMapString(map,"EHR_COMMIT_TIME");
                        String UNIONSSID = MapUtill.getMapString(map,"UNIONSSID");
                        String ORG_NAME = MapUtill.getMapString(map,"ORG_NAME");

                        record = new MedicineRecord();
                        record.setCzrq(new Date());
                        record.setCode(getCode());
                        record.setCatalogCode(CATALOG_CODE);
                        record.setEhrCommitTime(DateUtil.strToDate(EHR_COMMIT_TIME));
                        record.setEndTime(DateUtil.strToDate(END_TIME));
                        record.setEvent(EVENT);
                        record.setOrgId(ORG_ID);
                        record.setOrgName(ORG_NAME);
                        record.setPatient(patientInfo.getCode());
                        record.setSerial(SERIAL+"");
                        record.setType(TYPE+"");
                        record.setUnionssid(UNIONSSID);
                        record.setXmanId(XMAN_ID);
                        medicineRecordList.add(record);

                        if(!"[]".equals(MapUtill.getMapString(map,"MEDICINE"))){
                            List<Map<String, Object>> detailList = (List<Map<String, Object>>)map.get("MEDICINE");
                            if(detailList!=null&&detailList.size()>0){
                                for (Map<String, Object> drug:detailList){
                                    String MEDICINE_NAME = MapUtill.getMapString(drug,"MEDICINE_NAME");
                                    String MEDICINE_CODE = MapUtill.getMapString(drug,"MEDICINE_CODE");
                                    if(!StringUtils.isEmpty(MEDICINE_NAME)){
                                        detail = new MedicineDetail();
                                        detail.setCzrq(new Date());
                                        detail.setMedicineCode(MEDICINE_CODE);
                                        detail.setMedicineName(MEDICINE_NAME);
                                        detail.setMedicineRecordCode(record.getCode());
                                        medicineDetailList.add(detail);
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
        if(medicineDetailList.size()>0){
            medicineDetailDao.save(medicineDetailList);
        }
        if(medicineRecordList.size()>0){
            medicineRecordDao.save(medicineRecordList);
        }
    }


    private static String getJwDrugList(String strSSID){
        String drugUrl = "http://www.xmtyw.cn/wlyy/wlyy_service/record/drug";
        String params = "strSSID="+strSSID+"&startNum=1&endNum=100";
        String userAgent = "\"uid\":\"5fa5e88f7a4111e69f7c005056850d66\",\"doctorType\":3,\"name\":\"丘新花\",\"photo\":\"../../../images/d-male.png\",\"id\":2807,\"hospital\":\"ac02a9b87a3611e69f7c005056850d66\",\"userRole\":[{\"code\":\"350200\",\"name\":\"厦门市卫生与计划生育委员会\",\"areas\":\"350200\"}],\"token\":\"bdbe1d142e330a0e060f70489e383e86\"";
        String re = HttpUtil.sendGet(drugUrl,params,userAgent);
        JSONObject json = new JSONObject(re);
        int status = json.getInt("status");
        if(status==200){
            return json.getString("data");
        }
        return re;
    }

    public static void main(String[] args) throws Exception{
        String ssc = "D24500557";
        String drugs = getJwDrugList(ssc);
        ObjectMapper objectMapper = new ObjectMapper();
        try{
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Map.class);
            List<Map<String,Object>> list = objectMapper.readValue(drugs,javaType);
            if(list!=null&&list.size()>0){
                for (Map<String, Object> map:list){
                    String XMAN_ID = MapUtill.getMapString(map,"XMAN_ID");
                    String EVENT = MapUtill.getMapString(map,"EVENT");
                    Integer ORG_ID = MapUtill.getMapInt(map,"ORG_ID");
                    Integer TYPE = MapUtill.getMapInt(map,"TYPE");
                    Integer SERIAL = MapUtill.getMapInt(map,"SERIAL");
                    String END_TIME = MapUtill.getMapString(map,"END_TIME");
                    String CATALOG_CODE = MapUtill.getMapString(map,"CATALOG_CODE");
                    String EHR_COMMIT_TIME = MapUtill.getMapString(map,"EHR_COMMIT_TIME");
                    String UNIONSSID = MapUtill.getMapString(map,"UNIONSSID");
                    String ORG_NAME = MapUtill.getMapString(map,"ORG_NAME");
                    if(!"[]".equals(MapUtill.getMapString(map,"MEDICINE"))){
                        List<Map<String, Object>> detailList = (List<Map<String, Object>>)map.get("MEDICINE");
                        if(detailList!=null&&detailList.size()>0){
                            for (Map<String, Object> drug:detailList){
                                String MEDICINE_NAME = MapUtill.getMapString(map,"MEDICINE_NAME");
                                String MEDICINE_CODE = MapUtill.getMapString(map,"MEDICINE_CODE");
                            }
                        }
                        System.out.println(detailList.size());
                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
