package com.jeesharp.business.modules.finance.profitsharing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.finance.profitsharing.entity.ProfitSharingLog;
import com.jeesharp.business.modules.finance.profitsharing.mapper.ProfitSharingLogMapper;
import com.jeesharp.business.modules.finance.profitsharing.service.ProfitSharingLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分账操作日志领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ProfitSharingLogServiceImpl implements ProfitSharingLogService {

    private final ProfitSharingLogMapper profitSharingLogMapper;

    @Override
    @Transactional(readOnly = false)
    public void appendLog(String profitSharingOrderId, String action, String requestBody, String responseBody,
            boolean success, String errorMsg, String tenantId) {
        ProfitSharingLog log = new ProfitSharingLog();
        log.setProfitSharingOrderId(profitSharingOrderId);
        log.setAction(action);
        log.setRequestBody(requestBody);
        log.setResponseBody(responseBody);
        log.setSuccessFlag(success ? 1 : 0);
        log.setErrorMsg(StrUtil.sub(errorMsg, 0, 500));
        log.setTenantId(tenantId);
        profitSharingLogMapper.insert(log);
    }
}
