package com.izouma.awesomeadmin.activiti;

import com.izouma.awesomeadmin.model.SysRole;
import com.izouma.awesomeadmin.model.UserInfo;
import com.izouma.awesomeadmin.service.UserInfoService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class CustomGroupEntityManager extends GroupEntityManager {
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public List<Group> findGroupsByUser(String userId) {
        List<Group> groupList = new ArrayList<>();
        UserInfo userInfo = userInfoService.getUserInfoById(userId);
        if (userInfo == null) {
            return null;
        }
        if (userInfo.getRoleInfoList() != null) {
            for (SysRole sysRole : userInfo.getRoleInfoList()) {
                Group group = new GroupEntity();
                group.setId(sysRole.getId().toString());
                group.setName(sysRole.getName());
                group.setType("assignment");
                groupList.add(group);
            }
        }
        return groupList;
    }
}
