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

import cn.estsh.i3plus.core.api.iservice.busi.ISysLogOperateService;
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.platform.platbean.SysLogOperate;
import cn.estsh.i3plus.pojo.platform.platrepositorymongo.SysLogOperateRepository;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreBsonPack;
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 13:54
 * @Modify :
 **/
@Service
public class SysLogOperateService implements ISysLogOperateService {

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

    @Autowired
    public SysLogOperateRepository logOperateRDao;

    @Override
    @ApiOperation(value = "添加操作日志",notes = "添加操作日志")
    public SysLogOperate insertSysLogOperate(SysLogOperate logOperate) {
        return logOperateRDao.insert(logOperate);
    }

    @Override
    @ApiOperation(value = "根据id删除日志",notes = "根据id删除日志")
    public void deleteSysLogOperateById(Long id) {
        logOperateRDao.deleteById(id);
    }

    @Override
    @ApiOperation(value = "查询全部操作日志",notes = "查询全部操作日志")
    public List listSysLogOperate() {
        return logOperateRDao.listPager(null);
    }

    @Override
    public void deleteSysLogOperateByIds(Long[] ids) {
        for(Long id : ids){
            logOperateRDao.deleteById(id);
        }
    }

    @Override
    @ApiOperation(value = "根据id查询操作日志",notes = "根据id查询操作日志")
    public SysLogOperate getSysLogOperateById(Long id) {
        return logOperateRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "根据操作人姓名模糊查询日志",notes = "根据操作人姓名模糊查询日志")
    public List<SysLogOperate> listSysLogOperateByOperateUserName(String operateUserName) {
        return logOperateRDao.findByProperty("operateUserName",operateUserName);
    }

    @Override
    @ApiOperation(value = "查询所有日志数量")
    public long countAllSysLogOperate() {
        return logOperateRDao.listCount();
    }

    @Override
    @ApiOperation(value = "操作日志复杂查询，分页，排序",notes = "操作日志复杂查询，分页，排序")
    public ListPager<SysLogOperate> querySysLogOperateByPager(SysLogOperate logOperate, Pager pager) {
        if(logOperate == null) {
            pager = PagerHelper.getPager(pager, (int) logOperateRDao.count());
            return new ListPager(logOperateRDao.findAll(),pager);
        }else{
            Bson bson = CoreBsonPack.packBsonByLogOperate(logOperate);
            pager = PagerHelper.getPager(pager,logOperateRDao.findByBsonCount(bson));
            //分页，排序
            return new ListPager(logOperateRDao.findByBsonPager(bson,pager,logOperate.getOrderByParam(),logOperate.getAscOrDesc()),pager);
        }
    }

    @Override
    public List<SysLogOperate> findNewSysLogOperate(Integer num) {
        Pager page = new Pager();
        page.setCurrentPage(1);
        page.setPageSize(num);
        SysLogOperate sysLogOperate = new SysLogOperate();
        sysLogOperate.setOrderByParam("createDatetime");
        sysLogOperate.setAscOrDesc(CommonEnumUtil.ASC_OR_DESC.DESC.getValue());
        return logOperateRDao.findByBsonPager(null,page,sysLogOperate.getOrderByParam(),sysLogOperate.getAscOrDesc());
    }
}
