package com.njcn.rdms.module.system.controller.admin.logger;

import com.njcn.rdms.framework.apilog.core.annotation.ApiAccessLog;
import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageParam;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.framework.common.util.collection.CollectionUtils;
import com.njcn.rdms.framework.common.util.object.BeanUtils;
import com.njcn.rdms.framework.excel.core.util.ExcelUtils;
import com.njcn.rdms.module.system.controller.admin.logger.vo.operatelog.OperateLogPageReqVO;
import com.njcn.rdms.module.system.controller.admin.logger.vo.operatelog.OperateLogRespVO;
import com.njcn.rdms.module.system.dal.dataobject.logger.OperateLogDO;
import com.njcn.rdms.module.system.dal.dataobject.user.AdminUserDO;
import com.njcn.rdms.module.system.service.logger.OperateLogService;
import com.njcn.rdms.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.njcn.rdms.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 操作日志")
@RestController
@RequestMapping("/system/operate-log")
@Validated
public class OperateLogController {

    @Resource
    private OperateLogService operateLogService;

    @Resource
    private AdminUserService adminUserService;

    @GetMapping("/get")
    @Operation(summary = "查看操作日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:operate-log:query')")
    public CommonResult<OperateLogRespVO> getOperateLog(@RequestParam("id") Long id) {
        OperateLogRespVO respVO = BeanUtils.toBean(operateLogService.getOperateLog(id), OperateLogRespVO.class);
        fillUserNames(Collections.singletonList(respVO));
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "查看操作日志分页列表")
    @PreAuthorize("@ss.hasPermission('system:operate-log:query')")
    public CommonResult<PageResult<OperateLogRespVO>> pageOperateLog(@Valid OperateLogPageReqVO pageReqVO) {
        PageResult<OperateLogDO> pageResult = operateLogService.getOperateLogPage(pageReqVO);
        PageResult<OperateLogRespVO> respPageResult = BeanUtils.toBean(pageResult, OperateLogRespVO.class);
        fillUserNames(respPageResult.getList());
        return success(respPageResult);
    }

    @Operation(summary = "导出操作日志")
    @GetMapping("/export-excel")
    @PreAuthorize("@ss.hasPermission('system:operate-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOperateLog(HttpServletResponse response, @Valid OperateLogPageReqVO exportReqVO) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OperateLogRespVO> respVOList = BeanUtils.toBean(
                operateLogService.getOperateLogPage(exportReqVO).getList(), OperateLogRespVO.class);
        fillUserNames(respVOList);
        ExcelUtils.write(response, "操作日志.xls", "数据列表", OperateLogRespVO.class, respVOList);
    }

    private void fillUserNames(List<OperateLogRespVO> respVOList) {
        if (respVOList == null || respVOList.isEmpty()) {
            return;
        }
        Map<Long, AdminUserDO> userMap = adminUserService.getUserMap(
                CollectionUtils.convertSet(respVOList, OperateLogRespVO::getUserId));
        respVOList.forEach(respVO -> {
            AdminUserDO user = userMap.get(respVO.getUserId());
            if (user != null) {
                respVO.setUserName(user.getNickname());
            }
        });
    }

}
