package com.cirle.scientific.service.impl;/*
 *24901
 *2022/12/21
 *11:08
 */

import com.cirle.scientific.dao.QkjMemberAppDao;
import com.cirle.scientific.dao.QkjPatientAppDao;
import com.cirle.scientific.po.QkjMemberApp;
import com.cirle.scientific.po.QkjPatientApp;
import com.cirle.scientific.service.PatientAppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 24901
 */
@Service
@Slf4j
public class PatientAppServiceImpl implements PatientAppService {

    private QkjPatientAppDao qkjPatientAppDao;

    private QkjMemberAppDao qkjMemberAppDao;

    @Autowired
    public void setQkjMemberAppDao(QkjMemberAppDao qkjMemberAppDao) {
        this.qkjMemberAppDao = qkjMemberAppDao;
    }

    @Autowired
    public void setQkjPatientAppDao(QkjPatientAppDao qkjPatientAppDao) {
        this.qkjPatientAppDao = qkjPatientAppDao;
    }

    @Override
    public QkjPatientApp findPatient(String memCard, Integer objType, Integer objId) {
//        qkjPatientAppDao.findAllByMemCard(memCard, objType, objId)
        return null;
    }

    @Override
    public Integer createPatient(QkjPatientApp qkjPatientApp) {
        try {
            QkjPatientApp qkjPatientApp2 = qkjPatientAppDao.findAllByMemCardAndObjTypeAndObjIdAndPhone(qkjPatientApp.getMemCard(), qkjPatientApp.getObjType(), qkjPatientApp.getObjId(), qkjPatientApp.getPhone());
            if(null != qkjPatientApp2){
                qkjPatientApp.setId(qkjPatientApp2.getId());
            }
            qkjPatientApp.setStatus(0);
            qkjPatientApp.setIsAccomplishQuestion(0);
            qkjPatientApp.setOneQuestionnaireScore(0);
            qkjPatientApp.setTowQuestionnaireScore(0);
            qkjPatientApp.setCreateTime(new Date());
            QkjPatientApp qkjPatientApp1 = qkjPatientAppDao.save(qkjPatientApp);
            return qkjPatientApp1.getId();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("创建用户信息报错 e->{e}", e);
            return 500;
        }
    }

    @Override
    public List<QkjPatientApp> findIsAccomplishQuestion(String doctorCodeMemCard, String firstday, String lastday) {
        return qkjPatientAppDao.findIsAccomplishQuestion(doctorCodeMemCard, firstday, lastday);
    }

    @Override
    public void updateIsAccomplishQuestion(String memCard, Integer patientId, Integer objType, Integer objId, Integer isAccomplishQuestion, Integer oneQuestionnaireScore, Integer towQuestionnaireScore) {
        try {
            QkjPatientApp qkjPatientApp = qkjPatientAppDao.findAllByMemCard(memCard, patientId, objType, objId);
            if(null != qkjPatientApp && oneQuestionnaireScore!= null){
                qkjPatientApp.setOneQuestionnaireScore(oneQuestionnaireScore);
            }
            if(null != qkjPatientApp && towQuestionnaireScore!= null){
                qkjPatientApp.setTowQuestionnaireScore(towQuestionnaireScore);
            }
            if(null != qkjPatientApp && isAccomplishQuestion!= null){
                qkjPatientApp.setIsAccomplishQuestion(isAccomplishQuestion);
                qkjPatientAppDao.save(qkjPatientApp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("修改用户问卷是否完成方法报错 e->{e}",e);
        }
    }

    @Override
    public QkjPatientApp findAllByMemCardAndName(String memCard, String patientName) {
        return qkjPatientAppDao.findAllByMemCardAndName(memCard, patientName);
    }

}
