package net.northking.cctp.platform.api.user.service;

import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.exception.InvalidException;
import com.hzbank.testteam.autotest.dependencies.monsterDependency.dto.UserDTO;
import net.northking.cctp.platform.bus.feign.HzBankFeign;
import net.northking.cctp.platform.config.TaskSyncConfig;
import net.northking.cctp.platform.db.service.CltUserLoginService;
import net.northking.cctp.platform.dto.user.CltUserLoginDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * 用户服务
 *
 * @author xiusong.xie
 * @since 2.4
 */
@Service
@RefreshScope
@ConditionalOnMissingBean(name = "UserApiServiceImpl")
public class UserApiServiceImpl implements UserApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(UserApiServiceImpl.class);

  @Autowired
  private CltUserLoginService cltUserLoginService;



  @Autowired
  private HzBankFeign hzBankFeign;
  @Autowired
  private TaskSyncConfig taskSyncConfig;

  public UserApiServiceImpl() {
  }



  @Override
  public String selectOperCode(String userId) {
    Map<String, Object> params = new HashMap<>();
    params.put("id", Long.parseLong(userId));
    params.put("deleted", 0);
    UserDTO userDTO = hzBankFeign.selectOneByMap(params);
    if (userDTO == null) {
      throw new InvalidException("用户不存在或者已失效");
    }
    return userDTO.getOperCode();
  }

  @Override
  public List<CltUserLoginDto> getAllUsers(String username) {
    List<CltUserLoginDto> list = new ArrayList<>();
    if (StringUtils.hasText(username)) {
      username = username.trim();
    }
    Map<String, Object> params = new HashMap<>();
    params.put("employeeName", username);
    params.put("deleted", 0);
    params.put("limitCount", 100);
    List<UserDTO> userDTOS = hzBankFeign.selectByMap(params);
    // 字段转化
    userDTOS.forEach((userDTO -> list.add(new CltUserLoginDto(userDTO.getId().toString(),userDTO.getUserName(),
            userDTO.getEmployeeName(),userDTO.getOperCode(),taskSyncConfig.getTenantId(),userDTO.getOrgCode()))));
    return list;
  }

  @Override
  public String queryUserInfoByCode(String operCode) {
    return cltUserLoginService.getUserInfoByCode(operCode);
  }

}
