package com.usoftchina.bi.server.service.common;

import com.github.pagehelper.PageInfo;
import com.usoftchina.bi.core.base.TestPage;
import com.usoftchina.bi.server.dao.common.MessageLogMapper;
import com.usoftchina.bi.server.model.po.MessageLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author chenwei
 * @Date 2019-04-26
 */
@Service
public class MessageLogService {

    @Autowired
    private MessageLogMapper messageLogMapper;

    public PageInfo<MessageLog> list(TestPage testPage) {
        List<MessageLog> messageLogList = messageLogMapper.list(testPage.enablePaging());
        return new PageInfo<MessageLog>(messageLogList);
    }

    public void save(String module, String name, String username, String content){
        if (StringUtils.isEmpty(content)) {
            content = "保存操作";
        }
        MessageLog messageLog = new MessageLog(username, content, "保存成功", null, module, name);
        customizeLog(messageLog);
    }

    public void update(String module, String name, String username, String content){
        if (StringUtils.isEmpty(content)) {
            content = "更新操作";
        }
        MessageLog messageLog = new MessageLog(username, content, "更新成功", null, module, name);
        customizeLog(messageLog);
    }

    public void delete(String module, String name, String username, String content){
        if (StringUtils.isEmpty(content)) {
            content = "删除操作";
        }
        MessageLog messageLog = new MessageLog(username, content, "删除成功", null, module, name);
        customizeLog(messageLog);
    }

    public void customizeLog(MessageLog messageLog){
        messageLogMapper.insert(messageLog);
    }

}
