package com.jwipc.sys.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.convert.AuditLogConvert;
import com.jwipc.sys.entity.AuditLog;
import com.jwipc.sys.logic.AuditLogLogic;
import com.jwipc.sys.model.dto.AuditLogDTO;
import com.jwipc.sys.model.po.AuditLogPO;
import com.jwipc.sys.model.query.AuditLogQuery;
import com.jwipc.sys.service.IAuditLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AuditLogLogicImpl implements AuditLogLogic {

    private final IAuditLogService auditLogService;

    private final AuditLogConvert auditLogConvert;

    @Override
    public PageView findByPage(AuditLogQuery query) {
        final Page<AuditLog> dbPage = PageUtil.toPage(query);
        List<AuditLogPO> auditLogPOS = auditLogService.findPageByQuery(dbPage, query);
        return PageUtil.getPV(auditLogPOS, dbPage.getTotal());
    }

    @Override
    public void auditLogSave(AuditLogDTO auditLogDTO) {
        AuditLog auditLog = auditLogConvert.dto2EntityAuditLog(auditLogDTO);
        if(auditLog != null){
            //完善参数：
            auditLog.setUserId(Long.valueOf(LoginUtil.getUserId()));
            auditLog.setUserName(LoginUtil.getName());
            auditLog.setOperationTime(LocalDateTime.now());
            auditLogService.save(auditLog);
        }
    }

}
