package com.tiesheng.core.service;

import cn.hutool.json.JSONUtil;
import com.tiesheng.core.config.token.TokenParse;
import com.tiesheng.core.config.web.TieshengWebConfigurer;
import com.tiesheng.core.config.web.bean.CurrentWebUser;
import com.tiesheng.core.mapper.CoreLogOperationMapper;
import com.tiesheng.core.pojos.dao.CoreLogOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author hao
 */
@Service
public class CoreLogService extends TsServiceBase<CoreLogOperationMapper, CoreLogOperation> {

    @Autowired(required = false)
    TieshengWebConfigurer tieshengWebConfigurer;

    ///////////////////////////////////////////////////////////////////////////
    // 操作日志
    ///////////////////////////////////////////////////////////////////////////


    /**
     * 添加操作日志
     */
    public void addOperationLog(String title, String subject, Object params) {
        CoreLogOperation operation = new CoreLogOperation();
        if (tieshengWebConfigurer != null) {
            CurrentWebUser currentWebUser = tieshengWebConfigurer.getCurrentUserName();
            operation.setUserId(currentWebUser.getId());
            operation.setUserName(currentWebUser.getName());
        } else {
            operation.setUserId(TokenParse.getWithoutThr().getId());
        }
        operation.setTitle(title);
        operation.setSubject(subject);
        if (params != null) {
            operation.setParams(JSONUtil.toJsonStr(params));
        }
        save(operation);
    }

}
