package com.docus.server.message.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.docus.infrastructure.core.db.dao.impl.BaseDaoImpl;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.message.dao.TrInpatientOutpatientDao;
import com.docus.server.message.entity.TrInpatientOutpatient;
import com.docus.server.message.mapper.TrInpatientOutpatientMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author wyb
 */
@Repository
public class TrInpatientOutpatientDaoImpl extends BaseDaoImpl<TrInpatientOutpatientMapper, TrInpatientOutpatient> implements TrInpatientOutpatientDao {
    @Resource
    private IdService idService;

    @Override
    public void insertOrUpdateTrInpatientOutpatient(List<TrInpatientOutpatient> trInpatientOutpatientList) {
        for (TrInpatientOutpatient tr : trInpatientOutpatientList) {
            if (Objects.isNull(tr.getId())) {
                tr.setId(idService.getDateSeq());
            }
        }
        saveOrUpdateBatch(trInpatientOutpatientList, 1000);
    }

    @Override
    public void deleteTrInpatientOutpatientByInpatientId(String inpatientId) {
        LambdaQueryWrapper<TrInpatientOutpatient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TrInpatientOutpatient::getPatientId, inpatientId);
        baseMapper.delete(wrapper);
    }

    @Override
    public boolean exists(String inpatientId) {
        LambdaQueryWrapper<TrInpatientOutpatient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TrInpatientOutpatient::getPatientId, inpatientId);
        wrapper.last(" LIMIT 1");
        return   baseMapper.selectCount(wrapper)>1;
    }
}
