package com.yunzhupaas.onlinedev.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.vo.PageListVO;
import com.yunzhupaas.base.vo.PaginationVO;
import com.yunzhupaas.onlinedev.entity.VisualLogEntity;
import com.yunzhupaas.onlinedev.model.log.VisualLogPage;
import com.yunzhupaas.onlinedev.model.log.VisualLogVo;
import com.yunzhupaas.onlinedev.service.VisualLogService;
import com.yunzhupaas.permission.entity.UserEntity;
import com.yunzhupaas.permission.service.UserService;
import com.yunzhupaas.util.DateUtil;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.UploaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Tag(name = "在线开发数据日志", description = "OnlineLog")
@RestController
@RequestMapping("/api/visualdev/OnlineLog")
public class VisualLogController {
    @Autowired
    private VisualLogService visualLogService;
    @Autowired
    private UserService userService;

    @Operation(summary = "列表")
    @GetMapping
    public ActionResult<PageListVO<VisualLogVo>> list(VisualLogPage page) {
        List<VisualLogEntity> list = visualLogService.getLogList(page);
        List<String> userId = list.stream().map(t -> t.getCreatorUserId()).collect(Collectors.toList());
        List<UserEntity> userList = userService.getUserName(userId);
        List<VisualLogVo> listVo = new ArrayList<>();
        for (VisualLogEntity entity : list) {
            VisualLogVo vo = JsonUtil.getJsonToBean(entity, VisualLogVo.class);
            UserEntity userEntity = userList.stream().filter(t -> vo.getCreatorUserId().equals(t.getId())).findFirst().orElse(null);
            if (userEntity != null) {
                vo.setCreatorUserName(userEntity.getRealName());
                vo.setHeadIcon(UploaderUtil.uploaderImg(userEntity.getHeadIcon()));
            }
            vo.setCreatorTime(DateUtil.dateToString(entity.getCreatorTime(), "yyyy-MM-dd HH:mm"));
            listVo.add(vo);
        }
        PaginationVO paginationVO = JsonUtil.getJsonToBean(page, PaginationVO.class);
        return ActionResult.page(listVo, paginationVO);
    }
}

