package com.njcn.device.terminal.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.device.pq.pojo.param.LogsListParam;
import com.njcn.device.pq.pojo.po.PqsTerminalLogs;
import com.njcn.device.terminal.mapper.PqsTerminalLogsMapper;
import com.njcn.device.terminal.service.IPqsTerminalLogsService;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.vo.UserVO;
import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2023-04-13
 */
@Service
@RequiredArgsConstructor
public class PqsTerminalLogsServiceImpl extends ServiceImpl<PqsTerminalLogsMapper, PqsTerminalLogs> implements IPqsTerminalLogsService {

    private final PqsTerminalLogsMapper pqsTerminalLogsMapper;

    private final UserFeignClient userFeignClient;

    @Override
    public boolean saveLogs(PqsTerminalLogs pqsTerminalLogs) {
        return this.save(pqsTerminalLogs);
    }

    /**
     * 运维日志页面展示
     *
     * @author zbj
     * @date 2023/4/13
     */
    @Override
    public Page<PqsTerminalLogs> getList(LogsListParam logsListParam) {
        Page<PqsTerminalLogs> page;
        LambdaQueryWrapper<PqsTerminalLogs> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(logsListParam.getType()),PqsTerminalLogs::getLogsType,logsListParam.getType())
                .like(StrUtil.isNotBlank(logsListParam.getSearchValue()),PqsTerminalLogs::getTerminalDescribe,logsListParam.getSearchValue())
                .eq(StrUtil.isNotBlank(logsListParam.getId()),PqsTerminalLogs::getObjIndex,logsListParam.getId())
                .between(PqsTerminalLogs::getCreateTime, DateUtil.beginOfDay(DateUtil.parse(logsListParam.getSearchBeginTime())),DateUtil.endOfDay(DateUtil.parse(logsListParam.getSearchEndTime())))
                .orderByDesc(PqsTerminalLogs::getCreateTime);
        page = pqsTerminalLogsMapper.selectPage(new Page<>(PageFactory.getPageNum(logsListParam),PageFactory.getPageSize(logsListParam)),lambdaQueryWrapper);
        if(CollUtil.isNotEmpty(page.getRecords())){
            List<String> userIds = page.getRecords().stream().map(PqsTerminalLogs::getCreateBy).distinct().collect(Collectors.toList());
            List<UserVO> userVOList = userFeignClient.getUserVOByIdList(userIds).getData();
            Map<String,UserVO> userMap = userVOList.stream().collect(Collectors.toMap(UserVO::getId, Function.identity()));
            page.getRecords().forEach(it->{
                UserVO userVO = userMap.getOrDefault(it.getCreateBy(),null);
                if(Objects.nonNull(userVO)){
                    it.setCreateBy(userVO.getName());
                }else {
                    it.setCreateBy("/");
                }
            });
        }
        return page;
    }
}
