package org.qinan.safetyeval.infrastructure.adapter.gbs;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.system.client.user.facade.UserFacade;
import com.jjb.saas.system.client.user.request.*;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.qinan.safetyeval.domain.exception.BizException;
import org.springframework.stereotype.Component;

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

@Component
@RequiredArgsConstructor
public class GbsUserFacadeClient {

    @DubboReference(protocol = "dubbo", url = "${safety-eval.gbs-user-sync.user-facade-url:}")
    private UserFacade userFacade;

    public void add(UserAddCmd cmd) {
        //2069596397920849920{key: "1782463085576", deptId: "2069596397920849920", roleId: "2069671307598893058"}
        List<RoleDeptAddCmd> list = new ArrayList<>();
        RoleDeptAddCmd roleDeptAddCmd = new RoleDeptAddCmd();
        roleDeptAddCmd.setDeptId(2069596397920849920L);
        roleDeptAddCmd.setRoleId(2069671307598893058L);
        list.add(roleDeptAddCmd);
        cmd.setRoleDepts(list);
        SingleResponse<Long> singleResponse = userFacade.add(cmd);
        if (!singleResponse.isSuccess()) {
            throw new BizException(singleResponse.getErrCode(), singleResponse.getErrMessage());
        }
    }

    public void delete(Long userId) {
        Response response = userFacade.delete(userId);
        if (!response.isSuccess()) {
            throw new BizException(response.getErrCode(), response.getErrMessage());
        }
    }

    public void updatePassword(UserUpdatePasswordCmd cmd) {
        Response response = userFacade.updatePassword(cmd);
        if (!response.isSuccess()) {
            throw new BizException(response.getErrCode(), response.getErrMessage());
        }
    }

    public void quit(UserUpdateQuitCmd cmd) {
        userFacade.quit(cmd);
    }

    public void restoreJob(UserUpdateQuitCmd cmd) {
        userFacade.restoreJob(cmd);
    }

    public void updateStatusEnumBatch(FacadeUserUpdateStatusEnumBatchCmd cmd) {
        userFacade.updateStatusEnumBatch(cmd);
    }
}