package com.dbnt.faisp.main.authMgt.service;

import com.dbnt.faisp.main.authMgt.mapper.AuthMgtMapper;
import com.dbnt.faisp.main.authMgt.model.AccessConfig;
import com.dbnt.faisp.main.authMgt.model.ApprovalConfig;
import com.dbnt.faisp.main.authMgt.model.AuthMgt;
import com.dbnt.faisp.main.authMgt.repository.AccessConfigRepository;
import com.dbnt.faisp.main.authMgt.repository.ApprovalConfigRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@RequiredArgsConstructor
public class AuthMgtService {
  private final AuthMgtMapper authMgtMapper;
  private final AccessConfigRepository accessConfigRepository;
  private final ApprovalConfigRepository approvalConfigRepository;

  public List<AccessConfig> selectAccessConfigList(Integer userSeq, String url) {
    Map<String, Object> params = new HashMap<>();
    params.put("userSeq", userSeq);
    params.put("menuUrl", url);
    return authMgtMapper.selectAccessConfigList(params);
  }

  public List<ApprovalConfig> selectApprovalConfigList(Integer userSeq, String url) {
    Map<String, Object> params = new HashMap<>();
    params.put("userSeq", userSeq);
    params.put("menuUrl", url);
    return authMgtMapper.selectApprovalConfigList(params);
  }

  public void saveAuth(AuthMgt authMgt) {
    accessConfigRepository.saveAll(authMgt.getAccessConfigList());
    approvalConfigRepository.saveAll(authMgt.getApprovalConfigList());
  }

  public List<Integer> selectApprvUserList(String wrtOrgan, Integer menuKey, String apprvAuth) {
    Map<String, Object> params = new HashMap<>();
    params.put("ogCd", wrtOrgan);
    params.put("menuKey", menuKey);
    params.put("apprvAuth", apprvAuth);
    return authMgtMapper.selectApprvUserList(params);
  }
}
