package com.cico.nest.lcdp.boot.core.system.service.logger;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.user.AdminUserDO;
import com.cico.nest.lcdp.boot.core.system.controller.admin.logger.vo.operatelog.OperateLogPageReqVO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.logger.OperateLogDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.logger.OperateLogV2DO;
import com.cico.nest.lcdp.boot.core.system.dal.mysql.logger.OperateLogMapper;
import com.cico.nest.lcdp.boot.core.system.dal.mysql.logger.OperateLogV2Mapper;
import com.cico.nest.lcdp.boot.core.system.service.user.AdminUserService;
import com.cico.nest.lcdp.framework.api.logger.dto.LcdpOperateLogCreateDTO;
import com.cico.nest.lcdp.framework.api.logger.dto.LcdpOperateLogV2CreateDTO;
import com.cico.nest.lcdp.framework.api.logger.dto.LcdpOperateLogV2PageDTO;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.util.object.BeanUtils;
import com.cico.nest.lcdp.framework.common.util.string.StrUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;

import static com.cico.nest.lcdp.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * 操作日志 Service 实现类
 *
 * @author zws
 */
@Service
@Validated
@Slf4j
public class OperateLogServiceImpl implements OperateLogService {

    @Resource
    private OperateLogMapper operateLogMapper;
    @Resource
    private OperateLogV2Mapper operateLogV2Mapper;

    @Resource
    private AdminUserService userService;

    @Override
    public void createOperateLog(LcdpOperateLogCreateDTO createReqDTO) {
        OperateLogDO log = BeanUtils.toBean(createReqDTO, OperateLogDO.class);
        log.setUserId(Long.valueOf(createReqDTO.getUserId()));
        log.setJavaMethodArgs(StrUtils.maxLength(log.getJavaMethodArgs(), OperateLogDO.JAVA_METHOD_ARGS_MAX_LENGTH));
        log.setResultData(StrUtils.maxLength(log.getResultData(), OperateLogDO.RESULT_MAX_LENGTH));
        operateLogMapper.insert(log);
    }

    @Override
    public PageResult<OperateLogDO> getOperateLogPage(OperateLogPageReqVO pageReqVO) {
        // 处理基于用户昵称的查询
        Collection<Long> userIds = null;
        if (StrUtil.isNotEmpty(pageReqVO.getUserNickname())) {
            userIds = convertSet(userService.getUserListByNickname(pageReqVO.getUserNickname()), AdminUserDO::getId);
            if (CollUtil.isEmpty(userIds)) {
                return PageResult.empty();
            }
        }
        // 查询分页
        return operateLogMapper.selectPage(pageReqVO, userIds);
    }

    // ======================= LOG V2 =======================

    @Override
    public void createOperateLogV2(LcdpOperateLogV2CreateDTO createReqDTO) {
        OperateLogV2DO log = BeanUtils.toBean(createReqDTO, OperateLogV2DO.class);
        operateLogV2Mapper.insert(log);
    }

    @Override
    public PageResult<OperateLogV2DO> getOperateLogPage(LcdpOperateLogV2PageDTO pageReqDTO) {
        return operateLogV2Mapper.selectPage(pageReqDTO);
    }

}
