package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.RunningMonitoringLog;
import com.lightinit.hsdataplatform.entity.RunningMonitoringLogExample;
import com.lightinit.hsdataplatform.mapper.RunningMonitoringLogMapper;
import com.lightinit.hsdataplatform.mapper.RunningMonitoringMapper;
import com.lightinit.hsdataplatform.model.admin.LoggerSearchModel;
import com.lightinit.hsdataplatform.service.admin.ILoggerMoniterService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2019/3/17 14:42
 * @Description:
 **/
@Service
public class LoggerMoniterServiceImpl implements ILoggerMoniterService {

    @Autowired
    private RunningMonitoringLogMapper runningMonitoringLogMapper;
    @Override
    public List<RunningMonitoringLog> getList(LoggerSearchModel inputModel) {
        RunningMonitoringLogExample example = new RunningMonitoringLogExample();
        example.setOrderByClause("id desc limit "+(inputModel.getPage().getPageNo()-1)*inputModel.getPage().getPageSize()+","+inputModel.getPage().getPageSize());
        RunningMonitoringLogExample.Criteria criteria = example.createCriteria();
        criteria.andMonitoringTypeEqualTo(inputModel.getType());
        if(StringUtils.isNotEmpty(inputModel.getTitle())){
            criteria.andTitleLike("%"+inputModel.getTitle()+"%");
        }
        if(inputModel.getCreatedAtFrom()!=null && inputModel.getCreatedAtTo()!=null){
            if (inputModel.getCreatedAtFrom().compareTo(inputModel.getCreatedAtTo()) == 0){
                criteria.andLogTimeGreaterThanOrEqualTo(inputModel.getCreatedAtFrom());
            }else {
                criteria.andLogTimeBetween(inputModel.getCreatedAtFrom(), inputModel.getCreatedAtTo());
            }
        }else if(inputModel.getCreatedAtFrom()!=null){
            criteria.andLogTimeGreaterThanOrEqualTo(inputModel.getCreatedAtFrom());
        } else if(inputModel.getCreatedAtTo()!=null){
            criteria.andLogTimeLessThanOrEqualTo(inputModel.getCreatedAtTo());
        }
        return runningMonitoringLogMapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public long getListCount(LoggerSearchModel inputModel) {
        RunningMonitoringLogExample example = new RunningMonitoringLogExample();
        RunningMonitoringLogExample.Criteria criteria = example.createCriteria();
        criteria.andMonitoringTypeEqualTo(inputModel.getType());
        if(StringUtils.isNotEmpty(inputModel.getTitle())){
            criteria.andTitleLike("%"+inputModel.getTitle()+"%");
        }
        if(inputModel.getCreatedAtFrom()!=null && inputModel.getCreatedAtTo()!=null){
            criteria.andLogTimeBetween(inputModel.getCreatedAtFrom(),inputModel.getCreatedAtTo());
        }else if(inputModel.getCreatedAtFrom()!=null){
            criteria.andLogTimeGreaterThanOrEqualTo(inputModel.getCreatedAtFrom());
        } else if(inputModel.getCreatedAtTo()!=null){
            criteria.andLogTimeLessThanOrEqualTo(inputModel.getCreatedAtTo());
        }
        return runningMonitoringLogMapper.countByExample(example);
    }

    @Override
    public RunningMonitoringLog getDetail(Long id) {
        return runningMonitoringLogMapper.selectByPrimaryKey(id);
    }
}
