package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ISysDashboardService;
import cn.estsh.i3plus.core.apiservice.dao.ISysLogSystemDao;
import cn.estsh.i3plus.pojo.base.annotation.AnnoIgnoreLog;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
import cn.estsh.i3plus.pojo.platform.bean.SysUser;
import cn.estsh.i3plus.pojo.platform.platbean.SysLogOperate;
import cn.estsh.i3plus.pojo.platform.platrepositorymongo.SysLogExceptionRepository;
import cn.estsh.i3plus.pojo.platform.platrepositorymongo.SysLogOperateRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysDepartmentRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysOrganizeRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysUserRepository;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description :
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2018-12-15 15:26
 * @Modify:
 **/
@Service
public class SysDashboardService implements ISysDashboardService {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysConfigService.class);

    @Autowired
    private SysOrganizeRepository sysOrganizeRDao;

    @Autowired
    private SysDepartmentRepository sysDepartmentRDao;

    @Autowired
    private SysLogOperateRepository sysLogOperateRDao;

    @Autowired
    private SysLogExceptionRepository sysLogExceptionRDao;

    @Autowired
    private ISysLogSystemDao sysLogSystemDao;

    @Autowired
    private SysUserRepository sysUserRDao;

    @Override
    @AnnoIgnoreLog
    @ApiOperation(value = "查询组织个数")
    public long getSysOrganizeCount() {
        return sysOrganizeRDao.listCount();
    }

    @Override
    @AnnoIgnoreLog
    @ApiOperation(value = "获取部门数量")
    public long getSysDepartmentCount() {
        return sysDepartmentRDao.listCount();
    }

    @Override
    @AnnoIgnoreLog
    @ApiOperation(value = "查询用户登录日志",notes = "查询用户登录日志")
    public List<SysUser> findSysUserLoginLog(Integer size) {
        StringBuffer orderString = new StringBuffer();
        HqlPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()},new String[]{"userLoginLastDateTime"},orderString);

        return sysUserRDao.findByHqlTopWhere(orderString.toString(),size);
    }

    @Override
    @AnnoIgnoreLog
    @ApiOperation(value = "查询操作日志",notes = "查询最新的几条操作日志")
    public List findNewSysLogOperateSize(Integer size) {
        Pager page = new Pager();
        page.setCurrentPage(1);
        page.setPageSize(size);
        SysLogOperate sysLogOperate = new SysLogOperate();
        sysLogOperate.setOrderByParam("createDatetime");
        sysLogOperate.setAscOrDesc(CommonEnumUtil.ASC_OR_DESC.DESC.getValue());
        return sysLogOperateRDao.findByBsonPager(null,page,sysLogOperate.getOrderByParam(),sysLogOperate.getAscOrDesc());
    }

    @Override
    @AnnoIgnoreLog
    @ApiOperation(value = "查询操异常日志",notes = "查询最新的几条异常日志")
    public List findNewSysLogExceptionSize(Integer size) {
        Pager page = new Pager();
        page.setCurrentPage(1);
        page.setPageSize(size);
        SysLogOperate sysLogOperate = new SysLogOperate();
        sysLogOperate.setOrderByParam("createDatetime");
        sysLogOperate.setAscOrDesc(CommonEnumUtil.ASC_OR_DESC.DESC.getValue());
        return sysLogExceptionRDao.findByBsonPager(null,page,sysLogOperate.getOrderByParam(),sysLogOperate.getAscOrDesc());
    }

    @Override
    @AnnoIgnoreLog
    @ApiOperation(value = "查询系统日志",notes = "查询系统日志各级别占比")
    public Map findSysLogSystempByLevel(String startTime, String endTime) {
        return null;
    }

    @Override
    @AnnoIgnoreLog
    @ApiOperation(value = "查询系统日志",notes = "查询系统日志平均执行时间")
    public Map querySysLogSystemAvgExecuteTime(String startTime, String endTime) {
        return sysLogSystemDao.querySysLogSystemAvgExecuteTime(startTime,endTime);
    }

    @Override
    @AnnoIgnoreLog
    @ApiOperation(value = "查询系统日志",notes = "查询各模块系统日志数量")
    public Map querySysLogSystemByLevel(String startTime, String endTime) {
        return sysLogSystemDao.querySysLogSystemByLevel(startTime,endTime);
    }
}
