package com.codvision.monitor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.monitor.entity.SysLog;
import com.codvision.monitor.entity.vo.SysLogVO;
import com.codvision.monitor.mapper.SysLogMapper;
import com.codvision.monitor.service.ISysLogService;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-20
 */
@Service
public class SysLogServiceImpl extends BaseServiceImpl<SysLogMapper, SysLog, SysLogVO> implements ISysLogService {

    @Override
    public SysLogVO getDetail(Long id) {
        SysLogVO vo = new SysLogVO();
        SysLog po = this.getById(id);
        if (null != po) {
            BeanUtil.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public IPage<SysLogVO> getPageResult(QueryPage<SysLog> queryPage) {
        QueryWrapper<SysLog> queryWrapper = queryPage.getWrapper();
        queryWrapper.orderByDesc("create_time");
        IPage<SysLogVO> pageResult = this.getBasePage(queryPage.getPage(), queryWrapper);
        return pageResult;
    }

    @Override
    public List<SysLogVO> listAll(SysLog queryBean) {
        QueryWrapper<SysLog> wrapper = PageUtil.initWrapper(queryBean);
        List<SysLog> list = this.list(wrapper);
        return BeanUtil.copyToList(list, SysLogVO.class);
    }

    @Override
    public boolean save(SysLog sysLog) {
        Assert.notNull(sysLog, "系统日志不能为空");
        getBaseMapper().insert(sysLog);
        return true;
    }

    @Override
    public boolean updateById(SysLog sysLog) {
        Assert.notNull(sysLog, "系统日志不能为空");
        Assert.notNull(sysLog.getId(), "系统日志id不能为空");
        getBaseMapper().updateById(sysLog);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        SysLog sysLog = getById(id);
        Assert.notNull(sysLog, "系统日志不存在");
        getBaseMapper().deleteById(id);
        return true;
    }
}
