package com.app.admin.framework.log.service.impl;

import com.app.admin.framework.log.dao.SysOperationLogDao;
import com.app.admin.framework.log.model.SysOperationLog;
import com.app.admin.framework.log.service.SysOperationLogService;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.service.impl.CrudBaseServiceSupport;
import com.app.framework.core.support.view.grid.Paging;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统操作日志
 */
@Service
public class SysOperationLogServiceImpl extends CrudBaseServiceSupport<SysOperationLogDao, SysOperationLog> implements SysOperationLogService {

    public List<SysOperationLog> getPageList(SysOperationLog view, LoginUser loginUser, Paging paging) {
        LambdaQueryWrapper<SysOperationLog> wrapper = new LambdaQueryWrapper<>();
        if (loginUser.getUserType().equals(LoginUser.TYPE_SUPER)) {//超级管理员:查看所有日志

        } else if (loginUser.getUserType().equals(LoginUser.TYPE_ADMIN)) {//租户管理员:查看租户所有日志
            wrapper.eq(SysOperationLog::getOrganizationId, loginUser.getOrganizationId());
        } else {//查看自己的日志
            wrapper.eq(SysOperationLog::getOperatorId, loginUser.getId());
        }
        if (StringUtils.isNotBlank(view.getOperatorName())) {
            wrapper.like(SysOperationLog::getOperatorAccount, view.getOperatorName());
        }
        wrapper.orderByDesc(SysOperationLog::getOperationDate);
        return this.pageList(wrapper, paging);
    }

    /**
     * 保存对象
     *
     * @param entity 对象
     */
    public boolean saveOperationLog(SysOperationLog entity) {
        if (entity.getOrganizationId() == null) {
            entity.setOrganizationId(LoginUser.ORGANIZATION_SUPER);
        }
        boolean resultCount;
        if (entity.getOid() == null) {
            resultCount = this.save(entity);
        } else {
            resultCount = this.updateById(entity);
        }
        return resultCount;
    }

}