package com.jeeplus.modules.mongo.service;


import com.jeeplus.modules.mongo.dao.AbstractBaseMongoTemplete;
import com.jeeplus.modules.mongo.dao.IUserDao;
import com.jeeplus.modules.mongo.document.User;
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.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class UserMongoService extends AbstractBaseMongoTemplete implements IUserDao {

    /**
     * 新增
     * <br>------------------------------<br>
     * @param user
     */
    @Transactional(readOnly = false)
    public void insert(User user) {
        // TODO Auto-generated method stub
        mongoTemplate.insert(user);
    }
    /**
     * 批量新增
     * <br>------------------------------<br>
     * @param users
     */
    @Transactional(readOnly = false)
    public void insertAll(List<User> users) {
        // TODO Auto-generated method stub
        mongoTemplate.insertAll(users);
    }
    /**
     * 删除,按主键id, 如果主键的值为null,删除会失败
     * <br>------------------------------<br>
     * @param id
     */
    @Transactional(readOnly = false)
    public void deleteById(String id) {
        // TODO Auto-generated method stub
        User user = new User(id, null, 0);
        mongoTemplate.remove(user);
    }
    /**
     * 按条件删除
     * <br>------------------------------<br>
     * @param criteriaUser
     */
    @Transactional(readOnly = false)
    public void delete(User criteriaUser) {
        // TODO Auto-generated method stub
        Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge());;
        Query query = new Query(criteria);
        mongoTemplate.remove(query, User.class);
    }
    /**
     * 删除全部
     * <br>------------------------------<br>
     */
    @Transactional(readOnly = false)
    public void deleteAll() {
        // TODO Auto-generated method stub
        mongoTemplate.dropCollection(User.class);
    }
    /**
     * 按主键修改,
     * 如果文档中没有相关key 会新增 使用$set修改器
     * <br>------------------------------<br>
     * @param user
     */
    @Transactional(readOnly = false)
    public void updateById(User user) {
        // TODO Auto-generated method stub
        Criteria criteria = Criteria.where("id").is(user.getId());
        Query query = new Query(criteria);
        Update update = Update.update("age", user.getAge()).set("name", user.getName());
        mongoTemplate.updateFirst(query, update, User.class);
    }
    /**
     * 修改多条
     * <br>------------------------------<br>
     * @param criteriaUser
     * @param user
     */
    @Transactional(readOnly = false)
    public void update(User criteriaUser, User user) {
        // TODO Auto-generated method stub
        Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge());;
        Query query = new Query(criteria);
        Update update = Update.update("name", user.getName()).set("age", user.getAge());
        mongoTemplate.updateMulti(query, update, User.class);
    }
    /**
     * 根据主键查询
     * <br>------------------------------<br>
     * @param id
     * @return
     */
    public User findById(String id) {
        // TODO Auto-generated method stub
        return mongoTemplate.findById(id, User.class);
    }
    /**
     * 查询全部
     * <br>------------------------------<br>
     * @return
     */
    public List<User> findAll() {
        // TODO Auto-generated method stub
        return mongoTemplate.findAll(User.class);
    }
    /**
     * 按条件查询, 分页
     * <br>------------------------------<br>
     * @param criteriaUser
     * @param skip
     * @param limit
     * @return
     */
    public List<User> find(User criteriaUser, int skip, int limit) {
        // TODO Auto-generated method stub
        Query query = getQuery(criteriaUser);
        query.skip(skip);
        query.limit(limit);
        return mongoTemplate.find(query, User.class);
    }
    /**
     * 根据条件查询出来后 再去修改
     * <br>------------------------------<br>
     * @param criteriaUser  查询条件
     * @param updateUser    修改的值对象
     * @return
     */
    public User findAndModify(User criteriaUser, User updateUser) {
        // TODO Auto-generated method stub
        Query query = getQuery(criteriaUser);
        Update update = Update.update("age", updateUser.getAge()).set("name", updateUser.getName());
        return mongoTemplate.findAndModify(query, update, User.class);
    }
    /**
     * 查询出来后 删除
     * <br>------------------------------<br>
     * @param criteriaUser
     * @return
     */
    @Transactional(readOnly = false)
    public User findAndRemove(User criteriaUser) {
        // TODO Auto-generated method stub
        Query query = getQuery(criteriaUser);
        return mongoTemplate.findAndRemove(query, User.class);
    }
    /**
     * count
     * <br>------------------------------<br>
     * @param criteriaUser
     * @return
     */
    public long count(User criteriaUser) {
        // TODO Auto-generated method stub
        Query query = getQuery(criteriaUser);
        return mongoTemplate.count(query, User.class);
    }
    /**
     *
     * <br>------------------------------<br>
     * @param criteriaUser
     * @return
     */
    private Query getQuery(User criteriaUser) {
        if (criteriaUser == null) {
            criteriaUser = new User();
        }
        Query query = new Query();
        if (criteriaUser.getId() != null) {
            Criteria criteria = Criteria.where("id").is(criteriaUser.getId());
            query.addCriteria(criteria);
        }
        if (criteriaUser.getAge() > 0) {
            Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge());
            query.addCriteria(criteria);
        }
        if (criteriaUser.getName() != null) {
            Criteria criteria = Criteria.where("name").regex("^" + criteriaUser.getName());
            query.addCriteria(criteria);
        }
        return query;
    }




}
