package com.gxwebsoft.oa.service.impl;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.net.url.UrlQuery;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.AppUser;
import com.gxwebsoft.oa.mapper.AppUserMapper;
import com.gxwebsoft.oa.param.AppUserParam;
import com.gxwebsoft.oa.service.AppUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 应用成员Service实现
 *
 * @author 科技小王子
 * @since 2023-10-26 22:06:23
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {

  @Override
  public PageResult<AppUser> pageRel(AppUserParam param) {
    PageParam<AppUser, AppUserParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
    List<AppUser> list = baseMapper.selectPageRel(page, param);
    PageResult<AppUser> result = new PageResult<>(list, page.getTotal());
    final List<AppUser> appUserList = result.getList();
    final Set<Integer> userIds = appUserList.stream().map(AppUser::getUserId).collect(Collectors.toSet());
    // 请求主服务器获取用户信息
    HashMap<String, Object> map = new HashMap<>();
    map.put("userIds", userIds);
    // 链式构建GET请求
    final String build = UrlBuilder.of("https://server.gxwebsoft.com/api/system/user/page").setQuery(new UrlQuery(map)).build();
    String response = HttpRequest.get(build)
      .header("Authorization", param.getToken())
      .header("Tenantid", param.getTenantId().toString())
      .body(JSONObject.toJSONString(map))//表单内容
      .timeout(20000)//超时，毫秒
      .execute().body();

    final ApiResult<PageResult<User>> userResult = JSONObject.parseObject(response, new TypeReference<ApiResult<PageResult<User>>>() {
    });
    final PageResult<User> data = userResult.getData();
    System.out.println("data = " + data);
    final List<User> userList = data.getList();
    if (!CollectionUtils.isEmpty(userList)) {
      final Map<Integer, List<User>> collect = userList.stream().collect(Collectors.groupingBy(User::getUserId));
      appUserList.forEach(item -> {
        final List<User> users = collect.get(item.getUserId());
        if (!CollectionUtils.isEmpty(users)) {
          final User user = users.get(0);
          item.setNickname(user.getNickname());
          item.setAvatar(user.getAvatar());
          item.setPhone(user.getPhone());
          item.setEmail(user.getEmail());
        }
      });
      result.setList(appUserList);
    }
    return result;
  }

  @Override
  public List<AppUser> listRel(AppUserParam param) {
    List<AppUser> list = baseMapper.selectListRel(param);
    // 排序
    PageParam<AppUser, AppUserParam> page = new PageParam<>();
    //page.setDefaultOrder("create_time desc");
    return page.sortRecords(list);
  }

  @Override
  public AppUser getByIdRel(Integer appUserId) {
    AppUserParam param = new AppUserParam();
    param.setAppUserId(appUserId);
    return param.getOne(baseMapper.selectListRel(param));
  }

}
