package com.rf.psychological.miniapp.comment.service.impl;

import cn.hutool.core.date.DateUtil;
import com.rf.psychological.dao.model.CognitiveTaskEntity;
import com.rf.psychological.miniapp.collect.model.CollectEntity;
import com.rf.psychological.miniapp.comment.model.CommentEntity;
import com.rf.psychological.miniapp.comment.repository.CommentRepository;
import com.rf.psychological.miniapp.comment.service.CommentService;
import com.rf.psychological.scale.dao.model.SubjectEntity;
import com.rf.psychological.scale.service.CognitiveTaskService;
import com.rf.psychological.scale.service.SubjectService;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.PageRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:用户评论接口
 * @Author: mimang
 * @Date: 2023/5/16
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository repository;

    /**
     * 评论信息保存
     * @param entity
     * @return
     */
    @Override
    public CommentEntity save(CommentEntity entity) {
        entity.setCreateTime(DateUtil.now());
        entity.setUpdateTime(DateUtil.now());
        entity.setStatus(Constant.DEFAULT_VALUE_ONE);
        return repository.save(entity);
    }

    /**
     * 根据flag查询某量表或认知任务评论记录
     * @param flag
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<CommentEntity> queryByFlag(String flag, String status, int pageNum, int pageSize) {
        return repository.queryByFlag(PageRequest.of(pageNum - 1, pageSize), flag, status);
    }

    /**
     * 根据flag查询某量表或认知任务评论记录数量
     * @param flag
     * @param status
     * @return
     */
    @Override
    public List<CommentEntity> findAllByFlagAndStatus(String flag, String status) {
        return repository.findAllByFlagAndStatus(flag, status);
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @Override
    public CommentEntity findById(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public CommentEntity deleteById(CommentEntity commentEntity) {
        commentEntity.setUpdateTime(DateUtil.now());
        commentEntity.setStatus(Constant.DEFAULT_VALUE_ZERO);
        return repository.save(commentEntity);
    }


}
