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

import cn.estsh.i3plus.core.api.iservice.busi.ISysLogSystemService;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
import cn.estsh.i3plus.pojo.platform.platbean.SysLogSystem;
import cn.estsh.i3plus.pojo.platform.platrepositorymongo.SysLogSystemRepository;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreBsonPack;
import com.mongodb.client.model.Filters;
import io.swagger.annotations.ApiOperation;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description : 系统日志服务接口实现
 * @Reference :
 * @Author : yunhao
 * @Date : 2018-10-25 10:41
 * @Modify :
 **/
@Service
public class SysLogSystemService implements ISysLogSystemService {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysLogSystemService.class);

    @Autowired
    private SysLogSystemRepository logSystemRDao;

    @Override
    @ApiOperation(value = "添加系统日志",notes = "添加系统日志")
    public void insertSysLogSystem(SysLogSystem logSystem) {
        logSystemRDao.insert(logSystem);
    }


    @Override
    public void insertSysLogBatch(int num,int waitTime){
        long t1 = System.currentTimeMillis();
        SysLogSystem sysLogSystem = null;
        for (int i = 0; i < num * 1000; i++) {
            sysLogSystem = new SysLogSystem();
            sysLogSystem.setLogClass("新增测试class");
            sysLogSystem.setLogMethod("新增测试method" +System.currentTimeMillis());
            sysLogSystem.setLogArgs("新增测试参数" + System.currentTimeMillis());
            sysLogSystem.setExecuteTime(0L);
            sysLogSystem.setArgsCount(1);
            sysLogSystem.setLogDetail("2333");
            sysLogSystem.setLogLevel(ImppEnumUtil.LOG_LEVEL.DEBUG.getValue());
            sysLogSystem.setLogModuleId(CommonEnumUtil.SOFT_TYPE.CORE.getValue());
            sysLogSystem.setLogTitle("新增测试");

            logSystemRDao.insert(sysLogSystem);
            try {
                this.wait(waitTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LOGGER.info("插入SysLog耗时：" + (System.currentTimeMillis() - t1) + ",idx：" + i);
        }
    }

    @Override
    @ApiOperation(value = "删除日志",notes = "删除日志")
    public long deleteSysLogSystemById(Long id) {
        Bson bson = Filters.eq("id",id);
        return logSystemRDao.deleteByProperty(bson);
    }

    @Override
    @ApiOperation(value = "查询全部日志",notes = "查询全部日志")
    public List<SysLogSystem> listSysLogSystem() {
        return logSystemRDao.findAll();
    }

    @Override
    @ApiOperation(value = "根据id查询系统日志",notes = "根据id查询系统日志")
    public SysLogSystem getSysLogSystemById(long id) {
        return logSystemRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "系统日志分页复杂查询",notes = "系统日志分页复杂查询")
    public ListPager querySysLogSystemByPager(SysLogSystem logSystem, Pager pager) {
        if (logSystem == null) {
            pager = PagerHelper.getPager(pager,logSystemRDao.listCount());
            return new ListPager(logSystemRDao.listPager(pager),pager);
        } else {
            Bson bson = CoreBsonPack.packBsonByLogSystem(logSystem);
            pager = PagerHelper.getPager(pager,logSystemRDao.findByBsonCount(bson));
            return new ListPager(logSystemRDao.findByBsonPager(bson,pager,logSystem.getOrderByParam()
                                    ,logSystem.getAscOrDesc()),pager);
        }
    }

    @Override
    @ApiOperation(value = "系统日志批量删除",notes = "系统日志批量删除")
    public long deleteSysLogSystemByIds(Long[] ids) {
        return logSystemRDao.deleteByIds(ids);
    }

    @Override
    @ApiOperation(value = "系统日志按id区间查询",notes = "系统日志按id区间查询")
    public List<SysLogSystem> findSysLogSystemByIdInterval(long min, long max) {
        return logSystemRDao.findByBson(CoreBsonPack.packBsonByInterval("_id",min,max));
    }
}
