package org.wcs.serve.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wcs.WcsApplication;
import org.wcs.constant.enums.database.UserLevelEnum;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppBaseUserService;
import org.wcs.model.po.app.AppBaseUser;
import org.wcs.model.vo.serve.AppServeResponse;
import org.wcs.serve.service.intf.IDevService;

import java.util.List;
import java.util.Objects;

/**
 * 开发者 服务
 */
@Service
@RequiredArgsConstructor
public class DevService implements IDevService {

    private final AppBaseUserService baseUserService;

    /**
     * 重启应用
     * @return 重启结果
     */
    @Override
    public AppServeResponse restartApplication() {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录", null);
        }
        String userId = userIdObj.toString();
        List<AppBaseUser> users = baseUserService.queryByUserId(userId);
        if(users == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常", null);
        }
        if(users.isEmpty()) {
            return AppServeResponseFactory.fail("用户不存在或没有权限", null);
        }
        AppBaseUser user = users.getFirst();
        if(!Objects.equals(user.getUserLevel(), UserLevelEnum.TOP.getLevel())) {
            return AppServeResponseFactory.fail("用户没有权限", null);
        }
        WcsApplication.restart(); // 重启应用
        return AppServeResponseFactory.success("重启成功", null);
    }



}
