package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.mapper.SmartDataSourceLogMapper;
import com.template.model.pojo.SmartDataSourceLog;
import com.template.model.pojo.SmartDataSourceLog;
import com.template.mapper.SmartDataSourceLogMapper;
import com.template.model.result.PageUtils;
import com.template.services.SmartDataSourceLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 数据源操作日志 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-12-05
 */
@Service
public class SmartDataSourceLogServiceImpl extends ServiceImpl<SmartDataSourceLogMapper, SmartDataSourceLog> implements SmartDataSourceLogService {
    @Autowired
    private SmartDataSourceLogMapper smartDataSourceLogMapper;

    @Override
    public int insertSmartDataSourceLog(SmartDataSourceLog smartDataSourceLog) {
        int result = smartDataSourceLogMapper.insert(smartDataSourceLog);
        return result;
    }

//    @Override
//    public int updateSmartDataSourceLog(SmartDataSourceLog sa) {
//        int result = smartDataSourceLogMapper.updateById(sa);
//        return result;
//    }

    @Override
    public PageUtils<SmartDataSourceLog> queryPageSmartDataSourceLogs(int currentPage, int pageCount, String startTime,
                                                                      String endTime, SmartDataSourceLog smartDataSourceLog) {
        Page<SmartDataSourceLog> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartDataSourceLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(smartDataSourceLog.getLogActionName()), "log_action_name", smartDataSourceLog.getLogActionName());
        queryWrapper.like(StringUtils.hasText(smartDataSourceLog.getLogActionHost()), "log_action_host", smartDataSourceLog.getLogActionHost());
        queryWrapper.like(StringUtils.hasText(smartDataSourceLog.getLogActionPeople()), "log_action_people", smartDataSourceLog.getLogActionPeople());
        queryWrapper.like(StringUtils.hasText(smartDataSourceLog.getLogActionModule()), "log_action_module", smartDataSourceLog.getLogActionModule());
        queryWrapper.like(StringUtils.hasText(smartDataSourceLog.getLogActionBusiness()), "log_action_business", smartDataSourceLog.getLogActionBusiness());
        queryWrapper.like(StringUtils.hasText(smartDataSourceLog.getLogActionRemote()), "log_action_remote", smartDataSourceLog.getLogActionRemote());
        queryWrapper.like(StringUtils.hasText(smartDataSourceLog.getLogActionClass()), "log_action_class", smartDataSourceLog.getLogActionClass());
        if (startTime != null && endTime != null) {
            queryWrapper.between("log_action_create_time", startTime, endTime);
        }
        queryWrapper.orderByDesc("log_action_create_time");
        IPage<SmartDataSourceLog> result = smartDataSourceLogMapper.selectPage(page, queryWrapper);
        return new PageUtils<>(result);
    }

//    @Override
//    public int deleteSmartDataSourceLogById(int id) {
//        int result = smartDataSourceLogMapper.deleteById(id);
//        return result;
//    }

    @Override
    public SmartDataSourceLog getSmartById(int id) {
        SmartDataSourceLog result = smartDataSourceLogMapper.selectById(id);
        return result;
    }
}
