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

import com.rf.psychological.group.dao.dto.GroupAuthConTaskDto;
import com.rf.psychological.group.dao.dto.GroupAuthSubjectNameDto;
import com.rf.psychological.dao.dto.InstitutionAuthConTaskDto;
import com.rf.psychological.dao.dto.InstitutionAuthSubjectNameDto;
import com.rf.psychological.group.dao.model.GroupAuthEntity;
import com.rf.psychological.scale.dao.repository.CognitiveTaskRepository;
import com.rf.psychological.group.dao.repository.GroupAuthRepository;
import com.rf.psychological.scale.dao.repository.SubjectRepository;
import com.rf.psychological.group.service.GroupAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
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 lpf
 * @description:
 * @date 26/10/2021下午4:35
 */

@Service
public class GroupAuthServiceImpl implements GroupAuthService {

    @Autowired
    private GroupAuthRepository groupAuthRepository;

    @Autowired
    private SubjectRepository subjectRepository;

    @Autowired
    private CognitiveTaskRepository cognitiveTaskRepository;

    @Override
    public void deleteAllByGroupId(List<String> groupIds) {
        groupIds.forEach(gId -> {
            groupAuthRepository.deleteAllByGroupId(gId);
        });
    }

    @Override
    public void deleteGroupAuthBecauseInstitutionDelete(String institutionNo, List<String> ids) {
        this.groupAuthRepository.deleteGroupAuthBecauseInstitutionDelete(institutionNo, ids);
    }

    @Override
    public void deleteAuthBath(List<String> ids) {
        groupAuthRepository.deleteGroupAuthBath(ids);
    }

    @Override
    public GroupAuthEntity addGroupAuth(GroupAuthEntity groupAuthEntity) {
        return groupAuthRepository.saveAndFlush(groupAuthEntity);
    }

    @Override
    public Page<GroupAuthSubjectNameDto> findGroupAuth(String groupId, int beginNum, int pageNum, String name) {
        return subjectRepository.findGroupAuth(groupId, name, PageRequest.of(beginNum - 1, pageNum));
    }

    @Override
    public Page<GroupAuthConTaskDto> findGroupTaskAuth(String groupId, int beginNum, int pageNum,String name) {
        return cognitiveTaskRepository.findGroupAuth(groupId, name,PageRequest.of(beginNum - 1, pageNum));
    }

    @Override
    public Page<InstitutionAuthConTaskDto> findGroupNoAuthCoTaskList(String institutionNo, String groupId, String searchKey, int beginNum, int pageNum) {
        return groupAuthRepository.findGroupNoAuthCoTaskList(institutionNo, groupId, searchKey, PageRequest.of(beginNum - 1, pageNum));
    }

    @Override
    public GroupAuthEntity findGroupAuthByGroupIdAndSubjectId(String groupId, String bId) {
        GroupAuthEntity groupAuthEntity = new GroupAuthEntity();
        groupAuthEntity.setGroupId(groupId);
        groupAuthEntity.setBId(bId);
        Optional<GroupAuthEntity> one = groupAuthRepository.findOne(Example.of(groupAuthEntity));
        return one.orElse(null);
    }

    @Override
    public void deleteGroupByFlag(String flag) {
        this.groupAuthRepository.deleteGroupByFlag(flag);
    }

    @Override
    public void save(List<GroupAuthEntity> authEntityList) {
        this.groupAuthRepository.saveAll(authEntityList);
    }

    @Override
    public Page<InstitutionAuthSubjectNameDto> findGroupNoAuthList(String institutionNo, String groupId, String searchKey, int beginNum, int pageNum) {
        return groupAuthRepository.findGroupNoAuthList(institutionNo, groupId, searchKey, PageRequest.of(beginNum - 1, pageNum));
    }
}
