admin
2024-04-26 5e7b0ed4a154ad067cbcf4aa1a1c7cce32f9864c
fanli/src/main/java/com/yeshi/fanli/dao/dynamic/ArticleOfficialDao.java
@@ -1,159 +1,159 @@
package com.yeshi.fanli.dao.dynamic;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import org.springframework.data.domain.Sort;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.yeshi.fanli.entity.dynamic.ArticleOfficial;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.vo.dynamic.ArticleVO;
@Repository
public class ArticleOfficialDao {
   @Resource
   private MongoTemplate mongoTemplate;
   /**
    * 新增
    *
    * @param record
    */
   public void save(ArticleOfficial record) {
      if (record == null) {
         return;
      }
      mongoTemplate.save(record);
   }
   /**
    * 状态切换
    *
    * @param record
    */
   public void updateSatate(String id, int state) {
      Query query = new Query();
      query.addCriteria(Criteria.where("id").is(id));
      Update update = Update.update("state", state);
      mongoTemplate.updateMulti(query, update, ArticleOfficial.class);
   }
   /**
    * 根据id查询数据
    *
    * @param id
    * @return
    */
   public ArticleOfficial getById(String id) {
      Query query = new Query();
      query.addCriteria(Criteria.where("id").is(id));
      return mongoTemplate.findOne(query, ArticleOfficial.class);
   }
   /**
    * 删除
    *
    * @param id
    * @return
    */
   public void deleteById(String id) {
      ArticleOfficial info = getById(id);
      if (info == null) {
         return;
      }
      mongoTemplate.remove(info);
   }
   /**
    * 查询
    *
    * @return
    */
   public List<ArticleOfficial> query(int start, int count, String key, Integer state) {
      Query query = new Query();
      if (state != null) {
         query.addCriteria(Criteria.where("state").is(state));
      }
      if (!StringUtil.isNullOrEmpty(key))
         query.addCriteria(new Criteria().orOperator(
                      Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                            new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                            new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                   ));
      // 分页
        query.skip(start).limit(count);
      query.with(new Sort(Sort.Direction.DESC,"weight"));
      return mongoTemplate.find(query, ArticleOfficial.class);
   }
   public long count(String key, Integer state) {
      Query query = new Query();
      if (state != null) {
         query.addCriteria(Criteria.where("state").is(state));
      }
      if (!StringUtil.isNullOrEmpty(key))
         query.addCriteria(new Criteria().orOperator(
                      Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                            new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                            new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                   ));
      return mongoTemplate.count(query, ArticleOfficial.class);
   }
   /**
    * 查询有效
    * @param start
    * @param count
    * @param key
    * @return
    */
   public List<ArticleVO> queryValid(int start, int count, String key) {
      Date now = new Date();
      Query query = new Query();
      query.addCriteria(Criteria.where("state").is(1));
      query.addCriteria(Criteria.where("startTime").lte(now));
      query.addCriteria(Criteria.where("endTime").gte(now));
      if (!StringUtil.isNullOrEmpty(key))
         query.addCriteria(new Criteria().orOperator(
                      Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                            new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                            new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                   ));
        //分页
        query.skip(start).limit(count);
      query.with(new Sort(Sort.Direction.DESC,"weight")).with(new Sort(Sort.Direction.DESC,"createTime"));
      return mongoTemplate.find(query, ArticleVO.class);
   }
   /**
    * 统计有效
    * @param key
    * @return
    */
   public long countValid(String key) {
      Date now = new Date();
      Query query = new Query();
      query.addCriteria(Criteria.where("state").is(1));
      query.addCriteria(Criteria.where("startTime").lte(now));
      query.addCriteria(Criteria.where("endTime").gte(now));
      if (!StringUtil.isNullOrEmpty(key))
         query.addCriteria(new Criteria().orOperator(
                      Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                            new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                            new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                   ));
      return mongoTemplate.count(query, ArticleOfficial.class);
   }
}
package com.yeshi.fanli.dao.dynamic;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import org.springframework.data.domain.Sort;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.yeshi.fanli.entity.dynamic.ArticleOfficial;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.vo.dynamic.ArticleVO;
@Repository
public class ArticleOfficialDao {
   @Resource
   private MongoTemplate mongoTemplate;
   /**
    * 新增
    *
    * @param record
    */
   public void save(ArticleOfficial record) {
      if (record == null) {
         return;
      }
      mongoTemplate.save(record);
   }
   /**
    * 状态切换
    *
    * @param record
    */
   public void updateSatate(String id, int state) {
      Query query = new Query();
      query.addCriteria(Criteria.where("id").is(id));
      Update update = Update.update("state", state);
      mongoTemplate.updateMulti(query, update, ArticleOfficial.class);
   }
   /**
    * 根据id查询数据
    *
    * @param id
    * @return
    */
   public ArticleOfficial getById(String id) {
      Query query = new Query();
      query.addCriteria(Criteria.where("id").is(id));
      return mongoTemplate.findOne(query, ArticleOfficial.class);
   }
   /**
    * 删除
    *
    * @param id
    * @return
    */
   public void deleteById(String id) {
      ArticleOfficial info = getById(id);
      if (info == null) {
         return;
      }
      mongoTemplate.remove(info);
   }
   /**
    * 查询
    *
    * @return
    */
   public List<ArticleOfficial> query(int start, int count, String key, Integer state) {
      Query query = new Query();
      if (state != null) {
         query.addCriteria(Criteria.where("state").is(state));
      }
      if (!StringUtil.isNullOrEmpty(key))
         query.addCriteria(new Criteria().orOperator(
                      Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                            new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                            new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                   ));
      // 分页
        query.skip(start).limit(count);
      query.with(new Sort(Sort.Direction.DESC,"weight"));
      return mongoTemplate.find(query, ArticleOfficial.class);
   }
   public long count(String key, Integer state) {
      Query query = new Query();
      if (state != null) {
         query.addCriteria(Criteria.where("state").is(state));
      }
      if (!StringUtil.isNullOrEmpty(key))
         query.addCriteria(new Criteria().orOperator(
                      Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                            new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                            new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                   ));
      return mongoTemplate.count(query, ArticleOfficial.class);
   }
   /**
    * 查询有效
    * @param start
    * @param count
    * @param key
    * @return
    */
   public List<ArticleVO> queryValid(int start, int count, String key) {
      Date now = new Date();
      Query query = new Query();
      query.addCriteria(Criteria.where("state").is(1));
      query.addCriteria(Criteria.where("startTime").lte(now));
      query.addCriteria(Criteria.where("endTime").gte(now));
      if (!StringUtil.isNullOrEmpty(key))
         query.addCriteria(new Criteria().orOperator(
                      Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                            new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                            new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                   ));
        //分页
        query.skip(start).limit(count);
      query.with(new Sort(Sort.Direction.DESC,"weight")).with(new Sort(Sort.Direction.DESC,"createTime"));
      return mongoTemplate.find(query, ArticleVO.class);
   }
   /**
    * 统计有效
    * @param key
    * @return
    */
   public long countValid(String key) {
      Date now = new Date();
      Query query = new Query();
      query.addCriteria(Criteria.where("state").is(1));
      query.addCriteria(Criteria.where("startTime").lte(now));
      query.addCriteria(Criteria.where("endTime").gte(now));
      if (!StringUtil.isNullOrEmpty(key))
         query.addCriteria(new Criteria().orOperator(
                      Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                            new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                            new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                   ));
      return mongoTemplate.count(query, ArticleOfficial.class);
   }
}