package com.example.service;
import com.example.bean.Drugs;
import com.example.bean.MedicalRecord;
import com.example.mapper.MedicalRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.mapper.DrugsMapper;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.Timestamp;
import java.util.List;

@Service
public class MedicalRecordService {
    @Autowired
    private MedicalRecordMapper medicalRecordMapper;
    @Autowired
    private DrugsMapper drugsMapper;

    public int addNewMedicalRecord(@RequestBody MedicalRecord medicalRecord) {
        medicalRecord.setRecordtime(new Timestamp(System.currentTimeMillis()));//设置当前时间
        int x = medicalRecordMapper.addNewMedicalRecord(medicalRecord);
        return x;
        /*List<Drugs> drugs= medicalRecord.getDrugs();
        for(int i=0;i<drugs.size();i++){
            Drugs drug = drugs.get(i);
            int did = drug.getId();
            int mid = medicalRecord.getMid();
            drugsMapper.savaDrugstoMedicalRecord(mid,did);
        }*/
    }

    public int addDrugs(Drugs drugs){//这个方法中，drugs的id字段用来存放对应病历的id
        int mid = drugs.getId();
        int result1 = drugsMapper.addDrugs(drugs);
        int did = drugs.getId();
        System.out.println(mid + " " + did);
        //将病历加到联系表中
        int result2 = drugsMapper.savaDrugstoMedicalRecord(mid,did);
        return result1 + result2;
    }

    private int addDrugsToMedicalRecord(int[] drugsid,int mid) {
        drugsMapper.deleteDrugsById(mid);
        for(int i=0;i<drugsid.length;i++){
            int did = drugsid[i];
            drugsMapper.savaDrugstoMedicalRecord(mid,did);
        }
        return 1 ;
    }

    public List<MedicalRecord> selectByUserid(String userid)
    {
        return medicalRecordMapper.selectMedicalRecordByUserId(userid);
    }

    public List<MedicalRecord> selectByDid(String did)
    {
        return medicalRecordMapper.selectMedicalRecordByDid(did);
    }

    public MedicalRecord getMedicalRecordByMid(int mid){
        MedicalRecord medicalRecord = medicalRecordMapper.getMedicalRecordById(mid);
        return medicalRecord;
    }


    public int getSummaryByUid(String uid){
        return medicalRecordMapper.getSummaryByUid(uid);
    }

    public int updateMedicalRecord(MedicalRecord medicalRecord){
        return medicalRecordMapper.updateMedicalRecord(medicalRecord);
    }

    public int deleteMedicalRecordByMid(int mid){//删除相应的病历和处方
        List<Drugs> drugsByMid = drugsMapper.getDrugsByMid(mid);//获取与该病历相关的处方
        drugsMapper.deleteDurgsRelation(mid);//删除联系表里的相关记录
        for (Drugs drugs : drugsByMid) {//将每个处方删除
            drugsMapper.deleteDrugsById(drugs.getId());
        }
        return medicalRecordMapper.deleteMedicalRecordByMid(mid);//最后删除该病历
    }

    public int deleteDrugs(int did)
    {
        drugsMapper.deleteDrugsById(did);
        int result = drugsMapper.deleteDrugsRelationSingle(did);
        return result;
    }

    public List<Drugs> getDrugsById(int mid)
    {
        return drugsMapper.getDrugsByMid(mid);
    }
}
