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.security.JwtUtil;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.mapper.TaskRecordMapper;
import com.gxwebsoft.oa.service.TaskRecordService;
import com.gxwebsoft.oa.entity.TaskRecord;
import com.gxwebsoft.oa.param.TaskRecordParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
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-23 18:53:01
 */
@Service
public class TaskRecordServiceImpl extends ServiceImpl<TaskRecordMapper, TaskRecord> implements TaskRecordService {

    @Override
    public PageResult<TaskRecord> pageRel(TaskRecordParam param) {
        PageParam<TaskRecord, TaskRecordParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
        List<TaskRecord> list = baseMapper.selectPageRel(page, param);
        final PageResult<TaskRecord> result = new PageResult<>(list, page.getTotal());

        // 请求主服务器获取用户信息
        final List<TaskRecord> taskRecordList = result.getList();
        final Set<Integer> userIds = taskRecordList.stream().map(TaskRecord::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 Map<Integer, List<User>> collect = userResult.getData().getList().stream().collect(Collectors.groupingBy(User::getUserId));
        taskRecordList.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.setCity(user.getCity());
                item.setUser(user);
            }
        });
        result.setList(taskRecordList);
        return result;
    }

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

    @Override
    public TaskRecord getByIdRel(Integer taskRecordId) {
        TaskRecordParam param = new TaskRecordParam();
        param.setTaskRecordId(taskRecordId);
        return param.getOne(baseMapper.selectListRel(param));
    }

}
