package com.cpop.oam.framework.config;

import com.cpop.core.utils.SpringUtils;
import com.cpop.oam.business.entity.Role;
import com.cpop.oam.business.service.RoleService;
import com.cpop.oam.framework.enums.InitRoleEnum;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 初始化配置
 * @author DB
 * @since 2023-11-30 15:33:30
 * @version 1.0.0
 */
@Component
@Slf4j
public class InitConfig {

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        log.info("===========================项目启动角色初始化===========================");
        // 检查角色初始化
        RoleService roleService = SpringUtils.getBean(RoleService.class);
        for (InitRoleEnum value : InitRoleEnum.values()) {
            Role role = roleService.queryChain().where("id = ?", value.getId()).one();
            if (role == null) {
                Role newRole = new Role();
                newRole.setId(value.getId())
                    .setRoleName(value.getRoleName())
                    .setRoleValue(value.getRoleValue())
                    .setOrderNo(value.getOrderNo())
                    .setUserType(value.getUserType());
                roleService.save(newRole);
            }
        }
    }

}
