package com.rf.psychological.schedule;

import com.rf.psychological.group.dao.model.GroupAuthEntity;
import com.rf.psychological.group.dao.model.GroupEntity;
import com.rf.psychological.group.service.GroupAuthService;
import com.rf.psychological.group.service.GroupInfoService;
import com.rf.psychological.institution.model.InstitutionAuthEntity;
import com.rf.psychological.institution.service.InstitutionAuthService;
import com.rf.psychological.user.service.UserService;
import com.rf.psychological.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @author zzf
 * @description:定时任务，修复原有的默认分组信息
 * @date 2022/5/23 18:07
 */
@Configuration(value = "group")
@EnableScheduling
@Slf4j
public class StaticScheduleTask {


    @Autowired
    private UserService userService;

    @Autowired
    private InstitutionAuthService institutionAuthService;

    @Autowired
    private GroupAuthService groupAuthService;

    @Autowired
    private GroupInfoService groupInfoService;


    /**
     *3.添加定时任务
     * 服务启动完成后 5秒指定第一次任务，之后每当本次任务结束之后5分钟再执行下一次任务
     */
    //@Scheduled(initialDelay = 5000,fixedDelay = 3000000)
    private void configureTasks() {
        //检测用户是否有旧的默认分组，groupId ==0
        //如果有group==0 创建机构下的默认分组
        //将机构权限同步至默认分组权限
        //更新用户的分组分组信息
        List<String> institutionNos  = this.userService.findInstitutionNosByGroupId("0");
        if(institutionNos != null && institutionNos.size()>0){
            institutionNos.forEach(institutionNo->{
                GroupEntity groupEntity = this.groupInfoService.findGroupByInstitutionNoAndName(institutionNo,Constant.DEFAULT_GROUP_NAME);
                if(groupEntity == null){
                    groupEntity = new GroupEntity();
                    groupEntity.setInstitutionNo(institutionNo);
                    groupEntity.setGroupName(Constant.DEFAULT_GROUP_NAME);
                    groupEntity = this.groupInfoService.addGroup(groupEntity);
                    List<InstitutionAuthEntity> institutionAuthEntityList = this.institutionAuthService.getByInstitutionNo(institutionNo);
                    if(institutionAuthEntityList != null && institutionAuthEntityList.size()>0){
                        for(int i =0 ;i<institutionAuthEntityList.size();i++){
                            InstitutionAuthEntity institutionAuthEntity = institutionAuthEntityList.get(i);
                            GroupAuthEntity groupAuthEntity = new GroupAuthEntity();
                            groupAuthEntity.setBId(institutionAuthEntity.getBId());
                            groupAuthEntity.setType(institutionAuthEntity.getType());
                            groupAuthEntity.setGroupId(groupEntity.getId());
                            this.groupAuthService.addGroupAuth(groupAuthEntity);
                        }
                    }
                }
                this.userService.updateUserGroup(institutionNo,"0",groupEntity.getId());
            });
        }
    }



}
