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

import cn.hutool.core.date.DateUtil;
import com.rf.psychological.miniapp.searchhistory.model.SearchHistoryEntity;
import com.rf.psychological.miniapp.searchhistory.repository.SearchHistoryRepository;
import com.rf.psychological.miniapp.searchhistory.service.SearchHistoryService;
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.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 SearchHistoryServiceImpl  implements SearchHistoryService {

    @Autowired
    private SearchHistoryRepository repository;


    /**
     * 保存搜索内容接口
     * @param entity 搜索内容
     * @return
     */
    @Override
    public SearchHistoryEntity save(SearchHistoryEntity entity) {
        entity.setCreateTime(DateUtil.now());
        entity.setStatus("1");
        entity = repository.save(entity);
        return entity;
    }

    /**
     * 获取到搜索记录
     * @param openId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<SearchHistoryEntity> queryByOpenId(String openId, int page, int pageSize) {
        Specification specification = ((root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (StringUtils.isEmpty(openId)){
                list.add( criteriaBuilder.equal(root.get("userId"),openId));
            }
            list.add(criteriaBuilder.equal(root.get("status"),"1"));
            query.where(criteriaBuilder.and(list.toArray(new Predicate[0])));
            query.orderBy(criteriaBuilder.asc(root.get("createTime")));
            return query.getRestriction();
        });
        return repository.findAll(specification, PageRequestUtil.of(page,pageSize));
    }

    @Override
    public SearchHistoryEntity deleteById(String id) {
        SearchHistoryEntity entity = repository.getOne(id);
        entity.setStatus("0");
        return repository.save(entity);
    }

    /**
     * 根据用户ID删除搜索记录
     * @param id
     */
    @Override
    public void deleteByUserId(String id) {
        //获取到该用户下的搜索记录
        List<SearchHistoryEntity> list = repository.findAllByUserIdAndStatus(id,Constant.DEFAULT_VALUE_ONE);
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(entity -> {
                entity.setUpdateTime(DateUtil.now());
                entity.setStatus(Constant.DEFAULT_VALUE_ZERO);
            });
        }
        repository.saveAll(list);
    }

    @Override
    public List<String> queryListByUserId(String userId) {
        return repository.queryListByUserId(userId);
    }
}
