package com.interplug.qcast.biz.user;

import com.interplug.qcast.biz.user.dto.AdminUserSyncResponse;
import com.interplug.qcast.biz.user.dto.BusinessChargerSyncResponse;
import com.interplug.qcast.biz.user.dto.StoreRequest;
import com.interplug.qcast.biz.user.dto.UserRequest;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {
  private final UserMapper userMapper;

  /**
   * 판매점 정보 저장
   *
   * @param storeReq
   * @return
   * @throws Exception
   */
  public int setStoreSave(StoreRequest storeReq) throws Exception {
    int resultCnt = userMapper.setStoreSave(storeReq);
    userMapper.setStoreSapCdSave(storeReq);
    userMapper.setStoreNorthModuleSave(storeReq);
    return resultCnt;
  }

  /**
   * 사용자 정보저장
   *
   * @param userReqList
   * @return
   * @throws Exception
   */
  public int setUserSave(List<UserRequest> userReqList) throws Exception {
    int resultCnt = 0;
    if (!userReqList.isEmpty()) {
      for (UserRequest userReq : userReqList) {
        resultCnt += userMapper.setUserSave(userReq);
      }
    }
    return resultCnt;
  }

  /**
   * 판매점 sap 정보 저장
   *
   * @param storeReq
   * @return
   * @throws Exception
   */
  public int setStoreSapCdSave(StoreRequest storeReq) throws Exception {
    return userMapper.setStoreSapCdSave(storeReq);
  }

  /**
   * @param storeList
   * @throws Exception
   */
  public void setStoreBatch(List<StoreRequest> storeList) throws Exception {
    // 판매점 동기화
    for (StoreRequest storeRequest : storeList) {
      try {
        userMapper.setStoreSave(storeRequest);
        userMapper.setStoreSapCdSave(storeRequest);
        userMapper.setStoreNorthModuleSave(storeRequest);
      } catch (Exception e) {
        log.error(e.getMessage());
      }
    }
  }

  public void setUserBatch(List<UserRequest> userList) throws Exception {
    // 사용자 동기화
    for (UserRequest userRequest : userList) {
      try {
        userMapper.setUserSave(userRequest);
      } catch (Exception e) {
        log.error(e.getMessage());
      }
    }
  }

  /**
   * 영업사원 정보 동기화
   *
   * @param businessChargerSyncList 영업사원 목록
   * @return
   * @throws Exception
   */
  public int setBusinessChargerSyncSave(List<BusinessChargerSyncResponse> businessChargerSyncList)
      throws Exception {
    int cnt = 0;
    userMapper.deleteBusinessChargerSync();
    for (BusinessChargerSyncResponse businessChargerSyncData : businessChargerSyncList) {
      cnt += userMapper.setBusinessChargerSyncSave(businessChargerSyncData);
    }
    return cnt;
  }

  /**
   * 관리자 유저 정보 동기화
   *
   * @param adminUserSyncList 관리자 유저 목록
   * @return
   * @throws Exception
   */
  public int setAdminUserSyncSave(List<AdminUserSyncResponse> adminUserSyncList) throws Exception {
    int cnt = 0;
    for (AdminUserSyncResponse adminUserSyncData : adminUserSyncList) {
      if ("1".equals(adminUserSyncData.getDelFlg())) {
        cnt += userMapper.deleteAdminUserSync(adminUserSyncData);
      } else {
        cnt += userMapper.setAdminUserSyncSave(adminUserSyncData);
      }
    }
    return cnt;
  }
}
