package com.rf.psychological.scale.service.impl;


import com.rf.psychological.dao.model.CognitiveTaskEntity;
import com.rf.psychological.scale.dao.repository.CognitiveTaskRepository;
import com.rf.psychological.scale.service.CognitiveTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author zsy
 * @description:认知任务信息管理
 * @date 2021/6/17 15:55
 */
@Service
public class CognitiveTaskServiceImpl implements CognitiveTaskService {

    @Autowired
    private CognitiveTaskRepository cognitiveTaskRepository;


    /**
     * 保存
     *
     * @param cognitiveTaskEntity
     */
    @Override
    public void save(CognitiveTaskEntity cognitiveTaskEntity) {
        this.cognitiveTaskRepository.save(cognitiveTaskEntity);
    }

    @Override
    public List<CognitiveTaskEntity> findAll(int pageNum, int pageSize) {
        int beginIndex = (pageNum - 1) * pageSize;
        return this.cognitiveTaskRepository.findAll(beginIndex, pageSize);
    }

    @Override
    public int allNum() {
        return this.cognitiveTaskRepository.allNum();
    }

    @Override
    public List<CognitiveTaskEntity> getCognizeListByName(int pageNum, int pageSize, String searchStr) {
        int beginIndex = (pageNum - 1) * pageSize;
        return this.cognitiveTaskRepository.getCognizeListByName(beginIndex, pageSize, searchStr);
    }

    @Override
    public int allNumByName(String searchStr) {
        return this.cognitiveTaskRepository.allNumByName(searchStr);
    }

    @Override
    public List<CognitiveTaskEntity> findAllCategory(int pageNum, int pageSize, String categoryEname) {
        int beginIndex = (pageNum - 1) * pageSize;
        return this.cognitiveTaskRepository.findAllCategory(beginIndex, pageSize, categoryEname);
    }

    @Override
    public int allNumCategory(String categoryEname) {
        return this.cognitiveTaskRepository.allNumCategory(categoryEname);
    }

    @Override
    public Optional<CognitiveTaskEntity> getAllById(String id) {
        return cognitiveTaskRepository.findById(id);
    }

    @Override
    public List<CognitiveTaskEntity> getCognizeListByNameCategory(int pageNum, int pageSize, String scaleName, String categoryEname) {
        int beginIndex = (pageNum - 1) * pageSize;
        return this.cognitiveTaskRepository.getCognizeListByNameCategory(beginIndex, pageSize, scaleName, categoryEname);
    }

    @Override
    public int allNumByNameCategory(String scaleName, String categoryEname) {
        return this.cognitiveTaskRepository.allNumByNameCategory(scaleName, categoryEname);
    }

    @Override
    public void addTestNum(String taskCode) {
        this.cognitiveTaskRepository.addTestNum(taskCode);
    }

    @Override
    public Page<CognitiveTaskEntity> findUserTaskAuth(String uId, String scaleName, int beginNum, int pageSize, Boolean mobile) {
        if (mobile) {
            return cognitiveTaskRepository.pageUserTaskAuthInMobile(uId, scaleName, "1", PageRequest.of(beginNum - 1, pageSize));
        }
        return this.cognitiveTaskRepository.findUserTaskAuth(uId, scaleName, PageRequest.of(beginNum - 1, pageSize));
    }

    @Override
    public Page<CognitiveTaskEntity> findUserTaskAuthAndCateEName(String uId, String scaleName, String cateEName, int beginNum, int pageSize, Boolean mobile) {
        if (mobile) {
            return cognitiveTaskRepository.pageUserTaskAuthAndCateENameInMobile(uId, scaleName, cateEName, "1", PageRequest.of(beginNum - 1, pageSize));
        }
        return this.cognitiveTaskRepository.findUserTaskAuthAndCateEName(uId, scaleName, cateEName, PageRequest.of(beginNum - 1, pageSize));
    }

    @Override
    public Page<CognitiveTaskEntity> findInstitutionTaskAuthAndCateEName(String institutionNo, String scaleName, String cateEName, int beginNum, int pageSize, Boolean mobile) {
        if (mobile) {
            return cognitiveTaskRepository.pageInstitutionTaskAuthAndCateEName(institutionNo, scaleName, cateEName, "1", PageRequest.of(beginNum - 1, pageSize));
        }
        return cognitiveTaskRepository.findInstitutionTaskAuthAndCateEName(institutionNo, scaleName, cateEName, PageRequest.of(beginNum - 1, pageSize));
    }

    @Override
    public Page<CognitiveTaskEntity> findInstitutionNoCTAuthList(String institutionNo, String searchKey, int beginNum, int pageSize) {
        return this.cognitiveTaskRepository.findInstitutionNoCTAuthList(institutionNo, searchKey, PageRequest.of(beginNum - 1, pageSize));
    }

    @Override
    public CognitiveTaskEntity getCognizeListByTaskCode(String taskCode) {
        return this.cognitiveTaskRepository.getCognizeListByTaskCode(taskCode);
    }

    @Override
    public List<CognitiveTaskEntity> getTrendingSearches(int numberFive) {
        return this.cognitiveTaskRepository.getTrendingSearches(numberFive);
    }

    @Override
    public List<CognitiveTaskEntity> getRelevantEvaluation(String taskCode) {
        return this.cognitiveTaskRepository.getRelevantEvaluation(taskCode);
    }
}
