package com.qiaoba.module.log.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.module.log.entity.SysUseLog;
import com.qiaoba.module.log.entity.param.SysUseLogParam;
import com.qiaoba.module.log.mapper.SysUserLogMapper;
import com.qiaoba.module.log.service.SysUserLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 使用日志 服务层实现
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/7/5 13:51
 */
@Service
@RequiredArgsConstructor
public class SysUserLogServiceImpl implements SysUserLogService {

    private final SysUserLogMapper sysUserLogMapper;

    @Override
    public void insert(SysUseLog sysUseLog) {
        sysUserLogMapper.insert(sysUseLog);
    }

    @Override
    public TableDataInfo selectPageList(SysUseLogParam param, PageQuery pageQuery) {
        return TableDataInfo.build(sysUserLogMapper.selectPage(pageQuery.build(), param2Wrapper(param)));
    }

    @Override
    public List<SysUseLog> selectList(SysUseLogParam param) {
        return sysUserLogMapper.selectList(param2Wrapper(param));
    }

    @Override
    public void cleanUseLog() {
        sysUserLogMapper.delete(param2Wrapper(new SysUseLogParam()));
    }

    private QueryWrapper<SysUseLog> param2Wrapper(SysUseLogParam param) {
        QueryWrapper<SysUseLog> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByDesc(SysUseLog::getOperationTime)
                .between(ObjectUtil.isNotEmpty(param.getBeginTime()), SysUseLog::getOperationTime, param.getBeginTime(), param.getEndTime())
                .like(StrUtil.isNotBlank(param.getTitle()), SysUseLog::getTitle, param.getTitle())
                .like(StrUtil.isNotBlank(param.getOperatorName()), SysUseLog::getOperatorName, param.getOperatorName())
                .eq(ObjectUtil.isNotEmpty(param.getBusinessType()), SysUseLog::getBusinessType, param.getBusinessType())
                .eq(ObjectUtil.isNotEmpty(param.getStatus()), SysUseLog::getStatus, param.getStatus());
        return wrapper;
    }
}
