package com.yihu.jw.internet.service;

import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.doctor.dao.BaseDoctorHospitalDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.entity.hospital.mapping.PatientMappingDO;
import com.yihu.jw.entity.hospital.prescription.*;
import com.yihu.jw.hospital.mapping.dao.PatientMappingDao;
import com.yihu.jw.hospital.prescription.dao.*;
import com.yihu.jw.internet.dao.IntUpErrorLogDao;
import com.yihu.jw.internet.entity.InternetUpErrorLogDO;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.util.date.DateUtil;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Trick on 2019/10/25.
 */
@Service
@Transactional
public class DataGeneratorService extends BaseJpaService<InternetUpErrorLogDO, IntUpErrorLogDao> {

    @Autowired
    private OutpatientDao outpatientDao;
    @Autowired
    private PrescriptionDao prescriptionDao;
    @Autowired
    private PrescriptionInfoDao prescriptionInfoDao;
    @Autowired
    private WlyyInspectionDao wlyyInspectionDao;
    @Autowired
    private PatientRegisterDao patientRegisterDao;
    @Autowired
    private PatientMappingDao patientMappingDao;
    @Autowired
    private BaseDoctorDao baseDoctorDao;
    @Autowired
    private BaseDoctorHospitalDao baseDoctorHospitalDao;
    @Autowired
    private BasePatientDao basePatientDao;

    /**
     * 生成数据，测试监管平台数据上传
     * @param doctor 医生id
     * @param date 生成日期，“yyyy-MM-dd”
     * @return
     */
    public List<WlyyOutpatientDO> setData(String doctor,String date){

        List<WlyyOutpatientDO> outpatientDOs = new ArrayList<>();
        //居民门诊记录数据源
        Iterable<PatientMappingDO> patientMappingDOs = patientMappingDao.findAll();
        Iterator it = patientMappingDOs.iterator();
        //医生信息数据源
        BaseDoctorDO doctorDO = baseDoctorDao.findOne(doctor);
        //部门信息
        BaseDoctorHospitalDO hospitalDO = baseDoctorHospitalDao.findByDoctorCode(doctor).get(0);

        while (it.hasNext()){
            PatientMappingDO patientMappingDO = (PatientMappingDO)it.next();

            BasePatientDO patientDO = basePatientDao.findById(patientMappingDO.getPatient());

            if(patientDO!=null){
                //1.生成门诊记录
                WlyyOutpatientDO wlyyOutpatientDO = getOutPatient(patientDO,doctorDO,hospitalDO,date);

                //2.生成处方记录
                WlyyPrescriptionDO prescriptionDO = getPrescription(wlyyOutpatientDO,date);

                //3.生成药品
                getPreInfo(prescriptionDO,date);

                //4.生成检查检验
                getInspection(prescriptionDO,date);

                //5.门诊预约
                getRegisterDO(wlyyOutpatientDO,date);

                outpatientDOs.add(wlyyOutpatientDO);
            }
        }

        return outpatientDOs;
    }

    public List<WlyyOutpatientDO> delData(String doctor,String date){

        //居民门诊记录数据源
        Iterable<PatientMappingDO> patientMappingDOs = patientMappingDao.findAll();
        Iterator it = patientMappingDOs.iterator();

        //遍历居民删除预约记录
        while (it.hasNext()) {
            PatientMappingDO patientMappingDO = (PatientMappingDO) it.next();
            List<WlyyPatientRegisterDO> registerDOs = patientRegisterDao.findByPatientAndCreateTimeAndDel(patientMappingDO.getPatient(),DateUtil.strToDate(date+" 08:00:00"),1);
            if(registerDOs!=null&&registerDOs.size()>0){
                patientRegisterDao.delete(registerDOs);
            }
        }

        //删除复诊
        List<WlyyOutpatientDO> outpatientDOs = outpatientDao.findByDoctorAndCreateTimeAndPatientCancelRemark(doctor,DateUtil.strToDate(date+" 09:30:00"),"test_info");
        if(outpatientDOs!=null&&outpatientDOs.size()>0){
            outpatientDao.delete(outpatientDOs);
        }

        //删除复诊
        List<WlyyPrescriptionDO> prescriptionDOs = prescriptionDao.findByDoctorAndCreateTimeAndRemark(doctor,DateUtil.strToDate(date+" 10:00:00"),"test_info");
        if(prescriptionDOs!=null&&prescriptionDOs.size()>0){
            for(WlyyPrescriptionDO prescriptionDO :prescriptionDOs){
                List<WlyyPrescriptionInfoDO> infoDOs = prescriptionInfoDao.findByPrescriptionId(prescriptionDO.getId(),1);
                if(infoDOs!=null&&infoDOs.size()>0){
                    prescriptionInfoDao.delete(infoDOs);
                }
                List<WlyyInspectionDO> inspectionDOs = wlyyInspectionDao.findByPrescriptionId(prescriptionDO.getId(),1);
                if(inspectionDOs!=null&&inspectionDOs.size()>0){
                    wlyyInspectionDao.delete(inspectionDOs);
                }
            }
            prescriptionDao.delete(prescriptionDOs);
        }

        return outpatientDOs;
    }

    /**
     * 生成门诊记录
     * @param date
     * @return
     */
    public WlyyOutpatientDO getOutPatient(BasePatientDO patientDO,BaseDoctorDO doctorDO,BaseDoctorHospitalDO hospitalDO,String date){
        WlyyOutpatientDO outpatientDO = new WlyyOutpatientDO();

        outpatientDO.setStatus("2");

        outpatientDO.setPatient(patientDO.getId());
        outpatientDO.setPatientName(patientDO.getName());

        outpatientDO.setMobile(patientDO.getMobile());
        String idcard = patientDO.getIdcard();
        outpatientDO.setIdcard(idcard);
        //模拟社保卡，并非正式
        if(StringUtils.isNotBlank(idcard)){
            outpatientDO.setCardNo("D"+ idcard.substring(idcard.length()-8,idcard.length()));
        }

        outpatientDO.setDoctor(doctorDO.getId());
        outpatientDO.setDoctorName(doctorDO.getName());
        outpatientDO.setDept(hospitalDO.getDeptCode());
        outpatientDO.setDeptName(hospitalDO.getDeptName());
        outpatientDO.setHospital(hospitalDO.getOrgCode());
        outpatientDO.setHospitalName(hospitalDO.getOrgName());
        outpatientDO.setOutpatientType("1");
        outpatientDO.setDescription("头晕，没有食欲");

        outpatientDO.setCreateTime(DateUtil.strToDate(date+" 09:30:00"));
        outpatientDO.setConDate(DateUtil.strToDate(date+" 09:30:00"));
        outpatientDO.setAdmDate(DateUtil.strToDate(date+" 09:30:00"));
        outpatientDO.setEndTime(DateUtil.strToDate(date+" 11:30:00"));
        outpatientDO.setRegisterDate(DateUtil.strToDate(date+" 09:20:00"));

        outpatientDO.setType("1");
        outpatientDO.setMjz("mz");
        outpatientDO.setWinNo("6");
        outpatientDO.setIcd10("ZZGX71");
        outpatientDO.setIcd10Name("肝血瘀阻证");

        outpatientDO.setRegisterNo(getCode().substring(0,15));
        outpatientDO.setConNo(getCode().substring(0,15));

        outpatientDO.setPatientCancelRemark("test_info");
        return outpatientDao.save(outpatientDO);
    }

    public WlyyPrescriptionDO getPrescription(WlyyOutpatientDO outpatient,String date){

        WlyyPrescriptionDO prescriptionDO = new WlyyPrescriptionDO();

        prescriptionDO.setOutpatientId(outpatient.getId());
        prescriptionDO.setStatus(100);

        prescriptionDO.setPatientCode(outpatient.getPatient());
        prescriptionDO.setPatientName(outpatient.getPatientName());
        prescriptionDO.setIdcard(outpatient.getIdcard());

        prescriptionDO.setDept(outpatient.getDept());
        prescriptionDO.setDeptName(outpatient.getDeptName());
        prescriptionDO.setDoctor(outpatient.getDoctor());
        prescriptionDO.setDoctorName(outpatient.getDoctorName());
        prescriptionDO.setIdcard(outpatient.getIdcard());
        prescriptionDO.setHospital(outpatient.getHospital());
        prescriptionDO.setHospitalName(outpatient.getHospitalName());

        prescriptionDO.setType(1);
        prescriptionDO.setCreateTime(DateUtil.strToDate(date+" 10:00:00"));

        prescriptionDO.setRemark("test_info");

        return prescriptionDao.save(prescriptionDO);
    }

    public WlyyPrescriptionInfoDO getPreInfo(WlyyPrescriptionDO prescriptionDO,String date){
        /**
         * INSERT INTO `base`.`wlyy_prescription_info`
         * (`id`, `prescription_id`, `drug_no`, `drug_name`,
         * `disp_deposite`, `dosage`, `quantity`, `unit`, `unit_name`,
         * `pack_unit`, `pack_unit_name`, `usage_code`, `usage_name`,
         * `supply_code`, `supply_name`, `days`, `frequency`, `serial`,
         * `group_no`, `specification`, `pack_retprice`, `herbal_count`,
         * `post_count`, `del`) VALUES
         * ('2c9081aa6dfbce6e016dfc634a15002d', '2c9081aa6dfbce6e016dfc6309ad001c',
         * '08269 ', '聚乙二醇干扰素a-2b注射液', NULL,
         * '1', '1', '03', '支', '03', '支', '6id',
         * '每日6次', '001', '口服', '1', '2', '01', '71',
         * '180ug 0.5ml/支', '824.0000', '', '', '1');
         */
        WlyyPrescriptionInfoDO info = new WlyyPrescriptionInfoDO();
        info.setPrescriptionId(prescriptionDO.getId());
        info.setDrugNo("08269");
        info.setDrugName("聚乙二醇干扰素a-2b注射液");
        info.setDispDeposite("");
        info.setDosage("1");
        info.setQuantity("1");
        info.setUnit("03");
        info.setUnitName("支");
        info.setPackUnit("03");
        info.setPackUnitName("支");
        info.setUsageCode("6id");
        info.setUsageName("每日6次");
        info.setSupplyCode("001");
        info.setSupplyName("口服");
        info.setDays("1");
        info.setFrequency("1");
        info.setSerial("01");
        info.setGroupNo("71");
        info.setSpecification("180ug 0.5ml/支");
        info.setPackRetprice(824.0000);
        info.setDel(1);
        return prescriptionInfoDao.save(info);
    }

    public WlyyInspectionDO getInspection(WlyyPrescriptionDO prescriptionDO,String date){
        /**
         * INSERT INTO `base`.`wlyy_inspection` (`id`, `outpatient_id`,
         * `prescription_id`, `code`, `name`, `parent_code`,
         * `parent_name`, `dept`, `dept_name`, `quantity`,
         * `frequency`, `group_name`, `information`,
         * `check_equip`, `check_part`, `check_part_name`,
         * `adresult`, `goal`, `opview`, `specimen_name`,
         * `specimen_num`, `ywjc_result`, `ywjc_date`, `blzd`,
         * `fixationfluid`, `checkbox`, `mzpg`, `sfzg`, `create_time`)
         * VALUES ('2c9081aa6dfbce6e016dfc634a22002f',
         * '2c9081aa6dfbce6e016dfc6064280015',
         * '2c9081aa6dfbce6e016dfc6309ad001c', '360225',
         * '尿乳糜定性检查', NULL, NULL, '2160000', '',
         * '1', '1', '31', '23', '2', '1',
         * '头颅', '312', '321', '', '', '', '',
         * '', '', '', '1', '0', '0', '2019-10-24 14:10:29');
         */
        WlyyInspectionDO inspectionDO = new WlyyInspectionDO();
        inspectionDO.setOutpatientId(prescriptionDO.getOutpatientId());
        inspectionDO.setPrescriptionId(prescriptionDO.getId());
        inspectionDO.setCode("360225");
        inspectionDO.setName("尿乳糜定性检查");
        inspectionDO.setDept("2160000");
        inspectionDO.setDeptName("检验科");
        inspectionDO.setQuantity(1);
        inspectionDO.setFrequency("1");
        inspectionDO.setGroupName("31");
        inspectionDO.setInformation("23");
        inspectionDO.setCheckEquip("2");
        inspectionDO.setCheckPart("1");
        inspectionDO.setCheckPartName("头颅");
        inspectionDO.setAdresult("312");
        inspectionDO.setGoal("321");
        inspectionDO.setSfzg("0");
        inspectionDO.setMzpg("0");
        inspectionDO.setCheckbox("1");
        inspectionDO.setCreateTime(DateUtil.strToDate(date+" 09:30:00"));

        return wlyyInspectionDao.save(inspectionDO);
    }

    public WlyyPatientRegisterDO getRegisterDO(WlyyOutpatientDO outpatient,String date){
        WlyyPatientRegisterDO registerDO = new WlyyPatientRegisterDO();

        registerDO.setPatient(outpatient.getPatient());
        registerDO.setPatientName(outpatient.getPatientName());
        registerDO.setDept(outpatient.getDept());
        registerDO.setDeptName(outpatient.getDeptName());
        registerDO.setDoctor(outpatient.getDoctor());
        registerDO.setDoctorName(outpatient.getDoctorName());

        registerDO.setDate(date);
        registerDO.setRegisterNo(outpatient.getRegisterNo());
        registerDO.setConNo(outpatient.getConNo());
        registerDO.setCreateTime(DateUtil.strToDate(date+" 08:00:00"));

        return patientRegisterDao.save(registerDO);
    }
}
