package thyyxxk.webserver.service.covid;

import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.covid.CovidDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.covid.CovidDetail;
import thyyxxk.webserver.entity.covid.CovidIndex;
import thyyxxk.webserver.entity.covid.Region;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.List;

/**
 * @author dj
 */
@Service
public class CovidService {
    private final CovidDao dao;
    private static final String NO_PATIENT_Id = ":patientId";

    public CovidService(CovidDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<CovidIndex>> getList(String patientId) {
        return ResultVoUtil.success(dao.getList(patientId));
    }

    public ResultVo<CovidDetail> getDetail(Integer id) {
        return ResultVoUtil.success(dao.getDetail(id));
    }

    public ResultVo<CovidDetail> getPatientInfo(String patientId) {
        return ResultVoUtil.success(dao.getPatientInfo(patientId));
    }

    public ResultVo<List<Region>> getRegionProvince() {
        return ResultVoUtil.success(dao.getRegionProvince());
    }

    public ResultVo<List<Region>> getChildRegion(Integer parentCode) {
        return ResultVoUtil.success(dao.getChildRegion(parentCode));
    }
 
    public ResultVo<String> submit(CovidDetail pojo) {
        if (null == pojo.getPatientId() || NO_PATIENT_Id.equals(pojo.getPatientId().trim())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未找到此患者的个人信息，请联系系统管理员！");
        }
        Integer dateDiff = dao.getDateDiff(pojo.getIdcard(), pojo.getPatientId());
        if (null == dateDiff || dateDiff > 0) {
            dao.insertNewCovid19Assessment(pojo);
            return ResultVoUtil.success("提交成功。");
        } else {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您今天已提交过此问卷，请明天再试。");
        }
    }
}
