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

import com.rf.psychological.dao.dto.InstitutionAuthConTaskDto;
import com.rf.psychological.dao.dto.InstitutionAuthSubjectNameDto;
import com.rf.psychological.dao.model.CognitiveTaskEntity;
import com.rf.psychological.group.dao.model.GroupAuthEntity;
import com.rf.psychological.group.dao.model.GroupEntity;
import com.rf.psychological.group.dao.repository.GroupInfoRepository;
import com.rf.psychological.institution.model.InstitutionAuthEntity;
import com.rf.psychological.scale.dao.model.SubjectEntity;
import com.rf.psychological.scale.dao.repository.CognitiveTaskRepository;
import com.rf.psychological.group.dao.repository.GroupAuthRepository;
import com.rf.psychological.institution.repository.InstitutionAuthRepository;
import com.rf.psychological.scale.dao.repository.SubjectRepository;
import com.rf.psychological.institution.service.InstitutionAuthService;
import com.rf.psychological.utils.Constant;
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.ArrayList;
import java.util.List;

@Service
public class InstitutionAuthServiceImpl implements InstitutionAuthService {
    @Autowired
    private InstitutionAuthRepository institutionAuthRepository;

    @Autowired
    private SubjectRepository subjectRepository;

    @Autowired
    private CognitiveTaskRepository cognitiveTaskRepository;

    @Autowired
    private GroupAuthRepository groupAuthRepository;

    @Autowired
    private GroupInfoRepository groupInfoRepository;

    @Override
    public void saveInstitutionAuthList(List<InstitutionAuthEntity> institutionAuthEntityList) {
        if (institutionAuthEntityList.size() > 0) {
            this.institutionAuthRepository.saveAll(institutionAuthEntityList);
        }
    }

    @Override
    public void deleteAuthDataByInstitutionNo(String institutionNo) {
        this.institutionAuthRepository.deleteAuthDataByInstitutionNo(institutionNo);
    }

    @Override
    public List<InstitutionAuthEntity> getByInstitutionNo(String institutionNo) {
        InstitutionAuthEntity institutionAuthEntity = new InstitutionAuthEntity();
        institutionAuthEntity.setInstitutionNo(institutionNo);
        Example<InstitutionAuthEntity> one = Example.of(institutionAuthEntity);
        return institutionAuthRepository.findAll(one);
    }

    @Override
    public int getAllByTableInAuthAndSubInfoNum(String institutionNo, String type, String searchKey,String warn) {
        return institutionAuthRepository.getAllByTableInAuthAndSubInfoNum(institutionNo, type, searchKey,warn);
    }

    @Override
    public List<InstitutionAuthSubjectNameDto> getByInstitutionNoAndName(int pageNum, int pageSize, String institutionNo, String type, String searchKey,String warn) {
        return institutionAuthRepository.getAllByTableInAuthAndSubInfo((pageNum - 1) * pageSize, pageSize, institutionNo, type, searchKey,warn);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        ids.forEach(id -> {
            this.institutionAuthRepository.deleteById(id);
        });
    }

    @Override
    public List<InstitutionAuthConTaskDto> getAllConTask(int pageNum, int pageSize, String institutionNo, String type, String searchKey, Boolean mobile) {
        if (mobile) {
            return this.institutionAuthRepository.listTaskInMobile(PageRequest.of(pageNum - 1, pageSize), institutionNo, type, searchKey, "1");
        }
        return this.institutionAuthRepository.getAllConTask(PageRequest.of(pageNum - 1, pageSize), institutionNo, type, searchKey);
    }

    @Override
    public int getAllConTaskNum(String institutionNo, String type, String searchKey, Boolean mobile) {
        if (mobile) {
            return this.institutionAuthRepository.countTaskInMobile(institutionNo, type, searchKey, "1");
        }
        return this.institutionAuthRepository.getAllConTaskNum(institutionNo, type, searchKey);
    }

    @Override
    public void saveAllInstitutionNoAuth(String institutionNo, String type) {
        GroupEntity groupEntity = this.groupInfoRepository.findByInstitutionNoAndGroupName(institutionNo, Constant.DEFAULT_GROUP_NAME);
        List<InstitutionAuthEntity> institutionAuthEntityList = new ArrayList<>();
        List<GroupAuthEntity> list = new ArrayList<>();
        if (type.equals("0")) {
            //pageNub设为1主要为了获取一共有多少个权限没有保存
            Page<SubjectEntity> institutionNoSubAuthList = subjectRepository.findInstitutionNoSubAuthList(institutionNo, "", PageRequest.of(0, 1));
            long institutionNoSubAuthListTotalElements = institutionNoSubAuthList.getTotalElements();
            if (institutionNoSubAuthListTotalElements == 0){
                return;
            }
            Page<SubjectEntity> institutionNoSubAuthList1 = subjectRepository.findInstitutionNoSubAuthList(institutionNo, "", PageRequest.of(0, (int) institutionNoSubAuthListTotalElements));
            if(institutionNoSubAuthList1.getTotalElements()>0){
                institutionNoSubAuthList1.getContent().forEach(item ->{
                    InstitutionAuthEntity institutionAuthEntity = new InstitutionAuthEntity();
                    institutionAuthEntity.setInstitutionNo(institutionNo);
                    institutionAuthEntity.setBId(item.getId());
                    institutionAuthEntity.setType(type);
                    institutionAuthEntityList.add(institutionAuthEntity);

                    GroupAuthEntity entry = new GroupAuthEntity();
                    entry.setBId(item.getId());
                    entry.setGroupId(groupEntity.getId());
                    entry.setType(type);
                    list.add(entry);
                });


            }
            if(institutionAuthEntityList.size()>0)
                this.institutionAuthRepository.saveAll(institutionAuthEntityList);
            if(list.size()>0){
                this.groupAuthRepository.saveAll(list);
            }
        }
        if (type.equals("1")) {
            Page<CognitiveTaskEntity> institutionNoCTAuthList = cognitiveTaskRepository.findInstitutionNoCTAuthList(institutionNo, "", PageRequest.of(0, 1));
            long totalElements = institutionNoCTAuthList.getTotalElements();
            if (totalElements == 0){
                return;
            }
            Page<CognitiveTaskEntity> institutionNoCTAuthList1 = cognitiveTaskRepository.findInstitutionNoCTAuthList(institutionNo, "", PageRequest.of(0, (int) totalElements));

            if(institutionNoCTAuthList1.getTotalElements() >0){
                institutionNoCTAuthList1.getContent().forEach(item ->{
                    InstitutionAuthEntity institutionAuthEntity = new InstitutionAuthEntity(institutionNo, item.getId(), type);
                    institutionAuthEntityList.add(institutionAuthEntity);
                    GroupAuthEntity groupAuthEntity = new GroupAuthEntity();
                    groupAuthEntity.setBId(item.getId());
                    groupAuthEntity.setGroupId(groupEntity.getId());
                    groupAuthEntity.setType(type);
                    list.add(groupAuthEntity);
                    groupAuthRepository.save(groupAuthEntity);
                });
                if(institutionAuthEntityList.size()>0){
                    this.institutionAuthRepository.saveAll(institutionAuthEntityList);
                }
                if(list.size()>0){
                    this.groupAuthRepository.saveAll(list);
                }

            }
        }
    }

    @Override
    public void delAllInAuth(String institutionNo, String type) {
        institutionAuthRepository.deleteAllByInstitutionNoAndType(institutionNo, type);
        //删除机构下组的权限
        groupAuthRepository.deleteGroupAuthByInstitutionNoAndType(institutionNo, type);
    }

    @Override
    public void deleteInstitutionByFlag(String flag) {
        this.institutionAuthRepository.deleteInstitutionByFlag(flag);
    }
}
