100 lines
3.3 KiB
Java
100 lines
3.3 KiB
Java
package com.example.survey.dao.impl;
|
|
|
|
import com.example.survey.dao.RespondentDao;
|
|
import com.example.survey.entity.Respondent;
|
|
import com.example.survey.entity.inner.AdministrativeArea;
|
|
import com.example.survey.exception.RespondentException;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author Pope
|
|
*/
|
|
@Log4j2
|
|
@Repository
|
|
public class RespondentDaoImpl implements RespondentDao {
|
|
|
|
@Autowired
|
|
private MongoTemplate mongoTemplate;
|
|
|
|
@Override
|
|
public boolean insertRespondent(Respondent respondent) {
|
|
try {
|
|
mongoTemplate.save(respondent);
|
|
} catch (Exception e) {
|
|
log.error("调查对象插入失败");
|
|
log.error(e.getMessage());
|
|
throw new RespondentException("调查对象已存在");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public List<Respondent> listRespondentLimit(String userPhone, String state, AdministrativeArea administrativeArea, int offset, int number) {
|
|
Criteria criteria = new Criteria();
|
|
|
|
if (userPhone != null) {
|
|
criteria.and("userPhone").is(userPhone);
|
|
}
|
|
|
|
if (state != null) {
|
|
criteria.and("state").is(state);
|
|
}
|
|
|
|
if (administrativeArea != null) {
|
|
if(administrativeArea.getProvince()!=null){
|
|
criteria.and("administrativeArea.province").is(administrativeArea.getProvince());
|
|
}
|
|
if(administrativeArea.getCity()!=null){
|
|
criteria.and("administrativeArea.city").is(administrativeArea.getCity());
|
|
}
|
|
if(administrativeArea.getCounty()!=null){
|
|
criteria.and("administrativeArea.county").is(administrativeArea.getCounty());
|
|
}
|
|
}
|
|
Query query = new Query(criteria).skip(offset).limit(number);
|
|
return mongoTemplate.find(query, Respondent.class);
|
|
}
|
|
|
|
@Override
|
|
public boolean existRespondent(String idNumber) {
|
|
Query query = new Query(Criteria.where("idNumber").is(idNumber));
|
|
return mongoTemplate.exists(query, Respondent.class);
|
|
}
|
|
|
|
@Override
|
|
public long countRespondent(String userPhone, String state, AdministrativeArea administrativeArea) {
|
|
Criteria criteria = new Criteria();
|
|
|
|
if (userPhone != null) {
|
|
criteria.and("userPhone").is(userPhone);
|
|
}
|
|
|
|
if (state != null) {
|
|
criteria.and("state").is(state);
|
|
}
|
|
|
|
if (administrativeArea != null) {
|
|
if(administrativeArea.getProvince()!=null){
|
|
criteria.and("administrativeArea.province").is(administrativeArea.getProvince());
|
|
}
|
|
if(administrativeArea.getCity()!=null){
|
|
criteria.and("administrativeArea.city").is(administrativeArea.getCity());
|
|
}
|
|
if(administrativeArea.getCounty()!=null){
|
|
criteria.and("administrativeArea.county").is(administrativeArea.getCounty());
|
|
}
|
|
}
|
|
Query query = new Query(criteria);
|
|
return mongoTemplate.count(query, Respondent.class);
|
|
}
|
|
|
|
|
|
}
|